help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Maybe we can improve this function call-process-to-string?


From: Jean Louis
Subject: Re: Maybe we can improve this function call-process-to-string?
Date: Thu, 8 Apr 2021 21:33:57 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Stefan Monnier <monnier@iro.umontreal.ca> [2021-04-08 20:50]:
> > (defun call-process-to-string (program &optional infile display &rest args)
> >   (let* ((buffer-name "Output")
> >      (buffer (generate-new-buffer buffer-name))
> >      (status (apply #'call-process program infile buffer display args))
> >      (current-buffer (current-buffer))
> >      (output (if status
> >                  (progn 
> >                    (switch-to-buffer buffer)
> >                    (buffer-string))
> >                "")))
> >     (switch-to-buffer current-buffer)
> >     output))
> 
> The docstring of `switch-to-buffer` is fairly long, but the first
> 5 lines are recommended reading:
> 
>     Display buffer BUFFER-OR-NAME in the selected window.
>     
>     WARNING: This is NOT the way to work on another buffer temporarily
>     within a Lisp program!  Use `set-buffer' instead.  That avoids
>     messing with the window-buffer correspondences.

Oh, that is right, thanks.

> Also the above code forgot to kill the buffer you created.
> I recommend `with-temp-buffer` here instead:
> 
>     (with-temp-buffer
>       (apply #'call-process program infile t display args)
>       (buffer-string))
> 
> 
> -- Stefan

That looks perfect, thank you.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://rms-support-letter.github.io/




reply via email to

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