Refactor theme init across GUI, tty and daemon Emacs

This commit is contained in:
Henrik Lissner 2018-01-30 21:19:53 -05:00
parent 834e92c465
commit 23f914916e
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -434,44 +434,47 @@ character that looks like a space that `whitespace-mode' won't affect.")
(defun doom|init-theme (&optional frame) (defun doom|init-theme (&optional frame)
"Set the theme and load the font, in that order." "Set the theme and load the font, in that order."
(when doom-theme (with-selected-frame (or frame (selected-frame))
(load-theme doom-theme t)) (when doom-theme
(condition-case-unless-debug ex (load-theme doom-theme t))
(when (display-graphic-p) (condition-case-unless-debug ex
(when (fontp doom-font) (when (display-graphic-p)
(set-frame-font doom-font nil (if frame (list frame) t)) (when (fontp doom-font)
(set-face-attribute 'fixed-pitch frame :font doom-font)) (set-frame-font doom-font nil (if frame (list frame) t))
;; Fallback to `doom-unicode-font' for Unicode characters (set-face-attribute 'fixed-pitch frame :font doom-font))
(when (fontp doom-unicode-font) ;; Fallback to `doom-unicode-font' for Unicode characters
(set-fontset-font t 'unicode doom-unicode-font frame)) (when (fontp doom-unicode-font)
;; ...and for variable-pitch-mode: (set-fontset-font t 'unicode doom-unicode-font frame))
(when (fontp doom-variable-pitch-font) ;; ...and for variable-pitch-mode:
(set-face-attribute 'variable-pitch frame :font doom-variable-pitch-font))) (when (fontp doom-variable-pitch-font)
('error (set-face-attribute 'variable-pitch frame :font doom-variable-pitch-font)))
(if (string-prefix-p "Font not available: " (error-message-string ex)) ('error
(lwarn 'doom-ui :warning (if (string-prefix-p "Font not available: " (error-message-string ex))
"Could not find the '%s' font on your system, falling back to system font" (lwarn 'doom-ui :warning
(font-get (caddr ex) :family)) "Could not find the '%s' font on your system, falling back to system font"
(lwarn 'doom-ui :error (font-get (caddr ex) :family))
"Unexpected error while initializing fonts: %s" (lwarn 'doom-ui :error
(error-message-string ex))))) "Unexpected error while initializing fonts: %s"
(run-hooks 'doom-init-theme-hook)) (error-message-string ex)))))
(run-hooks 'doom-init-theme-hook)
(when frame
(remove-hook 'after-make-frame-functions #'doom|init-theme))))
;; Getting themes to remain consistent across GUI Emacs, terminal Emacs and ;; Getting themes to remain consistent across GUI Emacs, terminal Emacs and
;; daemon Emacs is hairy. ;; daemon Emacs is hairy. Running `doom|init-theme' sorts out the initial GUI
;; frame.
;; ;;
;; + Running `doom|init-theme' directly sorts out the initial GUI frame. ;; `doom|init-theme-in-frame' sorts out daemon and emacsclient frames by
;; + Attaching it to `after-make-frame-functions' sorts out daemon Emacs. ;; reloading the theme in those frame. However, if you open simultaneous
;; + Waiting for 0.1s in `doom|reload-ui-in-daemon' fixes daemon Emacs started ;; terminal and gui frames with emacsclient, you will get issues! There's always
;; with `server-start' in an interactive session of Emacs AND in tty Emacs. ;; `doom//reload-theme' if you need it.
(add-hook 'doom-init-ui-hook #'doom|init-theme)
(defun doom|reload-ui-in-daemon (frame) (defun doom|reload-ui-in-daemon (frame)
"Reload the theme (and font) in an daemon frame." "Reloads the theme in new daemon or tty frames."
(when (or (daemonp) (not (display-graphic-p))) (when (or (daemonp) (not (display-graphic-p)))
(with-selected-frame frame (doom|init-theme frame)))
(run-with-timer 0.1 nil #'doom|init-ui))))
(add-hook! 'after-make-frame-functions #'(doom|init-theme doom|reload-ui-in-daemon)) (add-hook 'doom-init-ui-hook #'doom|init-theme)
(add-hook 'after-make-frame-functions #'doom|reload-ui-in-daemon)
;; ;;