Turning Emacs into a Word Processor: Step 1: Subverting the 'default Face
The fundamental first challenge to overcome is the way Emacs distinguishes, or rather, does not distinguish, document body text as an independently styleable face. Most text in your emacs buffer has no explicit face, and so is rendered as 'default, which has the interesting property of being the parent face of every other face.
Using 'default as the body face for a word processor could be done, and this is what I tried in my first attempts to build a comfortable and familiar writing environment in Emacs. But it has some significant downsides. The first and most obvious is that any attribute you want to add to your buffer’s 'default face, like a font, a size, colour, etc,
has to be overridden in every other face. This isn’t a huge problem, really it doesn’t rise much above the level of annoying boilerplate.
But 'default has a particular property that makes it an unworkable fit for paragraph text in a word processor: it fixes a hard floor for text line heights, across all of Emacs. That’s not to say you can’t make smaller faces; of course you can. But the line height itself cannot be smaller than the height of the 'default face. So if 'default is 12pt Times New Roman, and I want to write a few lines at 8pt, those lines will have 8pt glyphs, but they will be vertically spaced as if they were using 12pt font — so they’ll look about 1.5x spaced.
Single-spaced text at any font size is something a word processor needs to be able to do. The most common use case (at least in academic writing, which is my interest) is footnotes. But the only way to get the actual line height small enough is to resize 'default to the size of the smallest line height you’ll use. So the downside is obvious: make 'default smaller, and body text becomes smaller, because body text is fontified as 'default.
The solution isn’t too complicated — we just need to apply a different face to body text in our buffer, then we can do what we please with 'default (we’ll do this buffer-locally of course; changing 'default globally really screws with the Emacs UI. Trust me — I’ve tried it).
Normally emacs uses font-lock-keywords which is a list of cons cells: (MATCHER . FACENAME), which takes matches a regex and applies the FACENAME face to match groups (actually there are several different forms but let’s keep it simple). What we need to do to recognize body text in org-mode is process the whole buffer, matching any text that has not been matched by another font-lock keyword. font-lock-keywords could do this using a function rather than a regex as its matcher, but that solution would depend on ensuring that our matcher is always last in the font-lock keywords. This is a bit of a long bet, because all sorts of major and minor modes can add their own keywords, so to do it reliably, after, all the keywords, we’ll add an :after advice to font-lock-fontify-keywords-region to make sure this work happens after all other faces have been settled (of course I’m assuming nobody else is as deeply rewiring Emacs’ text display as I am).
(advice-add 'font-lock-fontify-keywords-region :after #'face-extra--dispatch)
I’ll get into face-extra and its dispatcher later, but for now it simply receives the same arguments as a font-lock-keywords function and applies our own logic to it: check if org-mode’s keywords have applied a face or not, and fill in the 'default gaps. (I’m building on org-mode, but in the interest of being mode-independent, body-face-mode allows creating backends for other major modes, you can check the source to see how). So we walk the whole buffer as it’s fontified, and add our own, brand new 'body face to it. Now, anywhere we wouls have had 'default, we now wind up with this:
There are text properties here:
face body
fontified t
(output from ‘describe-char in the document body)
We can now do whatever we want to 'default — includig making it much smaller — and still style paragraph text to look good. Which lets us do things like this:
:properties: drawer in there. The theme here is foxglove-vellum from vegasharom’s excellent foxglove-themes.)
To get an arbitrary integer line height, we would have to set 'default to 1pt; we go with 6pt instead, as I judge this is a bit smaller as any face that would actually be useful and it’s easy to up-scale from there. I call this the floor size; picking it is definitely a judgement call, and if you want to use the code you can easily make it smaller if you see a need.