Skip to content

Commit d611bcc

Browse files
committed
Add transient menu interface
1 parent 2107af1 commit d611bcc

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

git-link-transient.el

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
;;; git-link-transient.el --- Transient interface for git-link -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2013-2024 Skye Shaw and others
4+
5+
;; This program is free software; you can redistribute it and/or modify
6+
;; it under the terms of the GNU General Public License as published by
7+
;; the Free Software Foundation, either version 3 of the License, or
8+
;; (at your option) any later version.
9+
10+
;; This program is distributed in the hope that it will be useful,
11+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
;; GNU General Public License for more details.
14+
15+
;; You should have received a copy of the GNU General Public License
16+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
;;; Commentary:
19+
20+
;; Transient interface (magit-like menu) for git-link.
21+
;; Call `git-link-dispatch' to show the menu.
22+
;;
23+
;; You need to have `transient' installed as a depenency.
24+
;; (it's not listed as the dependency of git-link because we want it to be optional.)
25+
26+
;;; Code:
27+
28+
(require 'cl-lib)
29+
(require 'transient)
30+
(require 'git-link)
31+
32+
(defun git-link-dispatch--action ()
33+
"Finally call `git-link' with transient arguments."
34+
(let* ((args (transient-args 'git-link-dispatch))
35+
(git-link-default-branch (transient-arg-value "branch=" args))
36+
(git-link-default-remote (transient-arg-value "remote=" args))
37+
(git-link-use-commit (transient-arg-value "use_commit" args))
38+
(git-link-use-single-line-number (transient-arg-value "line_number" args)))
39+
(call-interactively #'git-link)))
40+
41+
(defun git-link-dispatch--copy ()
42+
"The copy command in transient suffix."
43+
(interactive)
44+
(let ((git-link-open-in-browser nil)
45+
(git-link-add-to-kill-ring t))
46+
(git-link-dispatch--action)))
47+
48+
(defun git-link-dispatch--open ()
49+
"The open command in transient suffix."
50+
(interactive)
51+
(let ((git-link-open-in-browser t))
52+
(git-link-dispatch--action)))
53+
54+
(defclass git-link--transient-bare-option (transient-option) ()
55+
"Similar to `transient-option', but format without argument string.")
56+
57+
(cl-defmethod transient-format ((obj git-link--transient-bare-option))
58+
(format " %s %s (%s)"
59+
(transient-format-key obj)
60+
(transient-format-description obj)
61+
(let ((v (oref obj value)))
62+
(if (> (length v) 0)
63+
(propertize v 'face 'transient-value)
64+
(propertize "default" 'face 'transient-inactive-value)))))
65+
66+
(defclass git-link--transient-bare-switch (transient-switch) ()
67+
"Similar to `transient-switch', but format without argument string, only yes/no.")
68+
69+
(cl-defmethod transient-format ((obj git-link--transient-bare-switch))
70+
(format " %s %s (%s)"
71+
(transient-format-key obj)
72+
(transient-format-description obj)
73+
(if (oref obj value)
74+
(propertize "on" 'face 'transient-value)
75+
(propertize "off" 'face 'transient-inactive-value))))
76+
77+
(transient-define-infix git-link-dispatch--branch ()
78+
:class git-link--transient-bare-option
79+
:argument "branch="
80+
:description "Branch"
81+
:prompt "Branch: "
82+
:key "b"
83+
:init-value (lambda (obj) (oset obj value git-link-default-branch))
84+
:reader (lambda (prompt &rest _)
85+
(completing-read
86+
prompt
87+
(remove nil (list git-link-default-branch (git-link--branch))))))
88+
89+
(transient-define-infix git-link-dispatch--remote ()
90+
:class git-link--transient-bare-option
91+
:argument "remote="
92+
:description "Remote"
93+
:key "r"
94+
:init-value (lambda (obj) (oset obj value git-link-default-remote))
95+
:reader (lambda (&rest _) (git-link--read-remote)))
96+
97+
(transient-define-infix git-link-dispatch--use-commit ()
98+
:class git-link--transient-bare-switch
99+
:argument "use_commit"
100+
;; the value should be "use_commit" (the argument) or nil. not t
101+
:init-value (lambda (obj) (oset obj value (and git-link-use-commit "use_commit")))
102+
:description "Use commit"
103+
:key "c")
104+
105+
(transient-define-infix git-link-dispatch--line-number ()
106+
:class git-link--transient-bare-switch
107+
:argument "line_number"
108+
:description "Line number"
109+
:init-value (lambda (obj) (oset obj value (and git-link-use-single-line-number "line_number")))
110+
:if-not 'use-region-p
111+
:key "n")
112+
113+
;;;###autoload
114+
(transient-define-prefix git-link-dispatch ()
115+
"Git link dispatch."
116+
[:description
117+
"Options"
118+
(git-link-dispatch--branch)
119+
(git-link-dispatch--remote)
120+
(git-link-dispatch--use-commit)
121+
(git-link-dispatch--line-number)]
122+
[:description
123+
"Git link"
124+
("l" "Copy link" git-link-dispatch--copy)
125+
("o" "Open in browser" git-link-dispatch--open)])
126+
127+
(provide 'git-link-transient)
128+
;;; git-link-transient.el ends here

0 commit comments

Comments
 (0)