[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Run terminal command with output in current buffer
From: |
lisa-asket |
Subject: |
Run terminal command with output in current buffer |
Date: |
Fri, 16 Jul 2021 19:24:09 +0200 (CEST) |
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
- Re: terminal command with output in current buffer, (continued)
- Re: terminal command with output in current buffer, Emanuel Berg, 2021/07/18
- Re: terminal command with output in current buffer, Stefan Monnier, 2021/07/19
- Re: terminal command with output in current buffer, Emanuel Berg, 2021/07/19
- ‘read-string’ over ‘read-from-minibuffer’, Felix Dietrich, 2021/07/20
- Re: ‘read-string’ over ‘read-from-minibuffer’, Stefan Monnier, 2021/07/20
- Re: ‘read-string’ over ‘read-from-minibuffer’, Emanuel Berg, 2021/07/20
- Re: terminal command with output in current buffer, Felix Dietrich, 2021/07/20
- Re: terminal command with output in current buffer, Yuri Khan, 2021/07/20
- terminal command with output in current buffer, lisa-asket, 2021/07/20
- Re: Run terminal command with output in current buffer, Stefan Monnier, 2021/07/16
- Run terminal command with output in current buffer,
lisa-asket <=
- Re: Run terminal command with output in current buffer, Stefan Monnier, 2021/07/16
- Run terminal command with output in current buffer, lisa-asket, 2021/07/16
- Run terminal command with output in current buffer, lisa-asket, 2021/07/16
- Re: Run terminal command with output in current buffer, Emanuel Berg, 2021/07/18
- Re: Run terminal command with output in current buffer, Stefan Monnier, 2021/07/19
- Re: Run terminal command with output in current buffer, Emanuel Berg, 2021/07/19
- Run terminal command with output in current buffer, lisa-asket, 2021/07/16
- Re: Run terminal command with output in current buffer, Felix Dietrich, 2021/07/16
Re: Run terminal command with output in current buffer, Emanuel Berg, 2021/07/16