设置 Emacs 每个缓冲区/模式的默认字体
如何更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
怎么样:
然后您可以自定义
variable-pitch
面,缓冲区中的其他面将从该面继承,而不是默认面。阅读 buffer-face-mode 的文档以了解更多自定义详细信息。 (BufFace 还用于
text-scale-increase
和text-scale-decrease
...非常有用。)How about something like this:
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 fortext-scale-increase
andtext-scale-decrease
... very useful.)我必须给出部分答案,因为这太复杂了,无法当场弄清楚,而且我已经超出了我的时间预算。
Face 是一个框架属性。 一个帧可以同时显示多个缓冲区。 模式是缓冲区属性。 您询问如何根据每种模式改变脸部。 综合所有这些,似乎这个问题不可能没有一个完全正确的答案。
如果您假设给定的帧永远不会显示多个缓冲区,则可以近似得到所需的答案。 实际上,您可以使用类似的方法来完成此操作,但修改为使用特殊显示正则表达式和一组与您的降价模式缓冲区名称匹配的正则表达式。
但是,这可能不是您想要的。 您的问题似乎意味着更改单个框架的面部属性。
再次假设一帧永远不会一次显示多个缓冲区,您可以尝试建议切换到缓冲区。 但这可能还不够低,而且可能太慢。 (未经测试)
现在我的实际(不完整)答案...
更好,尽管更复杂,方法将指示降价模式为尚未分配内置面孔之一的所有区域使用新面孔。 您可以使用 copy-face 创建一个新面,并使用 set-face-* 为其赋予有趣的属性。
修改 markdown-mode 的 font-lock-defaults 以覆盖默认的 font-lock-fontify-region-function,如 font-lock.el 第 946 行附近以“字体化函数”开头的注释块中所述。 您可能可以使用稍微修改过的 font-lock-default-fontify-region,它在完成后立即执行一个额外的步骤:
额外的步骤解析该区域,类似于 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.
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)
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:
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.
可变音高模式非常棒。 我是通过这个帖子知道的。 但它可以做得更棒:
只需将您想要的无衬线字体的任何模式添加到列表中即可。
The
variable-pitch
-mode is awesome. I found out about it through this thread. But it can be made even more awesome:Just add whatever mode you want sans-serif fonts in to the list.
我发现 EmacsWiki 中有一段代码非常方便。 这样做的好处是不仅可以设置字体,还可以方便地配置
:height
、:width
等,并与
load-theme-buffer- 结合使用local
包中,您甚至可以轻松指定缓冲区的颜色主题: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 wellCombined with
load-theme-buffer-local
package, you can even specify the color theme for the buffer easily:即使对于 emacs 版本 22.3.1,它实际上也很简单......
只需尝试以下操作:
It's actually straightforward even for emacs version 22.3.1…
Just try the following: