magit-bookmark.el (4931B)
1 ;;; magit-bookmark.el --- Bookmarks for Magit buffers -*- lexical-binding:t -*- 2 3 ;; Copyright (C) 2008-2024 The Magit Project Contributors 4 5 ;; Inspired by an earlier implementation by Yuri Khan. 6 7 ;; Author: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev> 8 ;; Maintainer: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev> 9 10 ;; SPDX-License-Identifier: GPL-3.0-or-later 11 12 ;; Magit is free software: you can redistribute it and/or modify it 13 ;; under the terms of the GNU General Public License as published by 14 ;; the Free Software Foundation, either version 3 of the License, or 15 ;; (at your option) any later version. 16 ;; 17 ;; Magit is distributed in the hope that it will be useful, but WITHOUT 18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 19 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 20 ;; License for more details. 21 ;; 22 ;; You should have received a copy of the GNU General Public License 23 ;; along with Magit. If not, see <https://www.gnu.org/licenses/>. 24 25 ;;; Commentary: 26 27 ;; Support for bookmarks for most Magit buffers. 28 29 ;;; Code: 30 31 (require 'magit) 32 33 (require 'bookmark) 34 35 ;;; Common 36 37 (cl-defmethod magit-bookmark-get-filename (&context (major-mode magit-mode)) 38 (magit-toplevel)) 39 40 (cl-defmethod magit-bookmark-get-buffer-create 41 (bookmark (mode (derived-mode magit-mode))) 42 (let ((default-directory (bookmark-get-filename bookmark)) 43 (magit-display-buffer-function #'identity) 44 (magit-display-buffer-noselect t)) 45 (apply (intern (format "%s-setup-buffer" 46 (substring (symbol-name mode) 0 -5))) 47 (--map (bookmark-prop-get bookmark it) 48 (get mode 'magit-bookmark-variables))))) 49 50 ;;; Diff 51 ;;;; Diff 52 53 (put 'magit-diff-mode 'magit-bookmark-variables 54 '(magit-buffer-range-hashed 55 magit-buffer-typearg 56 magit-buffer-diff-args 57 magit-buffer-diff-files)) 58 59 (cl-defmethod magit-bookmark-name (&context (major-mode magit-diff-mode)) 60 (format "magit-diff(%s%s)" 61 (pcase (magit-diff-type) 62 ('staged "staged") 63 ('unstaged "unstaged") 64 ('committed magit-buffer-range) 65 ('undefined 66 (delq nil (list magit-buffer-typearg magit-buffer-range-hashed)))) 67 (if magit-buffer-diff-files 68 (concat " -- " (string-join magit-buffer-diff-files " ")) 69 ""))) 70 71 ;;;; Revision 72 73 (put 'magit-revision-mode 'magit-bookmark-variables 74 '(magit-buffer-revision-hash 75 magit-buffer-diff-args 76 magit-buffer-diff-files)) 77 78 (cl-defmethod magit-bookmark-name (&context (major-mode magit-revision-mode)) 79 (format "magit-revision(%s %s)" 80 (magit-rev-abbrev magit-buffer-revision) 81 (if magit-buffer-diff-files 82 (string-join magit-buffer-diff-files " ") 83 (magit-rev-format "%s" magit-buffer-revision)))) 84 85 ;;;; Stash 86 87 (put 'magit-stash-mode 'magit-bookmark-variables 88 '(magit-buffer-revision-hash 89 magit-buffer-diff-args 90 magit-buffer-diff-files)) 91 92 (cl-defmethod magit-bookmark-name (&context (major-mode magit-stash-mode)) 93 (format "magit-stash(%s %s)" 94 (magit-rev-abbrev magit-buffer-revision) 95 (if magit-buffer-diff-files 96 (string-join magit-buffer-diff-files " ") 97 (magit-rev-format "%s" magit-buffer-revision)))) 98 99 (cl-defmethod magit-bookmark--get-child-value 100 (section &context (major-mode magit-stash-mode)) 101 (string-replace magit-buffer-revision 102 magit-buffer-revision-hash 103 (oref section value))) 104 105 ;;; Log 106 ;;;; Log 107 108 (put 'magit-log-mode 'magit-bookmark-variables 109 '(magit-buffer-revisions 110 magit-buffer-log-args 111 magit-buffer-log-files)) 112 113 (cl-defmethod magit-bookmark-name (&context (major-mode magit-log-mode)) 114 (format "magit-log(%s%s)" 115 (string-join magit-buffer-revisions " ") 116 (if magit-buffer-log-files 117 (concat " -- " (string-join magit-buffer-log-files " ")) 118 ""))) 119 120 ;;;; Cherry 121 122 (put 'magit-cherry-mode 'magit-bookmark-variables 123 '(magit-buffer-refname 124 magit-buffer-upstream)) 125 126 (cl-defmethod magit-bookmark-name (&context (major-mode magit-cherry-mode)) 127 (format "magit-cherry(%s > %s)" 128 magit-buffer-refname 129 magit-buffer-upstream)) 130 131 ;;;; Reflog 132 133 (put 'magit-reflog-mode 'magit-bookmark-variables 134 '(magit-buffer-refname)) 135 136 (cl-defmethod magit-bookmark-name (&context (major-mode magit-reflog-mode)) 137 (format "magit-reflog(%s)" magit-buffer-refname)) 138 139 ;;; Misc 140 141 (put 'magit-status-mode 'magit-bookmark-variables nil) 142 143 (put 'magit-refs-mode 'magit-bookmark-variables 144 '(magit-buffer-upstream 145 magit-buffer-arguments)) 146 147 (put 'magit-stashes-mode 'magit-bookmark-variables nil) 148 149 (cl-defmethod magit-bookmark-name (&context (major-mode magit-stashes-mode)) 150 (format "magit-states(%s)" magit-buffer-refname)) 151 152 ;;; _ 153 (provide 'magit-bookmark) 154 ;;; magit-bookmark.el ends here