设置 Emacs 每个缓冲区/模式的默认字体

发布于 2024-07-13 15:13:35 字数 393 浏览 11 评论 0原文

如何更改 Emacs 用于在每种模式下设置文本样式的默认外观?

例如,假设我已经对我拥有的面部自定义感到满意,其中包括默认的固定宽度字体。 但是,在一种特定模式(例如 markdown-mode.el)下,我希望默认字体是可变宽度的。

为 Markdown 模式设置标题、链接等的样式非常简单:只需将光标放在样式文本和 Mxdescribe-face 上,然后单击链接即可对其进行自定义。

但是,默认面是在没有指定其他面时使用的面,因此它不是特定于 markdown-mode 的,如果修改将影响所有其他模式。

我可以在 markdown-mode-hook 中添加什么魔法来设置使用此模式的缓冲区的默认外观?

How do you change the default face which Emacs uses to style text on a per-mode basis?

For example, say that I am already happy with the face customizations that I have, which include a default fixed-width font. However, in one particular mode (markdown-mode.el, say), I want the default font to be variable-width.

It is easy to style headers, links etc. uniquely for markdown-mode: simply place the cursor over the styled text and M-x describe-face, then click the link to customize it.

However, the default face is the face used if no other face is specified, so it is not specific to markdown-mode and if modified will affect all other modes.

What magic can I put in the markdown-mode-hook to set the default face for buffers using this mode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清旖 2024-07-20 15:13:35

怎么样:

(add-hook 'markdown-mode-hook (lambda () (variable-pitch-mode t))

然后您可以自定义variable-pitch 面,缓冲区中的其他面将从该面继承,而不是默认面。

阅读 buffer-face-mode 的文档以了解更多自定义详细信息。 (BufFace 还用于 text-scale-increasetext-scale-decrease...非常有用。)

How about something like this:

(add-hook 'markdown-mode-hook (lambda () (variable-pitch-mode t))

You can then customize the variable-pitch face, and the other faces in the buffer will inherit from this instead of the default face.

Read the docs for buffer-face-mode for more customization details. (BufFace is also used for text-scale-increase and text-scale-decrease... very useful.)

锦上情书 2024-07-20 15:13:35

我必须给出部分答案,因为这太复杂了,无法当场弄清楚,而且我已经超出了我的时间预算。

Face 是一个框架属性。 一个帧可以同时显示多个缓冲区。 模式是缓冲区属性。 您询问如何根据每种模式改变脸部。 综合所有这些,似乎这个问题不可能没有一个完全正确的答案。

如果您假设给定的帧永远不会显示多个缓冲区,则可以近似得到所需的答案。 实际上,您可以使用类似的方法来完成此操作,但修改为使用特殊显示正则表达式和一组与您的降价模式缓冲区名称匹配的正则表达式。

(append special-display-buffer-names
        '("*VC-log*"
          "*Help*"
          ("*Completions*" 
           (height . 25)
           (font . "8x13"))))

但是,这可能不是您想要的。 您的问题似乎意味着更改单个框架的面部属性。

再次假设一帧永远不会一次显示多个缓冲区,您可以尝试建议切换到缓冲区。 但这可能还不够低,而且可能太慢。 (未经测试)

(defadvice switch-to-buffer (after switch-to-buffer activate compile)
  "change the frame's default face after switch-to-buffer"
  (doSomethingToChangePropertiesOfDefaultFace))

现在我的实际(不完整)答案...

更好,尽管更复杂,方法将指示降价模式为尚未分配内置面孔之一的所有区域使用新面孔。 您可以使用 copy-face 创建一个新面,并使用 set-face-* 为其赋予有趣的属性。

修改 markdown-mode 的 font-lock-defaults 以覆盖默认的 font-lock-fontify-region-function,如 font-lock.el 第 946 行附近以“字体化函数”开头的注释块中所述。 您可能可以使用稍微修改过的 font-lock-default-fontify-region,它在完成后立即执行一个额外的步骤:

  (unless font-lock-keywords-only
    (font-lock-fontify-syntactically-region beg end loudly))

额外的步骤解析该区域,类似于 font-lock-fontify-syntropically-region 所做的,打破了区域划分为“有趣的”子区域。 但这次您找到具有默认面的子区域,并将这些子区域的文本属性设置为您之前创建的新面。

总而言之,你的 .emacs 文件中应该只有几行 elisp,再加上制作一个 font-lock-default-fontify-region 的副本,它与原始版本只有很小的差异(调用一个新函数),另外,制作 font-lock-fontify-syntropically-region 的副本并修改它以执行您的命令(最困难的部分)。

实际上,如果您“之后”建议 font-lock-fontify-syntropically-region,那么您可能甚至不需要修改 font-lock-defaults 或 font-lock-default-fontify-region。

I have to give a partial answer because this is too complicated to figure out on the spot and I already blew my time budget.

Face is a frame property. A frame can display multiple buffers at the same time. Mode is a buffer property. You ask how to vary the face on a per-mode basis. Combining all this, it seems that the question cannot not have a single fully-correct answer.

You can approximate the desired answer if you assume that a given frame will never display more than one buffer. You can actually accomplish that with something like this, but modified to use special-display-regexps and a set of regexps that match your markdown-mode buffer names.

(append special-display-buffer-names
        '("*VC-log*"
          "*Help*"
          ("*Completions*" 
           (height . 25)
           (font . "8x13"))))

However, this is probably not what you want. Your question seems to imply changing the face properties of a single frame.

Again assuming that a frame will never display more than one buffer at a time, you can try advising switch-to-buffer. But that might not be sufficiently low level and it might be too slow. (untested)

