-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsuper-save.el
More file actions
370 lines (316 loc) · 14 KB
/
Copy pathsuper-save.el
File metadata and controls
370 lines (316 loc) · 14 KB
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
367
368
369
370
;;; super-save.el --- Auto-save buffers, based on your activity. -*- lexical-binding: t -*-
;; Copyright © 2015-2026 Bozhidar Batsov <bozhidar@batsov.com>
;; Author: Bozhidar Batsov <bozhidar@batsov.com>
;; URL: https://github.com/bbatsov/super-save
;; Keywords: convenience
;; Version: 0.5.0
;; Package-Requires: ((emacs "27.1"))
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; super-save auto-saves your buffers when certain events happen - e.g. you
;; switch between buffers, an Emacs frame loses focus, etc. You can think of it
;; as both something that augments and replaces the standard auto-save-mode.
;;
;;; Code:
(require 'seq)
(declare-function org-edit-src-save "org-src" ())
(declare-function edit-indirect--commit "edit-indirect" ())
(defvar super-save-in-progress nil
"Non-nil when a super-save operation is in progress.
Save hook functions can check this variable to skip expensive
processing during automatic saves. For example:
(add-hook \\='before-save-hook
(lambda ()
(unless super-save-in-progress
(my-expensive-formatting-function))))")
(defgroup super-save nil
"Smart-saving of buffers."
:group 'tools
:group 'convenience)
(defcustom super-save-triggers nil
"A list of commands which would trigger `super-save-command'.
With `super-save-when-buffer-switched' enabled (the default), the
window-system hooks already catch all buffer switches, so this list
defaults to nil. You can still add commands here for triggers that
don't involve a buffer switch (e.g. `ace-window')."
:group 'super-save
:type '(repeat symbol)
:package-version '(super-save . "0.1.0"))
(defcustom super-save-hook-triggers nil
"A list of hooks which would trigger `super-save-command'.
With `super-save-when-buffer-switched' and `super-save-when-focus-lost'
enabled (the defaults), this list defaults to nil as those options cover
the common triggers."
:group 'super-save
:type '(repeat symbol)
:package-version '(super-save . "0.3.0"))
(defcustom super-save-when-focus-lost t
"Save buffers when an Emacs frame loses focus.
Uses `after-focus-change-function' to detect focus changes."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.5.0"))
(defcustom super-save-when-buffer-switched t
"Save buffers when the selected window's buffer changes.
Uses `window-buffer-change-functions' and
`window-selection-change-functions' to detect buffer switches.
This catches all buffer switches regardless of how they happen,
unlike `super-save-triggers' which only catches specific commands."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.5.0"))
(defcustom super-save-auto-save-when-idle nil
"Save automatically when Emacs is idle."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.2.0"))
(defcustom super-save-all-buffers nil
"Auto-save all buffers, not just the current one.
Setting this to t can be interesting when you make indirect buffer edits, like
when editing `grep' results with `occur-mode' and `occur-edit-mode', or when
running a project-wide search and replace with `project-query-replace-regexp'
and so on. In these cases, we can indirectly edit several buffers without
actually visiting or switching to these buffers. Hence, this option
allows you to automatically save these buffers, even when they aren't
visible in any window."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.4.0"))
(defcustom super-save-idle-duration 5
"Delay in seconds for which Emacs has to be idle before auto-saving.
See `super-save-auto-save-when-idle'."
:group 'super-save
:type 'integer
:package-version '(super-save . "0.2.0"))
(defcustom super-save-remote-files t
"Save remote files when t, ignore them otherwise."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.3.0"))
(defcustom super-save-silent nil
"Save silently, don't display any message."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.4.0"))
(defcustom super-save-delete-trailing-whitespace nil
"Controls whether to delete the trailing whitespace before saving.
Set to `except-current-line' if you want to avoid the current line."
:group 'super-save
:type '(choice (boolean :tag "Enable/disable deleting trailing whitespace for the whole buffer.")
(symbol :tag "Delete trailing whitespace except the current line." except-current-line))
:package-version '(super-save . "0.4.0"))
(defcustom super-save-exclude nil
"A list of regexps for `buffer-file-name' excluded from super-save.
When a `buffer-file-name' matches any of the regexps it is ignored."
:group 'super-save
:type '(repeat (choice regexp))
:package-version '(super-save . "0.4.0"))
(defcustom super-save-max-buffer-size nil
"Maximal size of buffer (in characters), for which super-save works.
Exists mostly because saving constantly huge buffers can be slow in some cases.
Set to 0 or nil to disable."
:group 'super-save
:type 'integer
:package-version '(super-save . "0.4.0"))
(defcustom super-save-predicates
'((lambda () buffer-file-name)
(lambda () (buffer-modified-p (current-buffer)))
(lambda () (file-writable-p buffer-file-name))
(lambda () (if (and super-save-max-buffer-size (> super-save-max-buffer-size 0))
(< (buffer-size) super-save-max-buffer-size)
t))
(lambda ()
(if (file-remote-p buffer-file-name) super-save-remote-files t))
(lambda () (super-save-include-p buffer-file-name))
(lambda () (verify-visited-file-modtime (current-buffer)))
(lambda () (file-directory-p (file-name-directory buffer-file-name))))
"Predicates which return nil when the buffer doesn't need to be saved.
Predicate functions don't take any arguments. If a predicate doesn't know
whether the buffer needs to be super-saved or not, it must return t."
:group 'super-save
:type '(repeat function)
:package-version '(super-save . "0.4.0"))
(defun super-save-include-p (filename)
"Return non-nil if FILENAME doesn't match any of the `super-save-exclude'."
(not (seq-some (lambda (regexp) (string-match-p regexp filename)) super-save-exclude)))
(defun super-save-p ()
"Return t when current buffer should be saved, otherwise return nil.
This function relies on the variable `super-save-predicates'."
(seq-every-p (lambda (pred)
(condition-case err
(funcall pred)
(error
(message "super-save: predicate %S failed: %S" pred err)
nil)))
super-save-predicates))
(defun super-save-delete-trailing-whitespace-maybe ()
"Delete trailing whitespace, optionally avoiding the current line.
See `super-save-delete-trailing-whitespace'."
(cond
((eq super-save-delete-trailing-whitespace 'except-current-line)
(let ((start (line-beginning-position))
(current (point)))
(save-excursion
(when (< (point-min) start)
(save-restriction
(narrow-to-region (point-min) (1- start))
(delete-trailing-whitespace)))
(when (> (point-max) current)
(save-restriction
(narrow-to-region current (point-max))
(delete-trailing-whitespace))))))
(super-save-delete-trailing-whitespace
(delete-trailing-whitespace))))
(defcustom super-save-handle-org-src t
"Save org-src edit buffers using `org-edit-src-save'."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.5.0"))
(defcustom super-save-handle-edit-indirect t
"Save edit-indirect buffers using `edit-indirect--commit'."
:group 'super-save
:type 'boolean
:package-version '(super-save . "0.5.0"))
(defun super-save-org-src-buffer-p ()
"Return non-nil if the current buffer is an org-src edit buffer."
(and (bound-and-true-p org-src-mode)
(fboundp 'org-edit-src-save)))
(defun super-save-edit-indirect-buffer-p ()
"Return non-nil if the current buffer is an edit-indirect buffer."
(and (bound-and-true-p edit-indirect--overlay)
(fboundp 'edit-indirect--commit)))
(defun super-save-maybe-silently (fn)
"Call FN, suppressing messages if `super-save-silent' is non-nil."
(if super-save-silent
(with-temp-message ""
(let ((inhibit-message t)
(inhibit-redisplay t)
(message-log-max nil))
(funcall fn)))
(funcall fn)))
(defun super-save-buffer (buffer)
"Save BUFFER if needed, super-save style."
(with-current-buffer buffer
(cond
;; org-src edit buffer
((and super-save-handle-org-src
(super-save-org-src-buffer-p)
(buffer-modified-p))
(super-save-maybe-silently #'org-edit-src-save))
;; edit-indirect buffer
((and super-save-handle-edit-indirect
(super-save-edit-indirect-buffer-p)
(buffer-modified-p))
(super-save-maybe-silently #'edit-indirect--commit))
;; regular file buffer
((super-save-p)
(super-save-delete-trailing-whitespace-maybe)
(super-save-maybe-silently #'basic-save-buffer)))))
(defun super-save-command ()
"Save the relevant buffers if needed.
When `super-save-all-buffers' is non-nil, save all modified buffers, else, save
only the current buffer.
Binds `super-save-in-progress' to t so that save hook functions can
detect automatic saves and skip expensive processing."
(let ((super-save-in-progress t))
(mapc #'super-save-buffer (if super-save-all-buffers (buffer-list) (list (current-buffer))))))
(defun super-save-command-idle ()
"Save buffers if needed, respecting per-buffer idle save settings.
Like `super-save-command', but skips buffers where the buffer-local
value of `super-save-auto-save-when-idle' is nil. This is the
callback used by the idle timer.
Binds `super-save-in-progress' to t so that save hook functions can
detect automatic saves and skip expensive processing."
(let ((super-save-in-progress t)
(buffers (if super-save-all-buffers (buffer-list) (list (current-buffer)))))
(dolist (buf buffers)
(when (buffer-local-value 'super-save-auto-save-when-idle buf)
(super-save-buffer buf)))))
(defvar super-save-idle-timer)
(defun super-save-command-advice (&rest _args)
"A simple wrapper around `super-save-command' that's advice-friendly."
(super-save-command))
(defun super-save-advise-trigger-commands ()
"Apply super-save advice to the commands listed in `super-save-triggers'."
(mapc
(lambda (command)
(advice-add command :before #'super-save-command-advice))
super-save-triggers))
(defun super-save-remove-advice-from-trigger-commands ()
"Remove super-save advice from the commands listed in `super-save-triggers'."
(mapc
(lambda (command)
(advice-remove command #'super-save-command-advice))
super-save-triggers))
(defun super-save-initialize-idle-timer ()
"Initialize super-save idle timer if `super-save-auto-save-when-idle' is true."
(setq super-save-idle-timer
(when super-save-auto-save-when-idle
(run-with-idle-timer super-save-idle-duration t #'super-save-command-idle))))
(defun super-save-stop-idle-timer ()
"Stop super-save idle timer if `super-save-idle-timer' is set."
(when super-save-idle-timer (cancel-timer super-save-idle-timer)))
(defun super-save-focus-change-handler ()
"Save buffers when Emacs loses focus (no frame is focused)."
(unless (seq-some #'frame-focus-state (frame-list))
(super-save-command)))
(defvar super-save--window-change-pending nil
"Non-nil when a window-change save is already scheduled for this command loop.")
(defun super-save-window-change-handler (&optional _frame)
"Save buffers when the window buffer or selection changes.
Intended for use with `window-buffer-change-functions' and
`window-selection-change-functions'. Uses a flag to avoid
duplicate saves when both hooks fire for the same event."
(unless super-save--window-change-pending
(setq super-save--window-change-pending t)
(run-at-time 0 nil (lambda ()
(setq super-save--window-change-pending nil)
(super-save-command)))))
(defun super-save-initialize ()
"Setup super-save's advice and hooks."
(super-save-advise-trigger-commands)
(super-save-initialize-idle-timer)
(dolist (hook super-save-hook-triggers)
(add-hook hook #'super-save-command))
(when super-save-when-focus-lost
(add-function :after after-focus-change-function
#'super-save-focus-change-handler))
(when super-save-when-buffer-switched
(add-hook 'window-buffer-change-functions #'super-save-window-change-handler)
(add-hook 'window-selection-change-functions #'super-save-window-change-handler)))
(defun super-save-stop ()
"Cleanup super-save's advice and hooks."
(super-save-remove-advice-from-trigger-commands)
(super-save-stop-idle-timer)
(dolist (hook super-save-hook-triggers)
(remove-hook hook #'super-save-command))
(remove-function after-focus-change-function
#'super-save-focus-change-handler)
(remove-hook 'window-buffer-change-functions #'super-save-window-change-handler)
(remove-hook 'window-selection-change-functions #'super-save-window-change-handler))
;;;###autoload
(define-minor-mode super-save-mode
"A minor mode that saves your Emacs buffers when they lose focus."
:lighter " super-save"
:group 'super-save
:global t
(cond
(super-save-mode (super-save-initialize))
(t (super-save-stop))))
(provide 'super-save)
;;; super-save.el ends here