Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional execution of elisp code based on value of environment variable

In my .emacs config file I have a following entry:

(custom-set-variables
  (custom-set-faces
    '(font-lock-comment-face ((((class color)
                                (min-colors 8)
                                (background dark))
                                (:foreground "red"))))))

This fixes the font color when TERM environment variable is set to screen, but breaks it when TERM is set to xterm. Is there a way to read value of TERM variable and execute that code only if TERM's value is screen? I found this questin slightly helpful, but still I don't know how to read values of environment variables in elisp.

like image 645
Jan Stolarek Avatar asked Dec 11 '22 21:12

Jan Stolarek


1 Answers

First I will answer what you asked, below I will answer the question you really should have asked ;)

I get the value of an environment variable you use the function getenv. For example:

(getenv "TERM")   ->  "xterm-color"

However, this is a relatively clumsy way to check if your Emacs run in the terminal. Instead you can use the following:

(display-graphic-p &optional DISPLAY)

Return non-nil if DISPLAY is a graphic display.
Graphical displays are those which are capable of displaying several
frames and several different fonts at once.  This is true for displays
that use a window system such as X, and false for text-only terminals.
DISPLAY can be a display name, a frame, or nil (meaning the selected
frame's display).

An older, deprecated, version is to check the variable window-system.

like image 200
Lindydancer Avatar answered Jan 18 '23 23:01

Lindydancer