7

在Spacemacs中配置Fira Code并启用ligatures特性

 3 years ago
source link: https://xiaozhou.net/setup-spacemacs-with-fira-code-ligatures-2019-09-09.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在Spacemacs中配置Fira Code并启用ligatures特性

发表于 2019-09-09 | 分类于 技术控 | 2 Comments | 阅读次数: 1442
字数统计: 865 字 | 阅读时长 ≈ 4 分钟

Fira是Mozilla主推的字体系列。Fira Code是其中的一员,专为写程序而生。出来具有等宽等基本属性外,还加入了编程连字特性(ligatures)。编程连字特性,其实就是利用这个特性对编程中的常用符号进行优化,比如把输入的「!=」直接显示成「≠」或者把「>=」变成「≥ 」等等,以此来提高代码的可读性。

作为传说中的程序员,Fira Code也早已称为我的各大常用编辑器标配字体。Fira Code在其他编辑器和终端上配置较为简单,选中字体过后,启用Ligatures Feature即可。Spacemacs和Emacs的配置稍微复杂一些,这里分享一下Fira Code在Spacemacs下的配置。

获取并安装 Fira Code Symbol Font。对于Linux系统,可以直接把字体解压并放到 /usr/share/fonts 目录,并重载字体的cache,使其生效。

在Spacemacs中,使用<SPC> f e d 编辑 ~/.spacemacs配置文件。对于Emacs用户来说,可以编辑 ~/.emacs.d/init.el

在配置文件中,加入如下设置:

dotspacemacs-default-font '(("FuraCode Nerd Font Mono"
:size 16
:weight medium
:width normal
:powerline-scale 1.1)
("Fira Code Symbol"
:size 16
:weight normal
:width normal
:powerline-scale 1.1))

同样是编辑配置文件,在Spacemacs的配置项dotspacemacs-user-config中加入如下配置:

(defun dotspacemacs/user-config()
"Configuration function for user code."
;; Font Ligatures
(defun my-correct-symbol-bounds (pretty-alist)
"Prepend a TAB character to each symbol in this alist,
this way compose-region called by prettify-symbols-mode
will use the correct width of the symbols
instead of the width measured by char-width."
(mapcar (lambda (el)
(setcdr el (string ?\t (cdr el)))
el)
pretty-alist))
(defun my-ligature-list (ligatures codepoint-start)
"Create an alist of strings to replace with
codepoints starting from codepoint-start."
(let ((codepoints (-iterate '1+ codepoint-start (length ligatures))))
(-zip-pair ligatures codepoints)))
(setq my-fira-code-ligatures
(let* ((ligs '("www" "**" "***" "**/" "*>" "*/" "\\\\" "\\\\\\"
"{-" "[]" "::" ":::" ":=" "!!" "!=" "!==" "-}"
"--" "---" "-->" "->" "->>" "-<" "-<<" "-~"
"#{" "#[" "##" "###" "####" "#(" "#?" "#_" "#_("
".-" ".=" ".." "..<" "..." "?=" "??" ";;" "/*"
"/**" "/=" "/==" "/>" "//" "///" "&&" "||" "||="
"|=" "|>" "^=" "$>" "++" "+++" "+>" "=:=" "=="
"===" "==>" "=>" "=>>" "<=" "=<<" "=/=" ">-" ">="
">=>" ">>" ">>-" ">>=" ">>>" "<*" "<*>" "<|" "<|>"
"<$" "<$>" "<!--" "<-" "<--" "<->" "<+" "<+>" "<="
"<==" "<=>" "<=<" "<>" "<<" "<<-" "<<=" "<<<" "<~"
"<~~" "</" "</>" "~@" "~-" "~=" "~>" "~~" "~~>" "%%"
"x" ":" "+" "+" "*")))
(my-correct-symbol-bounds (my-ligature-list ligs #Xe100))))
(defun my-set-fira-code-ligatures ()
"Add fira code ligatures for use with prettify-symbols-mode."
(setq prettify-symbols-alist
(append my-fira-code-ligatures prettify-symbols-alist))
(prettify-symbols-mode))
(add-hook 'prog-mode-hook 'my-set-fira-code-ligatures))

最后,重载Spacemacs/Emacs的配置文件,使配置生效,即可启用Fira Code字体的Ligatures连字特性。

支持原创技术分享,据说打赏我的人,都找到了女朋友!

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK