branch: elpa/typst-ts-mode
commit 1e11d3c74e75d81c3ae437d6c64704f619dec6c6
Author: Huan Thieu Nguyen <nguyenthieuh...@gmail.com>
Commit: Huan Thieu Nguyen <nguyenthieuh...@gmail.com>

    refactor: apply patch #42
    
    https://lists.gnu.org/archive/html/emacs-devel/2025-03/msg01576.html
---
 typst-ts-compile.el                 |  14 +--
 typst-ts-core.el                    |   9 +-
 typst-ts-edit-indirect.el           |   5 +-
 typst-ts-editing.el                 | 175 ++++++++++++++++++------------------
 typst-ts-embedding-lang-settings.el |   5 +-
 typst-ts-faces.el                   |  10 +--
 typst-ts-lsp.el                     |  39 ++++----
 typst-ts-misc-commands.el           |  19 ++--
 8 files changed, 138 insertions(+), 138 deletions(-)

diff --git a/typst-ts-compile.el b/typst-ts-compile.el
index 2acb77e8ca..7b88c75cf7 100644
--- a/typst-ts-compile.el
+++ b/typst-ts-compile.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-compile.el --- Compile Typst Files  -*- lexical-binding: t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
@@ -18,6 +19,7 @@
 ;;; Commentary:
 
 ;;; Code:
+
 (require 'compile)
 (require 'typst-ts-variables)
 
@@ -64,12 +66,10 @@ If BUFFER is nil, it means use the current buffer.
 CHECK: non-nil mean check the file existence.
 Return nil if the BUFFER has not associated file or the there is
 no compiled pdf file when CHECK is non-nil."
-  (when-let* ((typst-file (buffer-file-name buffer)))
+  (and-let* ((typst-file (buffer-file-name buffer)))
     (let ((res (concat (file-name-as-directory typst-ts-output-directory) 
(file-name-base typst-file) ".pdf")))
-      (if check
-          (when (file-exists-p res)
-            res)
-        res))))
+      (and (or (not check) (file-exists-p res))
+           res))))
 
 
 (defun typst-ts-compile-and-preview--compilation-finish-function (cur-buffer)
diff --git a/typst-ts-core.el b/typst-ts-core.el
index adc4653004..bbaf8ca3eb 100644
--- a/typst-ts-core.el
+++ b/typst-ts-core.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-core.el --- core functions for typst-ts-mode -*- lexical-binding: 
t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
@@ -55,7 +56,7 @@
     (point)))
 
 (defun typst-ts-core-line-bol-nonwhite-pos (&optional pos)
-  "POS."
+  "Get the point of beginning of line or first nonwhite character at POS."
   (save-excursion
     (when pos
       (goto-char pos))
@@ -86,6 +87,8 @@ Currently the effect of FN shouldn't change line number."
         (ne-line-num (line-number-at-pos (treesit-node-end node))))
     (save-excursion
       (goto-char ns)
+      ;; TODO, `line-number-at-pos' on every line is expensive
+      ;; rewrite this to narrow the buffer
       (while (< (line-number-at-pos) ne-line-num)
         (funcall fn)
         (forward-line 1))
diff --git a/typst-ts-edit-indirect.el b/typst-ts-edit-indirect.el
index 7c3400dc13..7837d31572 100644
--- a/typst-ts-edit-indirect.el
+++ b/typst-ts-edit-indirect.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-watch-mode.el --- Edit blocks in separate buffer  -*- 
lexical-binding: t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
diff --git a/typst-ts-editing.el b/typst-ts-editing.el
index 8c0fd138fc..08c3d0f5a0 100644
--- a/typst-ts-editing.el
+++ b/typst-ts-editing.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-editing.el --- Helper functions for editing Typst documents -*- 
lexical-binding: t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
@@ -91,7 +92,7 @@ DIRECTION is one of following symbols:
                (seq-elt rows (1- row-index))))
             ('down
              (progn
-               (when (= (length rows) (1+ row-index))
+               (when (length= rows (1+ row-index))
                  (user-error "Already on last row"))
                (seq-elt rows (1+ row-index))))
             (_
@@ -112,83 +113,83 @@ DIRECTION is one of following symbols:
   (interactive)
   (typst-ts-editing-grid-row--move 'up))
 
-  (defun typst-ts-editing-grid-cell--move (direction)
-    "Move grid cell at point depending on DIRECTION up/down, left/right.
+(defun typst-ts-editing-grid-cell--move (direction)
+  "Move grid cell at point depending on DIRECTION up/down, left/right.
 DIRECTION is one of following symbols:
 `left', `right', `up', `down'.
 
 Up/down means moving the cell to another row while keeping the column index."
-    ;; inside table.header is different from the rest
-    (let (grid grid-cells cell to-switch)
-      (seq-setq (grid cell grid-cells) 
(typst-ts-editing-grid-cell--at-point-p))
-      (unless (and grid cell)
-        (user-error "Not inside a grid cell"))
-      (setq to-switch
-            (pcase direction
-              ((guard (and (memq direction '(down up))
-                           (string= "table.header"
-                                    (treesit-node-text
-                                     (treesit-node-child-by-field-name grid 
"item")))))
-               (user-error "A table.header only has one row"))
-              ('left
-               ;; skip the , prev twice
-               (treesit-node-prev-sibling (treesit-node-prev-sibling cell)))
-              ('right
-               ;; skip the , that's why next twice
-               (treesit-node-next-sibling (treesit-node-next-sibling cell)))
-              ((or 'up 'down)
-               (let ((amount-of-columns
-                      (typst-ts-editing-grid--column-number grid))
-                     (select-cell
-                      (lambda (row column)
-                        (seq-elt
-                         (seq-elt
-                          (seq-partition
-                           grid-cells
-                           (typst-ts-editing-grid--column-number grid))
-                          row)
-                         column)))
-                     row column)
-                 (seq-setq (row column)
-                           (typst-ts-editing-grid-cell--index
-                            cell grid-cells amount-of-columns))
-                 (if (eq direction 'up)
-                     (progn
-                       (when (= 0 row)
-                         (user-error "Already on first row"))
-                       (funcall select-cell (1- row) column))
-                   (when (= row amount-of-columns)
-                     (user-error "Already on last row"))
-                   (funcall select-cell (1+ row) column))))
-              (_ (error "DIRECTION: %s is not one of: `right' `left', `up', 
`down'"
-                        direction))))
-      (when (or (not to-switch)
-                (string= "tagged" (treesit-node-type to-switch))
-                (string= "(" (treesit-node-text to-switch))
-                (string= ")" (treesit-node-text to-switch)))
-        (user-error "There is no cell in the %s direction" direction))
-      (transpose-regions (treesit-node-start cell) (treesit-node-end cell)
-                         (treesit-node-start to-switch) (treesit-node-end 
to-switch))))
-
-  (defun typst-ts-editing-grid-cell-down ()
-    "See `typst-ts-editing-grid-cell--move'."
-    (interactive)
-    (typst-ts-editing-grid-cell--move 'down))
-
-  (defun typst-ts-editing-grid-cell-up ()
-    "See `typst-ts-editing-grid-cell--move'."
-    (interactive)
-    (typst-ts-editing-grid-cell--move 'up))
-
-  (defun typst-ts-editing-grid-cell-left ()
-    "See `typst-ts-editing-grid-cell--move'."
-    (interactive)
-    (typst-ts-editing-grid-cell--move 'left))
-
-  (defun typst-ts-editing-grid-cell-right ()
-    "See `typst-ts-editing-grid-cell--move'."
-    (interactive)
-    (typst-ts-editing-grid-cell--move 'right))
+  ;; inside table.header is different from the rest
+  (let (grid grid-cells cell to-switch)
+    (seq-setq (grid cell grid-cells) (typst-ts-editing-grid-cell--at-point-p))
+    (unless (and grid cell)
+      (user-error "Not inside a grid cell"))
+    (setq to-switch
+          (pcase direction
+            ((guard (and (memq direction '(down up))
+                         (string= "table.header"
+                                  (treesit-node-text
+                                   (treesit-node-child-by-field-name grid 
"item")))))
+             (user-error "A table.header only has one row"))
+            ('left
+             ;; skip the , prev twice
+             (treesit-node-prev-sibling (treesit-node-prev-sibling cell)))
+            ('right
+             ;; skip the , that's why next twice
+             (treesit-node-next-sibling (treesit-node-next-sibling cell)))
+            ((or 'up 'down)
+             (let ((amount-of-columns
+                    (typst-ts-editing-grid--column-number grid))
+                   (select-cell
+                    (lambda (row column)
+                      (seq-elt
+                       (seq-elt
+                        (seq-partition
+                         grid-cells
+                         (typst-ts-editing-grid--column-number grid))
+                        row)
+                       column)))
+                   row column)
+               (seq-setq (row column)
+                         (typst-ts-editing-grid-cell--index
+                          cell grid-cells amount-of-columns))
+               (if (eq direction 'up)
+                   (progn
+                     (when (= 0 row)
+                       (user-error "Already on first row"))
+                     (funcall select-cell (1- row) column))
+                 (when (= row amount-of-columns)
+                   (user-error "Already on last row"))
+                 (funcall select-cell (1+ row) column))))
+            (_ (error "DIRECTION: %s is not one of: `right' `left', `up', 
`down'"
+                      direction))))
+    (when (or (not to-switch)
+              (string= "tagged" (treesit-node-type to-switch))
+              (string= "(" (treesit-node-text to-switch))
+              (string= ")" (treesit-node-text to-switch)))
+      (user-error "There is no cell in the %s direction" direction))
+    (transpose-regions (treesit-node-start cell) (treesit-node-end cell)
+                       (treesit-node-start to-switch) (treesit-node-end 
to-switch))))
+
+(defun typst-ts-editing-grid-cell-down ()
+  "See `typst-ts-editing-grid-cell--move'."
+  (interactive)
+  (typst-ts-editing-grid-cell--move 'down))
+
+(defun typst-ts-editing-grid-cell-up ()
+  "See `typst-ts-editing-grid-cell--move'."
+  (interactive)
+  (typst-ts-editing-grid-cell--move 'up))
+
+(defun typst-ts-editing-grid-cell-left ()
+  "See `typst-ts-editing-grid-cell--move'."
+  (interactive)
+  (typst-ts-editing-grid-cell--move 'left))
+
+(defun typst-ts-editing-grid-cell-right ()
+  "See `typst-ts-editing-grid-cell--move'."
+  (interactive)
+  (typst-ts-editing-grid-cell--move 'right))
 
 (defun typst-ts-editing-grid--at-point-p ()
   "Whether the current point is on a grid/table.
@@ -274,6 +275,7 @@ When there is no columns field or the semantic meaning 
makes no sense return 1."
              (treesit-filter-child columns-value
                                    (lambda (n)
                                      (let ((text (treesit-node-text n)))
+                                       ;; TODO: use a regex instead
                                        (and (not (string= "," text))
                                             (not (string= ":" text))
                                             (not (string= "(" text))
@@ -336,12 +338,11 @@ When point is not on an item node return nil."
 DIRECTION should be `up' or `down'."
   (let* ( previous current next swap-with numbered-p
           (bind (lambda ()
-                  (pcase direction
+                  (pcase-exhaustive direction
                     ('up
                      (setq swap-with previous))
                     ('down
-                     (setq swap-with next))
-                    (_ (error "%s is not one of: `up' `down'" direction))))))
+                     (setq swap-with next))))))
     (seq-setq (previous current next numbered-p)
               (typst-ts-editing-item--with-siblings))
     (unless current
@@ -441,7 +442,7 @@ the `GLOBAL-MAP' (example: `right-word')."
                ((typst-ts-editing-grid-cell--at-point-p) "grid-cell")
                (t nil)))
          (end
-          (pcase direction
+          (pcase-exhaustive direction
             ('left
              "-left")
             ('right
@@ -449,9 +450,7 @@ the `GLOBAL-MAP' (example: `right-word')."
             ('up
              "-up")
             ('down
-             "-down")
-            (_ (error "DIRECTION: %s is not one of: `right' `left', `up', 
`down'"
-                      direction)))))
+             "-down"))))
     (if (not mid)
         (keymap-lookup global-map (substitute-command-keys
                                    (concat "\\[" prefix "meta" end "]")))
@@ -587,7 +586,6 @@ When there is no section it will insert a heading below 
point."
          (insert-tab)
          'success)
 
-
         ((setq node
                (typst-ts-core-parent-util-type
                 cur-line-nonwhite-bol-node "item" t t))
@@ -638,6 +636,7 @@ When there is no section it will insert a heading below 
point."
   ;; see `do-auto-fill' function and `;; Choose a fill-prefix automatically.'
   ;; for default automatical fill-prefix finding algorithm
   (let (fill-prefix)
+    ;; TODO: why is the variable bound and then returned?
     (setq
      fill-prefix
      (catch 'fill-prefix
@@ -651,10 +650,10 @@ When there is no section it will insert a heading below 
point."
 
 (defun typst-ts-editing-auto-fill-function ()
   "Auto Fill Function for `auto-fill-mode'."
-  (when (>= (current-column) (current-fill-column))
-    (let* ((fill-prefix (typst-ts-editing-calculate-fill-prefix))
-           (adaptive-fill-mode (null fill-prefix)))
-      (when fill-prefix (do-auto-fill)))))
+  (when-let* (((>= (current-column) (current-fill-column)))
+              (fill-prefix (typst-ts-editing-calculate-fill-prefix))
+              (adaptive-fill-mode (null fill-prefix)))
+    (do-auto-fill)))
 
 (defun typst-ts-editing-symbol-picker ()
   "Insert typst symbols.
diff --git a/typst-ts-embedding-lang-settings.el 
b/typst-ts-embedding-lang-settings.el
index 1b694bd7fc..10a271a0b0 100644
--- a/typst-ts-embedding-lang-settings.el
+++ b/typst-ts-embedding-lang-settings.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-embedding-lang-settings.el --- Embedding Languages Settings  -*- 
lexical-binding: t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
diff --git a/typst-ts-faces.el b/typst-ts-faces.el
index b3ea4e4110..f4d436e5b1 100644
--- a/typst-ts-faces.el
+++ b/typst-ts-faces.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-faces.el --- typst-ts-mode faces  -*- lexical-binding: t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
@@ -26,14 +27,13 @@
   :prefix "typst-ts-faces"
   :group 'typst-ts)
 
-
 (defcustom typst-ts-markup-header-same-height t
   "Whether to make header face in markup context share the same height."
   :type 'boolean)
 
 (defcustom typst-ts-markup-header-scale
   '(2.0 1.7 1.4 1.1 1.0 1.0)
-  "Header Scale."
+  "Header Scale for each header level, starting from = to ======."
   :type '(list number number number number number number))
 
 ;; Face  
=========================================================================
@@ -283,8 +283,6 @@
   "Face used for the script chars ^ and _."
   :group 'typst-ts-faces)
 
-
-
 (provide 'typst-ts-faces)
 
 ;;; typst-ts-faces.el ends here
diff --git a/typst-ts-lsp.el b/typst-ts-lsp.el
index 4068568c3a..4eb78475d7 100644
--- a/typst-ts-lsp.el
+++ b/typst-ts-lsp.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-lsp.el --- Eglot tinymist integration  -*- lexical-binding: t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
@@ -32,12 +33,12 @@
   :prefix "typst-ts-compile"
   :group 'typst-ts)
 
-(defcustom typst-ts-lsp-download-path (file-name-concat 
(locate-user-emacs-file ".cache") "lsp" "tinymist" "tinymist")
+(defcustom typst-ts-lsp-download-path
+  (file-name-concat (locate-user-emacs-file ".cache")
+                    "lsp" "tinymist" "tinymist")
   "Install path for the language server."
-  :group 'tools
-  :group 'typst-ts-lsp
-  :type 'file)
-
+  :type 'file
+  :group 'typst-ts-lsp)
 
 ;;;###autoload
 (defun typst-ts-lsp-download-binary ()
@@ -46,18 +47,18 @@ Will override old versions."
   (interactive)
   (unless (file-exists-p typst-ts-lsp-download-path)
     (make-directory (file-name-directory typst-ts-lsp-download-path) t))
-  (url-copy-file
-   (concat 
"https://github.com/Myriad-Dreamin/tinymist/releases/latest/download/tinymist-";
-           (pcase system-type
-             ('gnu/linux "linux")
-             ('darwin "darwin")
-             ('windows-nt "win32")
-             (_ "linux"))
-           ;; TODO too lazy to find out all the arch suffixes
-           "-x64")
-   typst-ts-lsp-download-path t)
-  (set-file-modes typst-ts-lsp-download-path
-                  (logior (file-modes typst-ts-lsp-download-path) #o100)))
+  (with-file-modes (logior (file-modes typst-ts-lsp-download-path) #o100)
+    (url-copy-file
+     (concat
+      
"https://github.com/Myriad-Dreamin/tinymist/releases/latest/download/tinymist-";
+      (pcase system-type
+        ('gnu/linux "linux")
+        ('darwin "darwin")
+        ('windows-nt "win32")
+        (_ "linux"))
+      ;; TODO too lazy to find out all the arch suffixes
+      "-x64")
+     typst-ts-lsp-download-path t)))
 
 (provide 'typst-ts-lsp)
 ;;; typst-ts-lsp.el ends here
diff --git a/typst-ts-misc-commands.el b/typst-ts-misc-commands.el
index 0d469608cc..526fc5e2a3 100644
--- a/typst-ts-misc-commands.el
+++ b/typst-ts-misc-commands.el
@@ -1,7 +1,8 @@
 ;;; typst-ts-misc-commands.el --- Miscellaneous commands for typst-ts-mode -*- 
lexical-binding: t; -*-
-;; Copyright (C) 2023-2024 The typst-ts-mode Project Contributors
 
-;; This file is NOT part of Emacs.
+;; Copyright (C) 2023-2025 The typst-ts-mode Project Contributors
+
+;; This file is NOT part of GNU Emacs.
 ;; This program is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
@@ -23,17 +24,12 @@
 
 (require 'treesit)
 
-;; (defgroup typst-ts-mc nil
-;;   "Typst ts miscellaneous commands."
-;;   :prefix "typst-ts-misc-commands"
-;;   :group 'typst-ts)
-
 (defun typst-ts-mc-install-grammar ()
   "Install Typst grammar."
   (interactive)
-  (let ((treesit-language-source-alist treesit-language-source-alist))
-    (add-to-list 'treesit-language-source-alist
-                 '(typst "https://github.com/Ziqi-Yang/tree-sitter-typst";))
+  (let ((treesit-language-source-alist
+         (cons '(typst "https://github.com/Ziqi-Yang/tree-sitter-typst";)
+               treesit-language-source-alist)))
     (treesit-install-language-grammar 'typst)))
 
 
@@ -43,6 +39,7 @@ Require pandoc to be installed."
   (interactive)
 
   ;; for simplicity
+  ;; TODO: suggest saving the buffer
   (unless buffer-file-name
     (user-error "You should save the file first!"))
 
@@ -54,6 +51,7 @@ Require pandoc to be installed."
          (output-file-name
           (file-name-with-extension file-name "md"))
          (buffer-name (format "*pandoc %s*" file-name)))
+    ;; TODO: check if installed pandoc supports typst
     (start-process "pandoc"
                    buffer-name
                    "pandoc" "-o" output-file-name file-name)
@@ -74,7 +72,6 @@ Require pandoc to be installed."
   (interactive)
   (browse-url "https://typst.app/universe";))
 
-
 (provide 'typst-ts-misc-commands)
 
 ;;; typst-ts-misc-commands.el ends here

Reply via email to