Home

Awesome

typst-preview.el

Warning: This is work in progress and quite rough around the edges, but the basic functionality is there.

Live preview of typst files inside emacs! Building on typst-preview, which was originally written for VS Code, and also inspired by typst-preview.nvim.

Features:

https://github.com/havarddj/typst-preview.el/assets/96797844/e44426b2-b404-416e-bb1d-54242f8bb077

Installation

MELPA

Currently not on Melpa

Manual installation

Install the typst-preview binary from https://github.com/Enter-tainer/typst-preview/releases and make sure it's in your $PATH. To test this, create test.typ and run

typst-preview test.typ

Then put typst-preview.el in your load-path, make sure websocket is installed, and put this in your init file:

(require 'typst-preview)

Or, if you use use-package, try:

(use-package websocket)
(use-package typst-preview
  :load-path "path-to-typst-preview.el")

If you use doom, try:

(package! typst-preview
  :recipe (:host github :repo "havarddj/typst-preview.el"))

Usage and configuration

Basic usage

Inside a .typ-file, run M-x typst-preview-mode. It will prompt you to set a master file, which by default is the file you are currently editing. This is useful if you have a file which links to other files using #include. Then it starts a preview in your default browser, and connects the source buffer to the server, sending live updates.

Start, stop and restart typst-preview using M-x typst-preview-start, M-x typst-preview-stop and M-x typst-preview-restart.

Jumping from source to preview: M-x typst-preview-send-position. This only works in text (i.e. not in a code block, say in math mode) because of how the typst compiler works. See https://github.com/Enter-tainer/typst-preview/issues/182.jk

Jumping to preview in Firefox currently doesn't work due to a bug in typst-preview. Until this is fixed, I recommend using xwidget or a different browser.

Change default browser

To preview the .typ file using xwidget, provided your emacs is built with xwidget-support:

(setq typst-preview-browser "xwidget") ;; default is "default"

To preview the .typ file in a non-default browser, you can set typst-preview-browser to a browser of your choice, and typst-preview will try to find the browser using (executable-find typst-preview-browser). This does not work on macOS, but might perhaps work on linux. You can alternatively add a hook to typst-preview-mode-hook to set browse-url-generic-program for typst buffers specifically.

Details:

Enabling typst-preview-mode runs typst-preview-start, which does a few things:

Advanced configuration

Here is a sample configuration using use-package which includes typst-ts-mode and typst-lsp.

(use-package websocket)
(use-package typst-preview
  :load-path "directory-of-typst-preview.el"
  :config
  (setq typst-preview-browser "default")
  (define-key typst-preview-mode-map (kbd "C-c C-j") 'typst-preview-send-position)
  )

(use-package typst-ts-mode
  :load-path "directory-of-typst-ts-mode.el"
  :custom
  (typst-ts-mode-watch-options "--open")
  :config
  ;; make sure to install typst-lsp from
  ;; https://github.com/nvarner/typst-lsp/releases
  (add-to-list 'lsp-language-id-configuration '(typst-ts-mode . "typst"))
  (lsp-register-client
   (make-lsp-client
    :new-connection (lsp-stdio-connection "typst-lsp")
    :major-modes '(typst-ts-mode)
    :server-id 'typst-lsp))
  )

License:

This project is licensed under the GPL License - see the LICENSE.md file for details

Todos: