diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2023-01-15 21:39:01 +0100 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2023-01-15 21:50:00 +0100 |
| commit | cae87e865bf35ea166b7ed6df93d44bb5d6dc7ad (patch) | |
| tree | 4f0015f71127a71d342a13e1bdd994ba64fa6ab0 /compat-29.el | |
| parent | ae4a2b5b4bad773976f3f60c33b42f43456d9094 (diff) | |
Add buffer-hash and with-buffer-unmodified-if-unchanged
Diffstat (limited to 'compat-29.el')
| -rw-r--r-- | compat-29.el | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/compat-29.el b/compat-29.el index fd3597c..cd94215 100644 --- a/compat-29.el +++ b/compat-29.el @@ -284,6 +284,43 @@ CONDITION." ;;;; Defined in subr-x.el +(compat-defmacro with-buffer-unmodified-if-unchanged (&rest body) ;; <OK> + "Like `progn', but change buffer-modified status only if buffer text changes. +If the buffer was unmodified before execution of BODY, and +buffer text after execution of BODY is identical to what it was +before, ensure that buffer is still marked unmodified afterwards. +For example, the following won't change the buffer's modification +status: + + (with-buffer-unmodified-if-unchanged + (insert \"a\") + (delete-char -1)) + +Note that only changes in the raw byte sequence of the buffer text, +as stored in the internal representation, are monitored for the +purpose of detecting the lack of changes in buffer text. Any other +changes that are normally perceived as \"buffer modifications\", such +as changes in text properties, `buffer-file-coding-system', buffer +multibyteness, etc. -- will not be noticed, and the buffer will still +be marked unmodified, effectively ignoring those changes." + (declare (debug t) (indent 0)) + (let ((hash (gensym)) + (buffer (gensym))) + `(let ((,hash (and (not (buffer-modified-p)) + (buffer-hash))) + (,buffer (current-buffer))) + (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 (buffer-live-p ,buffer)) + (with-current-buffer ,buffer + (when (and (buffer-modified-p) + (equal ,hash (buffer-hash))) + (restore-buffer-modified-p nil)))))))) + (compat-defun add-display-text-property (start end prop value ;; <OK> &optional object) "Add display property PROP with VALUE to the text from START to END. |
