all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Run terminal command with output in current buffer
@ 2021-07-16  6:43 lisa-asket
  2021-07-16  6:53 ` Jean-Christophe Helary
                   ` (2 more replies)
  0 siblings, 3 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16  6:43 UTC (permalink / raw)
  To: help-gnu-emacs


How can I run a terminal command and insert the output in the current buffer?



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

* Re: Run terminal command with output in current buffer
  2021-07-16  6:43 Run terminal command with output in current buffer lisa-asket
@ 2021-07-16  6:53 ` Jean-Christophe Helary
  2021-07-16  7:07 ` Eli Zaretskii
  2021-07-16 13:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2 siblings, 0 replies; 42+ messages in thread
From: Jean-Christophe Helary @ 2021-07-16  6:53 UTC (permalink / raw)
  To: lisa-asket; +Cc: help-gnu-emacs



> On Jul 16, 2021, at 15:43, lisa-asket@perso.be wrote:
> 
> 
> How can I run a terminal command and insert the output in the current buffer?

M-x eval-expression

(shell-command COMMAND (current-buffer)) ?




-- 
Jean-Christophe Helary @brandelune
https://mac4translators.blogspot.com
https://sr.ht/~brandelune/omegat-as-a-book/




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

* Re: Run terminal command with output in current buffer
  2021-07-16  6:43 Run terminal command with output in current buffer lisa-asket
  2021-07-16  6:53 ` Jean-Christophe Helary
@ 2021-07-16  7:07 ` Eli Zaretskii
  2021-07-16  9:04   ` lisa-asket
  2021-07-16 11:30   ` lisa-asket
  2021-07-16 13:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2 siblings, 2 replies; 42+ messages in thread
From: Eli Zaretskii @ 2021-07-16  7:07 UTC (permalink / raw)
  To: help-gnu-emacs

> From: lisa-asket@perso.be
> Date: Fri, 16 Jul 2021 08:43:27 +0200 (CEST)
> 
> How can I run a terminal command and insert the output in the current buffer?

"C-u M-! the command RET"

See the doc string of M-! for details.



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

* Run terminal command with output in current buffer
  2021-07-16  7:07 ` Eli Zaretskii
@ 2021-07-16  9:04   ` lisa-asket
  2021-07-16 11:32     ` Eli Zaretskii
  2021-07-16 11:30   ` lisa-asket
  1 sibling, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-16  9:04 UTC (permalink / raw)
  To: Eli Zaretskii, help-gnu-emacs



It guided we to this command.  I wonder how to call the command from my init file, have my own

function for it.



(shell-command COMMAND &optional OUTPUT-BUFFER ERROR-BUFFER)



What shall I do with output-buffer ?


From: Eli Zaretskii <eliz@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 09:07:21 Europe/Paris

> From: lisa-asket@perso.be
> Date: Fri, 16 Jul 2021 08:43:27 +0200 (CEST)
> 
> How can I run a terminal command and insert the output in the current buffer?

"C-u M-! the command RET"

See the doc string of M-! for details.




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

* Run terminal command with output in current buffer
  2021-07-16  7:07 ` Eli Zaretskii
  2021-07-16  9:04   ` lisa-asket
@ 2021-07-16 11:30   ` lisa-asket
  2021-07-16 11:58     ` lisa-asket
  2021-07-16 13:25     ` Felix Dietrich
  1 sibling, 2 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16 11:30 UTC (permalink / raw)
  To: Eli Zaretskii, help-gnu-emacs

I have progressed as follows, with the intention that I can run the following command



grep -hir --exclude=\*.el --include=\*.{org,texi} -C 8 "node-gnu-fdl" .





--------



(defun runcom ()
  "docstring"

  (interactive) 

  ;; grep -hir --exclude=\*.el --include=\*.{org,texi} -C 8 "node-gnu-fdl" .
  
  (cmd-excl (read-from-minibuffer "exclude: "))
  (setq cmd-excl (concat " -C --exclude=\*." cmd-excl))
  
  (cmd-incl (read-from-minibuffer "include: "))
  (setq cmd-incl (concat " -C --include=\*." cmd-incl))

  (cmd-cnum (read-from-minibuffer "cnum: "))
  (setq cmd-cnum (concat " -C " cmd-cnum))
  
  (cmd-ptrn (read-from-minibuffer "pattern: "))

  (setq cmd (concat "grep -hir " cmd-excl cmd-incl cmd-cnum cmd-ptrn))
  (shell-command cmd (current-buffer))

)




From: Eli Zaretskii <eliz@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 09:07:21 Europe/Paris

> From: lisa-asket@perso.be
> Date: Fri, 16 Jul 2021 08:43:27 +0200 (CEST)
> 
> How can I run a terminal command and insert the output in the current buffer?

"C-u M-! the command RET"

See the doc string of M-! for details.




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

* Re: Run terminal command with output in current buffer
  2021-07-16  9:04   ` lisa-asket
@ 2021-07-16 11:32     ` Eli Zaretskii
  2021-07-16 11:48       ` lisa-asket
  0 siblings, 1 reply; 42+ messages in thread
From: Eli Zaretskii @ 2021-07-16 11:32 UTC (permalink / raw)
  To: help-gnu-emacs

> From: lisa-asket@perso.be
> Date: Fri, 16 Jul 2021 11:04:02 +0200 (CEST)
> 
> It guided we to this command.  I wonder how to call the command from my init file, have my own
> function for it.
> 
> (shell-command COMMAND &optional OUTPUT-BUFFER ERROR-BUFFER)
> 
> What shall I do with output-buffer ?

Did you read the doc string?  It answers your question quite
explicitly.



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

* Run terminal command with output in current buffer
  2021-07-16 11:32     ` Eli Zaretskii
@ 2021-07-16 11:48       ` lisa-asket
  0 siblings, 0 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16 11:48 UTC (permalink / raw)
  To: Eli Zaretskii, help-gnu-emacs

From: Eli Zaretskii <eliz@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 13:32:40 Europe/Paris

> From: lisa-asket@perso.be
> Date: Fri, 16 Jul 2021 11:04:02 +0200 (CEST)
> 
> It guided we to this command. I wonder how to call the command from my init file, have my own
> function for it.
> 
> (shell-command COMMAND &optional OUTPUT-BUFFER ERROR-BUFFER)
> 
> What shall I do with output-buffer ?

Did you read the doc string? It answers your question quite
explicitly.

I can go without it, but I have put (current-buffer).



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

* Run terminal command with output in current buffer
  2021-07-16 11:30   ` lisa-asket
@ 2021-07-16 11:58     ` lisa-asket
  2021-07-16 15:14       ` Felix Dietrich
  2021-07-16 13:25     ` Felix Dietrich
  1 sibling, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-16 11:58 UTC (permalink / raw)
  To: lisa-asket, Eli Zaretskii, help-gnu-emacs

I experience problem with read-from-minibuffer


(cmd-excl (read-from-minibuffer "exclude: "))



Symbol's function definition is void




From: lisa-asket@perso.be
To: Eli Zaretskii <eliz@gnu.org>;
   help-gnu-emacs@gnu.org
