-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheyebrowse-restore.el
212 lines (167 loc) · 6.96 KB
/
eyebrowse-restore.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
;;; eyebrowse-restore.el --- Persistent Eyebrowse for all frames -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Jakub Kadlčík
;; Author: Jakub Kadlčík <frostyx@email.cz>
;; URL: https://github.com/FrostyX/eyebrowse-restore
;; Version: 1.0
;; Package-Requires: ((emacs "26.3") (eyebrowse "0.7.8") (dash "2.19.1") (s "1.13.0"))
;; Keywords: convenience, eyebrowse, helm, persistent
;;; License:
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Never lose your Eyebrowse window configurations again
;;; Code:
(require 'eyebrowse)
(require 'dash)
(require 's)
;;;; Customization
(defgroup eyebrowse-restore nil
"Persistent Eyebrowse for all frames."
:prefix "eyebrowse-restore-"
:group 'eyebrowse)
(defcustom eyebrowse-restore-dir
(concat user-emacs-directory "eyebrowse-restore")
"Path to the directory where to store Eyebrowse window configs."
:type 'directory
:group 'eyebrowse-restore)
(defcustom eyebrowse-restore-save-interval 300
"How often (in seconds) to save all Eyebrowse window configs.
If the interval is set to `nil', the timer is not run."
:type 'number
:group 'eyebrowse-restore)
(defcustom eyebrowse-restore-keep-old-backups 10
"How many old backups should we keep."
:type 'integer
:group 'eyebrowse-restore)
(defcustom eyebrowse-restore-if-only-one t
"Restore without asking?
If there is only one backup in the `eyebrowse-restore-dir',
automatically restore from it without prompting the user."
:type 'boolean
:group 'eyebrowse-restore)
;;;; Variables
(defvar eyebrowse-restore-timer nil
"Timer to automatically save Eyebrowse window configurations.")
;;;; Modes
;;;###autoload
(define-minor-mode eyebrowse-restore-mode
"Toggle `eyebrowse-restore-mode'.
This global minor mode provides a timer to automatically
save Eyebrowse window configurations for all Emacs frames.
It also provides a hook to save window configuration for a
frame before closing it."
:global t
(if eyebrowse-restore-mode
(progn
(add-to-list 'delete-frame-functions #'eyebrowse-restore-save)
(eyebrowse-restore--run-timer))
(progn
(setq delete-frame-functions
(delete #'eyebrowse-restore-save delete-frame-functions))
(eyebrowse-restore--cancel-timer))))
;;;; Commands
;;;###autoload
(defun eyebrowse-restore-save-all ()
"Save the Eyebrowse window configurations for all frames."
(interactive)
(make-directory eyebrowse-restore-dir t)
(dolist (frame (frame-list))
;; Ignore child frames (completion, minibuffer, etc)
(unless (frame-parameter frame 'parent-frame)
(eyebrowse-restore-save frame))))
;;;###autoload
(defun eyebrowse-restore-save (frame)
"Save the Eyebrowse window configurations for the specified FRAME."
(interactive)
(let* ((name (frame-parameter frame 'name))
(name (eyebrowse-restore--encode-name name))
(path (concat (file-name-as-directory eyebrowse-restore-dir) name))
(window-configs (eyebrowse--get 'window-configs frame)))
(with-temp-file path
(prin1 window-configs (current-buffer))))
(eyebrowse-restore--remove-unused-backups))
;;;###autoload
(defun eyebrowse-restore (&optional name)
"Restore an Eyebrowse window configuration from a backup.
If a backup NAME is not specified, a `completing-read' with
backup names appears. Selected backup of an Eyebrowse window
configurations is applied to the current frame.
Warning! The current Eyebrowse window configurations for the
active frame will be destroyed."
(interactive)
(let ((name (eyebrowse-restore--decode-name name))
(backups (mapcar #'eyebrowse-restore--decode-name
(eyebrowse-restore--list-backups))))
(if (and (not name)
(= (length backups) 1)
eyebrowse-restore-if-only-one)
(eyebrowse-restore (car backups))
(let* ((name (or name (completing-read "Eyebrowse backups: " backups)))
(name (eyebrowse-restore--encode-name name))
(path (concat (file-name-as-directory eyebrowse-restore-dir) name)))
(with-temp-buffer
(insert-file-contents path)
(eyebrowse--set 'window-configs
(read (buffer-string))))))))
;;;; Functions
;;;;; Private
(defun eyebrowse-restore--run-timer ()
"Run the `eyebrowse-restore-timer'."
(when eyebrowse-restore-save-interval
(setq eyebrowse-restore-timer
(run-at-time 0 eyebrowse-restore-save-interval
#'eyebrowse-restore-save-all))))
(defun eyebrowse-restore--cancel-timer ()
"Cancel the `eyebrowse-restore-timer'."
(when eyebrowse-restore-timer
(cancel-timer eyebrowse-restore-timer)))
(defun eyebrowse-restore--encode-name (name)
"Generate a backup file.
Create a safe backup name based on an input string, ideally
a frame NAME. The returned name should not cause any issues
on the filesystem."
(if name
(s-replace "/" "%2F" name)))
(defun eyebrowse-restore--decode-name (name)
"Convert encoded backup NAME back to the human-readable format."
(if name
(s-replace "%2F" "/" name)))
(defun eyebrowse-restore--list-backups ()
"List all files stored in the `eyebrowse-restore-dir'directory."
(let* ((with-attrs (directory-files-and-attributes eyebrowse-restore-dir))
(sort-by-date (lambda (x y) (time-less-p (nth 5 y) (nth 5 x))))
(sorted (sort with-attrs sort-by-date))
(files (mapcar #'car sorted)))
(seq-filter (lambda (x) (not (member x '("." "..")))) files)))
(defun eyebrowse-restore--list-unused-backups ()
"Return unused backups.
Return a list of unused backups while respecting the
`eyebrowse-restore-keep-old-backups' number of unused
backups to keep."
(-slice (seq-filter #'eyebrowse-restore--unused-backup-p
(eyebrowse-restore--list-backups))
eyebrowse-restore-keep-old-backups))
(defun eyebrowse-restore--unused-backup-p (name)
"Return t if there isn't any frame with this `NAME'."
(not (member
name
(mapcar (lambda (x) (frame-parameter x 'name))
(frame-list)))))
(defun eyebrowse-restore--remove-unused-backups ()
"Remove unused backups.
Remove all files from the `eyebrowse-restore-dir' that
doesn't correspond with any of the active frames or is
older than `eyebrowse-restore-keep-old-backups'."
(dolist (name (eyebrowse-restore--list-unused-backups))
(delete-file (concat (file-name-as-directory eyebrowse-restore-dir) name))))
;;;; Footer
(provide 'eyebrowse-restore)
;;; eyebrowse-restore.el ends here