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
|
;;; magit-bookmark.el --- bookmark support for Magit -*- lexical-binding: t -*-
;; Copyright (C) 2010-2018 The Magit Project Contributors
;;
;; You should have received a copy of the AUTHORS.md file which
;; lists all contributors. If not, see http://magit.vc/authors.
;; Author: Yuri Khan <yuri.v.khan@gmail.com>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
;; Magit is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Magit is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Magit. If not, see http://www.gnu.org/licenses.
;;; Commentary:
;; Support for bookmarks for most Magit buffers.
;;; Code:
(require 'magit)
(require 'bookmark)
;;; Supporting primitives
(defun magit-bookmark--jump (bookmark fn &rest args)
"Handle a Magit BOOKMARK.
This function will:
1. Bind `default-directory' to the repository root directory
stored in the `filename' bookmark property.
2. Invoke the function FN with ARGS as arguments. This needs to
restore the buffer.
3. Restore the expanded/collapsed status of top level sections
and the point position."
(declare (indent 2))
(let* ((default-directory (bookmark-get-filename bookmark)))
(if default-directory
(apply fn args)
(signal 'bookmark-error-no-filename (list 'stringp default-directory)))
(when (derived-mode-p 'magit-mode)
(when-let ((hidden-sections (bookmark-prop-get bookmark
'magit-hidden-sections)))
(dolist (child (oref magit-root-section children))
(if (member (cons (oref child type)
(oref child value))
hidden-sections)
(magit-section-hide child)
(magit-section-show child)))))
(--when-let (bookmark-get-position bookmark)
(goto-char it))
(--when-let (bookmark-get-front-context-string bookmark)
(when (search-forward it (point-max) t)
(goto-char (match-beginning 0))))
(--when-let (bookmark-get-rear-context-string bookmark)
(when (search-backward it (point-min) t)
(goto-char (match-end 0))))
nil))
(defun magit-bookmark--make-record (mode handler &optional make-props)
"Create a Magit bookmark.
MODE specifies the expected major mode of current buffer.
HANDLER should be a function that will be used to restore this
buffer.
MAKE-PROPS should be either nil or a function that will be called
with `magit-refresh-args' as the argument list, and may return an
alist whose every element has the form (PROP . VALUE) and
specifies additional properties to store in the bookmark."
(declare (indent 1))
(unless (eq major-mode mode)
(user-error "Not in a %s buffer" mode))
(let ((bookmark (bookmark-make-record-default 'no-file)))
(bookmark-prop-set bookmark 'handler handler)
(bookmark-set-filename bookmark (magit-toplevel))
(when (derived-mode-p 'magit-mode)
(bookmark-prop-set
bookmark 'magit-hidden-sections
(--map (cons (oref it type)
(oref it value))
(--filter (oref it hidden)
(oref magit-root-section children)))))
(when make-props
(pcase-dolist (`(,prop . ,value) (apply make-props magit-refresh-args))
(bookmark-prop-set bookmark prop value)))
bookmark))
;;; Status
;;;###autoload
(defun magit-bookmark--status-jump (bookmark)
"Handle a Magit status BOOKMARK."
(magit-bookmark--jump bookmark
(lambda () (magit-status-internal default-directory))))
;;;###autoload
(defun magit-bookmark--status-make-record ()
"Create a Magit status bookmark."
(magit-bookmark--make-record 'magit-status-mode
#'magit-bookmark--status-jump))
;;; Refs
;;;###autoload
(defun magit-bookmark--refs-jump (bookmark)
"Handle a Magit refs BOOKMARK."
(magit-bookmark--jump bookmark #'magit-show-refs
(bookmark-prop-get bookmark 'magit-refs)
(bookmark-prop-get bookmark 'magit-args)))
;;;###autoload
(defun magit-bookmark--refs-make-record ()
"Create a Magit refs bookmark."
(magit-bookmark--make-record 'magit-refs-mode
#'magit-bookmark--refs-jump
(lambda (refs args)
`((magit-refs . ,refs)
(magit-args . ,args)))))
;;; Log
;;;###autoload
(defun magit-bookmark--log-jump (bookmark)
"Handle a Magit log BOOKMARK."
(magit-bookmark--jump bookmark #'magit-log
(bookmark-prop-get bookmark 'magit-revs)
(bookmark-prop-get bookmark 'magit-args)
(bookmark-prop-get bookmark 'magit-files)))
(defun magit-bookmark--log-make-name (buffer-name revs _args files)
"Generate the default name for a log bookmark."
(concat
buffer-name " " (mapconcat #'identity revs " ")
(and files
(concat " touching " (mapconcat #'identity files " ")))))
;;;###autoload
(defun magit-bookmark--log-make-record ()
"Create a Magit log bookmark."
(magit-bookmark--make-record 'magit-log-mode
#'magit-bookmark--log-jump
(lambda (revs args files)
`((defaults . (,(magit-bookmark--log-make-name
(buffer-name) revs args files)))
(magit-revs . ,revs)
(magit-args . ,args)
(magit-files . ,files)))))
;;; Reflog
;;;###autoload
(defun magit-bookmark--reflog-jump (bookmark)
"Handle a Magit reflog BOOKMARK."
(magit-bookmark--jump bookmark
(lambda ()
(let ((magit-reflog-arguments (bookmark-prop-get bookmark 'magit-args)))
(magit-reflog (bookmark-prop-get bookmark 'magit-ref))))))
(defun magit-bookmark--reflog-make-name (buffer-name ref)
"Generate the default name for a reflog bookmark."
(concat buffer-name " " ref))
;;;###autoload
(defun magit-bookmark--reflog-make-record ()
"Create a Magit reflog bookmark."
(magit-bookmark--make-record 'magit-reflog-mode
#'magit-bookmark--reflog-jump
(lambda (ref args)
`((defaults . (,(magit-bookmark--reflog-make-name (buffer-name) ref)))
(magit-ref . ,ref)
(magit-args . ,args)))))
;;; Stashes
;;;###autoload
(defun magit-bookmark--stashes-jump (bookmark)
"Handle a Magit stash list BOOKMARK."
(magit-bookmark--jump bookmark #'magit-stash-list))
;;;###autoload
(defun magit-bookmark--stashes-make-record ()
"Create a Magit stash list bookmark."
(magit-bookmark--make-record 'magit-stashes-mode
#'magit-bookmark--stashes-jump))
;;; Cherry
;;;###autoload
(defun magit-bookmark--cherry-jump (bookmark)
"Handle a Magit cherry BOOKMARK."
(magit-bookmark--jump bookmark #'magit-cherry
(bookmark-prop-get bookmark 'magit-head)
(bookmark-prop-get bookmark 'magit-upstream)))
(defun magit-bookmark--cherry-make-name (buffer-name head upstream)
"Generate the default name for a cherry bookmark."
(concat buffer-name " " head " upstream " upstream))
;;;###autoload
(defun magit-bookmark--cherry-make-record ()
"Create a Magit cherry bookmark."
(magit-bookmark--make-record 'magit-cherry-mode
#'magit-bookmark--cherry-jump
(lambda (upstream head)
`((defaults . (,(magit-bookmark--cherry-make-name
(buffer-name) head upstream)))
(magit-head . ,head)
(magit-upstream . ,upstream)))))
;;; Diff
;;;###autoload
(defun magit-bookmark--diff-jump (bookmark)
"Handle a Magit diff BOOKMARK."
(magit-bookmark--jump bookmark #'magit-diff-setup
(bookmark-prop-get bookmark 'magit-rev-or-range)
(bookmark-prop-get bookmark 'magit-const)
(bookmark-prop-get bookmark 'magit-args)
(bookmark-prop-get bookmark 'magit-files)))
(defun magit-bookmark--resolve (rev-or-range)
"Return REV-OR-RANGE with ref names resolved to commit hashes."
(pcase (magit-git-lines "rev-parse" rev-or-range)
(`(,rev)
(magit-rev-abbrev rev))
((and `(,rev1 ,rev2)
(guard (/= ?^ (aref rev1 0)))
(guard (= ?^ (aref rev2 0))))
(concat (magit-rev-abbrev (substring rev2 1))
".."
(magit-rev-abbrev rev1)))
((and `(,rev1 ,rev2 ,rev3)
(guard (/= ?^ (aref rev1 0)))
(guard (/= ?^ (aref rev2 0)))
(guard (= ?^ (aref rev3 0))))
(ignore rev3)
(concat (magit-rev-abbrev rev1)
"..."
(magit-rev-abbrev rev2)))
(_
rev-or-range)))
(defun magit-bookmark--diff-make-name
(buffer-name rev-or-range const _args files)
"Generate a default name for a diff bookmark."
(if (member "--no-index" const)
(apply #'format "*magit-diff %s %s" files)
(concat buffer-name " "
(cond (rev-or-range)
((member "--cached" const) "staged")
(t "unstaged"))
(when files
(concat " in " (mapconcat #'identity files ", "))))))
;;;###autoload
(defun magit-bookmark--diff-make-record ()
"Create a Magit diff bookmark."
(magit-bookmark--make-record 'magit-diff-mode
#'magit-bookmark--diff-jump
(lambda (rev-or-range const args files)
(let ((resolved (magit-bookmark--resolve rev-or-range)))
`((defaults . (,(magit-bookmark--diff-make-name
(buffer-name) resolved const args files)))
(magit-rev-or-range . ,resolved)
(magit-const . ,const)
(magit-args . ,args)
(magit-files . ,files))))))
;;; Revision
;;;###autoload
(defun magit-bookmark--revision-jump (bookmark)
"Handle a Magit revision BOOKMARK."
(magit-bookmark--jump bookmark #'magit-show-commit
(bookmark-prop-get bookmark 'magit-rev)
(bookmark-prop-get bookmark 'args)
(bookmark-prop-get bookmark 'files)))
(defun magit-bookmark--revision-make-name (buffer-name rev _args files)
"Generate a default name for a revision bookmark."
(let ((subject (magit-rev-format "%s" rev)))
(concat buffer-name " "
(magit-rev-abbrev rev)
(cond (files (concat " " (mapconcat #'identity files " ")))
(subject (concat " " subject))))))
;;;###autoload
(defun magit-bookmark--revision-make-record ()
"Create a Magit revision bookmark."
;; magit-refresh-args stores the revision in relative form.
;; For bookmarks, the exact hash is more appropriate.
(magit-bookmark--make-record 'magit-revision-mode
#'magit-bookmark--revision-jump
(lambda (_rev _ args files)
`((defaults . (,(magit-bookmark--revision-make-name
(buffer-name) magit-buffer-revision-hash
args files)))
(magit-rev . ,magit-buffer-revision-hash)
(magit-args . ,args)
(magit-files . ,files)))))
;;; Stash
;;;###autoload
(defun magit-bookmark--stash-jump (bookmark)
"Handle a Magit stash BOOKMARK."
(magit-bookmark--jump bookmark #'magit-stash-show
(bookmark-prop-get bookmark 'magit-stash)
(bookmark-prop-get bookmark 'magit-args)
(bookmark-prop-get bookmark 'magit-files)))
(defun magit-bookmark--stash-make-name (buffer-name stash _args files)
"Generate the default name for a stash bookmark."
(concat buffer-name " " stash " "
(if files
(mapconcat #'identity files " ")
(magit-rev-format "%s" stash))))
;;;###autoload
(defun magit-bookmark--stash-make-record ()
"Create a Magit stash bookmark."
(magit-bookmark--make-record 'magit-stash-mode
#'magit-bookmark--stash-jump
(lambda (stash _ args files)
`((defaults . (,(magit-bookmark--stash-make-name
(buffer-name)
(magit-rev-abbrev magit-buffer-revision-hash)
args files)))
(magit-stash . ,magit-buffer-revision-hash)
(magit-args . ,args)
(magit-files . ,files)
(magit-hidden-sections
. ,(--map `(,(oref it type)
. ,(replace-regexp-in-string (regexp-quote stash)
magit-buffer-revision-hash
(oref it value)))
(--filter (oref it hidden)
(oref magit-root-section children))))))))
;;; Submodules
;;;###autoload
(defun magit-bookmark--submodules-jump (bookmark)
"Handle a Magit submodule list BOOKMARK."
(magit-bookmark--jump bookmark #'magit-list-submodules))
;;;###autoload
(defun magit-bookmark--submodules-make-record ()
"Create a Magit submodule list bookmark."
(magit-bookmark--make-record 'magit-submodule-list-mode
#'magit-bookmark--submodules-jump))
(provide 'magit-bookmark)
;;; magit-bookmark.el ends here
|