Skip to content

Commit

Permalink
feat(core)!: change font handling
Browse files Browse the repository at this point in the history
This commit changes the way MinEmacs load fonts. Users need to update their
personal config.el files to adapt to the new way.
  • Loading branch information
abougouffa committed Oct 26, 2023
1 parent 817db39 commit 7c4a8b8
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 70 deletions.
167 changes: 167 additions & 0 deletions core/me-fonts.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
;; me-fonts.el --- MinEmacs fonts -*- lexical-binding: t; -*-

;; Copyright (C) 2022-2023 Abdelhak Bougouffa

;; Author: Abdelhak Bougouffa (concat "abougouffa" "@" "fedora" "project" "." "org")

;;; Commentary:

;;; Code:


(defcustom minemacs-fonts-plist
'(:default
((:family "Iosevka" :height 130)
(:family "Cascadia Code" :height 130)
(:family "Fira Code" :height 130)
(:family "Jetbrains Mono" :height 110)
(:family "Hack" :height 130)
(:family "Roboto Mono" :height 120)
(:family "SF Mono" :height 130)
(:family "Source Code Pro" :height 130)
(:family "Menlo" :height 130)
(:family "Monaco" :height 130)
(:family "DejaVu Sans Mono" :height 130)
(:family "Consolas" :height 130))
:symbol
((:family "Segoe UI Symbol" :prepend t)
(:family "Symbola" :prepend t)
(:family "Symbol" :prepend t))
:emoji
((:family "Noto Color Emoji" :prepend t)
(:family "Apple Color Emoji" :prepent t)
(:family "Segoe UI Emoji" :prepend t)
(:family "Quivira" :prepend t))
;; Arabic script
:arabic
("KacstOne"
"Amiri Typewriter"
"Scheherazade"
"Koodak"
(:family "Amiri" :scale 0.9))
;; Chinese script
:han
((:family "LXGW Neo Xihei" :scale 1.3)
(:family "WenQuanYi Micro Hei Mono" :scale 1.3)
(:family "LXGW WenKai Screen" :scale 1.3)
(:family "LXGW WenKai Mono" :scale 1.3)
(:family "PingFang SC" :scale 1.3)
(:family "Microsoft Yahei UI" :scale 1.3)
(:family "Simhei" :scale 1.3)))
"Default fonts of MinEmacs."
:group 'minemacs-ui
:type '(choice
(list string)
(plist
(:family string)
(:scale number)
(:height number))))