Subject: Run terminal command with output in current buffer
Date: 16/07/2021 13:30:01 Europe/Paris

I have progressed as follows, with the intention that I can run the following command



grep -hir --exclude=\*.el --include=\*.{org,texi} -C 8 "node-gnu-fdl" .





--------



(defun runcom ()
  "docstring"

  (interactive) 

  ;; grep -hir --exclude=\*.el --include=\*.{org,texi} -C 8 "node-gnu-fdl" .
  
  (cmd-excl (read-from-minibuffer "exclude: "))
  (setq cmd-excl (concat " -C --exclude=\*." cmd-excl))
  
  (cmd-incl (read-from-minibuffer "include: "))
  (setq cmd-incl (concat " -C --include=\*." cmd-incl))

  (cmd-cnum (read-from-minibuffer "cnum: "))
  (setq cmd-cnum (concat " -C " cmd-cnum))
  
  (cmd-ptrn (read-from-minibuffer "pattern: "))

  (setq cmd (concat "grep -hir " cmd-excl cmd-incl cmd-cnum cmd-ptrn))
  (shell-command cmd (current-buffer))

)




From: Eli Zaretskii <eliz@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 09:07:21 Europe/Paris

> From: lisa-asket@perso.be
> Date: Fri, 16 Jul 2021 08:43:27 +0200 (CEST)
> 
> How can I run a terminal command and insert the output in the current buffer?

"C-u M-! the command RET"

See the doc string of M-! for details.





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

* Re: Run terminal command with output in current buffer
  2021-07-16 11:30   ` lisa-asket
  2021-07-16 11:58     ` lisa-asket
@ 2021-07-16 13:25     ` Felix Dietrich
  1 sibling, 0 replies; 42+ messages in thread
From: Felix Dietrich @ 2021-07-16 13:25 UTC (permalink / raw)
  To: help-gnu-emacs

lisa-asket@perso.be writes:
> ;; grep -hir --exclude=\*.el --include=\*.{org,texi} -C 8 "node-gnu-fdl" .
> 
> (cmd-excl (read-from-minibuffer "exclude: "))
> (setq cmd-excl (concat " -C --exclude=\*." cmd-excl))
>
> (cmd-incl (read-from-minibuffer "include: "))
> (setq cmd-incl (concat " -C --include=\*." cmd-incl))

The backslash is special in Emacs strings, and you need to escape it
with an extra backslash if you want it to be part of the string.
Compare the strings inserted by these two commands:

    (insert "\*") ; This will insert just ‘*’.

    (insert "\\*") ; This will insert ‘\*’.


In front of certain characters, the backlash translates these characters
into other characters [1][2].  Characters preceded by a backslash that
are not known “escape sequences” stand for themselves; this is the case
for ‘*’, and that is why you need to double the backslash.

Similarly, you may need to escape characters for the shell in the string
read from the minibuffer: have a look at the function
“shell-quote-argument”.

Is the first string to “concat” missing a space at itʼs end?

If this isnʼt an exercise for fun or otherwise: you can call grep from
within Emacs using the command “M-x grep”.


Footnotes:
[1]  (elisp) Basic Char Syntax

     <https://www.gnu.org/software/emacs/manual/html_node/elisp/Basic-Char-Syntax.html>

[2]  (elisp) Syntax for Strings

     <https://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-for-Strings.html>

-- 
Felix Dietrich



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

* Re: Run terminal command with output in current buffer
  2021-07-16  6:43 Run terminal command with output in current buffer lisa-asket
  2021-07-16  6:53 ` Jean-Christophe Helary
  2021-07-16  7:07 ` Eli Zaretskii
@ 2021-07-16 13:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-16 14:28   ` lisa-asket
  2 siblings, 1 reply; 42+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-16 13:48 UTC (permalink / raw)
  To: help-gnu-emacs

lisa-asket wrote:

> How can I run a terminal command and insert the output in
> the current buffer?

(shell-command "ls" t)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Run terminal command with output in current buffer
  2021-07-16 13:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-16 14:28   ` lisa-asket
  2021-07-16 15:06     ` lisa-asket
  0 siblings, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-16 14:28 UTC (permalink / raw)
  To: moasenwood, help-gnu-emacs

I am getting some problem reading from the minibuffer with this command 



(cmd-excl (read-from-minibuffer "exclude: "))


From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 15:48:52 Europe/Paris

lisa-asket wrote:

> How can I run a terminal command and insert the output in
> the current buffer?

(shell-command "ls" t)

-- 
underground experts united
https://dataswamp.org/~incal





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

* Run terminal command with output in current buffer
  2021-07-16 14:28   ` lisa-asket
@ 2021-07-16 15:06     ` lisa-asket
  0 siblings, 0 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16 15:06 UTC (permalink / raw)
  To: lisa-asket, moasenwood, help-gnu-emacs

I have done the following, which should work.



But  `shell-command` does not seem to run.  



--------



(defun runcom ()
  "docstring"

  (interactive) 

  ;; grep -hir --exclude=\*.el --include=\*.{org,texi} -C 8 "node-gnu-fdl" .

  (let ( cmd
     (cmd-excl (read-from-minibuffer "exclude: "))
         (cmd-incl (read-from-minibuffer "include: "))
     (cmd-cnum (read-from-minibuffer "cnum: "))
     (cmd-ptrn (read-from-minibuffer "pattern: "))
     (cmd-dpth (read-from-minibuffer "dpth: ")) )

    (setq cmd-excl (concat " --exclude=\\*." cmd-excl))
    (setq cmd-incl (concat " --include=\\*." cmd-incl))
    (setq cmd-cnum (concat " -C " cmd-cnum))

    (setq cmd (concat "grep -hir" cmd-excl cmd-incl
              cmd-cnum " " cmd-ptrn " " cmd-dpth))
    (message "%s" cmd)
    (shell-command cmd (current-buffer)) ))


From: lisa-asket@perso.be
To: moasenwood@zoho.eu;
   help-gnu-emacs@gnu.org
Subject: Run terminal command with output in current buffer
Date: 16/07/2021 16:28:58 Europe/Paris

I am getting some problem reading from the minibuffer with this command 



(cmd-excl (read-from-minibuffer "exclude: "))


From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 15:48:52 Europe/Paris

lisa-asket wrote:

> How can I run a terminal command and insert the output in
> the current buffer?

(shell-command "ls" t)

-- 
underground experts united
https://dataswamp.org/~incal






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

* Re: Run terminal command with output in current buffer
  2021-07-16 11:58     ` lisa-asket
@ 2021-07-16 15:14       ` Felix Dietrich
  2021-07-16 15:21         ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-16 15:22         ` lisa-asket
  0 siblings, 2 replies; 42+ messages in thread
From: Felix Dietrich @ 2021-07-16 15:14 UTC (permalink / raw)
  Cc: help-gnu-emacs

lisa-asket@perso.be writes:

> I experience problem with read-from-minibuffer
>
>
> (cmd-excl (read-from-minibuffer "exclude: "))

Assign values to variables by using “setq”.  Values can be arbitrary
expressions:

    (setq cmd-excl "value")

    (setq cmd-excl (read-from-minibuffer "exclude: "))

If you write “(cmd-excl…” the lisp interpreter will try to call a
function “cmd-excl”.

You can declare local variables with “let”:

    (let (cmd-excl cmd-incl)
      (setq cmd-excl "value"))

Values can be declared and assigned to at the same time:

    (let ((cmd-excl "value")
          cmd-incl)
      cmd-excl)

There is also “let*”:

    (let ((cmd-excl "value")
          (cmd-incl cmd-excl)) ; can access cmd-excl here already
                               ; because of let*
      cmd-incl)

-- 
Felix Dietrich



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

* Re: Run terminal command with output in current buffer
  2021-07-16 15:14       ` Felix Dietrich
