havarddj / typst-preview.el

Typst live preview minor mode for emacs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  • Live preview of edits
  • Source to preview jumping
  • Preview to source jumping
typst-preview-demo.mp4

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:

  • Asks for a master file, and if there is a preview process with same master, attaches the current file to that process. Otherwise it will:
    • Start typst-preview on the current file, sending results to the buffer *ws-typst-server*.
    • Connect to the typst-preview server using websocket.
  • Then it opens a browser pointing at the address of the preview, and
  • Adds a hook to after-change-functions which sends the buffer to the server at each keystroke.

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:

  • Open browsers in linux/windows, not just MacOS
  • Ensure that opening several .typ instances works
  • Clean up typst-preview-start
  • Add license
  • Fix "revert buffer makes typst restart" - should be enough to look for existing instance. Does reverting reset buffer-local variables?
  • Optionally centre buffer on preview-to-source
  • Migrate to README.md
  • Add screencast
  • Add outline functionality
  • Ensure that slides work properly
  • Put sample config with typst-lsp and typst-ts-mode in configuration sample
  • Fix stop-process issue

About

Typst live preview minor mode for emacs

License:GNU General Public License v3.0


Languages

Language:Emacs Lisp 84.1%Language:Typst 15.9%