all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tsdh@gnu.org>
To: Marcin Borkowski <mbork@mbork.pl>
Cc: Stefan Monnier <monnier@IRO.UMontreal.CA>, emacs-devel@gnu.org
Subject: Re: [RFC]: replace-region-contents
Date: Wed, 06 Feb 2019 12:10:21 +0100	[thread overview]
Message-ID: <8736p1dvfm.fsf@gnu.org> (raw)
In-Reply-To: <87ftt1b5rb.fsf@mbork.pl> (Marcin Borkowski's message of "Wed, 06 Feb 2019 10:55:36 +0100")

Marcin Borkowski <mbork@mbork.pl> writes:

>>>> --8<---------------cut here---------------start------------->8---
>>>> (defun replace-region-contents (beg end replace-fn)
>>>>   (save-excursion
>>>>     (save-restriction
>>>>       (narrow-to-region beg end)
>>>>       (goto-char (point-min))
>>>>       (let ((repl (funcall replace-fn)))
>>>> 	(if (bufferp repl)
>>>> 	    (replace-buffer-contents repl)
>>>> 	  (let ((source-buffer (current-buffer)))
>>>> 	    (with-temp-buffer
>>>> 	      (insert repl)
>>>> 	      (let ((tmp-buffer (current-buffer)))
>>>> 		(set-buffer source-buffer)
>>>> 		(replace-buffer-contents tmp-buffer)))))))))
>>>> --8<---------------cut here---------------end--------------->8---
>>>
>>> LGTM
>>
>> How would I actually use that version with a replace-fn returning a
>> buffer and not a string?  It looks to me that I need to do the whole
>> ceremony of creating a temporary buffer, setting buffers, and ensuring
>> that the temporary buffer is killed even in the case of an abnormal exit
>> myself.  That's the hassle my original version tried to eliminate in the
>> first place...
>
> I did not follow the whole thread, but why wouldn't `with-temp-buffer'
> be a suitable candidate to conduct exactly the ceremony you mentioned?

You cannot use `with-temp-buffer' inside replace-fn and then return that
temp buffer, because it'll be killed as soon as control flow escapes it.
So you need to define your replace-fn in the lexical scope of an already
established `with-temp-buffer' form.  But since you will need to access
the source buffer, you need to manually arrange in which buffer to
operate what.

> Also, at least sometimes, buffers are better than strings to perform
> e.g. replacements.

Yes, sure, although replacements could be performed in the source buffer
itself.

Bye,
Tassilo



  reply	other threads:[~2019-02-06 11:10 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-01 21:20 [RFC]: replace-region-contents Tassilo Horn
2019-02-02  9:33 ` Marcin Borkowski
2019-02-02 13:57   ` Tassilo Horn
2019-02-02 15:42 ` Stefan Monnier
2019-02-04  5:23   ` Tassilo Horn
2019-02-05  2:56     ` Stefan Monnier
2019-02-05  5:57       ` Tassilo Horn
2019-02-05 13:21         ` Tassilo Horn
2019-02-05 16:36           ` Eli Zaretskii
2019-02-05 17:19             ` Tassilo Horn
2019-02-06 16:00               ` Eli Zaretskii
2019-02-06 17:02                 ` Tassilo Horn
2019-02-06 17:33                   ` Eli Zaretskii
2019-02-06 18:07                     ` Tassilo Horn
2019-02-08 16:23                       ` Tassilo Horn
2019-02-08 16:28                         ` Stefan Monnier
2019-02-08 17:17                           ` Tassilo Horn
2019-02-08 21:37                             ` Eli Zaretskii
2019-02-08 21:53                               ` Stefan Monnier
2019-02-08 21:27                         ` Eli Zaretskii
2019-02-08 22:03                           ` Tassilo Horn
2019-02-08 22:19                             ` Eli Zaretskii
2019-02-09  0:00                           ` Tassilo Horn
2019-02-09  8:26                             ` Eli Zaretskii
2019-02-09  8:52                               ` Tassilo Horn
2019-02-05 13:43         ` Stefan Monnier
2019-02-06  8:07           ` Tassilo Horn
2019-02-06  9:55             ` Marcin Borkowski
2019-02-06 11:10               ` Tassilo Horn [this message]
2019-02-06 14:09             ` Stefan Monnier
2019-02-05 16:11         ` Eli Zaretskii
2019-02-02 16:17 ` Stefan Monnier
2019-02-03 15:59   ` Eli Zaretskii
2019-02-03 17:05     ` Stefan Monnier
2019-02-03 17:18       ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8736p1dvfm.fsf@gnu.org \
    --to=tsdh@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=mbork@mbork.pl \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.