Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell a shell that it is running from within Emacs?

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.

like image 971
thoni56 Avatar asked Dec 21 '22 09:12

thoni56


2 Answers

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

~ $ 
like image 140
zev Avatar answered Dec 27 '22 10:12

zev


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
like image 36
Foo Bah Avatar answered Dec 27 '22 10:12

Foo Bah