Next: , Previous: , Up: Advanced customization   [Index]

7.13 DIY Use colored Org source blocks per language

DIY Make Org block colors more or less colorful.

In versions of the Modus themes before ‘4.4.0’ there was an option to change the coloration of Org source blocks so that certain languages would have a distinctly colored background. This was not flexible enough, because (i) we cannot cover all languages effectively and (ii) the user had no choice over the ‘language --> color’ mapping.

As such, the old user option is no more. Users can use the following to achieve what they want:

[ All this is done by setting the Org user option org-src-block-faces, so it is not related to the palette overrides mechanism provided by the Modus themes. ]

(defun my-modus-themes-org-block-faces (&rest _)
  (modus-themes-with-colors
    ;; The `org-src-block-faces' does not get re-applied in existing
    ;; Org buffers.  Do M-x org-mode-restart for changes to take
    ;; effect.
    (setq org-src-block-faces
          `(("emacs-lisp" modus-themes-nuanced-magenta)
            ("elisp" modus-themes-nuanced-magenta)
            ("clojure" modus-themes-nuanced-magenta)
            ("clojurescript" modus-themes-nuanced-magenta)
            ("c" modus-themes-nuanced-blue)
            ("c++" modus-themes-nuanced-blue)
            ("sh" modus-themes-nuanced-yellow)
            ("shell" modus-themes-nuanced-yellow)
            ("python" modus-themes-nuanced-yellow)
            ("ipython" modus-themes-nuanced-yellow)
            ("r" modus-themes-nuanced-yellow)
            ("html" modus-themes-nuanced-green)
            ("xml" modus-themes-nuanced-green)
            ("css" modus-themes-nuanced-red)
            ("scss" modus-themes-nuanced-red)
            ("yaml" modus-themes-nuanced-cyan)
            ("conf" modus-themes-nuanced-cyan)
            ("docker" modus-themes-nuanced-cyan)))))

(add-hook 'modus-themes-after-load-theme-hook #'my-modus-themes-org-block-faces)

DIY Use a hook at the post-load-theme phase.

Note that the org-src-block-faces accepts a named face, as shown above, as well as a list of face attributes. The latter approach is not good enough because it hardcodes values in such a way that an org-mode-restart is necessary. Whereas the indirection of the named face lets the theme change the values while Org buffers continue to show the right colors.

Still, if a user prefers to hardcode face attributes, here is the idea:

;; This is for the sake of completeness.  I DO NOT RECOMMEND THIS
;; method because it hardcodes values and thus requires
;; `org-mode-restart' every time you change a theme.
(defun my-modus-themes-org-block-faces (&rest _)
  (modus-themes-with-colors
    (setq org-src-block-faces
          `(("emacs-lisp" (:inherit org-block :background ,bg-magenta-nuanced))
            ("elisp" (:inherit org-block :background ,bg-magenta-nuanced))
            ("clojure" (:inherit org-block :background ,bg-magenta-nuanced))
            ("clojurescript" (:inherit org-block :background ,bg-magenta-nuanced))
            ("c" (:inherit org-block :background ,bg-blue-nuanced))
            ("c++" (:inherit org-block :background ,bg-blue-nuanced))
            ("sh" (:inherit org-block :background ,bg-yellow-nuanced))
            ("shell" (:inherit org-block :background ,bg-yellow-nuanced))
            ("python" (:inherit org-block :background ,bg-yellow-nuanced))
            ("ipython" (:inherit org-block :background ,bg-yellow-nuanced))
            ("r" (:inherit org-block :background ,bg-yellow-nuanced))
            ("html" (:inherit org-block :background ,bg-green-nuanced))
            ("xml" (:inherit org-block :background ,bg-green-nuanced))
            ("css" (:inherit org-block :background ,bg-red-nuanced))
            ("scss" (:inherit org-block :background ,bg-red-nuanced))
            ("yaml" (:inherit org-block :background ,bg-cyan-nuanced))
            ("conf" (:inherit org-block :background ,bg-cyan-nuanced))
            ("docker" (:inherit org-block :background ,bg-cyan-nuanced))))))

(add-hook 'modus-themes-after-load-theme-hook #'my-modus-themes-org-block-faces)

Next: DIY Measure color contrast, Previous: DIY Custom Org emphasis faces, Up: Advanced customization   [Index]