From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: Maybe we can improve this function call-process-to-string? Date: Thu, 8 Apr 2021 14:53:18 +0300 Message-ID: References: <83a6q99pnd.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3717"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0.6 (2021-03-06) Cc: help-gnu-emacs@gnu.org To: Eli Zaretskii Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Apr 08 13:56:03 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lUTGU-0000gh-Bu for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 08 Apr 2021 13:56:02 +0200 Original-Received: from localhost ([::1]:47004 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lUTGT-0002Qh-Dl for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 08 Apr 2021 07:56:01 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33492) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lUTG5-0002QZ-7G for help-gnu-emacs@gnu.org; Thu, 08 Apr 2021 07:55:37 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:53503) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lUTFy-0002Sq-5N; Thu, 08 Apr 2021 07:55:36 -0400 Original-Received: from localhost ([::ffff:41.202.241.27]) (AUTH: PLAIN securesender, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 000000000001DF56.00000000606EEF2F.00006093; Thu, 08 Apr 2021 04:55:26 -0700 Mail-Followup-To: Eli Zaretskii , help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: <83a6q99pnd.fsf@gnu.org> Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:128897 Archived-At: 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/