@ 2021-07-16 15:21         ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-16 15:44           ` lisa-asket
  2021-07-19  0:42           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-16 15:22         ` lisa-asket
  1 sibling, 2 replies; 42+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-07-16 15:21 UTC (permalink / raw)
  To: help-gnu-emacs

> You can declare local variables with “let”:
>
>     (let (cmd-excl cmd-incl)
>       (setq cmd-excl "value"))

Please don't.  Use

    (let ((cmd-excl "value")
          cmd-incl)

instead.
[ This is less code to write, simpler code for the reader, simpler code
  for the compiler, and even marginally more efficient.  ]

Only use `setq` when you really want that variable to contain
different values at different times.


        Stefan




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

* Run terminal command with output in current buffer
  2021-07-16 15:14       ` Felix Dietrich
  2021-07-16 15:21         ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-16 15:22         ` lisa-asket
  1 sibling, 0 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16 15:22 UTC (permalink / raw)
  To: Felix Dietrich; +Cc: help-gnu-emacs

Thanks, have put it in `let`.



From: Felix Dietrich <felix.dietrich@sperrhaken.name>
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 17:14:36 Europe/Paris
Cc: help-gnu-emacs@gnu.org

lisa-asket@perso.be writes:

> I experience problem with read-from-minibuffer
>
>
> (cmd-excl (read-from-minibuffer "exclude: "))

Assign values to variables by using “setq”. Values can be arbitrary
expressions:

(setq cmd-excl "value")

(setq cmd-excl (read-from-minibuffer "exclude: "))

If you write “(cmd-excl…” the lisp interpreter will try to call a
function “cmd-excl”.

You can declare local variables with “let”:

(let (cmd-excl cmd-incl)
(setq cmd-excl "value"))

Values can be declared and assigned to at the same time:

(let ((cmd-excl "value")
cmd-incl)
cmd-excl)

There is also “let*”:

(let ((cmd-excl "value")
(cmd-incl cmd-excl)) ; can access cmd-excl here already
; because of let*
cmd-incl)

-- 
Felix Dietrich




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

* Run terminal command with output in current buffer
  2021-07-16 15:21         ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-16 15:44           ` lisa-asket
  2021-07-16 16:32             ` Felix Dietrich
  2021-07-16 16:36             ` Run " Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-19  0:42           ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 2 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16 15:44 UTC (permalink / raw)
  To: monnier, help-gnu-emacs

Thanks, I am using the following code



But when using `shell-command` I have to wait until the command is executed

before it gets displayed in the buffer.  Can I make it print as it is executing ?



(shell-command cmd (current-buffer))

--------



(defun runcom ()
  "docstring"

  (interactive) 

  ;; grep -hir --exclude=\*.el --include=\*.{org,texi} -C 8 "node-gnu-fdl" .

  (let ( (cmd-excl (read-from-minibuffer "exclude: "))
         (cmd-incl (read-from-minibuffer "include: "))
     (cmd-cnum (read-from-minibuffer "cnum: "))
     (cmd-ptrn (read-from-minibuffer "pattern: "))
     (cmd-dpth (read-from-minibuffer "dpth: "))
      cmd )

    (setq cmd-excl (concat " --exclude=\\*." cmd-excl))
    (setq cmd-incl (concat " --include=\\*." cmd-incl))
    (setq cmd-cnum (concat " -C " cmd-cnum))

    (setq cmd (concat "grep -hir" cmd-excl cmd-incl
              cmd-cnum " " cmd-ptrn " " cmd-dpth))
    (message "%s" cmd)
    (shell-command cmd (current-buffer))) )



From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 17:21:08 Europe/Paris

> You can declare local variables with “let”:
>
> (let (cmd-excl cmd-incl)
> (setq cmd-excl "value"))

Please don't. Use

(let ((cmd-excl "value")
cmd-incl)

instead.
[ This is less code to write, simpler code for the reader, simpler code
for the compiler, and even marginally more efficient. ]

Only use `setq` when you really want that variable to contain
different values at different times.


Stefan





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

* Re: Run terminal command with output in current buffer
  2021-07-16 15:44           ` lisa-asket
@ 2021-07-16 16:32             ` Felix Dietrich
  2021-07-16 17:00               ` lisa-asket
  2021-07-16 16:36             ` Run " Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 42+ messages in thread
From: Felix Dietrich @ 2021-07-16 16:32 UTC (permalink / raw)
  To: help-gnu-emacs

lisa-asket@perso.be writes:

> But when using `shell-command` I have to wait until the command is
> executed before it gets displayed in the buffer.  Can I make it print
> as it is executing ?

Reread the docstring of “shell-command”!  There you will find:

    “If COMMAND ends in ‘&’, execute it asynchronously.  The output
    appears in the buffer ‘*Async Shell Command*’.  That buffer is in
    shell mode.  You can also use ‘async-shell-command’ that
    automatically adds ‘&’.”

-- 
Felix Dietrich



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

* Re: Run terminal command with output in current buffer
  2021-07-16 15:44           ` lisa-asket
  2021-07-16 16:32             ` Felix Dietrich
@ 2021-07-16 16:36             ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-16 17:24               ` lisa-asket
  2021-07-16 18:04               ` lisa-asket
  1 sibling, 2 replies; 42+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-07-16 16:36 UTC (permalink / raw)
  To: help-gnu-emacs

>   (let ( (cmd-excl (read-from-minibuffer "exclude: "))
>          (cmd-incl (read-from-minibuffer "include: "))
>      (cmd-cnum (read-from-minibuffer "cnum: "))
>      (cmd-ptrn (read-from-minibuffer "pattern: "))
>      (cmd-dpth (read-from-minibuffer "dpth: "))
>       cmd )
>
>     (setq cmd-excl (concat " --exclude=\\*." cmd-excl))
>     (setq cmd-incl (concat " --include=\\*." cmd-incl))
>     (setq cmd-cnum (concat " -C " cmd-cnum))
>
>     (setq cmd (concat "grep -hir" cmd-excl cmd-incl
>               cmd-cnum " " cmd-ptrn " " cmd-dpth))
>     (message "%s" cmd)
>     (shell-command cmd (current-buffer))) )

I'd probably write this as something like:

    (let* ((cmd-excl (concat "--exclude=*."
                             (read-string "exclude: ")))
           (cmd-incl (concat "--include=*."
                             (read-string "include: ")))
           (cmd-cnum (read-string "cnum: "))
           (cmd-ptrn (read-string "pattern: "))
           (cmd-dpth (read-string "dpth: "))
           (cmd1 `("grep" "-hir" ,cmd-excl ,cmd-incl
                          "-C" ,cmd-cnum ,cmd-ptrn))
           (cmd (concat (mapconcat #'shell-quote-argument cmd1 " ")
                        " " cmd-depth)))
      (message "%s %s" cmd)
      (shell-command cmd (current-buffer))) )

