Function: zig-ts-mode

zig-ts-mode is an interactive and natively compiled function defined in zig-ts-mode.el.

Signature

(zig-ts-mode)

Documentation

Major mode for editing Zig, powered by tree-sitter.

In addition to any hooks its parent mode prog-mode might have run, this mode runs the hook zig-ts-mode-hook, as the final or penultimate step during initialization.

C-c C-b C-b zig-ts-build
C-c C-b C-e zig-ts-build-exe
C-c C-b C-l zig-ts-build-lib
C-c C-b C-o zig-ts-build-obj
C-c C-r zig-ts-run
C-c C-t zig-ts-test

Key Bindings

This command is not in any keymaps.

Source Code

;; Defined in /nix/store/jxyx97fhclw4jb395hn97pz60n2rh0hs-emacs-packages-deps/share/emacs/site-lisp/elpa/zig-ts-mode-20260921.1554/zig-ts-mode.el
;;;###autoload
(define-derived-mode zig-ts-mode prog-mode "Zig"
  "Major mode for editing Zig, powered by tree-sitter."
  :group 'zig-ts
  :syntax-table zig-ts--syntax-table

  (setq-local indent-tabs-mode nil)

  (when-let* ((missing (seq-filter (lambda (r)
                                     (not (treesit-language-available-p (car r))))
                                   zig-ts-grammar-recipes)))
    (when (y-or-n-p "Zig tree-sitter grammars are not installed yet.  Install now?")
      (zig-ts-install-grammars)))

  ;; Warn if installed grammars are outdated
  (zig-ts--check-grammar-compatibility)

  ;; Compile
  (setq-local compile-command (concat (shell-quote-argument zig-ts-zig-bin) " build"))

  ;; Comment
  (setq-local comment-start "// ")
  (setq-local comment-end "")
  (setq-local comment-start-skip (rx (group (seq "/" (+ "/")))
                                     (* (syntax whitespace))))
  (setq-local comment-end-skip (rx (* (syntax whitespace))
                                   (group (syntax comment-end))))
  (setq-local comment-line-break-function #'zig-ts--comment-indent-new-line)

  ;; Electric indentation on delimiters
  (setq-local electric-indent-chars
              (append "{}()[].,;" electric-indent-chars))

  ;; Fill paragraph
  (setq-local paragraph-start
              (rx (or (seq (* (syntax whitespace))
                           (group (* (seq "/" (+ "/"))))
                           (* (syntax whitespace))
                           eol)
                      "\f")))
  (setq-local paragraph-separate paragraph-start)
  (setq-local adaptive-fill-mode t)

  (when (treesit-ready-p 'zig)
    (treesit-parser-create 'zig)

    (setq-local syntax-propertize-function #'zig-ts--syntax-propertize)
    (add-hook 'syntax-propertize-extend-region-functions
              #'syntax-propertize-wholelines nil t)

    ;; Fill paragraph with tree-sitter feature
    (setq-local fill-paragraph-function #'zig-ts--fill-paragraph)

    ;; Font-lock
    (setq-local treesit-font-lock-settings zig-ts--font-lock-settings)
    (setq-local treesit-font-lock-feature-list zig-ts--font-lock-feature-list)

    ;; Indentation
    (setq-local treesit-simple-indent-rules zig-ts--indent-rules)

    ;; Navigation
    (setq-local treesit-simple-imenu-settings zig-ts--imenu-settings)
    (setq-local treesit-thing-settings zig-ts--thing-settings)

    ;; Defun
    (setq-local treesit-defun-name-function #'zig-ts--defun-name)
    (setq-local treesit-defun-type-regexp zig-ts--defun-type-regexp)

    (treesit-major-mode-setup)))