From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.devel Subject: Re: [RFC]: replace-region-contents Date: Wed, 06 Feb 2019 12:10:21 +0100 Message-ID: <8736p1dvfm.fsf@gnu.org> References: <871s4rqk7u.fsf@gnu.org> <87o97syvno.fsf@gnu.org> <878syubwv3.fsf@gnu.org> <877eede3vk.fsf@gnu.org> <87ftt1b5rb.fsf@mbork.pl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="136025"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Stefan Monnier , emacs-devel@gnu.org To: Marcin Borkowski Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Feb 06 12:10:51 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 1grL6R-000ZHi-Ks for ged-emacs-devel@m.gmane.org; Wed, 06 Feb 2019 12:10:51 +0100 Original-Received: from localhost ([127.0.0.1]:48671 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grL6Q-0006U3-Fc for ged-emacs-devel@m.gmane.org; Wed, 06 Feb 2019 06:10:50 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:51312) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grL6C-0006S9-27 for emacs-devel@gnu.org; Wed, 06 Feb 2019 06:10:37 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:37419) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1grL63-0007rh-UM; Wed, 06 Feb 2019 06:10:30 -0500 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:48721) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1grL60-0001uu-OT; Wed, 06 Feb 2019 06:10:26 -0500 Original-Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 0FA7521E49; Wed, 6 Feb 2019 06:10:24 -0500 (EST) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Wed, 06 Feb 2019 06:10:24 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrkeekgddviecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvufhfffgjkf gfgggtsehttdertddtredtnecuhfhrohhmpefvrghsshhilhhoucfjohhrnhcuoehtshgu hhesghhnuhdrohhrgheqnecukfhppedvuddvrddukeehrddvtdefrddufeegnecurfgrrh grmhepmhgrihhlfhhrohhmpehthhhorhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgr lhhithihqdekieejfeekjeekgedqieefhedvleekqdhtshguhheppehgnhhurdhorhhgse hfrghsthhmrghilhdrfhhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Original-Received: from pchta-and (portal.shd.de [212.185.203.134]) by mail.messagingengine.com (Postfix) with ESMTPA id 80D6F10288; Wed, 6 Feb 2019 06:10:22 -0500 (EST) In-Reply-To: <87ftt1b5rb.fsf@mbork.pl> (Marcin Borkowski's message of "Wed, 06 Feb 2019 10:55:36 +0100") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] 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:233040 Archived-At: Marcin Borkowski 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