From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.devel Subject: Re: [RFC]: replace-region-contents Date: Wed, 06 Feb 2019 10:55:36 +0100 Message-ID: <87ftt1b5rb.fsf@mbork.pl> References: <871s4rqk7u.fsf@gnu.org> <87o97syvno.fsf@gnu.org> <878syubwv3.fsf@gnu.org> <877eede3vk.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="53101"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: mu4e 1.1.0; emacs 27.0.50 Cc: Stefan Monnier , emacs-devel@gnu.org To: Tassilo Horn Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Feb 06 10:57:02 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1grJwy-000DbQ-JR for ged-emacs-devel@m.gmane.org; Wed, 06 Feb 2019 10:57:01 +0100 Original-Received: from localhost ([127.0.0.1]:47793 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grJwx-0000rm-Ii for ged-emacs-devel@m.gmane.org; Wed, 06 Feb 2019 04:56:59 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:57225) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grJwp-0000pp-8x for emacs-devel@gnu.org; Wed, 06 Feb 2019 04:56:52 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1grJwo-0008H6-3G for emacs-devel@gnu.org; Wed, 06 Feb 2019 04:56:51 -0500 Original-Received: from mail.mojserwer.eu ([195.110.48.8]:59128) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grJwn-00087d-Oa; Wed, 06 Feb 2019 04:56:49 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 523C4E669E; Wed, 6 Feb 2019 10:56:30 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pUc8bEaqO8kr; Wed, 6 Feb 2019 10:56:21 +0100 (CET) Original-Received: from localhost (83.25.59.168.ipv4.supernova.orange.pl [83.25.59.168]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 56A62E6659; Wed, 6 Feb 2019 10:56:21 +0100 (CET) In-reply-to: <877eede3vk.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.110.48.8 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:233037 Archived-At: On 2019-02-06, at 09:07, Tassilo Horn wrote: > Stefan Monnier 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? Also, at least sometimes, buffers are better than strings to perform e.g. replacements. (I have a blog post in the works where I actually measure the performance of one over the other, and due to immutability of strings there is a lot of GC overhead when you do string replacements a lot.) Best, -- Marcin Borkowski http://mbork.pl