Note the use of `read-string` (`read-from-minibuffer` is a low-level
function used to implement `read-string`, `read-number`, `read-buffer`,
`completing-read`, ...) and the use of `shell-quote-argument` to deal
with quoting those parts that need it.

I presumed that "dpth" is supposed to be a glob pattern, which is the
only place where you actually need the shell.  You could also use
`file-expand-wildcards` instead so you don't need a shell at all (and
hence don't need `shell-quote-argument` either) and can use
`call-process` instead of `shell-command` which stops you from worrying
about what happens if the users put a `|`, `;`, `$(cmd)`, or other fun
stuff in dpth.


        Stefan




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

* terminal command with output in current buffer
  2021-07-16 16:32             ` Felix Dietrich
@ 2021-07-16 17:00               ` lisa-asket
  2021-07-16 19:35                 ` Felix Dietrich
  0 siblings, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-16 17:00 UTC (permalink / raw)
  To: Felix Dietrich, help-gnu-emacs

Many thanks, I just did not follow what asynchronously meant.



Can one assign default values when `read-from-minibuffer` in empty?


From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 18:32:00 Europe/Paris

lisa-asket@perso.be writes:

> But when using `shell-command` I have to wait until the command is
> executed before it gets displayed in the buffer. Can I make it print
> as it is executing ?

Reread the docstring of “shell-command”! There you will find:

“If COMMAND ends in ‘&’, execute it asynchronously. The output
appears in the buffer ‘*Async Shell Command*’. That buffer is in
shell mode. You can also use ‘async-shell-command’ that
automatically adds ‘&’.”

-- 
Felix Dietrich




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

* Run terminal command with output in current buffer
  2021-07-16 16:36             ` Run " Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-16 17:24               ` lisa-asket
  2021-07-16 18:01                 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-16 18:04               ` lisa-asket
  1 sibling, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-16 17:24 UTC (permalink / raw)
  To: monnier, help-gnu-emacs

I use `dpth` for the directory path for grep.  The search pattern regexp is `cmd-ptrn`.



--------



From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 18:36:49 Europe/Paris

> (let ( (cmd-excl (read-from-minibuffer "exclude: "))
> (cmd-incl (read-from-minibuffer "include: "))
> (cmd-cnum (read-from-minibuffer "cnum: "))
> (cmd-ptrn (read-from-minibuffer "pattern: "))
> (cmd-dpth (read-from-minibuffer "dpth: "))
> cmd )
>
> (setq cmd-excl (concat " --exclude=\\*." cmd-excl))
> (setq cmd-incl (concat " --include=\\*." cmd-incl))
> (setq cmd-cnum (concat " -C " cmd-cnum))
>
> (setq cmd (concat "grep -hir" cmd-excl cmd-incl
> cmd-cnum " " cmd-ptrn " " cmd-dpth))
> (message "%s" cmd)
> (shell-command cmd (current-buffer))) )

I'd probably write this as something like:

(let* ((cmd-excl (concat "--exclude=*."
(read-string "exclude: ")))
(cmd-incl (concat "--include=*."
(read-string "include: ")))
(cmd-cnum (read-string "cnum: "))
(cmd-ptrn (read-string "pattern: "))
(cmd-dpth (read-string "dpth: "))
(cmd1 `("grep" "-hir" ,cmd-excl ,cmd-incl
"-C" ,cmd-cnum ,cmd-ptrn))
(cmd (concat (mapconcat #'shell-quote-argument cmd1 " ")
" " cmd-depth)))
(message "%s %s" cmd)
(shell-command cmd (current-buffer))) )

Note the use of `read-string` (`read-from-minibuffer` is a low-level
function used to implement `read-string`, `read-number`, `read-buffer`,
`completing-read`, ...) and the use of `shell-quote-argument` to deal
with quoting those parts that need it.

I presumed that "dpth" is supposed to be a glob pattern, which is the
only place where you actually need the shell. You could also use
`file-expand-wildcards` instead so you don't need a shell at all (and
hence don't need `shell-quote-argument` either) and can use
`call-process` instead of `shell-command` which stops you from worrying
about what happens if the users put a `|`, `;`, `$(cmd)`, or other fun
stuff in dpth.







Stefan





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

* Re: Run terminal command with output in current buffer
  2021-07-16 17:24               ` lisa-asket
@ 2021-07-16 18:01                 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-16 18:07                   ` lisa-asket
  0 siblings, 1 reply; 42+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-07-16 18:01 UTC (permalink / raw)
  To: help-gnu-emacs

lisa-asket@perso.be [2021-07-16 19:24:09] wrote:
> I use `dpth` for the directory path for grep.
> The search pattern regexp is `cmd-ptrn`.

Thanks for confirming, so I understood it right.
[ I had a doubt because this part of the arguments is supposed to be
  a set of file names (i.e. often a glob pattern) whereas `dpth` sounded
  like "depth" to me.    ]


        Stefan




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

* Run terminal command with output in current buffer
  2021-07-16 16:36             ` Run " Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-16 17:24               ` lisa-asket
@ 2021-07-16 18:04               ` lisa-asket
  1 sibling, 0 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16 18:04 UTC (permalink / raw)
  To: monnier, help-gnu-emacs

Have attempted to call  `read-number` for `cmd-cnum` which gets emacs to complain

when I enter the value `8` for cmd-cnum.



(cmd-cnum (read-number "-C "))


From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 18:36:49 Europe/Paris

> (let ( (cmd-excl (read-from-minibuffer "exclude: "))
> (cmd-incl (read-from-minibuffer "include: "))
> (cmd-cnum (read-from-minibuffer "cnum: "))
> (cmd-ptrn (read-from-minibuffer "pattern: "))
> (cmd-dpth (read-from-minibuffer "dpth: "))
> cmd )
>
> (setq cmd-excl (concat " --exclude=\\*." cmd-excl))
> (setq cmd-incl (concat " --include=\\*." cmd-incl))
> (setq cmd-cnum (concat " -C " cmd-cnum))
>
> (setq cmd (concat "grep -hir" cmd-excl cmd-incl
> cmd-cnum " " cmd-ptrn " " cmd-dpth))
> (message "%s" cmd)
> (shell-command cmd (current-buffer))) )

I'd probably write this as something like:

(let* ((cmd-excl (concat "--exclude=*."
(read-string "exclude: ")))
(cmd-incl (concat "--include=*."
(read-string "include: ")))
(cmd-cnum (read-string "cnum: "))
(cmd-ptrn (read-string "pattern: "))
(cmd-dpth (read-string "dpth: "))
(cmd1 `("grep" "-hir" ,cmd-excl ,cmd-incl
"-C" ,cmd-cnum ,cmd-ptrn))
(cmd (concat (mapconcat #'shell-quote-argument cmd1 " ")
" " cmd-depth)))
(message "%s %s" cmd)
(shell-command cmd (current-buffer))) )