;; Setup default fonts (depending on the OS)
(let ((mono-font (cond (os/linux "monospace")
(os/win "Lucida Console")
(os/mac "monospace")))
(varp-font (cond (os/linux "monospace")
(os/win "Tahoma")
(os/mac "monospace"))))
(defconst minemacs-default-fonts
`(:font-family ,mono-font
:font-size 13
:unicode-font-family nil
:variable-pitch-font-family ,varp-font
:variable-pitch-font-size 13)
"Default fonts of MinEmacs."))

(make-obsolete-variable 'minemacs-default-fonts nil "v3.0.0")

(defcustom minemacs-fonts nil
"Fonts to use within MinEmacs."
:group 'minemacs-ui
:type '(plist
(:font-family string)
(:font-size natnum)
(:unicode-font-family string)
(:variable-pitch-font-family string)
(:variable-pitch-font-size natnum)))

(make-obsolete-variable 'minemacs-fonts 'minemacs-fonts-plist "v3.0.0")

(defun +font--scale (spec)
(when (plistp spec) (plist-get spec :scale)))

(defun +font--family (spec)
(if (stringp spec) spec (plist-get spec :family)))

(defun +font--height (spec)
(when (plistp spec) (plist-get spec :height)))

(defun +font--prepend (spec)
(when (plistp spec) (plist-get spec :prepend)))

(defun +font-installed-p (font-family)
"Check if FONT-FAMILY is installed on the system."
(and (member font-family (font-family-list)) t))

(defun +apply-font-script (script-or-face)
"Set font for SCRIPT-OR-FACE from `minemacs-fonts-plist'."
(catch 'done
(dolist (font (plist-get minemacs-fonts-plist (intern (format ":%s" script-or-face))))
(let* ((family (+font--family font))
(scale (+font--scale font))
(height (+font--height font))
(prepend (+font--prepend font))
(spec (append `(:family ,family) (when height `(:height ,height)))))
(when (+font-installed-p family)
(if (not (memq script-or-face (mapcar #'car script-representative-chars)))
(apply #'set-face-attribute (append `(,script-or-face nil) spec))
(set-fontset-font t script-or-face (apply #'font-spec spec) nil prepend))
(when scale (add-to-list 'face-font-rescale-alist (cons family scale)))
(+log! "Font for `%s' set to \"%s\"%s" script-or-face family
(format "%s%s%s"
(if height (format " :height %s" height) "")
(if scale (format " :scale %s" scale) "")
(if prepend (format " :prepend %s" prepend) "")))
(throw 'done family))))))

;; Inspired by: github.com/seagle0128/.emacs.d/blob/master/custom-example.el
;;;###autoload
(defun +setup-fonts ()
"Setup fonts."
(interactive)
(mapc #'+apply-font-script
(reverse
(mapcar (lambda (k) (intern (substring (symbol-name k) 1)))
(+plist-keys minemacs-fonts-plist))))
;; Run hooks
(run-hooks 'minemacs-after-setup-fonts-hook))

(defun +set-fonts ()
"Set Emacs' fonts from `minemacs-fonts'."
(interactive)
(custom-set-faces
`(default
((t (:font ,(format "%s %d"
(or (plist-get minemacs-fonts :font-family)
(plist-get minemacs-default-fonts :font-family))
(or (plist-get minemacs-fonts :font-size)
(plist-get minemacs-default-fonts :font-size)))))))
`(fixed-pitch
((t (:inherit (default)))))
`(fixed-pitch-serif
((t (:inherit (default)))))
`(variable-pitch
((t (:font ,(format "%s %d"
(or (plist-get minemacs-fonts :variable-pitch-font-family)
(plist-get minemacs-default-fonts :variable-pitch-font-family))
(or (plist-get minemacs-fonts :variable-pitch-font-size)
(plist-get minemacs-default-fonts :variable-pitch-font-size))))))))
;; Run hooks
(run-hooks 'minemacs-after-set-fonts-hook))

(make-obsolete #'+set-fonts #'+setup-fonts "v3.0.0")

(add-hook 'window-setup-hook #'+setup-fonts)
(add-hook 'server-after-make-frame-hook #'+setup-fonts)


(provide 'me-fonts)
13 changes: 7 additions & 6 deletions core/me-loaddefs.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ Examples:

;;; Generated autoloads from ../elisp/+emacs.el

(autoload '+font-installed-p "../elisp/+emacs" "\
Check if FONT-FAMILY is installed on the system.
(fn FONT-FAMILY)")
(autoload '+dir-locals-reload-for-this-buffer "../elisp/+emacs" "\
Reload directory-local for the current buffer" t)
(autoload '+dir-locals-reload-for-all-buffers-in-this-directory "../elisp/+emacs" "\
Expand Down Expand Up @@ -356,8 +352,6 @@ Suppress new messages temporarily in the echo area and the `*Messages*' buffer w
Convert BODY to an interactive command.
(fn &rest BODY)" nil t)
(autoload '+set-fonts "../elisp/+minemacs" "\
Set Emacs' fonts from `minemacs-fonts'." t)
(autoload '+load-theme "../elisp/+minemacs" "\
Load Emacs' theme from `minemacs-theme'." t)
(autoload '+eval-when-idle "../elisp/+minemacs" "\
Expand Down Expand Up @@ -758,6 +752,13 @@ Fallback to FALLBACK-RELEASE when it can't get the last one.

(register-definition-prefixes "../modules/extras/me-elisp-extras" '("+elisp-" "+emacs-lisp--"))


;;; Generated autoloads from me-fonts.el

(autoload '+setup-fonts "me-fonts" "\
Setup fonts." t)
(register-definition-prefixes "me-fonts" '("+apply-font-script" "+font-" "+set-fonts" "minemacs-fonts"))


;;; Generated autoloads from ../modules/extras/me-gdb.el

Expand Down
31 changes: 4 additions & 27 deletions core/me-vars.el
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ It return a symbol like `x86_64', `aarch64', `armhf', ...")
"List of symbols representing Emacs' enabled features.
Compiled from the `system-configuration-features'.")

(defcustom minemacs-fonts nil
"Fonts to use within MinEmacs."
:group 'minemacs-ui
:type '(plist
(:font-family string)
(:font-size natnum)
(:unicode-font-family string)
(:variable-pitch-font-family string)
(:variable-pitch-font-size natnum)))

(defcustom minemacs-leader-key "SPC"
"MinEmacs leader key."
:group 'minemacs-keybinding
Expand Down Expand Up @@ -171,11 +161,13 @@ we set on `use-package' in `me-bootstrap'."
:group 'minemacs-core
:type 'hook)

(defcustom minemacs-after-set-fonts-hook nil
"Runs after setting MinEmacs fonts, runs at the end of `+set-fonts'."
(defcustom minemacs-after-setup-fonts-hook nil
"Runs after setting MinEmacs fonts, runs at the end of `+setup-fonts'."
:group 'minemacs-ui
:type 'hook)

(make-obsolete-variable 'minemacs-after-set-fonts-hook 'minemacs-after-setup-fonts-hook "v3.0.0")

(defcustom minemacs-after-load-theme-hook nil
"Runs after loading MinEmacs theme, runs at the end of `+load-theme'."
:group 'minemacs-ui
Expand Down Expand Up @@ -203,21 +195,6 @@ MinEmacs hooks will be run in this order:
"Special hook for build functions that are run after completing package updates.")
(defvaralias 'minemacs-build-functions-hook 'minemacs-build-functions)

;; Setup default fonts (depending on the OS)
(let ((mono-font (cond (os/linux "monospace")
(os/win "Lucida Console")
(os/mac "monospace")))
(varp-font (cond (os/linux "monospace")
(os/win "Tahoma")
(os/mac "monospace"))))
(defconst minemacs-default-fonts
`(:font-family ,mono-font
:font-size 13
:unicode-font-family nil
:variable-pitch-font-family ,varp-font
:variable-pitch-font-size 13)
"Default fonts of MinEmacs."))

(defcustom +env-file (concat minemacs-local-dir "system-env.el")
"The file in which the environment variables will be saved."
:group 'minemacs-core
Expand Down
5 changes: 0 additions & 5 deletions elisp/+emacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

;;; Code:

;;;###autoload
(defun +font-installed-p (font-family)
"Check if FONT-FAMILY is installed on the system."
(and (member font-family (font-family-list)) t))

;;;###autoload
(defun +dir-locals-reload-for-this-buffer ()
"Reload directory-local for the current buffer"
Expand Down
24 changes: 0 additions & 24 deletions elisp/+minemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,6 @@ If NO-MESSAGE-LOG is non-nil, do not print any message to *Messages* buffer."
(interactive)
,@body))

;;;###autoload
(defun +set-fonts ()
"Set Emacs' fonts from `minemacs-fonts'."
(interactive)
(custom-set-faces
`(default
((t (:font ,(format "%s %d"
(or (plist-get minemacs-fonts :font-family)
(plist-get minemacs-default-fonts :font-family))
(or (plist-get minemacs-fonts :font-size)
(plist-get minemacs-default-fonts :font-size)))))))
`(fixed-pitch
((t (:inherit (default)))))
`(fixed-pitch-serif
((t (:inherit (default)))))
`(variable-pitch
((t (:font ,(format "%s %d"
(or (plist-get minemacs-fonts :variable-pitch-font-family)
(plist-get minemacs-default-fonts :variable-pitch-font-family))
(or (plist-get minemacs-fonts :variable-pitch-font-size)
(plist-get minemacs-default-fonts :variable-pitch-font-size))))))))
;; Run hooks
(run-hooks 'minemacs-after-set-fonts-hook))

;;;###autoload
(defun +load-theme ()
"Load Emacs' theme from `minemacs-theme'."
Expand Down
9 changes: 1 addition & 8 deletions init.el
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@
;; the fonts, load the theme or play with the scratch buffer.
(unless (featurep 'me-org-export-async-init)
(+log! "Applying `minemacs-fonts'.")
;; Load fonts, values are read from `minemacs-fonts' if set in config.el,
;; otherwise, they are read from the default `minemacs-default-fonts'.
(+set-fonts)

;; Initially MinEmacs loads the `doom-one-light' theme, and when
;; `minemacs-theme' is set in user configuration, it is loaded here.
(+load-theme)
Expand Down Expand Up @@ -210,9 +206,6 @@
(when (file-exists-p user-conf-modules)
(+load user-conf-modules)))))

;; Load fonts early (they are read from the default `minemacs-default-fonts').
(+set-fonts)

;; NOTE: Ensure the `me-gc' module is in the core modules list. This module
;; enables the `gcmh-mode' package (a.k.a. the Garbage Collector Magic Hack).
;; This GCMH minimizes GC interference with the activity by using a high GC
Expand All @@ -235,7 +228,7 @@
(when (memq 'me-splash minemacs-core-modules) '(me-splash))
'(me-bootstrap)
(when (< emacs-major-version 29) '(me-compat))
'(me-builtin me-gc)
'(me-builtin me-gc me-fonts)
minemacs-core-modules)))

;; Load MinEmacs modules
Expand Down

0 comments on commit 7c4a8b8

Please sign in to comment.