Add user-save-mode

This commit is contained in:
Case Duckworth 2022-01-06 15:47:09 -06:00
parent 81152ca242
commit 84139db9a8
2 changed files with 49 additions and 1 deletions

View File

@ -104,6 +104,9 @@
(setup (:require reading)
(:global "C-c C-r" #'reading-mode))
(setup (:require user-save)
(user-save-mode +1))
(setup +key
(+ensure-after-init #'+key-global-mode))
@ -450,7 +453,7 @@
"C-c C-l" #'+org-insert-link-dwim
"C-c C-n" #'+org-next-heading-widen
"C-c C-p" #'+org-previous-heading-widen)
(:local-hook before-save-hook #'+org-before-save@prettify-buffer)
(:local-hook user-save-hook #'+org-before-save@prettify-buffer)
(advice-add #'org-delete-backward-char :override #'+org-delete-backward-char)
(with-eval-after-load 'org
(org-clock-persistence-insinuate)

45
lisp/user-save.el Normal file
View File

@ -0,0 +1,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