Note the use of `read-string` (`read-from-minibuffer` is a low-level
function used to implement `read-string`, `read-number`, `read-buffer`,
`completing-read`, ...) and the use of `shell-quote-argument` to deal
with quoting those parts that need it.

I presumed that "dpth" is supposed to be a glob pattern, which is the
only place where you actually need the shell. You could also use
`file-expand-wildcards` instead so you don't need a shell at all (and
hence don't need `shell-quote-argument` either) and can use
`call-process` instead of `shell-command` which stops you from worrying
about what happens if the users put a `|`, `;`, `$(cmd)`, or other fun
stuff in dpth.


Stefan





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

* Run terminal command with output in current buffer
  2021-07-16 18:01                 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-16 18:07                   ` lisa-asket
  0 siblings, 0 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-16 18:07 UTC (permalink / raw)
  To: monnier, help-gnu-emacs

From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Run terminal command with output in current buffer
Date: 16/07/2021 20:01:51 Europe/Paris

lisa-asket@perso.be [2021-07-16 19:24:09] wrote:
>> I use `dpth` for the directory path for grep.
>> The search pattern regexp is `cmd-ptrn`.

>Thanks for confirming, so I understood it right.
>[ I had a doubt because this part of the arguments is supposed to be
>a set of file names (i.e. often a glob pattern) whereas `dpth` sounded
>like "depth" to me. ]

>Stefan



Have now changed the name to be cmd-path to avoid mis-representation,











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

* Re: terminal command with output in current buffer
  2021-07-16 17:00               ` lisa-asket
@ 2021-07-16 19:35                 ` Felix Dietrich
  2021-07-16 19:48                   ` lisa-asket
  2021-07-19  1:10                   ` terminal command with output in current buffer Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 42+ messages in thread
From: Felix Dietrich @ 2021-07-16 19:35 UTC (permalink / raw)
  To: help-gnu-emacs

lisa-asket@perso.be writes:

> Many thanks, I just did not follow what asynchronously meant.

Fair enough.

> Can one assign default values when `read-from-minibuffer` in empty?

“read-from-minibuffer” as well as the functions suggested by Stefan
Monnier (“read-number”, “read-string”, …) have a parameter DEFAULT or
DEFAULT-VALUE (or similar) that you may use for that purpose; read their
documentation.

You may also check the return value of “read-from-minibuffer” or
“read-string” and act accordingly:

    (let ((read-result (read-from-minibuffer "Prompt: ")))
      (if (seq-empty-p read-result)
        (setq read-result "default-value")))

See also:

    <https://www.gnu.org/software/emacs/manual/html_node/eintr/if.html>

-- 
Felix Dietrich



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

* terminal command with output in current buffer
  2021-07-16 19:35                 ` Felix Dietrich
@ 2021-07-16 19:48                   ` lisa-asket
  2021-07-16 21:58                     ` Felix Dietrich
  2021-07-19  1:10                   ` terminal command with output in current buffer Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-16 19:48 UTC (permalink / raw)
  To: Felix Dietrich, help-gnu-emacs

I am getting a small problem when using  `(cmd-cnum (read-number "context: "))`



Wrong type argument: sequencep, 8 



From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: help-gnu-emacs@gnu.org
Subject: Re: terminal command with output in current buffer
Date: 16/07/2021 21:35:05 Europe/Paris

lisa-asket@perso.be writes:

> Many thanks, I just did not follow what asynchronously meant.

Fair enough.

> Can one assign default values when `read-from-minibuffer` in empty?

“read-from-minibuffer” as well as the functions suggested by Stefan
Monnier (“read-number”, “read-string”, …) have a parameter DEFAULT or
DEFAULT-VALUE (or similar) that you may use for that purpose; read their
documentation.

You may also check the return value of “read-from-minibuffer” or
“read-string” and act accordingly:

(let ((read-result (read-from-minibuffer "Prompt: ")))
(if (seq-empty-p read-result)
(setq read-result "default-value")))

See also:

<https://www.gnu.org/software/emacs/manual/html_node/eintr/if.html>

-- 
Felix Dietrich




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

* Re: terminal command with output in current buffer
  2021-07-16 19:48                   ` lisa-asket
@ 2021-07-16 21:58                     ` Felix Dietrich
  2021-07-17  5:58                       ` lisa-asket
  2021-07-17  9:09                       ` terminal command with output in current buff lisa-asket
  0 siblings, 2 replies; 42+ messages in thread
From: Felix Dietrich @ 2021-07-16 21:58 UTC (permalink / raw)
  To: help-gnu-emacs

lisa-asket@perso.be writes:

> I am getting a small problem when using `(cmd-cnum (read-number "context: "))`
>
> Wrong type argument: sequencep, 8 

Could you provide more context?  If you are trying to use ‘seq-empty-p’
with the result of ‘read-number’: that does not work because the result
of ‘read-number’ is a number not a sequence (which a string as returned
by ‘read-from-minibuffer’ is).

-- 
Felix Dietrich



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

* terminal command with output in current buffer
  2021-07-16 21:58                     ` Felix Dietrich
@ 2021-07-17  5:58                       ` lisa-asket
  2021-07-17  9:09                       ` terminal command with output in current buff lisa-asket
  1 sibling, 0 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-17  5:58 UTC (permalink / raw)
  To: Felix Dietrich, help-gnu-emacs

From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: help-gnu-emacs@gnu.org
Subject: Re: terminal command with output in current buffer
Date: 16/07/2021 23:58:51 Europe/Paris

lisa-asket@perso.be writes:

> I am getting a small problem when using `(cmd-cnum (read-number "context: "))`
>
> Wrong type argument: sequencep, 8 

Could you provide more context? If you are trying to use ‘seq-empty-p’
with the result of ‘read-number’: that does not work because the result
of ‘read-number’ is a number not a sequence (which a string as returned
by ‘read-from-minibuffer’ is).



Right, this is what I have



      (cmd-cnum (read-number "context: " 8))

      (cmd-temp `("grep" "-hir" ,cmd-excl ,cmd-incl
              "-C" ,cmd-cnum ,cmd-ptrn))
      
      (cmd (concat (mapconcat #'shell-quote-argument cmd-temp " ")
            " " cmd-path)) )



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

* terminal command with output in current buff
  2021-07-16 21:58                     ` Felix Dietrich
  2021-07-17  5:58                       ` lisa-asket
@ 2021-07-17  9:09                       ` lisa-asket
  2021-07-17  9:19                         ` lisa-asket
  1 sibling, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-17  9:09 UTC (permalink / raw)
  To: Felix Dietrich, help-gnu-emacs

Thought the following could solve the problem, but was not so.



 (cmd-cnum (write-to-string (read-number "context: " 8)))



 (cmd-temp `("grep" "-hir" ,cmd-excl ,cmd-incl
              "-C" ,cmd-cnum ,cmd-ptrn))



From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: help-gnu-emacs@gnu.org
Subject: Re: terminal command with output in current buffer
Date: 16/07/2021 23:58:51 Europe/Paris

lisa-asket@perso.be writes:

> I am getting a small problem when using `(cmd-cnum (read-number "context: "))`
>
> Wrong type argument: sequencep, 8 

Could you provide more context? If you are trying to use ‘seq-empty-p’
with the result of ‘read-number’: that does not work because the result
of ‘read-number’ is a number not a sequence (which a string as returned
by ‘read-from-minibuffer’ is).

-- 
Felix Dietrich




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

* terminal command with output in current buff
  2021-07-17  9:09                       ` terminal command with output in current buff lisa-asket
@ 2021-07-17  9:19                         ` lisa-asket
  2021-07-17 11:33                           ` Felix Dietrich
  0 siblings, 1 reply; 42+ messages in thread
From: lisa-asket @ 2021-07-17  9:19 UTC (permalink / raw)
  To: lisa-asket, Felix Dietrich, help-gnu-emacs

number-to-string seems to be what is required



(cmd-cnum (number-to-string (read-number "context: " 8)))
(cmd-temp `("grep" "-hir" ,cmd-excl ,cmd-incl "-C" ,cmd-cnum ,cmd-ptrn))



After selecting wy search pattern and storing it in `cmd-ptrn`, how can I highlight 

the pattern when tho grep results are being transferred to the current buffer ?





From: lisa-asket@perso.be
To: Felix Dietrich <felix.dietrich@sperrhaken.name>;
   help-gnu-emacs@gnu.org
Subject: terminal command with output in current buff
Date: 17/07/2021 11:09:02 Europe/Paris

Thought the following could solve the problem, but was not so.



 (cmd-cnum (write-to-string (read-number "context: " 8)))



 (cmd-temp `("grep" "-hir" ,cmd-excl ,cmd-incl
              "-C" ,cmd-cnum ,cmd-ptrn))



From: Felix Dietrich <felix.dietrich@sperrhaken.name>
To: help-gnu-emacs@gnu.org
Subject: Re: terminal command with output in current buffer
Date: 16/07/2021 23:58:51 Europe/Paris

lisa-asket@perso.be writes:

> I am getting a small problem when using `(cmd-cnum (read-number "context: "))`
>
> Wrong type argument: sequencep, 8 

Could you provide more context? If you are trying to use ‘seq-empty-p’
with the result of ‘read-number’: that does not work because the result
of ‘read-number’ is a number not a sequence (which a string as returned
by ‘read-from-minibuffer’ is).

-- 
Felix Dietrich





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

* Re: terminal command with output in current buff
  2021-07-17  9:19                         ` lisa-asket
@ 2021-07-17 11:33                           ` Felix Dietrich
  0 siblings, 0 replies; 42+ messages in thread
From: Felix Dietrich @ 2021-07-17 11:33 UTC (permalink / raw)
  To: help-gnu-emacs

lisa-asket@perso.be writes:

> I am getting a small problem when using `(cmd-cnum (read-number "context: "))`
>
> Wrong type argument: sequencep, 8

Please provide, if possible, small self-contained examples that
reproduce your problem and more context around your snippets: I had to
hunt for the context in your past messages.  Here is a simplified
version of your code that causes the problem and serves as an example of
what I mean by “self-contained example”:


    (let* ((cmd-cnum (read-number "Number: "))
           (cmd-temp `("grep" ,cmd-cnum))
           ;; same as: (cmd-temp (list "grep" cmd-cnum))
      (mapconcat #'shell-quote-argument cmd-temp " "))


> number-to-string seems to be what is required

Anyway you have found a solution to your problem.  Let me just add a
short explanation in case the issue is not entirely clear to you yet:
‘shell-quote-argument’ works on strings; when you map over a list, you
apply the function on each element of the list and collect the results.
Therefore, in your call to ‘mapconcat’ the function
‘shell-quote-argument’ will eventually be passed the value of
‘cmd-cnum’, which you have inserted into the list.  Examples that do not
work:


    (shell-quote-argument 5)
    ⇒ Wrong type argument: sequencep, 5

    (mapconcat #'shell-quote-argument '(5) " ")
    ⇒ Wrong type argument: sequencep, 5

    (mapconcat #'shell-quote-argument '("grep" 5) " ")
    ⇒ Wrong type argument: sequencep, 5

    (mapconcat #'shell-quote-argument '(5 "grep") " ")
    ⇒ Wrong type argument: sequencep, 5


> After selecting wy search pattern and storing it in `cmd-ptrn`, how
> can I highlight the pattern when tho grep results are being
> transferred to the current buffer ?

Now itʼs getting complicated and I couldnʼt give you an answer of the of
my head: look into adding text properties [1] to strings and at process
filters [2].  How could you identify the information elements grep puts
on each line and their position?  It will probably also be insightful to
look at how the Emacs command “M-x grep” [3] is implemented.


Footnotes:
[1]  <https://www.gnu.org/software/emacs/manual/html_node/elisp/Text-Props-and-Strings.html>

[2]  <https://www.gnu.org/software/emacs/manual/html_node/elisp/Filter-Functions.html>

[3]  <https://www.gnu.org/software/emacs/manual/html_node/emacs/Grep-Searching.html>


-- 
Felix Dietrich



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

* Re: Run terminal command with output in current buffer
  2021-07-16 15:21         ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-16 15:44           ` lisa-asket
@ 2021-07-19  0:42           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-19  4:49             ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 42+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-19  0:42 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

> Only use `setq` when you really want that variable to
> contain different values at different times.

How do you do it when you compute a value as part of a loop?

We just saw it with binary-search, so let's use that
as example. OK, so they use `setf', not `setq' (set function,
not set function quote - ?)

That's better, really? Not in that sense at least?

(defun binary-search (value array)
  (let ((low 0)
        (high (1- (length array))))
    (cl-do () ((< high low) nil)
      (let ((middle (floor (+ low high) 2)))
        (cond ((> (aref array middle) value)
               (setf high (1- middle)))
              ((< (aref array middle) value)
               (setf low (1+ middle)))
              (t (cl-return middle)))))))

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: terminal command with output in current buffer
  2021-07-16 19:35                 ` Felix Dietrich
  2021-07-16 19:48                   ` lisa-asket
@ 2021-07-19  1:10                   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-19  4:54                     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-20 12:55                     ` terminal command with output in current buffer Felix Dietrich
  1 sibling, 2 replies; 42+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-19  1:10 UTC (permalink / raw)
  To: help-gnu-emacs

Felix Dietrich wrote:

> (let ((read-result (read-from-minibuffer "Prompt: ")))
>   (if (seq-empty-p read-result)
>     (setq read-result "default-value")))

What? :P `setq' again...

  (let*((read-result (read-from-minibuffer "Prompt: "))
        (value       (if (string= "" read-result)
                         "default-value"
                       read-result) ))
    value)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Run terminal command with output in current buffer
  2021-07-19  0:42           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-19  4:49             ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-19 20:51               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 42+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-07-19  4:49 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg via Users list for the GNU Emacs text editor [2021-07-19 02:42:44] wrote:
> Stefan Monnier via Users list for the GNU Emacs text editor wrote:
>> Only use `setq` when you really want that variable to
>> contain different values at different times.
> How do you do it when you compute a value as part of a loop?

AFAICT in your case you do want the variable(s) to contain different values
at different times.

> We just saw it with binary-search, so let's use that as example. OK,
> so they use `setf', not `setq' (set function, not set function
> quote - ?)

`setf` and `setq` are basically the same thing here.

BTW, if you really do want to avoid `setq` (and hence `setf` as well),
you can of course do it, using recursion:

    (defun binary-search (value array)
      (cl-labels ((loop (low high)
                    (let ((middle (floor (+ low high) 2)))
                      (cond ((> (aref array middle) value)
                             (loop low (1- middle)))
                            ((< (aref array middle) value)
                             (loop (1+ middle) high))
                            (t (cl-return middle))))))
        (loop 0 (1- (length array)))))


-- Stefan




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

* Re: terminal command with output in current buffer
  2021-07-19  1:10                   ` terminal command with output in current buffer Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-19  4:54                     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-19 23:12                       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-20 12:05                       ` ‘read-string’ over ‘read-from-minibuffer’ Felix Dietrich
  2021-07-20 12:55                     ` terminal command with output in current buffer Felix Dietrich
  1 sibling, 2 replies; 42+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-07-19  4:54 UTC (permalink / raw)
  To: help-gnu-emacs

>   (let*((read-result (read-from-minibuffer "Prompt: "))

Please don't use `read-from-minibuffer` unless you're defining
a `read-<foo>` function.
Use `read-string` instead.


        Stefan




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

* Re: Run terminal command with output in current buffer
  2021-07-19  4:49             ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-19 20:51               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 42+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-19 20:51 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

> AFAICT in your case you do want the variable(s) to contain
> different values at different times.

You know there is one and only one correct value that you are
supposed to use in the program, only to compute that you use
a loop, so the "you do want the variable(s) to contain
different values at different times" is only true in terms of
the computation of the final value, if you follow. But maybe
that qualifies?

> `setf` and `setq` are basically the same thing here.

I know right?

> BTW, if you really do want to avoid `setq` (and hence `setf`
> as well), you can of course do it, using recursion:

Yes, but maybe you really do want to avoid that even more...

Nah, I think `setq' in loops are the exception that confirms
the other rule, that one should only use it for global vars.

And when should you use global vars?

When they already exist :)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: terminal command with output in current buffer
  2021-07-19  4:54                     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-19 23:12                       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-20 12:05                       ` ‘read-string’ over ‘read-from-minibuffer’ Felix Dietrich
  1 sibling, 0 replies; 42+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-19 23:12 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

>> (let*((read-result (read-from-minibuffer "Prompt: "))
>
> Please don't use `read-from-minibuffer` unless you're
> defining a `read-<foo>` function. Use `read-string` instead.

OK, thanks, 5 places to fix...

I wonder what the last function, "replace-regexp-1" is
supposed to do, what the -1 means, and what sets it apart
from what should be there already, is what I'm thinking ... or
am I wrong?

;; in https://dataswamp.org/~incal/emacs-init/edit.el ...

(defun insert-string-centered (str &optional width)
  (interactive
   (list (read-string "string: ")
         (read-number "width: " (window-text-width) ) ))
  (let*((span    (if (and width (< 0 width)) width (window-text-width)))
        (str-len (length str))
        (pad     (- (/ (- span str-len) 2)
                    (if (zerop (mod str-len 2)) 1 0) ))
        (pad-str (make-string pad ?\s)) )
    (insert pad-str str) ))
(defalias 'isc #'insert-string-centered)

;; https://dataswamp.org/~incal/emacs-init/time-cmp.el

(defun dope (from boxes pills dose)
  (interactive
   (list
    (read-string "from: " (format-time-string "%F"))
    (read-number "boxes: ")
    (read-number "pills/box: " 50)
    (read-number "pills/day: "  6) ))
  (let*((days (/ (* boxes pills) dose))
        (done (time-add (date-to-time (format "%sT00:00+01:00" from))
                        (days-to-time (1- days)) ))
        (str  (format-time-string "%F" done))
        (more (format-time-string "%F" (time-add done (days-to-time -10)))) )
    (insert (format "%s  [%s]  %s" from more str) )))

;; https://dataswamp.org/~incal/emacs-init/sort-incal.el

(defun sort-line-words (beg end &optional set-delim)
  (interactive "r\nP")
  (let*((str       (region-to-string))
        (delim-str (when set-delim (read-string "delimiter: ")))
        (str-list  (split-string str delim-str))
        (sorted    (erc-sort-strings str-list)) )
    (kill-region beg end)
    (if set-delim
        (progn
          (dolist (s (nreverse (cdr (nreverse sorted))))
            (insert (format "%s%s" s delim-str)))
          (insert (format "%s" (car (last sorted)))))
      (insert-string-list sorted) )))

;; https://dataswamp.org/~incal/emacs-init/latex.el

(defun replace-regexp-1 (regexp to-string &optional beg end)
  (interactive
   `(,(read-string "regexp: ")
     ,(read-string "to string: ")
     ,@(if (use-region-p)
           (list (region-beginning) (region-end))
         (list (point-min) (point-max))) ))
  (let ((beg-set (or beg (point-min)))
        (end-set (or end (point-max))) )
    (save-excursion
      (goto-char end-set)
      (while (re-search-backward regexp beg-set t)
        (replace-match to-string) ))))
