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.

203 lines
7.3 KiB

преди 5 години
  1. ;;; magit-bookmark.el --- bookmark support for Magit -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010-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. ;; Inspired by an earlier implementation by Yuri Khan.
  9. ;; Magit is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 3, or (at your option)
  12. ;; any later version.
  13. ;;
  14. ;; Magit is distributed in the hope that it will be useful, but WITHOUT
  15. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  17. ;; License for more details.
  18. ;;
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with Magit. If not, see http://www.gnu.org/licenses.
  21. ;;; Commentary:
  22. ;; Support for bookmarks for most Magit buffers.
  23. ;;; Code:
  24. (eval-when-compile
  25. (require 'subr-x))
  26. (require 'magit)
  27. (require 'bookmark)
  28. ;;; Core
  29. (defun magit--make-bookmark ()
  30. "Create a bookmark for the current Magit buffer.
  31. Input values are the major-mode's `magit-bookmark-name' method,
  32. and the buffer-local values of the variables referenced in its
  33. `magit-bookmark-variables' property."
  34. (if (plist-member (symbol-plist major-mode) 'magit-bookmark-variables)
  35. (let ((bookmark (bookmark-make-record-default 'no-file)))
  36. (bookmark-prop-set bookmark 'handler 'magit--handle-bookmark)
  37. (bookmark-prop-set bookmark 'mode major-mode)
  38. (bookmark-prop-set bookmark 'filename (magit-toplevel))
  39. (bookmark-prop-set bookmark 'defaults (list (magit-bookmark-name)))
  40. (dolist (var (get major-mode 'magit-bookmark-variables))
  41. (bookmark-prop-set bookmark var (symbol-value var)))
  42. (bookmark-prop-set
  43. bookmark 'magit-hidden-sections
  44. (--keep (and (oref it hidden)
  45. (cons (oref it type)
  46. (if (derived-mode-p 'magit-stash-mode)
  47. (replace-regexp-in-string
  48. (regexp-quote magit-buffer-revision)
  49. magit-buffer-revision-hash
  50. (oref it value))
  51. (oref it value))))
  52. (oref magit-root-section children)))
  53. bookmark)
  54. (user-error "Bookmarking is not implemented for %s buffers" major-mode)))
  55. ;;;###autoload
  56. (defun magit--handle-bookmark (bookmark)
  57. "Open a bookmark created by `magit--make-bookmark'.
  58. Call the `magit-*-setup-buffer' function of the the major-mode
  59. with the variables' values as arguments, which were recorded by
  60. `magit--make-bookmark'. Ignore `magit-display-buffer-function'."
  61. (let ((buffer (let ((default-directory (bookmark-get-filename bookmark))
  62. (mode (bookmark-prop-get bookmark 'mode))
  63. (magit-display-buffer-function #'identity)
  64. (magit-display-buffer-noselect t))
  65. (apply (intern (format "%s-setup-buffer"
  66. (substring (symbol-name mode) 0 -5)))
  67. (--map (bookmark-prop-get bookmark it)
  68. (get mode 'magit-bookmark-variables))))))
  69. (set-buffer buffer) ; That is the interface we have to adhere to.
  70. (when-let ((hidden (bookmark-prop-get bookmark 'magit-hidden-sections)))
  71. (with-current-buffer buffer
  72. (dolist (child (oref magit-root-section children))
  73. (if (member (cons (oref child type)
  74. (oref child value))
  75. hidden)
  76. (magit-section-hide child)
  77. (magit-section-show child)))))
  78. nil))
  79. (cl-defgeneric magit-bookmark-name ()
  80. "Return name for bookmark to current buffer."
  81. (format "%s%s"
  82. (substring (symbol-name major-mode) 0 -5)
  83. (if-let ((vars (get major-mode 'magit-bookmark-variables)))
  84. (cl-mapcan (lambda (var)
  85. (let ((val (symbol-value var)))
  86. (if (and val (atom val))
  87. (list val)
  88. val)))
  89. vars)
  90. "")))
  91. ;;; Diff
  92. ;;;; Diff
  93. (put 'magit-diff-mode 'magit-bookmark-variables
  94. '(magit-buffer-range-hashed
  95. magit-buffer-typearg
  96. magit-buffer-diff-args
  97. magit-buffer-diff-files))
  98. (cl-defmethod magit-bookmark-name (&context (major-mode magit-diff-mode))
  99. (format "magit-diff(%s%s)"
  100. (pcase (magit-diff-type)
  101. (`staged "staged")
  102. (`unstaged "unstaged")
  103. (`committed magit-buffer-range)
  104. (`undefined
  105. (delq nil (list magit-buffer-typearg magit-buffer-range-hashed))))
  106. (if magit-buffer-diff-files
  107. (concat " -- " (mapconcat #'identity magit-buffer-diff-files " "))
  108. "")))
  109. ;;;; Revision
  110. (put 'magit-revision-mode 'magit-bookmark-variables
  111. '(magit-buffer-revision-hash
  112. magit-buffer-diff-args
  113. magit-buffer-diff-files))
  114. (cl-defmethod magit-bookmark-name (&context (major-mode magit-revision-mode))
  115. (format "magit-revision(%s %s)"
  116. (magit-rev-abbrev magit-buffer-revision)
  117. (if magit-buffer-diff-files
  118. (mapconcat #'identity magit-buffer-diff-files " ")
  119. (magit-rev-format "%s" magit-buffer-revision))))
  120. ;;;; Stash
  121. (put 'magit-stash-mode 'magit-bookmark-variables
  122. '(magit-buffer-revision-hash
  123. magit-buffer-diff-args
  124. magit-buffer-diff-files))
  125. (cl-defmethod magit-bookmark-name (&context (major-mode magit-stash-mode))
  126. (format "magit-stash(%s %s)"
  127. (magit-rev-abbrev magit-buffer-revision)
  128. (if magit-buffer-diff-files
  129. (mapconcat #'identity magit-buffer-diff-files " ")
  130. (magit-rev-format "%s" magit-buffer-revision))))
  131. ;;; Log
  132. ;;;; Log
  133. (put 'magit-log-mode 'magit-bookmark-variables
  134. '(magit-buffer-revisions
  135. magit-buffer-log-args
  136. magit-buffer-log-files))
  137. (cl-defmethod magit-bookmark-name (&context (major-mode magit-log-mode))
  138. (format "magit-log(%s%s)"
  139. (mapconcat #'identity magit-buffer-revisions " ")
  140. (if magit-buffer-log-files
  141. (concat " -- " (mapconcat #'identity magit-buffer-log-files " "))
  142. "")))
  143. ;;;; Cherry
  144. (put 'magit-cherry-mode 'magit-bookmark-variables
  145. '(magit-buffer-refname
  146. magit-buffer-upstream))
  147. (cl-defmethod magit-bookmark-name (&context (major-mode magit-cherry-mode))
  148. (format "magit-cherry(%s > %s)"
  149. magit-buffer-refname
  150. magit-buffer-upstream))
  151. ;;;; Reflog
  152. (put 'magit-reflog-mode 'magit-bookmark-variables
  153. '(magit-buffer-refname))
  154. (cl-defmethod magit-bookmark-name (&context (major-mode magit-reflog-mode))
  155. (format "magit-reflog(%s)" magit-buffer-refname))
  156. ;;; Misc
  157. (put 'magit-status-mode 'magit-bookmark-variables nil)
  158. (put 'magit-refs-mode 'magit-bookmark-variables
  159. '(magit-buffer-upstream
  160. magit-buffer-arguments))
  161. (put 'magit-stashes-mode 'magit-bookmark-variables nil)
  162. (cl-defmethod magit-bookmark-name (&context (major-mode magit-stashes-mode))
  163. (format "magit-states(%s)" magit-buffer-refname))
  164. ;;; _
  165. (provide 'magit-bookmark)
  166. ;;; magit-bookmark.el ends here