[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

`with-temp-buffer', `with-output-to-string': `with-temp-buffer-to-string

From: Garreau\, Alexandre
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
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

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

#+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))
         (goto-char (point-max))
         (when (= ?\n (char-before))
           (delete-char -1))

Btw, in `with-output-to-string', instead of just using
`generate-new-buffer', it directly does “(get-buffer-create
(generate-new-buffer-name " *string-output*"))”: is there a reason for
that?  otherwise it’s just longer and less readable.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]