(defalias 'rr #'replace-regexp-1)

-- 
underground experts united
https://dataswamp.org/~incal




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

* ‘read-string’ over ‘read-from-minibuffer’
  2021-07-19  4:54                     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-19 23:12                       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-07-20 12:05                       ` Felix Dietrich
  2021-07-20 15:11                         ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 42+ messages in thread
From: Felix Dietrich @ 2021-07-20 12:05 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>   (let*((read-result (read-from-minibuffer "Prompt: "))
>
> Please don't use `read-from-minibuffer` unless you're defining
> a `read-<foo>` function.
> Use `read-string` instead.

Could you elaborate?  I see that ‘read-string’ ensures that there is no
‘minibuffer-completion-table’ set, which is relevant for recursive
minibuffers, if I understand itʼs comment correctly.  Anything else one
should be wary about?

-- 
Felix Dietrich



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

* Re: terminal command with output in current buffer
  2021-07-19  1:10                   ` terminal command with output in current buffer Emanuel Berg via Users list for the GNU Emacs text editor
  2021-07-19  4:54                     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-20 12:55                     ` Felix Dietrich
  2021-07-20 18:15                       ` Yuri Khan
  1 sibling, 1 reply; 42+ messages in thread
From: Felix Dietrich @ 2021-07-20 12:55 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> Felix Dietrich wrote:
>
>> (let ((read-result (read-from-minibuffer "Prompt: ")))
>>   (if (seq-empty-p read-result)
>>     (setq read-result "default-value")))
>
> What? :P `setq' again...
>
>   (let*((read-result (read-from-minibuffer "Prompt: "))
>         (value       (if (string= "" read-result)
>                          "default-value"
>                        read-result) ))
>     value)

This is whatʼs bothering you the most? ;)  I have reread the part of the
elisp manual about reading text from the minibuffer, and it suggests one
“do all minibuffer input as part of reading the arguments for a command,
in the ‘interactive’ specification.” [1]  Not directly applicable to a
stand-alone let snippet but relevant in the context of the thread (which
involved reading user input for an interactive command).


Footnotes:
[1]  (info "(elisp) Text from Minibuffer")

-- 
Felix Dietrich



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

* Re: ‘read-string’ over ‘read-from-minibuffer’
  2021-07-20 12:05                       ` ‘read-string’ over ‘read-from-minibuffer’ Felix Dietrich
