I am finding that in some cases (e.g. when TERM=xterm-16color), the expression

(frame-parameter nil 'background-mode)

...evaluates to light when it is in fact a dark background.

How does Emacs decide the value of a frame's background-mode at startup, and how can I affect the decision?

EDIT: I realize that I can always run (setq frame-background-mode 'whatever), but this is not what I mean by "affect the decision" above. Rather, I mean: how can I modify the way I invoke Emacs (including the shell environment in which I invoke it) so that its default algorithm for setting frame-background-mode at startup gets it right.

EDIT2: I do not see how the answers to Emacs Blue color too dark answer my post's main question, namely: how does Emacs figure out the value it should set frame-background-mode to at startup? (For the sake of this question, assume that there is no .emacs/initialization file at startup.)

  • Does this answer your question? Emacs Blue color too dark
    – NickD
    Aug 1 at 13:24
  • @NickD: thank you for the pointer, but I don't see the answer to my first question (i.e., how does Emacs set the value of background-mode to begin with) in that thread. Also, though that thread does give at least one way to affect the decision (namely, by running (setq background-mode ...)) this is heavier than what I was hoping to find. (My question's original version did not make this last point clear at all, but I just edited it to fix this.)
    – kjo
    Aug 1 at 14:11
  • I meant to point to @wasamasa's answer in that question, not to the question itself but I messed it up. The links should be emacs.stackexchange.com/a/36032/14825. And as it turns out, he has also written an answer to your other question (TLDR: it is not pretty and it does not really work): emacs.stackexchange.com/a/16811/14825 and check out the blog post that he links to. AFAIK, nothing has changed in that area in the past 6 years.
    – NickD
    Aug 1 at 15:19
  • @NickD: thank you for the clarification. I still find phils' post below is the one that best answers the question I asked.
    – kjo
    Aug 1 at 21:03

Have a look at the code for frame-set-background-mode and frame-terminal-default-bg-mode.

The latter "checks the ‘frame-background-mode’ variable, the X resource named "backgroundMode" (if FRAME is an X frame), and finally the ‘background-mode’ terminal parameter."

Customize the frame-background-mode user option to enforce a value.

Individual terminals/emulators are handled via the various files in the lisp/term/ directory; e.g.: /usr/local/share/emacs/27.2/lisp/term/ (but depending on how Emacs is installed); or else refer to https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/term

See the README file in that directory for details of how Emacs selects which of those files to load. Once you've established which file is in use, you would need to read the code to see how backgrounds are established, as functionality can vary between terminals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.