21. Writing in Emacs
Emacs does with a CompMD document what Neovim does: it colours its components and params, says what is wrong as you type, completes a component's name and its params, shows the value of each {{…}} beside it, and draws the document in place while the text you type stays as written; in a window of its own, a heading is larger than the text. This chapter sets Emacs up for CompMD, then goes through writing there, then its settings.
Quick setup#
You need Emacs 30 or later, git, and a C compiler (which builds CompMD's grammars).
1. The mode, compmd-ts-mode, from its repository:
M-x package-vc-install RET https://github.com/comp-markdown/compmd-ts-mode RET
or with use-package:
(use-package compmd-ts-mode
:vc (:url "https://github.com/comp-markdown/compmd-ts-mode" :rev :newest)
:hook (compmd-ts-mode . eglot-ensure))
Once it is on MELPA, M-x package-install RET compmd-ts-mode RET, or :ensure t. In Doom Emacs, add (package! compmd-ts-mode :recipe (:host github :repo "comp-markdown/compmd-ts-mode")) to packages.el, the hook to config.el with use-package!, and run doom sync.
2. The grammars, once: M-x treesit-install-language-grammar three times, for compmd, compmd-inline and cel. The mode knows where each comes from.
3. Open a .comp.md file. With the hook above, Eglot starts CompMD's language server, compmd-lsp: a program Emacs starts beside it, which reads the document as a site does and tells the editor what it found (without the hook, M-x eglot). The first time, when it isn't installed, Emacs asks whether to download it, and puts the release's file for your computer in place after checking its sum. npm install -g @compmd/lsp or cargo install compmd-lsp installs it on your PATH instead.
How to use#
What you see#
Open a document, and it is drawn in place: a heading's #s show as its level, H1 to H6, its line in its colour, larger in a window of its own; a code block's fences are set back and its language coloured; bullets and checkboxes are symbols; quotes have a bar and alerts their icon; a table is a box, its cells padded so the columns line up even when the text isn't aligned; a container's lines have a bar, │, down their left side, wrapped lines too, its opener and closer as written; the frontmatter's fences are rules and its lines set back.
The line the cursor is on shows its text as written, so you always edit what is there: move off it, and it is drawn again. Nothing is changed in the file: the drawing is only how Emacs shows it.
Fixing a mistake#
A mistake is underlined where it is, by Flymake, with the diagnostic's code and message: a component that isn't imported, a param it doesn't take, a value of the wrong type. M-x flymake-show-buffer-diagnostics lists them all. When there is a fix, M-x eglot-code-actions offers it at the mistake: import the pack that has a component, add a registry. The diagnostics and their fixes has every code.
Adding a component#
Type :: and complete (C-M-i, or as you type with Corfu or Company): the components your document can use, from your project's folder and its installed packs; picking one from a pack adds its import to the frontmatter for you. Inside {…}, it offers the component's params and then their values; in an expression, md. paths and the functions you can call. With the cursor on a component, the echo area shows its signature, the params it takes and their types (chapter 15). The value of each interpolation and expression shows beside it, as the last render computed it: # {{md.doc.title}} with = Harbour notes after it.
Tables#
Type | in a table, and its cells are padded so the pipes line up in the file; M-x eglot-format-buffer aligns every table. M-x eglot-code-actions on a table offers to insert, delete or move a row or a column, or change a column's alignment. A table wider than a window whose lines wrap (visual-line-mode, as Doom's text buffers have) is drawn narrowed to fit it, its cells wrapped in their columns, until the cursor is in it.
Links and headings#
M-. on a link goes to its file or heading, and M-, back; M-? on a heading lists every link to it; M-x eglot-rename on a heading renames it, and every link to it follows, in every file of the project. A link to a file or a heading that isn't there is warned about. M-x imenu lists the document's headings and containers.
The preview#
C-c C-p (or the code action Open preview) opens the document in your browser, as a site renders it, and follows as you type.
Markdown files#
Eglot starts the same server for markdown-mode and markdown-ts-mode buffers, and they are drawn too. A .md file is CompMD when its frontmatter has an md key (chapter 1); otherwise it is plain Markdown, and gets what Markdown has: the drawing, tables aligned as you type, links and headings, and the preview. Its ::: stays text, as GitHub shows it. The code action Read as CompMD adds the key.
What it adds#
Without it, a .comp.md file is Markdown to Emacs, at best: its components are plain text, a typo in a param shows only when the page is built, and a table lines up only if you line it up. With it, Emacs knows what the site will make of the document as you type: what is wrong and how to fix it, what a component takes, what a value comes to, and how the page will look.
Configuration#
The server's settings go in eglot-workspace-configuration, under :compmd, and apply without a restart (:json-false for false):
(setq-default eglot-workspace-configuration
'(:compmd (:render (:enabled t ; :json-false: nothing drawn
:glyphs "unicode" ; or "nerd" (Nerd Font icons), "ascii"
:reveal "line" ; or "element" (the whole element as written), "off"
:heading (:marker t :fill t)
:code (:language "inline" :fence t) ; or "above", "fence", "off"
:table (:borders t)
:container (:gutter t)
:frontmatter (:rules t :dim t)
:markdownFiles t) ; :json-false: .md files served but not drawn
:tables (:alignOnType t :alignOnFormat t)
:markdown (:enabled t :wikiLinks :json-false))))
Each element of the drawing (:heading, :code, :table, :list, :checkbox, :quote, :callout, :rule, :container, :component, :param, :frontmatter) takes :json-false to leave it as written. The Developer's Guide's chapter 24 lists every setting.
Faces. Each style is a face, compmd-ts-heading-1 to compmd-ts-heading-6, compmd-ts-heading-marker, compmd-ts-code-fence, compmd-ts-container-border, compmd-ts-component, compmd-ts-param and the others, inheriting Emacs's own (outline-1 to outline-6, shadow, font-lock-type-face), so a theme restyles them: M-x customize-group RET compmd-ts-drawing.
The mode's own options (M-x customize-group RET compmd-ts): compmd-ts-lsp-install-directory, where the downloaded server goes; compmd-ts-lsp-version, the release it downloads; compmd-ts-drawing-delay, how long the buffer must be still before it is drawn again.
Another Markdown server. Eglot runs one server for a buffer, so in Markdown buffers compmd-lsp takes the place of Marksman, or another you had. To keep yours for .md files, put its entry first, once the drawing loads:
(with-eval-after-load 'compmd-ts-drawing
(add-to-list 'eglot-server-programs '((markdown-mode markdown-ts-mode) "marksman")))
markdown-mode's own hidden markup (markdown-hide-markup) draws a .md file too: with both on, set :markdownFiles :json-false.
Next: writing in Helix.