4

Having got a bunch of custom colors defined (thanks to Drew), I need to use them to customize the background color per host. I have named the colors after the hostname, eg

(setq pentacle "#314159")
(setq athame "#271828")

so then I tried (at the bottom of the .emacs file)

(set-background-color (nth 0 (split-string (system-name) "\\.")))

but on (for example) pentacle.mydomain.com I get the message in the minibuffer

Undefined color: "pentacle"

I assume I need one or more additional levels of indirection in expanding the system name and splitting off the first token from the FQDN, but I can't see how to do this so that it gets recognized as the variable I defined.

7

The path you have chosen introducing symbols to keep the colors requires you to convert the names you get by system-name to those symbols and evaluate them to get the color.

You could achieve this by

(set-background-color
 (symbol-value (intern (nth 0 (split-string (system-name) "\\.")))))

Although doable I think the above is rather unconventional.

A more conventional approach would keep the mapping of machine names to colors in a structure like an association list.

Your example could be implemented as follows.

(setq colors-for-machines
      '(("pentacle" . "#314159")
        ("athame" . "#271828")))

Retrieve the right value:

(set-background-color
 (cdr
  (assoc (nth 0 (split-string (system-name) "\\."))
         colors-for-machines)))
|improve this answer|||||
  • 2
    No need for eval; just call symbol-value. – Basil May 30 '18 at 10:19
  • 1
    Great answer. You not only showed how to do what the OP was trying to do; you showed the better and more typical way to get the job done. – Drew May 30 '18 at 13:42
  • Thanks for your comments and edits! In particular the change to symbol-value and syntax highlighting! – Marco Wahl May 31 '18 at 8:44

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.