reddit / emacs

`set-fontset-font` vs. `set-frame-font`, what's the difference?

zu0107 · 2022-10-16 13:19
I am a novice emacs user. In an attempt to set the font in my `init.el` file, I came across two different functions: `set-fontset-font` and `set-frame-font`. `set-frame-font` sets the font of current frame correctly, whereas `set-fontset-font` doesn't do anything. To my understanding, invoking `(set-fontset-font t ...)` is basically modifying the default font set, making new frames to use the font I specified, so this behavior is surprising to me. I frequently fiddle with code that contains non-alphanumeric characters (Hangul, Hiragana/Katakana, ~~Emoji~~...), and I use different fonts for each of them. It would be great if I can use `set-fontset-font` as that function has this feature built-in. Is there a way to make it a *one-function-to-rule-them-all*? P.S. I tried modifying `default-frame-alist` with `(font . "fontset-default")` but apparently that is not allowed? Now my `init.el` is broken!
casouri · 2022-10-17 03:47
Alan has an article explaining set-font set-font in detail: http://idiocy.org/emacs-fonts-and-fontsets.html I have an article on exploiting the full benefit of fontsets: https://archive.casouri.cat/note/2021/fontset/index.html
zu0107 · 2022-10-17 06:03
Thanks a ton for the article! Super helpful. One clarification though... As per the original post, I tried modifying the default fontset using `(set-fontset-font t ...)`, but somehow changes are not reflected to the frames created on startup. It is located in a theme file I created, which in turn is loaded by `init.el`. In Alan's blog, I see: > You can also use `prepend` [as the last argument of `set-fontset-font`, which puts the font first in the list, although after the default set with `set-face-attribute`. Does that mean that essentially I need to have at least one `set-face-attribute` to set the startup font?
casouri · 2022-10-17 06:25
\`(set-fontset-font t ...)\` should just work. What exactly do you have in \`...\`? Does evaluating \`(set-fontset-font t ...)\` (as opposed to loading it from a file) do anything?
zu0107 · 2022-10-17 09:12
This is what I have currently: (set-fontset-font t 'unicode (font-spec :name "semteulche") nil 'prepend) (set-fontset-font t 'hangul (font-spec :name "Noto Sans Mono CJK KR") nil 'append) (set-fontset-font t 'kana (font-spec :name "Noto Sans Mono CJK JP") nil 'append) (set-fontset-font t 'han (font-spec :name "Noto Sans Mono CJK CN") nil 'append) (set-fontset-font t (if (version< emacs-version "28.1") '(#x1f300 . #x1fad0) 'emoji) "Noto Color Emoji" nil 'append) And evaluating this from a `*scratch*` buffer does nothing much. I do see new entries in the `describe-fontset` output, though.
casouri · 2022-10-18 00:36
Why do you use \`prepend\` for the first form? I'll try removing the \`prepend\` and \`append\` and see if anything changes.
zu0107 · 2022-10-18 01:26
Well, `semteulche` is still under development, so some glyphs are missing. I wanted Noto Sans as fallback. I'll try removing `prepend` once I get back home!
alanthird · 2022-10-19 10:05
I'm not sure if this is what you're asking, but ASCII glyphs aren't affected by `set-fontset-font`, so you need to use something like `set-face-attribute` to set the default font for them. If you're already setting the default font by some other method, then you don't need to use `set-face-attribute`.
alanthird · 2022-10-19 10:14
I find the whole prepend/append thing somewhat confusing. In my experience what you want to do is set your first fallback font for each character set/language/whatever without using either, then subsequent fallback fonts should be appended. The only reason to use prepend is if you already have one or more fallback fonts set for the character set but you want to add another, and have it take precedence without removing the existing configuration.
zu0107 · 2022-10-19 10:34
Oh, that I didn't know about. Does that mean that I cannot change fonts for regular English alphabets by fiddling with the default font set? Kinda seems like a weird thing to hard-code, I gotta say...
alanthird · 2022-10-19 10:47
Yes.
zu0107 · 2022-10-19 11:21
Well, that explains this odd behavior. Thanks a ton!!