Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
7.4 KiB

пре 5 година
  1. ;;; magit-transient.el --- support for transients -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2008-2019 The Magit Project Contributors
  3. ;;
  4. ;; You should have received a copy of the AUTHORS.md file which
  5. ;; lists all contributors. If not, see http://magit.vc/authors.
  6. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  7. ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
  8. ;; Magit is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; Magit is distributed in the hope that it will be useful, but WITHOUT
  14. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. ;; License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with Magit. If not, see http://www.gnu.org/licenses.
  20. ;;; Commentary:
  21. ;; This library implements Magit-specific prefix and suffix classes,
  22. ;; and their methods.
  23. ;;; Code:
  24. (eval-when-compile
  25. (require 'subr-x))
  26. (require 'transient)
  27. (require 'magit-git)
  28. (require 'magit-mode)
  29. (require 'magit-process)
  30. ;;; Classes
  31. (defclass magit--git-variable (transient-variable)
  32. ((scope :initarg :scope)))
  33. (defclass magit--git-variable:choices (magit--git-variable)
  34. ((choices :initarg :choices)
  35. (fallback :initarg :fallback :initform nil)
  36. (default :initarg :default :initform nil)))
  37. (defclass magit--git-variable:urls (magit--git-variable)
  38. ((seturl-arg :initarg :seturl-arg :initform nil)))
  39. ;;; Methods
  40. ;;;; Init
  41. (cl-defmethod transient-init-scope ((obj magit--git-variable))
  42. (oset obj scope
  43. (cond (transient--prefix
  44. (oref transient--prefix scope))
  45. ((slot-boundp obj 'scope)
  46. (funcall (oref obj scope) obj)))))
  47. (cl-defmethod transient-init-value ((obj magit--git-variable))
  48. (let ((variable (format (oref obj variable)
  49. (oref obj scope))))
  50. (oset obj variable variable)
  51. (oset obj value
  52. (cond ((oref obj multi-value)
  53. (magit-get-all variable))
  54. (t
  55. (magit-git-string "config" "--local" variable))))))
  56. ;;;; Read
  57. (cl-defmethod transient-infix-read :around ((obj magit--git-variable:urls))
  58. (mapcar (lambda (url)
  59. (if (string-prefix-p "~" url)
  60. (expand-file-name url)
  61. url))
  62. (cl-call-next-method obj)))
  63. (cl-defmethod transient-infix-read ((obj magit--git-variable:choices))
  64. (let ((choices (oref obj choices)))
  65. (when (functionp choices)
  66. (setq choices (funcall choices)))
  67. (if-let ((value (oref obj value)))
  68. (cadr (member value choices))
  69. (car choices))))
  70. ;;;; Readers
  71. (defun magit-transient-read-person (prompt initial-input history)
  72. (magit-completing-read
  73. prompt
  74. (mapcar (lambda (line)
  75. (save-excursion
  76. (and (string-match "\\`[\s\t]+[0-9]+\t" line)
  77. (list (substring line (match-end 0))))))
  78. (magit-git-lines "shortlog" "-n" "-s" "-e" "HEAD"))
  79. nil nil initial-input history))
  80. (defun magit-transient-read-revision (prompt initial-input history)
  81. (or (magit-completing-read prompt (cons "HEAD" (magit-list-refnames))
  82. nil nil initial-input history
  83. (or (magit-branch-or-commit-at-point)
  84. (magit-get-current-branch)))
  85. (user-error "Nothing selected")))
  86. ;;;; Set
  87. (cl-defmethod transient-infix-set ((obj magit--git-variable) value)
  88. (let ((variable (oref obj variable)))
  89. (oset obj value value)
  90. (if (oref obj multi-value)
  91. (magit-set-all value variable)
  92. (magit-set value variable))
  93. (magit-refresh)
  94. (unless (or value transient--prefix)
  95. (message "Unset %s" variable))))
  96. (cl-defmethod transient-infix-set ((obj magit--git-variable:urls) values)
  97. (let ((previous (oref obj value))
  98. (seturl (oref obj seturl-arg))
  99. (remote (oref transient--prefix scope)))
  100. (oset obj value values)
  101. (dolist (v (-difference values previous))
  102. (magit-call-git "remote" "set-url" seturl "--add" remote v))
  103. (dolist (v (-difference previous values))
  104. (magit-call-git "remote" "set-url" seturl "--delete" remote
  105. (concat "^" (regexp-quote v) "$")))
  106. (magit-refresh)))
  107. ;;;; Draw
  108. (cl-defmethod transient-format-description ((obj magit--git-variable))
  109. (or (oref obj description)
  110. (oref obj variable)))
  111. (cl-defmethod transient-format-value ((obj magit--git-variable))
  112. (if-let ((value (oref obj value)))
  113. (if (oref obj multi-value)
  114. (if (cdr value)
  115. (mapconcat (lambda (v)
  116. (concat "\n "
  117. (propertize v 'face 'transient-value)))
  118. value "")
  119. (propertize (car value) 'face 'transient-value))
  120. (propertize (car (split-string value "\n"))
  121. 'face 'transient-value))
  122. (propertize "unset" 'face 'transient-inactive-value)))
  123. (cl-defmethod transient-format-value ((obj magit--git-variable:choices))
  124. (let* ((variable (oref obj variable))
  125. (choices (oref obj choices))
  126. (local (magit-git-string "config" "--local" variable))
  127. (global (magit-git-string "config" "--global" variable))
  128. (default (oref obj default))
  129. (fallback (oref obj fallback))
  130. (fallback (and fallback
  131. (when-let ((val (magit-get fallback)))
  132. (concat fallback ":" val)))))
  133. (when (functionp choices)
  134. (setq choices (funcall choices)))
  135. (concat
  136. (propertize "[" 'face 'transient-inactive-value)
  137. (mapconcat (lambda (choice)
  138. (propertize choice 'face (if (equal choice local)
  139. (if (member choice choices)
  140. 'transient-value
  141. 'font-lock-warning-face)
  142. 'transient-inactive-value)))
  143. (if (and local (not (member local choices)))
  144. (cons local choices)
  145. choices)
  146. (propertize "|" 'face 'transient-inactive-value))
  147. (and (or global fallback default)
  148. (concat
  149. (propertize "|" 'face 'transient-inactive-value)
  150. (cond (global
  151. (propertize (concat "global:" global)
  152. 'face (cond (local
  153. 'transient-inactive-value)
  154. ((member global choices)
  155. 'transient-value)
  156. (t
  157. 'font-lock-warning-face))))
  158. (fallback
  159. (propertize fallback
  160. 'face (if local
  161. 'transient-inactive-value
  162. 'transient-value)))
  163. (default
  164. (propertize (concat "default:" default)
  165. 'face (if local
  166. 'transient-inactive-value
  167. 'transient-value))))))
  168. (propertize "]" 'face 'transient-inactive-value))))
  169. ;;; _
  170. (provide 'magit-transient)
  171. ;;; magit-transient.el ends here