From 1b5b3cdd52131657013ab42c06b7e2bb113ff132 Mon Sep 17 00:00:00 2001 From: mofaph Date: Mon, 28 May 2012 16:58:04 +0800 Subject: [PATCH] =?UTF-8?q?conf-font.el:=20=E4=B8=BA=20Emacs=20=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=8D=E5=90=8C=E7=9A=84=E5=AD=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf-font.el | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ init.el | 1 + 2 files changed, 65 insertions(+) create mode 100644 conf-font.el diff --git a/conf-font.el b/conf-font.el new file mode 100644 index 0000000..4e2ceaf --- /dev/null +++ b/conf-font.el @@ -0,0 +1,64 @@ +;;; Token from http://www.emacser.com + +;; 首先,判断某个字体在系统中是否安装 +(defun qiang-font-existsp (font) + (if (null (x-list-fonts font)) + nil t)) + +;; 其次,要按顺序找到一个字体列表中第一个已经安装可用的字体 +(defvar font-list '("Microsoft Yahei" + "文泉驿等宽微米黑" + "黑体" + "新宋体" + "宋体")) + +;; find-if is in common list package +(eval-when-compile (require 'cl)) +(if window-system (find-if #'qiang-font-existsp font-list)) + +;; 还要有个辅助函数,用来生产带上 font size 信息的 font 描述文本 +(defun qiang-make-font-string (font-name font-size) + (if (and (stringp font-size) + (equal ":" (string (elt font-size 0)))) + (format "%s%s" font-name font-size) + (format "%s %s" font-name font-size))) + +;; 有了这些函数,下面出场的就是自动设置字体函数了 +(defun qiang-set-font (english-fonts + english-font-size + chinese-fonts + &optional chinese-font-size) + "english-font-size could be set to \":pixelsize=18\" or a integer. +If set/leave chinese-font-size to nil, it will follow english-font-size" + (require 'cl) ; for find if + (let ((en-font (qiang-make-font-string + (find-if #'qiang-font-existsp english-fonts) + english-font-size)) + (zh-font (font-spec :family (find-if #'qiang-font-existsp chinese-fonts) + :size chinese-font-size))) + + ;; Set the default English font + ;; + ;; The following 2 method cannot make the font settig work in new frames. + ;; (set-default-font "Consolas:pixelsize=18") + ;; (add-to-list 'default-frame-alist '(font . "Consolas:pixelsize=18")) + ;; We have to use set-face-attribute + (message "Set English Font to %s" en-font) + (set-face-attribute + 'default nil :font en-font) + + ;; Set Chinese font + ;; Do not use 'unicode charset, it will cause the english font setting invalid + (message "Set Chinese Font to %s" zh-font) + (dolist (charset '(kana han symbol cjk-misc bopomofo)) + (set-fontset-font (frame-parameter nil 'font) + charset + zh-font)))) + +;; 利用这个函数,Emacs 字体设置就是小菜一碟了 +(when (eq window-system 'x) + (qiang-set-font + '("Consolas" "Monaco" "DejaVu Sans Mono" "Monospace" "Courier New") ":pixelsize=14" + '("Microsoft Yahei" "文泉驿等宽微米黑" "黑体" "新宋体" "宋体"))) + +(provide 'conf-font) diff --git a/init.el b/init.el index 221c315..f64c640 100644 --- a/init.el +++ b/init.el @@ -18,6 +18,7 @@ (require 'conf-hl-line) (require 'conf-winner) (require 'conf-key-binding) +(require 'conf-font) ;; using el-get to manage all other third party packages (require 'conf-el-get)