From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [RFC]: replace-region-contents Date: Tue, 05 Feb 2019 08:43:25 -0500 Message-ID: References: <871s4rqk7u.fsf@gnu.org> <87o97syvno.fsf@gnu.org> <878syubwv3.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="84213"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Tassilo Horn Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Feb 05 14:44:01 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 1gr117-000LkW-Kv for ged-emacs-devel@m.gmane.org; Tue, 05 Feb 2019 14:44:01 +0100 Original-Received: from localhost ([127.0.0.1]:60494 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gr116-0007zA-G7 for ged-emacs-devel@m.gmane.org; Tue, 05 Feb 2019 08:44:00 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:54091) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gr10r-0007xh-U9 for emacs-devel@gnu.org; Tue, 05 Feb 2019 08:43:46 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gr10q-0006Oo-D4 for emacs-devel@gnu.org; Tue, 05 Feb 2019 08:43:45 -0500 Original-Received: from chene.dit.umontreal.ca ([132.204.246.20]:46228) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gr10d-0006EA-Mx; Tue, 05 Feb 2019 08:43:32 -0500 Original-Received: from pastel.home (lechon.iro.umontreal.ca [132.204.27.242]) by chene.dit.umontreal.ca (8.14.7/8.14.1) with ESMTP id x15DhPTV026833; Tue, 5 Feb 2019 08:43:25 -0500 Original-Received: by pastel.home (Postfix, from userid 20848) id 8D9B669522; Tue, 5 Feb 2019 08:43:25 -0500 (EST) In-Reply-To: <878syubwv3.fsf@gnu.org> (Tassilo Horn's message of "Tue, 05 Feb 2019 06:57:52 +0100") X-NAI-Spam-Flag: NO X-NAI-Spam-Threshold: 5 X-NAI-Spam-Score: 0 X-NAI-Spam-Rules: 2 Rules triggered EDT_SA_DN_PASS=0, RV6476=0 X-NAI-Spam-Version: 2.3.0.9418 : core <6476> : inlines <7011> : streams <1812158> : uri <2791249> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 132.204.246.20 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:232988 Archived-At: > --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 > I guess Eli might argue that this version encourages passing strings > around instead of using buffers. Passing strings around is not bad. I think many applications where replace-region-contents is desirable will generate strings more efficiently than buffers rather than the other way around (after all, if the rest can be in a buffer, there's a good chance that you can do the modifications in-place on-the-fly rather than go through replace-region-contents). > I'd explain in the doc string that in the case of a string return > value, we're going thru a temporary buffer anyway, so if your > REPLACE-FN ends in (buffer-substring ...), you're clearly doing > something wrong... Sounds good. Stefan