From: Raffaele Ricciardi <rfflrccrd@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Examining the output of a shell command?
Date: Mon, 29 Jun 2015 13:26:38 +0200 [thread overview]
Message-ID: <cvcobeF8753U1@mid.individual.net> (raw)
In-Reply-To: <mailman.5831.1435499861.904.help-gnu-emacs@gnu.org>
On 28/06/15 15:57, Pascal J. Bourguignon wrote:
> (shell-command-to-string "echo $RED hello $NORMAL")
> --> "[31m hello [0m> "
Hence, how could I capture a colorized output in Emacs as in a Bash
session?
Below is a Bash script that recursively searches "*.el" files for the
string "defun" and highlights matches (the complex `find` call is the
shell command generated by `M-x rgrep` for the same task):
#!/bin/bash
export GREP_COLORS="mt=01;31:fn=:ln=:bn=:se=:sl=:cx=:ne"
export GREP_OPTIONS="--color=auto"
find . -type d \( -path \*/SCCS -o -path \*/RCS -o -path \*/CVS -o
-path \*/MCVS -o -path \*/.svn -o -path \*/.git -o -path \*/.hg -o -path
\*/.bzr -o -path \*/_MTN -o -path \*/_darcs -o -path \*/\{arch\} \)
-prune -o \! -type d \( -name \*_flymake.\* -o -name .\#\* -o -name \*.o
-o -name \*\~ -o -name \*.bin -o -name \*.lbin -o -name \*.so -o -name
\*.a -o -name \*.ln -o -name \*.blg -o -name \*.bbl -o -name \*.elc -o
-name \*.lof -o -name \*.glo -o -name \*.idx -o -name \*.lot -o -name
\*.fmt -o -name \*.tfm -o -name \*.class -o -name \*.fas -o -name \*.lib
-o -name \*.mem -o -name \*.x86f -o -name \*.sparcf -o -name \*.dfsl -o
-name \*.pfsl -o -name \*.d64fsl -o -name \*.p64fsl -o -name \*.lx64fsl
-o -name \*.lx32fsl -o -name \*.dx64fsl -o -name \*.dx32fsl -o -name
\*.fx64fsl -o -name \*.fx32fsl -o -name \*.sx64fsl -o -name \*.sx32fsl
-o -name \*.wx64fsl -o -name \*.wx32fsl -o -name \*.fasl -o -name
\*.ufsl -o -name \*.fsl -o -name \*.dxl -o -name \*.lo -o -name \*.la -o
-name \*.gmo -o -name \*.mo -o -name \*.toc -o -name \*.aux -o -name
\*.cp -o -name \*.fn -o -name \*.ky -o -name \*.pg -o -name \*.tp -o
-name \*.vr -o -name \*.cps -o -name \*.fns -o -name \*.kys -o -name
\*.pgs -o -name \*.tps -o -name \*.vrs -o -name \*.pyc -o -name \*.pyo
\) -prune -o -type f \( -iname \*.el \) -exec grep -i -n -e defun
/dev/null {} +
I have tested the above Bash script with `bash --noprofile --norc` as
well, to disable any initialization. As far as i can tell, Emacs is
running Bash -- `(getenv "SHELL")` returns "/bin/bash" -- without any
initialization scripts -- `(getenv "BASH_ENV")` returns `nil`.
Below is an Emacs Lisp function that returns the output of the same
shell command as a string:
(defun emacs-grep-test ()
"Return the output of the `grep` program."
(let ((process-environment (copy-list process-environment)))
(setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:sl=:cx=:ne")
(setenv "GREP_OPTIONS" "--color=auto")
(shell-command-to-string "find . -type d \\( -path \\*/SCCS -o
-path \\*/RCS -o -path \\*/CVS -o -path \\*/MCVS -o -path \\*/.svn -o
-path \\*/.git -o -path \\*/.hg -o -path \\*/.bzr -o -path \\*/_MTN -o
-path \\*/_darcs -o -path \\*/\\{arch\\} \\) -prune -o \\! -type d \\(
-name \\*_flymake.\\* -o -name .\\#\\* -o -name \\*.o -o -name \\*\\~ -o
-name \\*.bin -o -name \\*.lbin -o -name \\*.so -o -name \\*.a -o -name
\\*.ln -o -name \\*.blg -o -name \\*.bbl -o -name \\*.elc -o -name
\\*.lof -o -name \\*.glo -o -name \\*.idx -o -name \\*.lot -o -name
\\*.fmt -o -name \\*.tfm -o -name \\*.class -o -name \\*.fas -o -name
\\*.lib -o -name \\*.mem -o -name \\*.x86f -o -name \\*.sparcf -o -name
\\*.dfsl -o -name \\*.pfsl -o -name \\*.d64fsl -o -name \\*.p64fsl -o
-name \\*.lx64fsl -o -name \\*.lx32fsl -o -name \\*.dx64fsl -o -name
\\*.dx32fsl -o -name \\*.fx64fsl -o -name \\*.fx32fsl -o -name
\\*.sx64fsl -o -name \\*.sx32fsl -o -name \\*.wx64fsl -o -name
\\*.wx32fsl -o -name \\*.fasl -o -name \\*.ufsl -o -name \\*.fsl -o
-name \\*.dxl -o -name \\*.lo -o -name \\*.la -o -name \\*.gmo -o -name
\\*.mo -o -name \\*.toc -o -name \\*.aux -o -name \\*.cp -o -name \\*.fn
-o -name \\*.ky -o -name \\*.pg -o -name \\*.tp -o -name \\*.vr -o -name
\\*.cps -o -name \\*.fns -o -name \\*.kys -o -name \\*.pgs -o -name
\\*.tps -o -name \\*.vrs -o -name \\*.pyc -o -name \\*.pyo \\) -prune -o
-type f \\( -iname \\*.el \\) -exec grep -i -n -e defun /dev/null {} +")))
However, the string returned by `M-: (emacs-grep-test) RET` does not
contain escape sequences to colorize the output, as I would expect.
next prev parent reply other threads:[~2015-06-29 11:26 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-28 12:47 Examining the output of a shell command? Raffaele Ricciardi
2015-06-28 13:57 ` Pascal J. Bourguignon
[not found] ` <mailman.5831.1435499861.904.help-gnu-emacs@gnu.org>
2015-06-28 14:03 ` Dan Espen
2015-06-28 14:20 ` Michael Heerdegen
[not found] ` <mailman.5833.1435501234.904.help-gnu-emacs@gnu.org>
2015-06-28 17:17 ` Dan Espen
2015-06-28 18:28 ` Michael Heerdegen
2015-06-29 1:39 ` Barry Margolin
2015-06-28 14:31 ` Raffaele Ricciardi
2015-06-28 14:55 ` Eli Zaretskii
[not found] ` <mailman.5839.1435503359.904.help-gnu-emacs@gnu.org>
2015-06-28 15:03 ` Raffaele Ricciardi
2015-06-28 15:43 ` Eli Zaretskii
[not found] ` <mailman.5843.1435506194.904.help-gnu-emacs@gnu.org>
2015-06-28 15:51 ` Raffaele Ricciardi
2015-06-28 15:52 ` Pascal J. Bourguignon
2015-06-29 1:53 ` Emanuel Berg
[not found] ` <mailman.5887.1435542902.904.help-gnu-emacs@gnu.org>
2015-06-29 8:13 ` Pascal J. Bourguignon
2015-06-29 11:26 ` Raffaele Ricciardi [this message]
2015-06-29 12:08 ` Pascal J. Bourguignon
2015-06-29 14:51 ` Eli Zaretskii
[not found] ` <mailman.5928.1435589507.904.help-gnu-emacs@gnu.org>
2015-06-29 15:56 ` Raffaele Ricciardi
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=cvcobeF8753U1@mid.individual.net \
--to=rfflrccrd@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/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.