diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/user-save.el | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lisp/user-save.el b/lisp/user-save.el new file mode 100644 index 0000000..01adc22 --- /dev/null +++ b/lisp/user-save.el | |||
@@ -0,0 +1,45 @@ | |||
1 | ;;; user-save.el -*- lexical-binding: t; -*- | ||
2 | |||
3 | ;; Because `super-save-mode' automatically saves every time we move away from a | ||
4 | ;; buffer, it tends to run a lot of `before-save-hook's that don't need to be | ||
5 | ;; run that often. For that reason, I'm writing a mode where C-x C-s saves | ||
6 | ;; /and/ runs all the "real" before-save-hooks, so that super-save won't | ||
7 | ;; automatically do things like format the buffer all the time. | ||
8 | |||
9 | ;;; Code: | ||
10 | |||
11 | (defvar user-save-hook nil | ||
12 | "Hook to run when the user, not Emacs, saves the buffer.") | ||
13 | |||
14 | (defvar user-save-mode-map (let ((map (make-sparse-keymap))) | ||
15 | (define-key map (kbd "C-x C-s") #'user-save-buffer) | ||
16 | map) | ||
17 | "Keymap for `user-save-mode'. | ||
18 | This map shadows the default map for `save-buffer'.") | ||
19 | |||
20 | (defun user-save-buffer (&optional arg) | ||
21 | "Save current buffer in visited file if modified. | ||
22 | This function is precisely the same as `save-buffer', but with | ||
23 | one modification: it also runs functions in `user-save-hook'. | ||
24 | This means that if you have some functionality in Emacs to | ||
25 | automatically save buffers periodically, but have hooks you want | ||
26 | to automatically run when the buffer saves that are | ||
27 | computationally expensive or just aren't something you want to | ||
28 | run all the time, put them in `user-save-hook'. | ||
29 | |||
30 | ARG is passed directly to `save-buffer'." | ||
31 | (interactive '(called-interactively)) | ||
32 | (message "Saving the buffer...") | ||
33 | (with-demoted-errors (run-hooks 'user-save-hook)) | ||
34 | (save-buffer arg) | ||
35 | (message "Saving the buffer...Done.")) | ||
36 | |||
37 | ;;;###autoload | ||
38 | (define-minor-mode user-save-mode | ||
39 | "Mode to enable an an extra user-save hook." | ||
40 | :lighter " US" | ||
41 | :global t | ||
42 | :keymap 'user-save-mode-map) | ||
43 | |||
44 | (provide 'user-save) | ||
45 | ;;; user-save.el ends here | ||