all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Maybe we can improve this function call-process-to-string?
@ 2021-04-08  7:40 Jean Louis
  2021-04-08  7:55 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Jean Louis @ 2021-04-08  7:40 UTC (permalink / raw
  To: Help GNU Emacs

Hello,

There is function `shell-command-to-string' which is very handy. But
when there are problems with quoting it is better to use
`call-process'. Yet there is no function `call-process-to-string',
which I have made as below.

Maybe people can tell me how to improve this function?

(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))

(call-process-to-string "identify" nil nil "/home/data1/protected/2021-03-27-10:02:52.png")

Is there maybe some other function that can give me string from
buffer?


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/





^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08  7:40 Maybe we can improve this function call-process-to-string? Jean Louis
@ 2021-04-08  7:55 ` Eli Zaretskii
  2021-04-08 11:53   ` Jean Louis
  2021-04-08 13:08 ` Michael Albinus
  2021-04-08 17:49 ` Stefan Monnier
  2 siblings, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-04-08  7:55 UTC (permalink / raw
  To: help-gnu-emacs

> Date: Thu, 08 Apr 2021 10:40:17 +0300
> From: Jean Louis <bugs@gnu.support>
> 
> There is function `shell-command-to-string' which is very handy. But
> when there are problems with quoting

Problems that shell-quote-argument doesn't solve?  If so, can you show
those problems?

> it is better to use
> `call-process'. Yet there is no function `call-process-to-string',
> which I have made as below.

Why do you need such a command?  Emacs can copy text between buffers,
so you don't need to cons a string to insert it into another buffer.

In general, one should avoid strings in Emacs Lisp, because buffer
memory is handled much more efficiently than string memory.

> Is there maybe some other function that can give me string from
> buffer?

buffer-substring?



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08  7:55 ` Eli Zaretskii
@ 2021-04-08 11:53   ` Jean Louis
  2021-04-08 13:39     ` Eli Zaretskii
  0 siblings, 1 reply; 19+ messages in thread
From: Jean Louis @ 2021-04-08 11:53 UTC (permalink / raw
  To: Eli Zaretskii; +Cc: help-gnu-emacs

Dear Eli,

Thanks, I will be more using `shell-quote-argument' in those
`shell-command' functions.

> > it is better to use
> > `call-process'. Yet there is no function `call-process-to-string',
> > which I have made as below.
> 
> Why do you need such a command?  Emacs can copy text between buffers,
> so you don't need to cons a string to insert it into another buffer.

In a function in this example I need to verify image size, sometimes
hundreds of image sizes:

(defun wrs-markdown-image-identify-size (image-file)
  "Returns the width and height in format =WIDTHxHEIGHT for discount markdown"
  (let ((command (format "identify -format '=%%wx%%h' %s" image-file)))
    (shell-command-to-string command)))

Different version is here:

(defun image-dimension (file)
  "Returns list of width and height of the image"
  (when (rcd-which "identify")
    (let* ((dimensions (call-process-to-string "identify" nil nil "-format" "%w %h" file)))
      (mapcar 'string-to-number (split-string dimensions)))))

I can improve the first version:

(defun wrs-markdown-image-identify-size (image-file)
  "Returns the width and height in format =WIDTHxHEIGHT for discount markdown"
  (let* ((image-file (shell-quote-argument image-file))
	 (command (format "identify -format '=%%wx%%h' %s" image-file)))
    (shell-command-to-string command)))


(wrs-markdown-image-identify-size "/home/data1/protected/Media/Pictures/'Screenshot from 2021-03-11 08-59-\"34\".png'") ⇒ "=1280x800"

It seems that it will handle complicated file names. Function
`shell-quote-argument' is good to be implemented everywhere it is
needed. 

> In general, one should avoid strings in Emacs Lisp, because buffer
> memory is handled much more efficiently than string memory.

I understand the concept, not at all how to practically run a system
command and receive it as a string. There is either
`shell-command-to-string' or `call-process' which can write to buffer.

If it writes to buffer and I need that information I have to change to
buffer, get information and return it back as string.

> > Is there maybe some other function that can give me string from
> > buffer?
> 
> buffer-substring?

Because none of `buffer-substring' nor `buffer-string' can specify the
buffer name then I have to switch temporarily to other buffer, get
string with `buffer-string' and return back. I was thinking there is
some function doing that straight, like (buffer-string BUFFER), but I
don't find such.

I have made this one:

(defun buffer-to-string (buffer)
  "Return `buffer-string' for specified BUFFER."
  (let ((current-buffer (current-buffer)))
    (switch-to-buffer buffer)
    (let ((output (buffer-substring-no-properties (point-min) (point-max))))
      (switch-to-buffer current-buffer)
      output)))

Then this works:

(buffer-to-string (get-buffer "*scratch*"))

Then I can improve this function:

(defun call-process-to-string (program &optional infile display &rest args)
  (let* ((buffer-name "RCD Emacs Lisp output")
	 (buffer (generate-new-buffer buffer-name))
	 (status (apply #'call-process program infile buffer display args))
	 (current-buffer (current-buffer))
	 (output (buffer-to-string buffer)))
    (kill-buffer buffer)
    output))

Yet not sure if it is best way.

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/




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08  7:40 Maybe we can improve this function call-process-to-string? Jean Louis
  2021-04-08  7:55 ` Eli Zaretskii
@ 2021-04-08 13:08 ` Michael Albinus
  2021-04-08 15:50   ` Jean Louis
  2021-04-08 17:49 ` Stefan Monnier
  2 siblings, 1 reply; 19+ messages in thread
From: Michael Albinus @ 2021-04-08 13:08 UTC (permalink / raw
  To: Jean Louis; +Cc: Help GNU Emacs

Jean Louis <bugs@gnu.support> writes:

> Hello,

Hi Jean,

> There is function `shell-command-to-string' which is very handy. But
> when there are problems with quoting it is better to use
> `call-process'. Yet there is no function `call-process-to-string',
> which I have made as below.
>
> Maybe people can tell me how to improve this function?

`process-lines' is closed to what you want. Try, for example

(process-lines "ls" "-al" ".")

It returns a list of strings (lines), which you could use in loops like
`while', `dolist' etc.

> Jean

Best regards, Michael.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 11:53   ` Jean Louis
@ 2021-04-08 13:39     ` Eli Zaretskii
  2021-04-08 15:56       ` Jean Louis
  0 siblings, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-04-08 13:39 UTC (permalink / raw
  To: help-gnu-emacs

> Date: Thu, 8 Apr 2021 14:53:18 +0300
> From: Jean Louis <bugs@gnu.support>
> Cc: help-gnu-emacs@gnu.org
> 
> > In general, one should avoid strings in Emacs Lisp, because buffer
> > memory is handled much more efficiently than string memory.
> 
> I understand the concept, not at all how to practically run a system
> command and receive it as a string.

Why do you need a string?  The string is a means to an end, right?
What is that end?

> > buffer-substring?
> 
> Because none of `buffer-substring' nor `buffer-string' can specify the
> buffer name then I have to switch temporarily to other buffer, get
> string with `buffer-string' and return back. I was thinking there is
> some function doing that straight, like (buffer-string BUFFER), but I
> don't find such.

I suggest to look up with-current-buffer and with-temp-buffer.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 13:08 ` Michael Albinus
@ 2021-04-08 15:50   ` Jean Louis
  0 siblings, 0 replies; 19+ messages in thread
From: Jean Louis @ 2021-04-08 15:50 UTC (permalink / raw
  To: Michael Albinus; +Cc: Help GNU Emacs

* Michael Albinus <michael.albinus@gmx.de> [2021-04-08 16:09]:
> `process-lines' is closed to what you want. Try, for example
> 
> (process-lines "ls" "-al" ".")

Good to know especially for those short outputs from single line.

-- 
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/




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 13:39     ` Eli Zaretskii
@ 2021-04-08 15:56       ` Jean Louis
  2021-04-08 16:25         ` Eli Zaretskii
  2021-04-08 17:18         ` Arthur Miller
  0 siblings, 2 replies; 19+ messages in thread
From: Jean Louis @ 2021-04-08 15:56 UTC (permalink / raw
  To: Eli Zaretskii; +Cc: help-gnu-emacs

* Eli Zaretskii <eliz@gnu.org> [2021-04-08 16:40]:
> > Date: Thu, 8 Apr 2021 14:53:18 +0300
> > From: Jean Louis <bugs@gnu.support>
> > Cc: help-gnu-emacs@gnu.org
> > 
> > > In general, one should avoid strings in Emacs Lisp, because buffer
> > > memory is handled much more efficiently than string memory.
> > 
> > I understand the concept, not at all how to practically run a system
> > command and receive it as a string.
> 
> Why do you need a string?  The string is a means to an end, right?
> What is that end?

Hahaha, I am not sure if you are joking, but of course string is a
mean to an end... funny. Now, I need it for example, to get widht and
height from a image by using system command `identify', sometimes I
will extract GPS coordinate from an image, so I call system commands
from Emacs Lisp that processes bunch of images.

For example, to quickly construct a Markdown hyperlink to the image
below, I enter the Dired and do M-x md-image-hyperlink

  /home/data1/protected/public_html/gnu.support/images/1536:
  total used in directory 304K available 42.4 GiB
  -rw-r--r-- 1 173K Oct  8  2016 2016-10-08-23:12:44.jpg
  -rw-r--r-- 1 125K Mar 18  2017 gnu-head-large.jpg

With this result:

[![https://gnu.support/images/1536/gnu-head-large.jpg](https://gnu.support/images/1536/gnu-head-large.jpg =1536x1024 "https://gnu.support/images/1536/gnu-head-large.jpg")](https://gnu.support/images/1536/gnu-head-large.jpg "https://gnu.support/images/1536/gnu-head-large.jpg")

That is where `shell-command-to-string' comes handy, and HTML pages
meant for Discount flavor of markdown can be generated.

(defun md-image-hyperlink ()
  (interactive)
  (let ((files (dired-get-marked-files))
	(list '()))
    (dolist (file files)
      (let* ((small-image (public-html-rest file))
	     (large-image (replace-regexp-in-string "/320/\\|/400/\\|/640/\\|/800/" "/1536/" small-image))
	     (command (format "identify -format '[![%s](%s =%%wx%%h \"%s\")](%s \"%s\")' '%s'" small-image small-image small-image large-image large-image file)))
	(message "%s" command)
	(push (shell-command-to-string command) list)))
    (kill-new (with-temp-buffer
		(dolist (item list)
		  (insert item))
		(buffer-string)))))

> > Because none of `buffer-substring' nor `buffer-string' can specify the
> > buffer name then I have to switch temporarily to other buffer, get
> > string with `buffer-string' and return back. I was thinking there is
> > some function doing that straight, like (buffer-string BUFFER), but I
> > don't find such.
> 
> I suggest to look up with-current-buffer and with-temp-buffer.

I would not know how to get output from system command by using those
functions without using shell-command-to-string or call-process

Jean



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 15:56       ` Jean Louis
@ 2021-04-08 16:25         ` Eli Zaretskii
  2021-04-08 18:17           ` Jean Louis
  2021-04-08 17:18         ` Arthur Miller
  1 sibling, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-04-08 16:25 UTC (permalink / raw
  To: help-gnu-emacs

> Date: Thu, 8 Apr 2021 18:56:30 +0300
> From: Jean Louis <bugs@gnu.support>
> Cc: help-gnu-emacs@gnu.org
> 
> > Why do you need a string?  The string is a means to an end, right?
> > What is that end?
> 
> Hahaha, I am not sure if you are joking, but of course string is a
> mean to an end... funny. Now, I need it for example, to get widht and
> height from a image by using system command `identify', sometimes I
> will extract GPS coordinate from an image, so I call system commands
> from Emacs Lisp that processes bunch of images.

So you actually need the numbers reported by those commands?  if so,
you can read them from the buffer into which the command's output is
stored, right?  You don't actually need the numbers in their string
form, right?

> > > Because none of `buffer-substring' nor `buffer-string' can specify the
> > > buffer name then I have to switch temporarily to other buffer, get
> > > string with `buffer-string' and return back. I was thinking there is
> > > some function doing that straight, like (buffer-string BUFFER), but I
> > > don't find such.
> > 
> > I suggest to look up with-current-buffer and with-temp-buffer.
> 
> I would not know how to get output from system command by using those
> functions without using shell-command-to-string or call-process

You said buffer-substring doesn't take a buffer as an argument.  I'm
suggesting something like

  (with-current-buffer (get-buffer "foo")
    (buffer-substring ...))



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 15:56       ` Jean Louis
  2021-04-08 16:25         ` Eli Zaretskii
@ 2021-04-08 17:18         ` Arthur Miller
  2021-04-08 18:32           ` Jean Louis
  1 sibling, 1 reply; 19+ messages in thread
From: Arthur Miller @ 2021-04-08 17:18 UTC (permalink / raw
  To: Eli Zaretskii; +Cc: help-gnu-emacs

Jean Louis <bugs@gnu.support> writes:

> * Eli Zaretskii <eliz@gnu.org> [2021-04-08 16:40]:
>> > Date: Thu, 8 Apr 2021 14:53:18 +0300
>> > From: Jean Louis <bugs@gnu.support>
>> > Cc: help-gnu-emacs@gnu.org

> I would not know how to get output from system command by using those
> functions without using shell-command-to-string or call-process

M-! your-shell-command 

That will give you a buffer named `*Shell Command Output*`. You can
either switch interactively to that buffer and execute your lisp program
via M-x or M-: (depending if it's just a function or interactive
command).

Alternatively you can call it from lisp like:

(shell-command "your comand --with-some-args") and have same buffer.

Might be good to provide optional arguments for target buffer and error
buffer so you get eventual errors separated in other buffer then one you
will process.

I would also not use a string for the final result(s), just do everyting
in that buffer, remove text you don't need and construct your link(s) in
that buffer, it will probably be faster than creating temp buffer and
taking buffer strings.

> 	(push (shell-command-to-string command) list)))
>     (kill-new (with-temp-buffer
> 		(dolist (item list)
> 		  (insert item))
> 		(buffer-string)))))

It is really inneficient to push result form the command to a list to
traverse it later and insert into buffer, when Emacs does that already
for you by default.

> [![https://gnu.support/images/1536/gnu-head-large.jpg](https://gnu.support/images/1536/gnu-head-large.jpg =1536x1024 "https://gnu.support/images/1536/gnu-head-large.jpg")](https://gnu.support/images/1536/gnu-head-large.jpg "https://gnu.support/images/1536/gnu-head-large.jpg")

Your website looks very deceptive to me. I don't remember the name of
that guy that used to post on this list for a while that was rude to
you, but looking at your site now I understand what he ment. I have no
idea who you are or what you do, but something tells me that is not an
official GNU support site, isn't it? Maybe you have all best intentions,
but it looks shady, more akin to a scam page. Hopefully that form on the
front page looks so ugly so nobody will fall for it, but if I would you
and value GNU movement as much as you do, I would probably change that
domain name and removed that from the web, as well as changed that email
from "bugs@gnu.support" to something less deceptive, because it also
looks shady to me. I am by no mean associated with GNU, more than being
a mere user of GNU software, so I can only express my personal feeling
when I saw it.

I understand you mean well but that is how it looks like. I
understand you mean well :).

Best regards
/a



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08  7:40 Maybe we can improve this function call-process-to-string? Jean Louis
  2021-04-08  7:55 ` Eli Zaretskii
  2021-04-08 13:08 ` Michael Albinus
@ 2021-04-08 17:49 ` Stefan Monnier
  2021-04-08 18:33   ` Jean Louis
  2021-04-08 18:40   ` Jean Louis
  2 siblings, 2 replies; 19+ messages in thread
From: Stefan Monnier @ 2021-04-08 17:49 UTC (permalink / raw
  To: help-gnu-emacs

> (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.

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




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 16:25         ` Eli Zaretskii
@ 2021-04-08 18:17           ` Jean Louis
  2021-04-08 18:36             ` Eli Zaretskii
  0 siblings, 1 reply; 19+ messages in thread
From: Jean Louis @ 2021-04-08 18:17 UTC (permalink / raw
  To: Eli Zaretskii; +Cc: help-gnu-emacs

* Eli Zaretskii <eliz@gnu.org> [2021-04-08 19:30]:
> So you actually need the numbers reported by those commands?  if so,
> you can read them from the buffer into which the command's output is
> stored, right?  You don't actually need the numbers in their string
> form, right?

In that particular example numbers are just used as string, but
sometimes I need numbers. That may not be most important. I do not
understand your method of getting output from external command. 

How practically to do it?

I understood reading it from buffer is different than reading from
shell-command-to-string.

Then to have output in buffer, I need call-process, but then again I
need to enter that buffer and read string out of it. Example is here:

(defun call-process-to-string (program &optional infile display &rest args)
  (let* ((buffer-name "RCD Emacs Lisp output")
	 (buffer (generate-new-buffer buffer-name))
	 (status (apply #'call-process program infile buffer display args))
	 (current-buffer (current-buffer))
	 (output (buffer-to-string buffer)))
    (kill-buffer buffer)
    output))

Again I have to use strings there.

> > I would not know how to get output from system command by using those
> > functions without using shell-command-to-string or call-process
> 
> You said buffer-substring doesn't take a buffer as an argument.  I'm
> suggesting something like
> 
>   (with-current-buffer (get-buffer "foo")
>     (buffer-substring ...))

That again comes back as a string, right? I do use that type, in the
above function for example.

Jean




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 17:18         ` Arthur Miller
@ 2021-04-08 18:32           ` Jean Louis
  2021-04-08 19:41             ` Arthur Miller
  0 siblings, 1 reply; 19+ messages in thread
From: Jean Louis @ 2021-04-08 18:32 UTC (permalink / raw
  To: Arthur Miller; +Cc: help-gnu-emacs

* Arthur Miller <arthur.miller@live.com> [2021-04-08 20:21]:
> I would also not use a string for the final result(s), just do everyting
> in that buffer, remove text you don't need and construct your link(s) in
> that buffer, it will probably be faster than creating temp buffer and
> taking buffer strings.

Thank you Arthur, just that I need it in programmed way. I don't
construct links myself interactively, I may interactively select files
in Dired, then functions do the rest and construct links.

Of course if I wish to inject some output from a system command
interactively, I have it built in into my fingers.

C-u M-! and there it goes. I need to reconstruct how to write that
command, as it is just built in the body.

Those needs are integrated, for example selecting bunch of directories
in Dired, and letting functions traverse and construct Markdown WWW
links or other WWW links, transferring it as a reference to somebody
or into the database or into the page.

> > 	(push (shell-command-to-string command) list)))
> >     (kill-new (with-temp-buffer
> > 		(dolist (item list)
> > 		  (insert item))
> > 		(buffer-string)))))
> 
> It is really inneficient to push result form the command to a list to
> traverse it later and insert into buffer, when Emacs does that already
> for you by default.

I would like to agree, but don't understand it. I don't like
dolist/dotimes any more, so I am replacing it with `while' here
below. There is still pushing into the list.

How would I do it by default?

(defun md-image-hyperlink ()
  (interactive)
  (let ((files (dired-get-marked-files))
	(list '()))
    (while files
      (let* ((file (pop files))
	     (small-image (public-html-rest file))
	     (large-image (replace-regexp-in-string "/320/\\|/400/\\|/640/\\|/800/" "/1536/" small-image))
	     (command (format "identify -format '[![%s](%s =%%wx%%h \"%s\")](%s \"%s\")' '%s'" small-image small-image small-image large-image large-image file)))
	;; (message "%s" command)
	(push (shell-command-to-string command) list)))
    (kill-new (with-temp-buffer
		(while list
		  (insert (pop list)))
		(buffer-string)))))

> Your website looks very deceptive to me. I don't remember the name of
> that guy that used to post on this list for a while that was rude to
> you, but looking at your site now I understand what he ment. I have no
> idea who you are or what you do, but something tells me that is not an
> official GNU support site, isn't it?

There was no domain with support.gnu, so I took gnu.support

> Maybe you have all best intentions, but it looks shady, more akin to
> a scam page.

OK, did you get maybe some offer to buy? Did you have any relation
with the site? Lost something?

 _     ___  _     
| |   / _ \| |    
| |  | | | | |    
| |__| |_| | |___ 
|_____\___/|_____|
                  

I really admire programmers, but sometimes I get stunned with
nonsensical illogical reasoning.

> Hopefully that form on the front page looks so ugly so nobody will
> fall for it

Do I really care? Does anybody care? Do I need to care?

> but if I would you and value GNU movement as much as you do, I would
> probably change that domain name and removed that from the web, as
> well as changed that email from "bugs@gnu.support" to something less
> deceptive, because it also looks shady to me.

Enjoy your impressions, I will rather watch SF.

> I am by no mean associated with GNU, more than being a mere user of
> GNU software, so I can only express my personal feeling when I saw
> it.

That is totally fine, thank you for sharing. Recommended reading:
https://nowandme.com/


-- 
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/




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 17:49 ` Stefan Monnier
@ 2021-04-08 18:33   ` Jean Louis
  2021-04-08 18:40   ` Jean Louis
  1 sibling, 0 replies; 19+ messages in thread
From: Jean Louis @ 2021-04-08 18:33 UTC (permalink / raw
  To: Stefan Monnier; +Cc: help-gnu-emacs

* 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/




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 18:17           ` Jean Louis
@ 2021-04-08 18:36             ` Eli Zaretskii
  2021-04-08 18:44               ` Jean Louis
  0 siblings, 1 reply; 19+ messages in thread
From: Eli Zaretskii @ 2021-04-08 18:36 UTC (permalink / raw
  To: help-gnu-emacs

> Date: Thu, 8 Apr 2021 21:17:15 +0300
> From: Jean Louis <bugs@gnu.support>
> Cc: help-gnu-emacs@gnu.org
> 
> * Eli Zaretskii <eliz@gnu.org> [2021-04-08 19:30]:
> > So you actually need the numbers reported by those commands?  if so,
> > you can read them from the buffer into which the command's output is
> > stored, right?  You don't actually need the numbers in their string
> > form, right?
> 
> In that particular example numbers are just used as string, but
> sometimes I need numbers. That may not be most important. I do not
> understand your method of getting output from external command. 
> 
> How practically to do it?
> 
> I understood reading it from buffer is different than reading from
> shell-command-to-string.
> 
> Then to have output in buffer, I need call-process, but then again I
> need to enter that buffer and read string out of it.

You can use with-current-buffer for that.

> Again I have to use strings there.

Depends on what you want to do with that string.  My point is:
whatever you want to do with the string, do it with buffer text
instead.

> > > I would not know how to get output from system command by using those
> > > functions without using shell-command-to-string or call-process
> > 
> > You said buffer-substring doesn't take a buffer as an argument.  I'm
> > suggesting something like
> > 
> >   (with-current-buffer (get-buffer "foo")
> >     (buffer-substring ...))
> 
> That again comes back as a string, right?

Yes, but only because _you_ asked a buffer-substring.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 17:49 ` Stefan Monnier
  2021-04-08 18:33   ` Jean Louis
@ 2021-04-08 18:40   ` Jean Louis
  1 sibling, 0 replies; 19+ messages in thread
From: Jean Louis @ 2021-04-08 18:40 UTC (permalink / raw
  To: Stefan Monnier; +Cc: help-gnu-emacs

* Stefan Monnier <monnier@iro.umontreal.ca> [2021-04-08 20:50]:
> 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))

Thanks, that now is fully Emacs style. I know this all as tools, but
still need getting used to the tools in the environment.

(defun call-process-to-string (program &optional infile display &rest args)
  (with-temp-buffer
    (apply #'call-process program infile t display args)
    (buffer-string)))

(call-process-to-string "ls" nil nil "-lS" "/tmp/pies.ctl") ⇒ "srwx------ 1 admin admin 0 Apr  6 11:01 /tmp/pies.ctl
"

-- 
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/




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 18:36             ` Eli Zaretskii
@ 2021-04-08 18:44               ` Jean Louis
  0 siblings, 0 replies; 19+ messages in thread
From: Jean Louis @ 2021-04-08 18:44 UTC (permalink / raw
  To: Eli Zaretskii; +Cc: help-gnu-emacs

* Eli Zaretskii <eliz@gnu.org> [2021-04-08 21:37]:
> > Then to have output in buffer, I need call-process, but then again I
> > need to enter that buffer and read string out of it.
> 
> You can use with-current-buffer for that.
> 
> > Again I have to use strings there.
> 
> Depends on what you want to do with that string.  My point is:
> whatever you want to do with the string, do it with buffer text
> instead.

I have got the point, thank you. I am changing code shaped by advises.

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/




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 18:32           ` Jean Louis
@ 2021-04-08 19:41             ` Arthur Miller
  2021-04-09  8:52               ` Jean Louis
  0 siblings, 1 reply; 19+ messages in thread
From: Arthur Miller @ 2021-04-08 19:41 UTC (permalink / raw
  To: Eli Zaretskii; +Cc: help-gnu-emacs

Jean Louis <bugs@gnu.support> writes:

> There was no domain with support.gnu, so I took gnu.support

And if there was you would took it and look more deceptive? :)

Why do you need to pretend that you are some official representing GNU?

Do you use bugs@gnu.support even in correspondence with other people?

If your intention is really just to promote FSF, so why not
jean@gnu.support or something less pretentious?

>> Maybe you have all best intentions, but it looks shady, more akin to
>> a scam page.
>
> OK, did you get maybe some offer to buy? Did you have any relation
> with the site? Lost something?

So what? Just because you didn't scammed me, I or nobody shouldn't care?

What is the master plan? Is it some kind of a honeypot? If some poor
sould contacts you, you will politely ask them to install ssh software
and give you sudo login so you can "fix" the problem for them?

I mean if you just wish to promote FSF why that form for people to
contact you. If you wish to help people there would be probably more
humane and less pretentious ways.

>  _     ___  _     
> | |   / _ \| |    
> | |  | | | | |    
> | |__| |_| | |___ 
> |_____\___/|_____|
>                   
>
> I really admire programmers, but sometimes I get stunned with
> nonsensical illogical reasoning.

:-) Yeah, I am very illogical.

It might seem illogical to confront a scammer, but it is the reaction I
wanted to see. 

I don't know, you maybe are just weird or you are truly a scammer. I
don't have enough data to make a judgement, I am divided what I think.

I appologize to other people on this list for bringing this up, I am
just perplexed.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-08 19:41             ` Arthur Miller
@ 2021-04-09  8:52               ` Jean Louis
  2021-04-09 10:07                 ` tomas
  0 siblings, 1 reply; 19+ messages in thread
From: Jean Louis @ 2021-04-09  8:52 UTC (permalink / raw
  To: Arthur Miller; +Cc: help-gnu-emacs

* Arthur Miller <arthur.miller@live.com> [2021-04-08 22:50]:
> Jean Louis <bugs@gnu.support> writes:
> 
> > There was no domain with support.gnu, so I took gnu.support
> 
> And if there was you would took it and look more deceptive? :)
> 
> Why do you need to pretend that you are some official representing GNU?
> 
> Do you use bugs@gnu.support even in correspondence with other
> people?

Arthur, I know you are intelligent, and you know that I am not
representing GNU, but you like to troll around.

You have email address with LIVE.COM which belongs to Microsoft
Corporation and so Arthur, why do you pretend to be some official of
Microsoft Corporation?

> What is the master plan? Is it some kind of a honeypot? If some poor
> sould contacts you, you will politely ask them to install ssh software
> and give you sudo login so you can "fix" the problem for them?

> It might seem illogical to confront a scammer, but it is the reaction I
> wanted to see. 
> 
> I don't know, you maybe are just weird or you are truly a scammer. I
> don't have enough data to make a judgement, I am divided what I
> think.

I don't find it funny Arthur, I feel this is harassing.

Other thing that you don't understand is that I am not just somebody
behind a computer, and you are playing with wrong person. You would
not be standing if you would tell me this face to face. So I invite
you for a meeting then we can solve issues.

-- 
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/




^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Maybe we can improve this function call-process-to-string?
  2021-04-09  8:52               ` Jean Louis
@ 2021-04-09 10:07                 ` tomas
  0 siblings, 0 replies; 19+ messages in thread
From: tomas @ 2021-04-09 10:07 UTC (permalink / raw
  To: Arthur Miller, help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 674 bytes --]

On Fri, Apr 09, 2021 at 11:52:32AM +0300, Jean Louis wrote:
> * Arthur Miller <arthur.miller@live.com> [2021-04-08 22:50]:

[...]

> > I don't know, you maybe are just weird or you are truly a scammer. I
> > don't have enough data to make a judgement, I am divided what I
> > think.

Unfortunate indeed.

> [...] You would
> not be standing if you would tell me this face to face. So I invite
> you for a meeting then we can solve issues.

Unfortunate, too.

Please, folks. Do behave like grown-ups. Don't assume malice on
each other's side and let things cool a bit down.

I know by reading this list that both of you can be outstanding
persons. Just be it :)

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2021-04-09 10:07 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-08  7:40 Maybe we can improve this function call-process-to-string? Jean Louis
2021-04-08  7:55 ` Eli Zaretskii
2021-04-08 11:53   ` Jean Louis
2021-04-08 13:39     ` Eli Zaretskii
2021-04-08 15:56       ` Jean Louis
2021-04-08 16:25         ` Eli Zaretskii
2021-04-08 18:17           ` Jean Louis
2021-04-08 18:36             ` Eli Zaretskii
2021-04-08 18:44               ` Jean Louis
2021-04-08 17:18         ` Arthur Miller
2021-04-08 18:32           ` Jean Louis
2021-04-08 19:41             ` Arthur Miller
2021-04-09  8:52               ` Jean Louis
2021-04-09 10:07                 ` tomas
2021-04-08 13:08 ` Michael Albinus
2021-04-08 15:50   ` Jean Louis
2021-04-08 17:49 ` Stefan Monnier
2021-04-08 18:33   ` Jean Louis
2021-04-08 18:40   ` Jean Louis

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.