@ 2021-07-20 15:11                         ` Stefan Monnier via Users list for the GNU Emacs text editor
  2021-07-20 15:46                           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 42+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2021-07-20 15:11 UTC (permalink / raw)
  To: help-gnu-emacs

Felix Dietrich [2021-07-20 14:05:03] wrote:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>>>   (let*((read-result (read-from-minibuffer "Prompt: "))
>> Please don't use `read-from-minibuffer` unless you're defining
>> a `read-<foo>` function.
>> Use `read-string` instead.
>
> Could you elaborate?

`read-from-minibuffer` is the function designed as the core of `read-string`,
`read-number`, `completing-read`, and friends.

If you want to write such a `read-<foo>` function, then
`read-from-minibuffer` is for you.

If you want to just read a *string*, then `read-string` is what you
should use.  It doesn't behave very differently from
`read-from-minibuffer`, but it makes your code's intentions more clear.


        Stefan




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

* Re: ‘read-string’ over ‘read-from-minibuffer’
  2021-07-20 15:11                         ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2021-07-20 15:46                           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 42+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-07-20 15:46 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

> `read-from-minibuffer` is the function designed as the core
> of `read-string`, `read-number`, `completing-read`,
> and friends.
>
> If you want to write such a `read-<foo>` function, then
> `read-from-minibuffer` is for you.
>
> If you want to just read a *string*, then `read-string` is
> what you should use. It doesn't behave very differently from
> `read-from-minibuffer`, but it makes your code's intentions
> more clear.

Yes, we understand, it is not meant to be used directly from
Lisp in general but only from the type-specific functions,
however what happens is that you stumble upon it, test it and
it works, and then use it. Maybe the byte compiler can tell
you not to use it? (Can you make it not say the same when used
correctly/as intended?)

The docstring for `read-from-minibuffer', tho very long, it
does not mention either `read-string' or `read-number',
actually putting this information there might not help for the
most part since the impression is straightforward (which is
good!) and after testing it and seeing that it works as
expected, people will be even less inclined to read that long
doc string.

But, if you put it there, at least you can yank that part from
the docstring into a post like this so the "elaborating" part
will be faster :P

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: terminal command with output in current buffer
  2021-07-20 12:55                     ` terminal command with output in current buffer Felix Dietrich
@ 2021-07-20 18:15                       ` Yuri Khan
  2021-07-20 18:23                         ` lisa-asket
  0 siblings, 1 reply; 42+ messages in thread
From: Yuri Khan @ 2021-07-20 18:15 UTC (permalink / raw)
  To: Felix Dietrich; +Cc: help-gnu-emacs

On Tue, 20 Jul 2021 at 20:07, Felix Dietrich
<felix.dietrich@sperrhaken.name> wrote:

> This is whatʼs bothering you the most? ;)  I have reread the part of the
> elisp manual about reading text from the minibuffer, and it suggests one
> “do all minibuffer input as part of reading the arguments for a command,
> in the ‘interactive’ specification.”

What’s bothering *me* in this thread is that we still don’t know the
*actual problem* lisa-asket is trying to solve (as opposed to
fragments of an envisioned solution).



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

* terminal command with output in current buffer
  2021-07-20 18:15                       ` Yuri Khan
@ 2021-07-20 18:23                         ` lisa-asket
  0 siblings, 0 replies; 42+ messages in thread
From: lisa-asket @ 2021-07-20 18:23 UTC (permalink / raw)
  To: Yuri Khan, Felix Dietrich; +Cc: help-gnu-emacs

From: Yuri Khan <yuri.v.khan@gmail.com>
To: Felix Dietrich <felix.dietrich@sperrhaken.name>
Subject: Re: terminal command with output in current buffer
Date: 20/07/2021 20:15:53 Europe/Paris
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>

On Tue, 20 Jul 2021 at 20:07, Felix Dietrich
<felix.dietrich@sperrhaken.name> wrote:

> This is whatʼs bothering you the most? ;) I have reread the part of the
> elisp manual about reading text from the minibuffer, and it suggests one
> “do all minibuffer input as part of reading the arguments for a command,
> in the ‘interactive’ specification.”

What’s bothering *me* in this thread is that we still don’t know the
*actual problem* lisa-asket is trying to solve (as opposed to
fragments of an envisioned solution).

The problem has been solved, but then others continued discussing some

intricacies and observations, which is fine with me.  




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

end of thread, other threads:[~2021-07-20 18:23 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-16  6:43 Run terminal command with output in current buffer lisa-asket
2021-07-16  6:53 ` Jean-Christophe Helary
2021-07-16  7:07 ` Eli Zaretskii
2021-07-16  9:04   ` lisa-asket
2021-07-16 11:32     ` Eli Zaretskii
2021-07-16 11:48       ` lisa-asket
2021-07-16 11:30   ` lisa-asket
2021-07-16 11:58     ` lisa-asket
2021-07-16 15:14       ` Felix Dietrich
2021-07-16 15:21         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-16 15:44           ` lisa-asket
2021-07-16 16:32             ` Felix Dietrich
2021-07-16 17:00               ` lisa-asket
2021-07-16 19:35                 ` Felix Dietrich
2021-07-16 19:48                   ` lisa-asket
2021-07-16 21:58                     ` Felix Dietrich
2021-07-17  5:58                       ` lisa-asket
2021-07-17  9:09                       ` terminal command with output in current buff lisa-asket
2021-07-17  9:19                         ` lisa-asket
2021-07-17 11:33                           ` Felix Dietrich
2021-07-19  1:10                   ` terminal command with output in current buffer Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-19  4:54                     ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-19 23:12                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-20 12:05                       ` ‘read-string’ over ‘read-from-minibuffer’ Felix Dietrich
2021-07-20 15:11                         ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-20 15:46                           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-20 12:55                     ` terminal command with output in current buffer Felix Dietrich
2021-07-20 18:15                       ` Yuri Khan
2021-07-20 18:23                         ` lisa-asket
2021-07-16 16:36             ` Run " Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-16 17:24               ` lisa-asket
2021-07-16 18:01                 ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-16 18:07                   ` lisa-asket
2021-07-16 18:04               ` lisa-asket
2021-07-19  0:42           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-19  4:49             ` Stefan Monnier via Users list for the GNU Emacs text editor
2021-07-19 20:51               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-16 15:22         ` lisa-asket
2021-07-16 13:25     ` Felix Dietrich
2021-07-16 13:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-07-16 14:28   ` lisa-asket
2021-07-16 15:06     ` lisa-asket

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.