diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2022-05-03 21:22:53 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2022-05-03 21:22:53 +0200 |
commit | 59353ec7b579213de3c70950d5d938b7540ce72f (patch) | |
tree | e7f3cae34f7c91cb880a01a85565c00086101a37 /lisp/emacs-lisp/subr-x.el | |
parent | 8ef34a065a10330777b172a7e5608f7939e7af29 (diff) | |
download | emacs-59353ec7b579213de3c70950d5d938b7540ce72f.tar.gz emacs-59353ec7b579213de3c70950d5d938b7540ce72f.tar.bz2 emacs-59353ec7b579213de3c70950d5d938b7540ce72f.zip |
Add new macro with-buffer-unmodified-if-unchanged
* lisp/emacs-lisp/subr-x.el (with-buffer-unmodified-if-unchanged):
New macro.
* lisp/textmodes/fill.el (fill-paragraph): Macro code copied from
here. Adjust and use the macro.
Diffstat (limited to 'lisp/emacs-lisp/subr-x.el')
-rw-r--r-- | lisp/emacs-lisp/subr-x.el | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 6c763bd04d9..afa0423d90e 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -416,6 +416,31 @@ this defaults to the current buffer." (error "No process selected")) process))) +(defmacro with-buffer-unmodified-if-unchanged (&rest body) + "Like `progn', but change buffer modification status only if buffer is changed. +That is, if the buffer is marked as unmodified before BODY, and +BODY does modifications that, in total, means that the buffer is +identical to the buffer before BODY, mark the buffer as +unmodified again. In other words, this won't change buffer +modification status: + + (with-buffer-unmodified-if-unchanged + (insert \"a\") + (delete-char -1))" + (declare (debug t) (indent 0)) + (let ((hash (gensym))) + `(let ((,hash (and (not (buffer-modified-p)) + (buffer-hash)))) + (prog1 + (progn + ,@body) + ;; If we didn't change anything in the buffer (and the buffer + ;; was previously unmodified), then flip the modification status + ;; back to "unchanged". + (when (and ,hash + (equal ,hash (buffer-hash))) + (set-buffer-modified-p nil)))))) + (provide 'subr-x) ;;; subr-x.el ends here |