From 84139db9a8869b31f831fb154bdc209e06db5ab3 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 6 Jan 2022 15:47:09 -0600 Subject: Add user-save-mode --- lisp/user-save.el | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lisp/user-save.el (limited to 'lisp') 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 @@ +;;; 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 -- cgit 1.4.1-21-gabe81