about summary refs log tree commit diff stats
path: root/lisp/user-save.el
blob: 01adc22c696f17fab01003a439f8478475e4bedd (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
;;; 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:

(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-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 "Saving the buffer...")
  (with-demoted-errors (run-hooks 'user-save-hook))
  (save-buffer arg)
  (message "Saving the buffer...Done."))

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

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