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?
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.)It's actually straightforward even for emacs version 22.3.1…
Just try the following:
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.
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: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.