Frage

I'm not quite down with the syntax for dynamic symbols etc. I guess I can probably do something with dolist and a list of colors here, but not sure what:

  (custom-set-faces
   `(term-color-black ((t (:inherit term-color-black :background ,(face-attribute 'term-color-black :foreground)))))
   `(term-color-red ((t (:inherit term-color-red :background ,(face-attribute 'term-color-red :foreground)))))
   `(term-color-green ((t (:inherit term-color-green :background ,(face-attribute 'term-color-green :foreground)))))
   `(term-color-yellow ((t (:inherit term-color-yellow :background ,(face-attribute 'term-color-yellow :foreground)))))
   `(term-color-blue ((t (:inherit term-color-blue :background ,(face-attribute 'term-color-blue :foreground)))))
   `(term-color-magenta ((t (:inherit term-color-magenta :background ,(face-attribute 'term-color-magenta :foreground)))))
   `(term-color-cyan ((t (:inherit term-color-cyan :background ,(face-attribute 'term-color-cyan :foreground)))))
   `(term-color-white ((t (:inherit term-color-white :background ,(face-attribute 'term-color-white :foreground))))))
War es hilfreich?

Lösung

This is not 100% identical, but in most cases will be equivalent:

(defmacro set-term-faces (names)
  `(custom-set-faces
    ,@(loop for color in names
         for sym = (intern (concat "term-color-" (symbol-name color)))
         collect (list 'quote
                       `(,sym ((t (:inherit ,sym
                                   :background
                                   ,(face-attribute sym :foreground)))))))))

(set-term-faces (black red green yellow blue magenta cyan white))

The discrepancy is at the point of when the evaluation of ,(face-attribute ...) happens. I.e. this macro doesn't produce the same source code you have, it already evaluates the expression after comma, so if your code was inside a macro, that would make a difference.

Andere Tipps

You can refactor that code, but you probably shouldn't. All (custom-set-...) code is generated automatically by Emacs' "easy customization" system. Thus if you refactor it, there is a good chance that

  1. you break compatibility with the customization system
  2. your refactored code will be overwritten by the original code next time you customize a face

However, if you find your .emacs file too cluttered, you can configure Emacs to write customization code to a separate file. See this answer to a related question, and also Emacs' documentation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top