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: Fri, 16 Jul 2021 12:36:49 -0400 [thread overview]
Message-ID: <jwvzgumi5x8.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: ea-mime-60f1a979-f01-62098eaa@www-7.mailo.com
> (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
next prev parent reply other threads:[~2021-07-16 16:36 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Stefan Monnier via Users list for the GNU Emacs text editor [this message]
2021-07-16 17:24 ` Run " 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvzgumi5x8.fsf-monnier+emacs@gnu.org \
--to=help-gnu-emacs@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.