Skip to content

havarddj/typst-preview.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 a tinymist binary from https://github.com/Myriad-Dreamin/tinymist and make sure it's in your $PATH. To test this, create test.typ and run

tinymist preview test.typ

It is also possible to use the old typst-preview, found at https://github.com/Enter-tainer/typst-preview/releases. Run

typst-preview test.typ

to test it. Since typst-preview has been deprecated in favour of tinymist preview, the latter is the new default. You can change back to typst-preview using

(setq typst-preview-executable "typst-preview")

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.

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
  ;; or use tinymist
  (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:

  • Find a better way to get name of static-host without searching websocket buffer
  • Make sure we can have multiple files with same name (which emacs distinguishes with "filename.typ")
  • Make compatible with tinymist in addition to typst-preview
  • Fix "revert buffer makes typst restart" - should be enough to look for existing instance. Does reverting reset buffer-local variables?
  • Add outline functionality (NB: this might come for free from tinymist?)
  • Ensure that slides work properly
  • Build more robust restart functionality
  • add buffer-kill hook
  • Clear variables after stopping
  • When buffer closed, remove from list of active files
  • Sync memory files on save

  • Solve some out of bounds problem? (jump to source on certain symbols)

  • Don't start processes on startup? Or at least don't open windows

  • Refresh local variables of connected files when running clear-active-files

  • Option for pinning main document on a per-file basis

    • respect file variable set in first line
    • option to set file variable in beginning of file
  • Open browsers in linux/windows, not just MacOS

  • Ensure that opening several .typ instances works

  • Clean up typst-preview-start

  • Add license

  • Put sample config with typst-lsp and typst-ts-mode in configuration sample

  • Fix stop-process issue

  • Optionally centre buffer on preview-to-source

  • Migrate to README.md

  • Add screencast

  • Add dark mode option

  • Fix global-minor-mode?

  • Get rid of annoying 'No active typst-preview'-message

About

Typst live preview minor mode for emacs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published