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: Tue, 05 Feb 2019 06:57:52 +0100 Message-ID: <878syubwv3.fsf@gnu.org> References: <871s4rqk7u.fsf@gnu.org> <87o97syvno.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="262262"; 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: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Feb 05 06:58:09 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 1gqtkF-00164l-Go for ged-emacs-devel@m.gmane.org; Tue, 05 Feb 2019 06:58:07 +0100 Original-Received: from localhost ([127.0.0.1]:54841 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqtkE-0000PP-H4 for ged-emacs-devel@m.gmane.org; Tue, 05 Feb 2019 00:58:06 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:49024) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqtk3-0000O4-L9 for emacs-devel@gnu.org; Tue, 05 Feb 2019 00:57:56 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:59114) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gqtk2-0001ih-IO; Tue, 05 Feb 2019 00:57:54 -0500 Original-Received: from auth2-smtp.messagingengine.com ([66.111.4.228]:49805) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.82) (envelope-from ) id 1gqtk2-0007YU-Fg; Tue, 05 Feb 2019 00:57:54 -0500 Original-Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 12A9A217CF; Tue, 5 Feb 2019 00:57:54 -0500 (EST) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Tue, 05 Feb 2019 00:57:54 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrkeehgdeklecutefuodetggdotefrodftvf 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 49854100BA; Tue, 5 Feb 2019 00:57:53 -0500 (EST) In-Reply-To: (Stefan Monnier's message of "Mon, 04 Feb 2019 21:56:54 -0500") 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:232982 Archived-At: Stefan Monnier writes: >>> Why two functions instead of just one? >> You mean by copying the region from the source buffer to the temporary >> buffer, and then a single function could act just in there? > > No, a function which directly returns the text to insert in the form > of a string (or a buffer, I guess). Like so? --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--- In the `json-pretty-print' scenario, that would indeed be even easier (because `json-encode' returns a string anyway): --8<---------------cut here---------------start------------->8--- (defun json-pretty-print (begin end) "Pretty-print selected region." (interactive "r") (let ((json-encoding-pretty-print t) ;; Distinguish an empty objects from 'null' (json-null :json-null) ;; Ensure that ordering is maintained (json-object-type 'alist)) (replace-region-contents begin end (lambda () (json-encode (json-read)))))) --8<---------------cut here---------------end--------------->8--- I don't have a preference. I guess Eli might argue that this version encourages passing strings around instead of using buffers. 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... Bye, Tassilo