Skip to content

Instantly share code, notes, and snippets.

@coldnew
Last active November 11, 2020 13:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coldnew/7398835 to your computer and use it in GitHub Desktop.
Save coldnew/7398835 to your computer and use it in GitHub Desktop.
org-mode template for my chinese and english font setting
* 字體設置 :font:
** 英文字體與中文字體設置
- 英文字體
#+begin_src emacs-lisp
(defvar emacs-english-font "Monaco"
"The font name of English.")
#+end_src
- 中文字體
#+begin_src emacs-lisp
(defvar emacs-cjk-font "Hiragino Sans GB W3"
"The font name for CJK.")
#+End_src
** 預設字體大小設定
為了可以在 org-mode 的表格中縮放字體也不會有中英文排版亂掉的情況,我的
字體設定是以 pair 的形式來進行設定,*emacs-font-size-pair* 存放預設的
字體,若有使用我自訂的字體放大/縮小命令,則這個變數也會更著被修改。
#+BEGIN_SRC emacs-lisp
(defvar emacs-font-size-pair '(15 . 18)
"Default font size pair for (english . chinese)")
#+END_SRC
** 在圖形介面下使用我所設定的字體
這邊主要定義 *set-font* 這個函式,他會根據傳送給他的字體資訊,即時設定
整個 emacs 的字體大小,這樣也不會有切換 buffer,但是字體確沒有跟著之前的
設定而變大/變小。
#+BEGIN_SRC emacs-lisp
(defun font-exist-p (fontname)
"test if this font is exist or not."
(if (or (not fontname) (string= fontname ""))
nil
(if (not (x-list-fonts fontname))
nil t)))
(defun set-font (english chinese size-pair)
"Setup emacs English and Chinese font on x window-system."
(if (font-exist-p english)
(set-frame-font (format "%s:pixelsize=%d" english (car size-pair)) t))
(if (font-exist-p chinese)
(dolist (charset '(kana han symbol cjk-misc bopomofo))
(set-fontset-font (frame-parameter nil 'font) charset
(font-spec :family chinese :size (cdr size-pair))))))
;; Setup font size based on emacs-font-size-pair
(set-font emacs-english-font emacs-cjk-font emacs-font-size-pair)
#+END_SRC
** 使用 C-= 或是 C-- 來調整字體大小
這邊設定了一個 *emacs-font-size-pair-list* 變數,這個變數存放了即使使
用這一對字體大小,org-mode 的表格也不會走樣的中英字體設定。
#+BEGIN_SRC emacs-lisp
(defvar emacs-font-size-pair-list
'(( 5 . 6) (10 . 12)
(13 . 16) (15 . 18) (17 . 20)
(19 . 22) (20 . 24) (21 . 26)
(24 . 28) (26 . 32) (28 . 34)
(30 . 36) (34 . 40) (36 . 44))
"This list is used to store matching (englis . chinese) font-size.")
(defun emacs-step-font-size (step)
"Increase/Decrease emacs's font size."
(let ((scale-steps emacs-font-size-pair-list))
(if (< step 0) (setq scale-steps (reverse scale-steps)))
(setq emacs-font-size-pair
(or (cadr (member emacs-font-size-pair scale-steps))
emacs-font-size-pair))
(when emacs-font-size-pair
(message "emacs font size set to %.1f" (car emacs-font-size-pair))
(set-font emacs-english-font emacs-cjk-font emacs-font-size-pair))))
(defun increase-emacs-font-size ()
"Decrease emacs's font-size acording emacs-font-size-pair-list."
(interactive) (emacs-step-font-size 1))
(defun decrease-emacs-font-size ()
"Increase emacs's font-size acording emacs-font-size-pair-list."
(interactive) (emacs-step-font-size -1))
(global-set-key (kbd "C-=") 'increase-emacs-font-size)
(global-set-key (kbd "C--") 'decrease-emacs-font-size)
#+END_SRC
** 設定顯示字體時的格式
使用 *list-face-display* 可以看到所有的 face 顏色與字體。
#+begin_src emacs-lisp
(setq list-faces-sample-text
(concat
"ABCDEFTHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz\n"
"11223344556677889900 壹貳參肆伍陸柒捌玖零"))
#+end_src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment