unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH RFC v1] subr.el: add with-temp-indirect-buffer
@ 2023-08-01  4:28 Lockywolf
  2023-08-12  6:43 ` Eli Zaretskii
  0 siblings, 1 reply; 2+ messages in thread
From: Lockywolf @ 2023-08-01  4:28 UTC (permalink / raw)
  To: emacs-devel; +Cc: Lockywolf

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




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH RFC v1] subr.el: add with-temp-indirect-buffer
  2023-08-01  4:28 [PATCH RFC v1] subr.el: add with-temp-indirect-buffer Lockywolf
@ 2023-08-12  6:43 ` Eli Zaretskii
  0 siblings, 0 replies; 2+ messages in thread
From: Eli Zaretskii @ 2023-08-12  6:43 UTC (permalink / raw)
  To: Lockywolf; +Cc: emacs-devel, for_emacs.clonebuffer_2023-08-01

> From: Lockywolf <for_emacs.clonebuffer_2023-08-01@lockywolf.net>
> Cc: Lockywolf <for_emacs.clonebuffer_2023-08-01@lockywolf.net>
> 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.

Does anyone think we need this macro in Emacs?  Did people frequently
had to invent something like that?

If this is an infrequent need, I'd rather leave this to Lisp programs
that need it.

Thanks.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-12  6:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-01  4:28 [PATCH RFC v1] subr.el: add with-temp-indirect-buffer Lockywolf
2023-08-12  6:43 ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).