summary refs log tree commit diff stats
path: root/lisp/user-save.el
blob: 8e01af1c4c3eb93f6d758e409c04a48da01fd839 (plain)
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
;;; user-save.el -*- lexical-binding: t; -*-

;; Because `super-save-mode' automatically saves every time we move away from a
;; buffer, it tends to run a lot of `before-save-hook's that don't need to be
;; run that often.  For that reason, I'm writing a mode where C-x C-s saves
;; /and/ runs all the "real" before-save-hooks, so that super-save won't
;; automatically do things like format the buffer all the time.

;;; Code:

(defgroup user-save nil
  "Group for `user-save-mode' customizations."
  :group 'emacs
  :prefix "user-save-")

(defcustom user-save-hook-into-kill-emacs nil
  "Add a hook to perform `user-save' to `kill-emacs-hook'.
This option is only useful is `user-save-mode' is active when
Emacs is killed."
  :type 'boolean)

(defvar user-save-hook nil
  "Hook to run when the user, not Emacs, saves the buffer.")

(defvar user-save-mode-map (let ((map (make-sparse-keymap)))
                             (define-key map (kbd "C-x C-s") #'user-save-buffer)
                             map)
  "Keymap for `user-save-mode'.
This map shadows the default map for `save-buffer'.")

(defun user-save-run-hooks (&rest _)
  "Run the hooks in `user-save-hook'.
This does /not/ also save the buffer."
  (with-demoted-errors "User-save-hook error: %S"
      (run-hooks 'user-save-hook)))

(defun user-save-buffer (&optional arg)
  "Save current buffer in visited file if modified.
This function is precisely the same as `save-buffer', but with
one modification: it also runs functions in `user-save-hook'.
This means that if you have some functionality in Emacs to
automatically save buffers periodically, but have hooks you want
to automatically run when the buffer saves that are
computationally expensive or just aren't something you want to
run all the time, put them in `user-save-hook'.

ARG is passed directly to `save-buffer'."
  (interactive '(called-interactively))
  (message "User-Saving the buffer...")
  (user-save-run-hooks)
  (save-buffer arg)
  (message "User-Saving the buffer...Done."))

(defun user-save-some-buffers (&optional pred)
  "Save some buffers as though the user saved them.
This function does not ask the user about each buffer, but PRED
is used in almost the same way as `save-some-buffers': if it's
nil or t, it will save all file-visiting modified buffers; if
it's a zero-argument function, that will be called to determine
whether the buffer needs to be saved."
  ;; This could maybe be much better.
  (interactive "P")
  (unless pred (setq pred save-some-buffers-default-predicate))
  (dolist (buf (buffer-list))
    (with-current-buffer buf
      (when (and (buffer-modified-p)
                 (buffer-file-name)
                 (or (null pred)
                     (if (functionp pred) (funcall pred) pred)))
        (user-save-buffer)))))

;;;###autoload
(define-minor-mode user-save-mode
  "Mode to enable an an extra user-save hook."
  :lighter " US"
  :keymap user-save-mode-map)

;;;###autoload
(defun turn-off-user-save-mode ()
  "Turn off `user-save-mode' in the current buffer."
  (user-save-mode -1))

;;;###autoload
(define-globalized-minor-mode user-save-global-mode user-save-mode user-save-mode
  (if user-save-global-mode
      (when user-save-hook-into-kill-emacs
        (add-hook 'kill-emacs-hook #'user-save-some-buffers))
    (remove-hook 'kill-emacs-hook #'user-save-some-buffers)))

(provide 'user-save)
;;; user-save.el ends here