emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH RFC v1] subr.el: add with-temp-indirect-buffer


From: Lockywolf
Subject: [PATCH RFC v1] subr.el: add with-temp-indirect-buffer
Date: Tue, 1 Aug 2023 12:28:13 +0800

Add a macro to make a indirect clone of current buffer and run
some code in its context.
---
I would like to propose the following change, which implements a
macro called `with-temp-indirect-buffer', which can be useful
when writing automation codes which manipulate buffer view
properties, such as narrowing and widening.

lisp/subr.el | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/lisp/subr.el b/lisp/subr.el
index 2e2caf9fe27..1de0cb1d297 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -4890,6 +4890,25 @@ with-temp-buffer
            (and (buffer-name ,temp-buffer)
                 (kill-buffer ,temp-buffer)))))))
 
+(defmacro with-temp-indirect-buffer (&rest body)
+  "Create a temporary indirect buffer, and evaluate BODY there like `progn'.
+The buffer does not run the hooks `kill-buffer-hook',
+`kill-buffer-query-functions', and `buffer-list-update-hook'.
+See also `with-temp-file' and `with-output-to-string'."
+  (declare (indent 0) (debug t))
+  (let* ((temp-buffer (make-symbol "temp-indirect-buffer"))
+         (temp-buffer-name (generate-new-buffer-name (buffer-name))))
+    `(let ((,temp-buffer (clone-indirect-buffer ,temp-buffer-name nil t)))
+       ;; `kill-buffer' can change current-buffer in some odd cases.
+       ;; 2023-08-01: I have no idea what this means, so when writing
+       ;; this macro I just blindly copied the pattern from the
+       ;; with-temp-buffer
+       (with-current-buffer ,temp-buffer
+         (unwind-protect
+            (progn ,@body)
+           (and (buffer-name ,temp-buffer)
+              (kill-buffer ,temp-buffer)))))))
+
 (defmacro with-silent-modifications (&rest body)
   "Execute BODY, pretending it does not modify the buffer.
 This macro is typically used around modifications of
-- 
2.35.8




reply via email to

[Prev in Thread] Current Thread [Next in Thread]