|
@@ -1,69 +1,113 @@
|
|
|
#+begin_src emacs-lisp
|
|
|
|
|
|
-; Warn and ask user before saving buffers that have shurnk too much
|
|
|
-;
|
|
|
-; My .org files usually only grow. If one shrinks much,
|
|
|
-; I want to know/confirming it before saving.
|
|
|
-; The code below accomplishes it this.
|
|
|
-;
|
|
|
-; There is a hack. before-save-hook provides no way for a hook
|
|
|
-; function to abort the write (e.g. if the user answers "no").
|
|
|
-; The only solution I could come up with to throw an "error"
|
|
|
-; is to set the file read-only and let the rest of the write
|
|
|
-; process complain. This is not elegant. Suggestions for
|
|
|
-; improvment solicited.
|
|
|
-
|
|
|
-; Maybe do this instead. Or in addition
|
|
|
-; https://karl-voit.at/2014/08/20/org-losses-determining-post-commit/
|
|
|
-
|
|
|
-;; Copyright © 2023 by George Jones
|
|
|
-
|
|
|
-;; Author: George Jones (http://port111.com/george)
|
|
|
-;; Version:
|
|
|
-;; Package-Version:
|
|
|
-;; Package-Commit:
|
|
|
-;; Created: 2023-01-15
|
|
|
-;; Package-Requires:
|
|
|
-;; Keywords:
|
|
|
-;; License: GPL v3
|
|
|
-;; URL: http://ergoemacs.org/emacs/xah-lookup.html
|
|
|
-
|
|
|
-;; License: GPL v3
|
|
|
+;; ChatGPT prompt:
|
|
|
+;;
|
|
|
+;; in emacs lisp how do I write a before-save-hook that aborts the save
|
|
|
+;; based on a yes no question?
|
|
|
+;;
|
|
|
+;;Chat GPT answer
|
|
|
+;;(defun my-before-save-hook ()
|
|
|
+;; (when (not (yes-or-no-p "Save changes? "))
|
|
|
+;; (set-buffer-modified-p nil)
|
|
|
+;; (error "Save Aborted")))
|
|
|
+;;(add-hook 'before-save-hook 'my-before-save-hook)
|
|
|
+
|
|
|
|
|
|
(defvar check-buffer-shrinkage-delta (* 1 1000)
|
|
|
"Warn the user before saving is the buffer shrinks more than this many bytes")
|
|
|
-(setq check-buffer-shrinkage-delta 100)
|
|
|
+(setq check-buffer-shrinkage-delta 5)
|
|
|
|
|
|
(defvar check-buffer-shrinkage-name "org$"
|
|
|
"Warn user before saving files matching this pattern that have shurnk")
|
|
|
|
|
|
-(defun check-buffer-size-on-save ()
|
|
|
- "Warn user before writing if buffer has shrunk too much since last save.
|
|
|
- Check for shrinkage large than CHECK-BUFFER-SHRINKAGE-DELTA bytes.
|
|
|
- Match file names matching CHECK-BUFFER-SHRINKAGE-NAME"
|
|
|
-
|
|
|
- (if (string-match check-buffer-shrinkage-name (buffer-file-name))
|
|
|
+(defun my-before-save-hook ()
|
|
|
+ (when (string-match check-buffer-shrinkage-name (buffer-file-name))
|
|
|
(let ((delta (- buffer-saved-size (buffer-size))))
|
|
|
(when (> delta check-buffer-shrinkage-delta)
|
|
|
- (unless (y-or-n-p (format "WARNING: Buffer has shurnk by %d bytes since last save?" delta))
|
|
|
- (progn
|
|
|
- ; This is a hack. There appears to be no way to have
|
|
|
- ; a before-save-hook abort. I've tried user-error, throw,
|
|
|
- ; and debug-on-error. It ignores them all, so here once
|
|
|
- ; I've determined that I DON'T want the write to succeed
|
|
|
- ; I will set mode to read-only.
|
|
|
- (message (format "Setting %s to mode 444 to keep write from succeeding" (buffer-file-name)))
|
|
|
- (set-file-modes (buffer-file-name) #o444 'nofollow)
|
|
|
- ; This should abort, but is being ignored when hooks run.
|
|
|
- (user-error "Error: %s" "Not saved. User aborted.")
|
|
|
- ) ; progn
|
|
|
- ) ; unless
|
|
|
- ) ; when
|
|
|
- ) ; let
|
|
|
-) ; if
|
|
|
-) ; defun
|
|
|
-
|
|
|
-;; This debug is NOT being honored. No way to abort save
|
|
|
-;; (setq debug-on-error t)
|
|
|
-(add-hook 'before-save-hook 'check-buffer-size-on-save)
|
|
|
+ (when (not (yes-or-no-p "Buffer shrunk, save changes? "))
|
|
|
+ (set-buffer-modified-p nil)
|
|
|
+ (error "Save Aborted"))))))
|
|
|
+(add-hook 'before-save-hook 'my-before-save-hook)
|
|
|
+
|
|
|
+ ;; Chat GPT answer
|
|
|
+ ;; (defun my-before-save-hook ()
|
|
|
+ ;; (if (string-match check-buffer-shrinkage-name (buffer-file-name))
|
|
|
+ ;; (let ((delta (- buffer-saved-size (buffer-size))))
|
|
|
+ ;; (when (> delta check-buffer-shrinkage-delta)
|
|
|
+ ;; (when (not (yes-or-no-p "Buffer shrunk, save changes? "))
|
|
|
+ ;; (set-buffer-modified-p nil)
|
|
|
+ ;; (error "Save Aborted"))))))
|
|
|
+
|
|
|
+ (add-hook 'before-save-hook 'my-before-save-hook)
|
|
|
+
|
|
|
+
|
|
|
+ ;; ; Warn and ask user before saving buffers that have shurnk too much
|
|
|
+ ;; ;
|
|
|
+ ;; ; My .org files usually only grow. If one shrinks much,
|
|
|
+ ;; ; I want to know/confirming it before saving.
|
|
|
+ ;; ; The code below accomplishes it this.
|
|
|
+ ;; ;
|
|
|
+ ;; ; There is a hack. before-save-hook provides no way for a hook
|
|
|
+ ;; ; function to abort the write (e.g. if the user answers "no").
|
|
|
+ ;; ; The only solution I could come up with to throw an "error"
|
|
|
+ ;; ; is to set the file read-only and let the rest of the write
|
|
|
+ ;; ; process complain. This is not elegant. Suggestions for
|
|
|
+ ;; ; improvment solicited.
|
|
|
+
|
|
|
+ ;; ; Maybe do this instead. Or in addition
|
|
|
+ ;; ; https://karl-voit.at/2014/08/20/org-losses-determining-post-commit/
|
|
|
+
|
|
|
+ ;; ;; Copyright © 2023 by George Jones
|
|
|
+
|
|
|
+ ;; ;; Author: George Jones (http://port111.com/george)
|
|
|
+ ;; ;; Version:
|
|
|
+ ;; ;; Package-Version:
|
|
|
+ ;; ;; Package-Commit:
|
|
|
+ ;; ;; Created: 2023-01-15
|
|
|
+ ;; ;; Package-Requires:
|
|
|
+ ;; ;; Keywords:
|
|
|
+ ;; ;; License: GPL v3
|
|
|
+ ;; ;; URL: http://ergoemacs.org/emacs/xah-lookup.html
|
|
|
+
|
|
|
+ ;; ;; License: GPL v3
|
|
|
+
|
|
|
+ ;; (defvar check-buffer-shrinkage-delta (* 1 1000)
|
|
|
+ ;; "Warn the user before saving is the buffer shrinks more than this many bytes")
|
|
|
+ ;; (setq check-buffer-shrinkage-delta 100)
|
|
|
+
|
|
|
+ ;; (defvar check-buffer-shrinkage-name "org$"
|
|
|
+ ;; "Warn user before saving files matching this pattern that have shurnk")
|
|
|
+
|
|
|
+ ;; (defun check-buffer-size-on-save ()
|
|
|
+ ;; "Warn user before writing if buffer has shrunk too much since last save.
|
|
|
+ ;; Check for shrinkage large than CHECK-BUFFER-SHRINKAGE-DELTA bytes.
|
|
|
+ ;; Match file names matching CHECK-BUFFER-SHRINKAGE-NAME"
|
|
|
+
|
|
|
+ ;; (if (string-match check-buffer-shrinkage-name (buffer-file-name))
|
|
|
+ ;; (let ((delta (- buffer-saved-size (buffer-size))))
|
|
|
+ ;; (when (> delta check-buffer-shrinkage-delta)
|
|
|
+ ;; (unless (y-or-n-p (format "WARNING: Buffer has shurnk by %d bytes since last save?" delta))
|
|
|
+ ;; (progn
|
|
|
+ ;; ; This is a hack. There appears to be no way to have
|
|
|
+ ;; ; a before-save-hook abort. I've tried user-error, throw,
|
|
|
+ ;; ; and debug-on-error. It ignores them all, so here once
|
|
|
+ ;; ; I've determined that I DON'T want the write to succeed
|
|
|
+ ;; ; I will set mode to read-only.
|
|
|
+ ;; (message (format "Setting %s to mode 444 to keep write from succeeding" (buffer-file-name)))
|
|
|
+ ;; (set-file-modes (buffer-file-name) #o444 'nofollow)
|
|
|
+ ;; ; This should abort, but is being ignored when hooks run.
|
|
|
+ ;; (user-error "Error: %s" "Not saved. User aborted.")
|
|
|
+ ;; ) ; progn
|
|
|
+ ;; ) ; unless
|
|
|
+ ;; ) ; when
|
|
|
+ ;; ) ; let
|
|
|
+ ;; ) ; if
|
|
|
+ ;; ) ; defun
|
|
|
+
|
|
|
+ ;; ;; This debug is NOT being honored. No way to abort save
|
|
|
+ ;; ;; (setq debug-on-error t)
|
|
|
+ ;; (add-hook 'before-save-hook 'check-buffer-size-on-save)
|
|
|
#+end_src
|
|
|
+
|
|
|
+#+RESULTS:
|
|
|
+| time-stamp | whitespace-cleanup | my-before-save-hook | gmj-update-date-save-hook |
|