(defadvice switch-to-buffer (after switch-to-buffer activate compile)
  "change the frame's default face after switch-to-buffer"
  (doSomethingToChangePropertiesOfDefaultFace))

And now for my actual (incomplete) answer...

A better, albeit more complicated, approach would instruct markdown-mode to use a new face for all regions that are not already assigned one of the built-in faces. You can create a new face with copy-face and give it interesting properties with set-face-*.

Modify markdown-mode's font-lock-defaults to override the default font-lock-fontify-region-function as described in the comment block near line 946 of font-lock.el that begins, "Fontification functions". You can probably use a very slightly modified font-lock-default-fontify-region that does just one extra step immediately after it does:

  (unless font-lock-keywords-only
    (font-lock-fontify-syntactically-region beg end loudly))

The extra step parses the region similar to what font-lock-fontify-syntactically-region does, breaking the region into "interesting" sub-regions. But this time you find sub-regions that have the default face and you put-text-property those sub-regions to the new face that you previously created.

In all this feels like it should be only a couple lines of elisp in your .emacs file, plus make a copy of font-lock-default-fontify-region that has only a minor diff from the original (call one new function), plus make a copy of font-lock-fontify-syntactically-region and modify it to do your bidding (the most difficult part).

Actually, if you "after" advise font-lock-fontify-syntactically-region then you probably don't even need to modify font-lock-defaults or font-lock-default-fontify-region.

独自←快乐 2024-07-20 15:13:35

可变音高模式非常棒。 我是通过这个帖子知道的。 但它可以做得更棒:

(dolist (hook '(erc-mode-hook
        LaTeX-mode-hook
        org-mode-hook
        edit-server-start-hook
        markdown-mode-hook))
  (add-hook hook (lambda () (variable-pitch-mode t))))

只需将您想要的无衬线字体的任何模式添加到列表中即可。

The variable-pitch-mode is awesome. I found out about it through this thread. But it can be made even more awesome:

(dolist (hook '(erc-mode-hook
        LaTeX-mode-hook
        org-mode-hook
        edit-server-start-hook
        markdown-mode-hook))
  (add-hook hook (lambda () (variable-pitch-mode t))))

Just add whatever mode you want sans-serif fonts in to the list.

躲猫猫 2024-07-20 15:13:35

我发现 EmacsWiki 中有一段代码非常方便。 这样做的好处是不仅可以设置字体,还可以方便地配置 :height:width 等,并

;; Use variable width font faces in current buffer
(defun my-buffer-face-mode-variable ()
  "Set font to a variable width (proportional) fonts in current buffer"
  (interactive)
  (setq buffer-face-mode-face '(:family "DejaVu Sans" :height 100 :width semi-condensed))
  (buffer-face-mode))
;; Use monospaced font faces in current buffer
(defun my-buffer-face-mode-fixed ()
  "Sets a fixed width (monospace) font in current buffer"
  (interactive)
  (setq buffer-face-mode-face '(:family "Consolas" :height 100))
  (buffer-face-mode))
;; Set default font faces for Info and ERC modes
(add-hook 'erc-mode-hook 'my-buffer-face-mode-variable)
(add-hook 'Info-mode-hook 'my-buffer-face-mode-variable)

load-theme-buffer- 结合使用local 包中,您甚至可以轻松指定缓冲区的颜色主题:

(defun my-buffer-face-mode-variable ()
  "Set font to a variable width (proportional) fonts in current buffer"
  (interactive)
  (setq buffer-face-mode-face '(:family "DejaVu Sans" :height 100 :width semi-condensed))
  (buffer-face-mode)
  (load-theme-buffer-local 'leuven (current-buffer)))

There is a block of code which I find very convenient, from EmacsWiki. The advantage of this is that you can set not only font face, but conveniently configure :height, :width etc as well

;; Use variable width font faces in current buffer
(defun my-buffer-face-mode-variable ()
  "Set font to a variable width (proportional) fonts in current buffer"
  (interactive)
  (setq buffer-face-mode-face '(:family "DejaVu Sans" :height 100 :width semi-condensed))
  (buffer-face-mode))
;; Use monospaced font faces in current buffer
(defun my-buffer-face-mode-fixed ()
  "Sets a fixed width (monospace) font in current buffer"
  (interactive)
  (setq buffer-face-mode-face '(:family "Consolas" :height 100))
  (buffer-face-mode))
;; Set default font faces for Info and ERC modes
(add-hook 'erc-mode-hook 'my-buffer-face-mode-variable)
(add-hook 'Info-mode-hook 'my-buffer-face-mode-variable)

Combined with load-theme-buffer-local package, you can even specify the color theme for the buffer easily:

(defun my-buffer-face-mode-variable ()
  "Set font to a variable width (proportional) fonts in current buffer"
  (interactive)
  (setq buffer-face-mode-face '(:family "DejaVu Sans" :height 100 :width semi-condensed))
  (buffer-face-mode)
  (load-theme-buffer-local 'leuven (current-buffer)))
一梦浮鱼 2024-07-20 15:13:35

即使对于 emacs 版本 22.3.1,它实际上也很简单......

只需尝试以下操作:

(progn 
  (set-buffer "your buffer name here")
  (overlay-put (make-overlay 0 (buffer-size)) 'face 'your-face))

It's actually straightforward even for emacs version 22.3.1…

Just try the following:

(progn 
  (set-buffer "your buffer name here")
  (overlay-put (make-overlay 0 (buffer-size)) 'face 'your-face))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文