Skip to content

Instantly share code, notes, and snippets.

@hnOsmium0001
Created December 8, 2023 06:40
Show Gist options
  • Save hnOsmium0001/08608deb8a96d4b7e7b6de99120ffca1 to your computer and use it in GitHub Desktop.
Save hnOsmium0001/08608deb8a96d4b7e7b6de99120ffca1 to your computer and use it in GitHub Desktop.
Doom Emacs org-mode CJK separate font

这份配置其实并不怎样。它工作原理是这样的:调用 (buffer-face-mode) 以替换当前buffer的默认face,而org-mode所用的各种face都会从默认face继承font-family。我也不太记得为什么要新建一个fontset而不是直接写 (font-spec :family "Blah") 以得到制定好中文字体的face了,大概和不用fontset没法正确设置ASCII范围字符有关吧。

说实话如果不管你用不用Doom Emacs,都应该直接用 (custom-theme-set-faces) 来直接操作 org-default 这个face,总会比这个方案靠谱一些……

(after! org
;; org-mode下的中文字体(用汉字、拉丁字母等宽的更纱黑体)
(pcase system-type
('gnu/linux (setq should-org-use-separate-font t
org-separate-font-family "Sarasa Mono SC"))
(_ (setq should-org-use-separate-font nil
org-separate-font-family nil)))
(when should-org-use-separate-font
;; 创建专门的fontset,只包含Sarasa Gothic字体
;; NOTE: 这里下面是可以直接设置字体的,像是 "-*-*-*-*-*--*-*-*-*-*-*-fontset-monospacecjkv, han:Sarasa Mono SC 18" 这样
;; 不过这么写的字号解析好像和 `font-spec' 不太一样,18号XLFD要比18号 `font-spec' 要大很多
(defconst fontset-monospace-cjkv (create-fontset-from-fontset-spec "-*-*-*-*-*--*-*-*-*-*-*-fontset-monospacecjkv"))
;; 创建专门的继承自以上fontset的face
(make-face 'org-default-cjkv)
(setq fontset-monospace-cjkv-faces '(org-default-cjkv))
;; 配置之前创建的 fontset 和 face
(defun hnosm/config-fontset-monospace-cjkv (font-size)
(interactive "nFont size: ")
;; Set fontset font spec
(dolist (charset '(latin kana han cjk-misc bopomofo))
(set-fontset-font fontset-monospace-cjkv
charset
(font-spec :family org-separate-font-family :size font-size)))
;; Set face to use `fontset-monospace-cjkv', we need to do this every time because for some reason, Emacs
;; "caches" ASCII characters' font information and calling `set-fontset-font' only affects non-ASCII chars
(dolist (face fontset-monospace-cjkv-faces)
(set-face-attribute face nil
;; NOTE: 不知道为什么,`:font'和`:fontset'都必须写才能正常工作
;; 前者控制ASCII字符的字体(也就相当于直接设置face的`:family'),后者控制其他汉字等字符的字体
;; NOTE: `:fontset'好像是个undocumented parameter,不过能用我也管不了那么多了
:font fontset-monospace-cjkv
:fontset fontset-monospace-cjkv)))
;; Default value
;; TODO make this detect display DPI and scale automatically
(let ((font-size (pcase (system-name)
;; Insert your own device-specific logic here
(_ 20))))
(hnosm/config-fontset-monospace-cjkv font-size)))
(defun hnosm/org-mode-hook ()
;; Less distractions
(display-line-numbers-mode -1)
;; Disable word autocompletion - nuclear solution, too lazy to specifically remove the correct backends
(company-mode -1)
(+org/buffer-name-to-title)
(+org/run-startblock)
(when should-org-use-separate-font
(setq buffer-face-mode-face 'org-default-cjkv)
(buffer-face-mode)))
(add-hook 'org-mode-hook 'hnosm/org-mode-hook))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment