5

I have a program that uses ansi terminal codes to do fancy stuff like erasing lines when it runs in the terminal. When that is run from within Emacs, it obviously screws up the buffer.

The program has an option to not use those, so I'd like to be able to turn on that option when running in a shell within Emacs.

I execute this program as a part of a Makefile, either from the terminal or using compile-mode in Emacs.

So is there a way to have compile-mode set an environment variable (or something similar) that would be easy to catch in the Makefile to use the option when appropriate?

Manually adding an extra argument to make in the make-command (in either of the cases) is what I am trying to avoid.

thoni56
  • 3,145
  • 3
  • 31
  • 49

2 Answers2

7

Emacs will set the $INSIDE_EMACS environmental variable as well. This can be useful to you if you need to know not only whether you're running the shell session inside emacs but which version of emacs you're using and how you're running the shell session (which is useful to know if you're using ansi control characters in your scripts). Here's what I get when I'm in a bash shell in Emacs (using 'shell'):

~ $ echo $INSIDE_EMACS
24.0.50.3,comint
~ $ 

Here's the same when I'm in a Bourne shell in Emacs (using 'ansi-term'):

sh-3.2$ echo $INSIDE_EMACS
24.0.50.3,term:0.96
sh-3.2$ 

Here's what I get when I do the same from a terminal (not in Emacs):

~ $ echo $INSIDE_EMACS

~ $ 
zev
  • 3,480
  • 18
  • 13
4

EMACS sets the $EMACS environment variable within M-x shell sessions, so you should just check for that (assuming you don't inadvertently define EMACS elsewhere within your shell scripts):

# from login shell
echo $EMACS

# within Emacs shell
bash-4.2$ echo $EMACS
t
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • Perfect! Exactly what I was hoping for, but is so hard to find. – thoni56 Aug 20 '11 at 09:31
  • 3
    I figured this out once by just looking at the output of `env` in a shell running under emacs. – sanityinc Aug 20 '11 at 17:47
  • 2
    Foo Bah: You say "eshell", but I don't think you actually mean that? Experimentally `M-x eshell` does *not* set an `EMACS` environment variable, and also you would not be seeing a "bash" shell prompt with `eshell`. That environment variable does seem to be present for other shells started from Emacs, however (`M-x shell`, and `M-x term` or `ansi-term`). – phils Mar 25 '12 at 03:37
  • 2
    FYI: I notice that if I spawn a different terminal program (say xterm) from within M-x term mode in emacs, that both $EMACS and $INSIDE_EMACS remain set in the new xterm session. No other environment variables appear to carry over to the new xterm session. This seems weird. – bengineerd Apr 06 '12 at 22:36
  • 2
    According to http://www.gnu.org/software/emacs/manual/html_node/emacs/Interactive-Shell.html, the `EMACS` variable is deprecated and, "programs that use it should switch to using `INSIDE_EMACS` instead". – oyouareatubeo Nov 13 '15 at 01:13