From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: "Garreau\, Alexandre" Newsgroups: gmane.emacs.devel Subject: `with-temp-buffer', `with-output-to-string': `with-temp-buffer-to-string' / `with-current-buffer-to-string' [Was: Re: `call-process', to a string] Date: Mon, 29 Oct 2018 21:37:41 +0100 Message-ID: <87in1ksd4a.fsf_-_@portable.galex-713.eu> References: <87woq0sf4a.fsf@portable.galex-713.eu> <87pnvssegv.fsf@portable.galex-713.eu> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1540852616 21892 195.159.176.226 (29 Oct 2018 22:36:56 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 29 Oct 2018 22:36:56 +0000 (UTC) User-Agent: Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu, GTK+ Version 3.22.11) of 2017-09-15, modified by Debian To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Oct 29 23:36:52 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHG9T-0005aK-Uf for ged-emacs-devel@m.gmane.org; Mon, 29 Oct 2018 23:36:52 +0100 Original-Received: from localhost ([::1]:49385 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHGBa-0004fa-4W for ged-emacs-devel@m.gmane.org; Mon, 29 Oct 2018 18:39:02 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37338) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHGBQ-0004eJ-Tf for emacs-devel@gnu.org; Mon, 29 Oct 2018 18:38:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gHGBQ-0001Rd-3j for emacs-devel@gnu.org; Mon, 29 Oct 2018 18:38:52 -0400 Original-Received: from portable.galex-713.eu ([2a00:5884:8305::1]:59518) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gHGBP-0001RT-Rt for emacs-devel@gnu.org; Mon, 29 Oct 2018 18:38:52 -0400 Original-Received: from [::1] (helo=portable.galex-713.eu) by portable.galex-713.eu with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gHEIA-00074R-4r for emacs-devel@gnu.org; Mon, 29 Oct 2018 21:37:55 +0100 PGP-FINGERPRINT: E109 9988 4197 D7CB B0BC 5C23 8DEB 24BA 867D 3F7F Accept-Language: fr, en, eo, it, br In-Reply-To: <87pnvssegv.fsf@portable.galex-713.eu> (Alexandre Garreau's message of "Mon, 29 Oct 2018 21:08:32 +0100") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:5884:8305::1 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:230779 Archived-At: On 2018-10-29 at 21:08, Garreau, Alexandre wrote: > On 2018-10-29 at 20:54, Garreau, Alexandre wrote: >> (substring >> (with-temp-buffer >> (call-process "xdg-user-dir" nil t nil "DOWNLOAD") >> (buffer-string)) >> 0 -1) >> >> I am not sure this is the simplest way to call a process and get its >> output as string, without using the shell (there seem to be a >> shell-command-to-string, but no call-process-to-string, writing another >> mail about this). Searching for a better form, I found `with-output-to-string', which, in name, looks like what I want, but is for standard-output, for when emacs is ran without a frame I guess. May I suggest a new form `with-temp-buffer-to-string', inspired from `with-output-to-string', but for what programs might see as current buffer:=20 #+BEGIN_SRC emacs-lisp (defmacro with-temp-buffer-to-string (&rest body) "Execute BODY, return the text it sent to current-buffer, as a string." (declare (indent 0) (debug t)) `(with-temp-buffer (progn ,@body (goto-char (point-max)) (when (=3D ?\n (char-before)) (delete-char -1)) (buffer-string)))) #+END_SRC Btw, in `with-output-to-string', instead of just using `generate-new-buffer', it directly does =E2=80=9C(get-buffer-create (generate-new-buffer-name " *string-output*"))=E2=80=9D: is there a reason = for that? otherwise it=E2=80=99s just longer and less readable.