[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 14:53:18 +0300 |
User-agent: |
Mutt/2.0.6 (2021-03-06) |
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/
- Maybe we can improve this function call-process-to-string?, Jean Louis, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Eli Zaretskii, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?,
Jean Louis <=
- Re: Maybe we can improve this function call-process-to-string?, Eli Zaretskii, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Jean Louis, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Eli Zaretskii, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Jean Louis, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Eli Zaretskii, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Jean Louis, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Arthur Miller, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Jean Louis, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Arthur Miller, 2021/04/08
- Re: Maybe we can improve this function call-process-to-string?, Jean Louis, 2021/04/09