jrblevin / markdown-mode

Emacs Markdown Mode
http://jblevins.org/projects/markdown-mode/
GNU General Public License v3.0
875 stars 160 forks source link

中文用户的字体对齐问题 (the non-fixed-width font problem in chinese environment) #795

Closed alphatan closed 10 months ago

alphatan commented 10 months ago

问题描述 (Description)

“代码”、“缩进”、“预设”部分的字体会引起对齐问题。 (the font in the sections of code, quote, pre would perform as non-fixed-width)

non-fixed-width-char

原因 (Cause)

emacs 声明的等宽(fixed-pitch)字体并不是等宽的。 (The font symbol fixed-pitch declared as fixed-width is not in practice)

而 markdown-mode.el 中的 mardown-code-face 就引用了这个字体。 (And it's referenced by markdown-code-face in markdown-mode.el)

解决 (Solution)

diff --git a/markdown-mode.el b/markdown-mode.el
index 4bb1a57..bfcdb87 100644
--- a/markdown-mode.el
+++ b/markdown-mode.el
@@ -1902,19 +1902,19 @@ See `markdown-hide-markup' for additional details."
   :group 'markdown-faces)

 (defface markdown-code-face
-  '((t (:inherit fixed-pitch)))
+  '((t (:inherit default)))
   "Face for inline code, pre blocks, and fenced code blocks.
 This may be used, for example, to add a contrasting background to
 inline code fragments and code blocks."
   :group 'markdown-faces)

 (defface markdown-inline-code-face
-  '((t (:inherit (markdown-code-face font-lock-constant-face))))
+  '((t (:inherit (font-lock-constant-face markdown-code-face ))))
   "Face for inline code."
   :group 'markdown-faces)

 (defface markdown-pre-face
-  '((t (:inherit (markdown-code-face font-lock-constant-face))))
+  '((t (:inherit (font-lock-constant-face markdown-code-face ))))
   "Face for preformatted text."
   :group 'markdown-faces)

注:更换 markdown-code-face font-lock-constant-face 的位置是为了让 default 外的值起作用,否则其为第一个元素时,后面的元素将不起作用。 (Note: exchanging the position of markdown-code-face and font-lock-constant-face is to make the values beside default in effect. Or, as default is the first element, the rest will be discarded)

修改后的结果 (Result)

non-fixed-width-char-result

alphatan commented 10 months ago

这是一个自问自答的问题,为在中文环境中遇到同样问题的人提供搜索得到的解决方案。 (This is a self-questioning issue, for the user who met the same problem in chinese environment.)

这可能是一个很小众的问题,因此我也不会提交一个 Pull Request。 (This may be a very niche problem. So, I will not initiate a Pull Request.)

alphatan commented 10 months ago

一个更好的解决方案: (A better solution:)

  1. ~/.emacs 中为 defaultfixed-pitch 设置相同的等宽字体 (set the same fixed-width font for default and fixed-pitch in ~/.emacs)
    ;; default font
    (set-face-attribute 'default nil :font (font-spec :family "SimSun" :size 16))
    ;; fixed-width font
    ;;      Don't set it by (set-face-attribute 'fixed-pitch nil :inherit 'default)
    ;;      or it will break the font inheritance tree
    (set-face-attribute 'fixed-pitch nil :font (font-spec :family "SimSun" :size 16))
  2. 不修改 markdown-mode.el (Leave the markdown-mode.el unchanged.)