Skip to content

[mirror] Typst tree sitter major mode for Emacs.

License

Notifications You must be signed in to change notification settings

Ziqi-Yang/typst-ts-mode

Repository files navigation

Typst Tree-Sitter Mode

Tree Sitter support for Typst. Minimum Emacs version requirement: 29.

Static Badge

Discussion on Zulip
Tickets: Issues, feature requests, etc.
Announce Announcements. Subscribe to it to receive the latest news for typst-ts-mode.
Send a Patch

Requirement

  1. Emacs >= 29

  2. Latest Typst.
    typst 0.10.0 (70ca0d25)

  3. Tree Sitter grammar for Typst: https://github.com/uben0/tree-sitter-typst
    commit: 3c3e5f8e0caeba6157e26a1bedf8321e1da62799 - Feb 28, 2024
    To install the grammar, you can execute the following elisp code:

    (add-to-list 'treesit-language-source-alist
                 '(typst "https://github.com/uben0/tree-sitter-typst"))
    (treesit-install-language-grammar 'typst)

    Note there are also tree sitter modules, treesit-auto and treesit-langs for you to easily manage your tree sitter modes and grammars.

Installation

Example configuration.

(use-package typst-ts-mode
  :ensure (:type git :host sourcehut :repo "meow_king/typst-ts-mode" :files (:defaults "*.el"))
  :custom
  ;; don't add "--open" if you'd like `watch` to be an error detector
  (typst-ts-mode-watch-options "--open")
  
  ;; experimental settings (I'm the main dev, so I enable these)
  (typst-ts-mode-enable-raw-blocks-highlight t)
  (typst-ts-mode-highlight-raw-blocks-at-startup t))

Keys

C-c C-c c : typst-ts-compile-and-preview
C-c C-c C : typst-ts-compile
C-c C-c w : typst-ts-watch-mode
C-c C-c p : typst-ts-mode-preview
M- : typst-ts-mode-heading-decrease
M- : typst-ts-mode-heading-increase
M- : typst-ts-mode-heading-up
M- : typst-ts-mode-heading-down
M-: typst-ts-mode-meta-return
: typst-ts-mode-return
TAB : typst-ts-mode-cycle

NOTE: outline-minor-mode is enabled by typst-ts-mode, so you can use command defined by outline-minor-mode such as outline-cycle.

Customization Options

For customizable options: customize -> typst-ts.

Here are some options you may find useful:

  1. typst-ts-mode-indent-offset (default 4)
    Use outline-indent-mode to behave like org-indent-mode.
  2. typst-ts-compile-executable-location
  3. typst-ts-watch-options.
    Set this to --open so typst will open the compiled file for you.
  4. typst-ts-compile-options.
    Note that setting --open has no use for this customization variable. You can execute the shell command typst compile <file> --open && sleep 1 to view what is happening.
  5. typst-ts-watch-auto-display-compilation-error. (default t)
    so the typst watch process buffer appear when an error occurs, and disappear when there is no error. You may find sometimes there is only one error at a time, and it is because Typst itself do this style. You may find auto-save-visited-mode, auto-save or super-save useful (or annoying).
  6. typst-ts-compile-before-compilation-hook and typst-ts-compile-after-compilation-hook
  7. typst-ts-mode-return-autoincrement autoincrement lists when pressing RETURN (default t).
  8. typst-ts-mode-grammar-location: used for grammar version check (may not be accurate every time)

Fontification

  1. typst-ts-mode-fontification-precise-level (default 'middle)
    Available values: min, middle and max. Different level affects the precision of the fontification. For example, - item, we may fontify the whole expression using one face, or two faces. Note it is related to the performance of fontification process, especially the first fontification process (when you open the file).
  2. typst-ts-markup-header-same-height and typst-ts-markup-header-scale
    Control header height. Note that it only works when typst-ts-mode-fontification-precise-level is set to max.
  3. Override default font lock rules
    Please see the documentation of typst-ts-mode-font-lock-rules, you can find how to override the whole font lock rules or only small part of the font lock rules.

Raw block highlighting

This is an experimental feature
Note: currently only support Emacs 30 (master branch). But non-treesit font lock for raw block feature will be added soon (both 29 and 30).
Only support tree-sitter languages.
For more detailed documentation about raw block highlighting see this documentation

  1. typst-ts-mode-enable-raw-blocks-highlight (default nil)
  2. typst-ts-mode-highlight-raw-blocks-at-startup (default nil)
  3. typst-ts-highlight-raw-block-langs-not-in-predefined-settings (default t)

Consult Imenu Integration

If you use consult-iemnu command consult, you way want this setting.

(setq
 consult-imenu-config
 (append consult-imenu-config
         '((typst-ts-mode :topLevel "Headings" :types
                          ((?h "Headings" typst-ts-markup-header-face)
                           (?f "Functions" font-lock-function-name-face))))))

Contribute

  1. please work on develop branch, which will be combined into main branch every one week or so.

Co-Maintainer

Huan Nguyen

Do you want to develop a new Tree Sitter Major Mode?

Here are some resources: