I switch between different themes for my daily Emacs usage (via package
auto-dark-mode).  After a theme switch, I adjust a few faces to use
current theme colour palette.  However, I am not sure what to do with
colours defined in `org-src-block-faces'.

What I am using is the following (simplified version) in
`modus-themes-after-load-theme-hook':

--8<---------------cut here---------------start------------->8---
(modus-themes-with-colors
  (setopt org-src-block-faces
          `(("emacs-lisp" (:background ,bg-magenta-nuanced)))))
--8<---------------cut here---------------end--------------->8---

However such a setting is not applied to any of currently open
`org-mode' buffers.  You should be able to see what I am seeing I
observe with the following reproducer [1]:

  emacs -Q -l load-theme-org-src-block-faces.el

Attachment: load-theme-org-src-block-faces.el
Description: reproducer


[1] There's a timing aspect involved.  I assumed that 5 second is enough
for Emacs to apply colors in the sample buffer.

So far I came up with the following solution, running as a part of
aforementioned `modus-themes-after-load-theme-hook':

--8<---------------cut here---------------start------------->8---
(dolist (buffer (buffer-list))
  (with-current-buffer buffer
    (when (derived-mode-p '(org-mode))
      ;; prefer for theme to switch quickly, heavy lifting in background
      (run-with-idle-timer
       1 nil 
       (lambda ()
         (with-current-buffer buffer
           (save-excursion)
           (let* ((start (point-min))
                  (end (point-max))
                  (last start))
             (goto-char start)
             (while (progn
                      (org-fontify-meta-lines-and-blocks end)
                      (when-let* ((pt (point))
                                  ((/= pt last)))
                        (setq last pt))))
             (goto-char start)
             (org-fontify-inline-src-blocks end))))))))
--8<---------------cut here---------------end--------------->8---

While the snipped "does the job", I am not sure if this is optimal
solution nor that I am not reinventing a wheel here.  Is there some
known method how to update `org-src-block-faces' when theme changes and
have it applied in open `org-mode' buffers?

Cheers,
PK

Reply via email to