-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmark-view.el
226 lines (192 loc) · 8.21 KB
/
bookmark-view.el
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
;;; bookmark-view.el --- Bookmark views -*- lexical-binding: t -*-
;; Copyright (C) 2020-2024 Daniel Mendler
;; Author: Daniel Mendler
;; Created: 2020
;; License: GPL-3.0-or-later
;; Version: 0.2
;; Package-Requires: ((emacs "27.1"))
;; Homepage: https://github.com/minad/bookmark-view
;; This file is not part of GNU Emacs.
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides commands which allow storing the window
;; configuration (called a view) as an Emacs bookmark. The view can be
;; restored after restart
;;; Code:
(require 'bookmark)
(require 'seq)
(eval-when-compile
(require 'subr-x))
(defgroup bookmark-view nil
"Bookmark views."
:group 'convenience
:prefix "bookmark-view-")
(defcustom bookmark-view-name-format "<count> [%Y-%m-%d %H:%M] <buffers>"
"Name format used for default name of new view bookmarks."
:type 'string)
(defcustom bookmark-view-name-regexp "\\`[0-9]+ \\[[ 0-9:-]+\\] "
"Regexp matching view names that can be popped."
:type 'string)
(defcustom bookmark-view-filter-function
#'bookmark-view-filter-default
"Filter function called for each buffer.
Return t if the current buffer is supposed to be bookmarked."
:type 'symbol)
(defvar bookmark-view-history nil
"History of bookmark views used by `bookmark-view-read'.")
(defun bookmark-view-filter-default ()
"Default filter function called for each buffer.
Return t if the current buffer is supposed to be bookmarked."
(not (and (eq bookmark-make-record-function #'bookmark-make-record-default)
(string-match-p "\\` " (buffer-name)))))
(defun bookmark-view--make-record ()
"Return a new bookmark record for the current buffer.
The current buffer must not have a backing file."
(if (and (not buffer-file-name)
(eq bookmark-make-record-function #'bookmark-make-record-default))
`(,(bookmark-buffer-name)
(buffer . ,(buffer-name))
(handler . ,#'bookmark-view-handler-fallback))
(bookmark-make-record)))
(defun bookmark-view--buffers (&optional frame)
"Return list of buffers of FRAME to be bookmarked."
(seq-filter (lambda (x)
(with-current-buffer x
(funcall bookmark-view-filter-function)))
(mapcar #'window-buffer (window-list frame 'no-minibuf))))
(defun bookmark-view--get (&optional frame)
"Get view state of FRAME as a bookmark record."
(let ((bufs (bookmark-view--buffers)))
`((buffer . ,(mapcar (lambda (x)
(with-current-buffer x
(bookmark-view--make-record)))
bufs))
(filename . ,(format "*View[%s]*" (length bufs)))
(window . ,(window-state-get (frame-root-window frame) 'writable))
(handler . ,#'bookmark-view-handler))))
(defun bookmark-view--put (state &optional frame)
"Put view STATE into FRAME, restoring windows and buffers."
(save-window-excursion
(dolist (buf (alist-get 'buffer state))
(condition-case err
(bookmark-jump buf #'ignore)
(error (delay-warning 'bookmark-view (format "Error %S when opening %S" err buf))))))
(window-state-put (alist-get 'window state) (frame-root-window frame)))
(defun bookmark-view-default-name (&optional frame)
"Default name for current view of FRAME."
(replace-regexp-in-string
"<count>"
(number-to-string
(1+ (seq-count
(apply-partially #'string-match-p bookmark-view-name-regexp)
(bookmark-view-names))))
(replace-regexp-in-string
"<buffers>"
(string-join (sort (mapcar #'buffer-name (bookmark-view--buffers frame))
#'string-lessp) " ")
(let ((system-time-locale "C"))
(format-time-string bookmark-view-name-format))
'fixedcase 'literal)
'fixedcase 'literal))
(defun bookmark-view--group (name transform)
"Group function for bookmark named NAME.
For TRANSFORM non-nil return transformed bookmark name."
(cond
(transform name)
((string-match-p bookmark-view-name-regexp name) "Stack")
(t "Named")))
(defun bookmark-view-read (prompt &optional default)
"Prompting with PROMPT for bookmarked view.
Return DEFAULT if user input is empty."
(completing-read prompt
(let ((names (bookmark-view-names)))
(lambda (str pred action)
(if (eq action 'metadata)
`(metadata (category . bookmark)
(group-function . ,#'bookmark-view--group))
(complete-with-action action names str pred))))
nil nil nil 'bookmark-view-history default))
;;;###autoload
(defun bookmark-view-names ()
"Return a list of names of all view bookmarks."
(bookmark-maybe-load-default-file)
(mapcar #'car (seq-filter (lambda (x)
(eq #'bookmark-view-handler (alist-get 'handler (cdr x))))
bookmark-alist)))
;;;###autoload
(defun bookmark-view-handler-fallback (bm)
"Handle buffer bookmark BM, used for buffers without file."
(let* ((bm (bookmark-get-bookmark-record bm))
(name (alist-get 'buffer bm)))
(unless (get-buffer name)
(with-current-buffer (get-buffer-create name)
(insert (format "bookmark-view: Buffer %s not found" name))))))
;;;###autoload
(defun bookmark-view-handler (bm)
"Handle view bookmark BM."
;; Restore the view in the bookmark-after-jump-hook, since
;; it must happen after the execution of the display function.
(letrec ((hook (lambda ()
(setq bookmark-after-jump-hook (delq hook bookmark-after-jump-hook))
(bookmark-view--put (bookmark-get-bookmark-record bm)))))
(push hook bookmark-after-jump-hook)))
;;;###autoload
(defun bookmark-view (name)
"If view bookmark NAME exists, open it, otherwise save current view."
(interactive (list (bookmark-view-read "View: " (bookmark-view-default-name))))
(if (assoc name bookmark-alist)
(bookmark-view-open name)
(bookmark-view-save name)))
;;;###autoload
(defun bookmark-view-save (name &optional no-overwrite)
"Save current view under the given NAME.
If NO-OVERWRITE is non-nil push to the bookmark list
without overwriting an already existing bookmark."
(interactive (list (bookmark-view-read "Save view: " (bookmark-view-default-name))))
(bookmark-store name (bookmark-view--get) no-overwrite)
(message "View `%s' saved" name))
;;;###autoload
(defun bookmark-view-open (bm)
"Open view bookmark BM."
(interactive (list (bookmark-view-read "Open view: ")))
(bookmark-jump bm #'ignore))
;;;###autoload
(defun bookmark-view-delete (name)
"Delete view bookmark NAME."
(interactive (list (bookmark-view-read "Delete view: ")))
(bookmark-delete name)
(message "View `%s' deleted" name))
;;;###autoload
(defun bookmark-view-rename (old &optional new)
"Rename bookmark from OLD name to NEW name."
(interactive (list (bookmark-view-read "Old view name: ")))
(bookmark-rename old new))
;;;###autoload
(defun bookmark-view-push ()
"Save current view as a bookmark with a default name."
(interactive)
(let ((name (bookmark-view-default-name)))
(bookmark-view-save name 'no-overwrite)
;; Add to view history to mark the new item as recent
(add-to-history 'bookmark-view-history name)))
;;;###autoload
(defun bookmark-view-pop ()
"Pop a bookmark view with a name matching `bookmark-view-name-regexp'."
(interactive)
(let ((name (or (seq-find
(apply-partially #'string-match-p bookmark-view-name-regexp)
(bookmark-view-names))
(user-error "View stack is empty"))))
(bookmark-view-open name)
(bookmark-view-delete name)))
(provide 'bookmark-view)
;;; bookmark-view.el ends here