branch: elpa/typst-ts-mode
commit bb9c2d7d613622349552dc44adf16a9bda88b2cd
Author: Ziqi Yang <mr.ziqiy...@gmail.com>
Commit: Ziqi Yang <mr.ziqiy...@gmail.com>

    feat: add convenient commands
---
 typst-ts-mode.el | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 67 insertions(+), 5 deletions(-)

diff --git a/typst-ts-mode.el b/typst-ts-mode.el
index d2b3ea163d..d8c5ca66ab 100644
--- a/typst-ts-mode.el
+++ b/typst-ts-mode.el
@@ -35,11 +35,6 @@
   :group 'text
   :group 'languages)
 
-(defgroup typst-ts-markup nil
-  "Typst tree sitter markup."
-  :prefix "typst-ts-markup"
-  :group 'typst-ts)
-
 (defgroup typst-ts-faces nil
   "Typst tree sitter faces."
   :prefix "typst-ts-faces"
@@ -50,6 +45,33 @@
   :type 'integer
   :group 'typst-ts)
 
+(defcustom typst-ts-mode-executable-location "typst"
+  "The location or name(if in `exec-path') for Typst executable."
+  :type 'string
+  :group 'typst-ts)
+
+(defcustom typst-ts-mode-compile-options ""
+  "User defined compile options for `typst-ts-mode-compile'.
+The compile options will be passed to the `typst compile' sub-command."
+  :type 'string
+  :group 'typst-ts)
+
+(defcustom typst-ts-mode-watch-options ""
+  "User defined compile options for `typst-ts-mode-watch'.
+The compile options will be passed to the `typst watch' sub-command."
+  :type 'string
+  :group 'typst-ts)
+
+(defcustom typst-ts-mode-watch-process-name "*Typst-Watch*"
+  "Process name for `typst watch' sub-command."
+  :type 'string
+  :group 'typst-ts)
+
+(defcustom typst-ts-mode-watch-process-buffer-name "*Typst-Watch*"
+  "Process buffer name for `typst watch' sub-command."
+  :type 'string
+  :group 'typst-ts)
+
 (defcustom typst-ts-markup-header-same-height nil
   "Whether to make header face in markup context share the same height."
   :type 'boolean
@@ -468,6 +490,40 @@ TYPES."
   "Generate name of NODE for displaying in Imenu."
   (treesit-node-text node))
 
+;;;###autoload
+(defun typst-ts-mode-compile ()
+  "Compile current typst file to pdf."
+  (interactive)
+  (compile compile-command))
+
+(defun typst-ts-mode-watch ()
+  "Watch(hot compile) current typst file."
+  (interactive)
+  (start-process-shell-command
+   typst-ts-mode-watch-process-name typst-ts-mode-watch-process-buffer-name
+   (format "%s watch %s %s"
+           typst-ts-mode-executable-location
+           (file-name-nondirectory buffer-file-name)
+           typst-ts-mode-watch-options)))
+
+(defun typst-ts-mode-watch-stop ()
+  "Stop watch process."
+  (interactive)
+  (delete-process typst-ts-mode-watch-process-name))
+
+(defun typst-ts-mode-watch-toggle ()
+  "Toggle watch process."
+  (interactive)
+  (if (get-process typst-ts-mode-watch-process-name)
+      (typst-ts-mode-watch-stop)
+    (typst-ts-mode-watch)))
+
+(defvar typst-ts-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "C-c C-c") #'typst-ts-mode-compile)
+    (define-key map (kbd "C-c C-x") #'typst-ts-mode-watch-toggle)
+    map))
+
 ;;;###autoload
 (define-derived-mode typst-ts-mode text-mode "Typst"
   "Major mode for editing Typst, powered by tree-sitter."
@@ -509,6 +565,12 @@ TYPES."
                  typst-ts-mode--imenu-name-function)
                 ("Headings" "^heading$" nil 
typst-ts-mode--imenu-name-function)))
 
+  (setq-local compile-command
+              (format "%s compile %s %s"
+                      typst-ts-mode-executable-location
+                      (file-name-nondirectory buffer-file-name)
+                      typst-ts-mode-compile-options))
+
   (treesit-major-mode-setup))
 
 ;; TODO check consistence with typst-mode

Reply via email to