22. Emacs
Emacs gets CompMD's grammars, the language server (chapter 24) through Eglot, and the drawing from one package, compmd-ts-mode. This chapter is that package for a developer: setting it up, against a release or your own build of the server, what it does in a project of yours, and its configuration. Writing in Emacs is the Author's Guide's chapter 21.
Quick setup#
Emacs 30 or later, git and a C compiler.
-
The package,
comp-markdown/compmd-ts-mode:M-x package-vc-installwith its URL,use-packagewith:vc, MELPA once it is listed there, or in Doom Emacs apackage!recipe anddoom sync. Start the server with each buffer:(use-package compmd-ts-mode :vc (:url "https://github.com/comp-markdown/compmd-ts-mode" :rev :newest) :hook (compmd-ts-mode . eglot-ensure)) -
The grammars:
M-x treesit-install-language-grammarforcompmd,compmd-inlineandcel; the mode adds their sources, fromcomp-markdown/compmd-grammars, totreesit-language-source-alist. -
The server: on the
PATH(npm install -g @compmd/lsp,cargo install compmd-lsp), or downloaded by the mode when Eglot doesn't find it:compmd-ts-lsp-installfetches the release's binary for the platform intocompmd-ts-lsp-install-directoryand checks it against the release'sSHA256SUMS.
Your own build of the server, to try a change to it: put its folder first on exec-path, before Eglot starts, and M-x eglot-reconnect after each build:
(add-to-list 'exec-path (expand-file-name "~/src/compmd/target/debug"))
How to use#
What the package is#
| File | What it does |
|---|---|
compmd-ts-mode.el |
the major mode on Emacs's tree-sitter, with the three grammars' highlighting, Imenu and outlines; the server's entry in eglot-server-programs for it and for markdown-mode and markdown-ts-mode, the download, compmd-ts-preview (C-c C-p), and compmd.config.json as a project root for project.el |
compmd-ts-drawing.el |
the renderer: compmd-ts-server, an Eglot server class that declares the drawing and takes its refreshes, and the server's plan (compmd/drawing) as overlays, a face per style |
The renderer draws all seven primitives (chapter 24): a prefix is a line-prefix and wrap-prefix, so a container's wrapped lines keep their gutter, and a block is a display string over its lines. It sends each request its window's width and whether it wraps, so a table wider than a window that wraps comes laid out to fit it. A mark gives way while the cursor is on its lines, found by cursor-sensor-functions. The package doesn't declare concealsMarkdown: markdown-mode hides markup only on demand.
In a project of yours#
The server reads a document's project as your app does (chapter 24), and project.el finds the same root, so project-find-file and Eglot agree. The scenarios a developer meets are Neovim's (chapter 21): a pack's components completing with their params, an app's configuration applied to its documents, Generate the signature for a host-language component, compmd/blocks for code renderers. C-c C-p opens the page as your host renders it.
Checking it#
In comp-markdown/compmd, cargo make emacs-check builds the server and the grammars from the checkout and runs the package's test/check.el in a batch Emacs: the mode, its faces, Imenu, the server through Eglot, the preview, and the drawing by its overlays. cargo make emacs-terminal-check runs a terminal Emacs in a tmux session of its own and reads its screen: the gutter, a table's box, a heading giving way under the cursor, and the drawing after a jump to the document's end and back. test/lint.el runs MELPA's checks (package-lint, checkdoc, byte-compiling with warnings as errors). cargo make emacs-interactive [file] opens Emacs with the mode in place of your config. The published package's CI runs the check and the lint against the released server.
Configuration#
The server's settings, under :compmd in eglot-workspace-configuration, a plist (:json-false for false), applied without a restart:
(setq-default eglot-workspace-configuration
'(:compmd (:packs (:suggest ["@acme"] :network t)
:preview (:browser "firefox")
:render (:reveal "element" :glyphs "nerd")
:markdown (:enabled t :wikiLinks t)
:tables (:alignOnType t :alignOnFormat t)
:links (:enabled t :completion t :diagnostics t :rename t :wiki t)
:trace "verbose")))
A project's own settings go in its .dir-locals.el, as Eglot reads them. Chapter 24 has every setting.
The package's own: compmd-ts-lsp-version (the release compmd-ts-lsp-install downloads), compmd-ts-lsp-install-directory, and compmd-ts-drawing-delay (the seconds without a change before the plan is asked for again). The faces, compmd-ts-heading-1 … compmd-ts-param, inherit Emacs's own, so a theme restyles them (M-x customize-group RET compmd-ts-drawing). M-x eglot-events-buffer shows the messages between Emacs and the server.
Eglot runs one server for a buffer: in Markdown buffers compmd-lsp takes another Markdown server's place. A writer who keeps theirs puts its entry first once compmd-ts-drawing loads.
Next: Helix.