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

    refactor: Refactored heading increase/decrease functions.
---
 README.md        |   4 +-
 typst-ts-mode.el | 113 ++++++++++++++++++++++++++++++++-----------------------
 2 files changed, 67 insertions(+), 50 deletions(-)

diff --git a/README.md b/README.md
index c3fb7264cb..81f3bcc1e7 100644
--- a/README.md
+++ b/README.md
@@ -79,8 +79,8 @@ For reference, this is my configuration.
 `C-c C-c C` : `typst-ts-mode-compile`  
 `C-c C-c w` : `typst-ts-mode-watch-toggle`  
 `C-c C-c p` : `typst-ts-mode-preview`  
-`M-<left>`  : `typst-ts-mode-shift-heading-left`  
-`M-<right>` : `typst-ts-mode-shift-heading-right`  
+`M-<left>`  : `typst-ts-mode-heading-decrease`
+`M-<right>` : `typst-ts-mode-heading-increase`
 
 ## Customization Options
 
diff --git a/typst-ts-mode.el b/typst-ts-mode.el
index ad05a4da54..f83eae6a7d 100644
--- a/typst-ts-mode.el
+++ b/typst-ts-mode.el
@@ -31,6 +31,7 @@
 
 (require 'treesit)
 (require 'compile)
+(require 'subr-x)
 
 (defgroup typst-ts nil
   "Tree Sitter enabled Typst Writing."
@@ -124,7 +125,7 @@ is eliminated."
 (defcustom typst-ts-markup-header-scale
   '(2.0 1.7 1.4 1.1 1.0 1.0)
   "Header Scale."
-  :type 'list
+  :type '(list integer integer integer integer integer integer)
   :set (lambda (symbol value)
          (set-default symbol value)
          (when typst-ts-markup-header-same-height
@@ -567,64 +568,80 @@ buffer before compilation."
       (remove-hook 'compilation-finish-functions
                    (typst-ts-mode-compile--compilation-finish-function 
cur-buffer)))))
 
-(defun typst-ts-mode-meta--dwim (right-p)
-  "Either increase/decrease heading level or move by word.
-RIGHT-P non-nil for increasing heading level, RIGHT-P nil for decreasing."
-  (let ((node nil)
-       (tmp nil))
-    (setq node
-         ;; = header
-         (cond
-          ((string=
-            (treesit-node-type
-             (setq tmp (treesit-node-parent
-                        (treesit-node-at (point)))))
-            "heading")
-           (treesit-node-child tmp 0))
-          ;; = header with point at the end of line
-          ((string=
-            (treesit-node-type
-             (setq tmp (treesit-node-parent
-                        (treesit-node-at (line-beginning-position)))))
-            "heading")
-           (treesit-node-child tmp 0))
-          (t nil)))
-    (if node
-       (typst-ts-mode-shift--heading right-p node)
-      (execute-kbd-macro
-       (if right-p
-          (read-kbd-macro "M-<right>")
-        (read-kbd-macro "M-<left>"))))))
-
-(defun typst-ts-mode-shift--heading (right-p node)
+(defun typst-ts-mode-heading--at-point-p ()
+  "Is thing at point a heading?
+Return the heading node when yes otherwise nil."
+  (let ((node nil))
+    ;; = header
+    (cond
+     ((string=
+       (treesit-node-type
+       (setq node
+             (treesit-node-parent
+              (treesit-node-at (point)))))
+       "heading")
+      node)
+     ;; = header with point at the end of line
+     ((string=
+       (treesit-node-type
+       (setq node (treesit-node-parent
+                   (treesit-node-at (line-beginning-position)))))
+       "heading")
+      node)
+     (t nil))))
+
+(defun typst-ts-mode-heading--increase/decrease (direction node)
   "Increase or decrease the heading level.
-RIGHT-P nil means increase the level while RIGHT-P non-nil means decrease.
+DIRECTION right means increase the level while DIRECTION right means decrease.
 NODE is the heading node.
 This does not handle #heading function."
-  (let* ((heading-string "")
-        (heading-level 0))
+  (let ((heading-string "")
+       (heading-level 0))
     (setq heading-level
          (length (setq heading-string (treesit-node-text node))))
-    (when (and (= heading-level 1) (not right-p))
+    (when (and (= heading-level 1) (eq direction 'left))
       (user-error "Cannot decrease level 1 heading"))
-    (save-excursion (replace-string-in-region heading-string
-                                             (if right-p
-                                                 (concat heading-string "=")
-                                                 (substring-no-properties 
heading-string 1 heading-level))
-                                             (treesit-node-start node)
-                                             (treesit-node-end node)))))
+    (replace-region-contents
+     (treesit-node-start node)
+     (treesit-node-end node)
+     (lambda ()
+       (pcase direction
+        ('right (concat heading-string "="))
+        ('left (substring-no-properties heading-string 1 heading-level))
+        (_ (error "%s is not one of: `LEFT' `RIGHT'" direction)))))))
+
+(defun typst-ts-mode-meta--dwim (direction)
+  "Do something depending on the context with meta key + DIRECTION.
+`left': `typst-ts-mode-heading-decrease'
+`right': `typst-ts-mode-heading-increase'
+When there is no relevant action to do it will execute the relevant function in
+the `GLOBAL-MAP' (example: `right-word')."
+  (let ((heading (typst-ts-mode-heading--at-point-p))
+       (key nil))
+    (if heading
+       (typst-ts-mode-heading--increase/decrease direction (treesit-node-child 
heading 0))
+      (progn
+       (setq key
+             (substitute-command-keys
+              (concat
+               "\\[typst-ts-mode-heading-"
+               (pcase direction
+                 ('left "decrease]")
+                 ('right "increase]")
+                 (_ (error "%s is not one of: `RIGHT' `LEFT'" direction))))))
+       (call-interactively (keymap-lookup global-map key))))))
+
 ;;;###autoload
-(defun typst-ts-mode-shift-heading-right ()
+(defun typst-ts-mode-heading-increase ()
   "Increase the heading level."
   (interactive)
-  (typst-ts-mode-meta--dwim t))
+  (typst-ts-mode-meta--dwim 'right))
   
 ;;;###autoload
-(defun typst-ts-mode-shift-heading-left ()
+(defun typst-ts-mode-heading-decrease ()
   "Decrease heading level."
   (interactive)
-  (typst-ts-mode-meta--dwim nil))
-
+  (typst-ts-mode-meta--dwim 'left))
 
 ;;;###autoload
 (defun typst-ts-mode-compile ()
@@ -790,8 +807,8 @@ PROC: process; OUTPUT: new output from PROC."
     (define-key map (kbd "C-c C-c C") #'typst-ts-mode-compile)
     (define-key map (kbd "C-c C-c w") #'typst-ts-mode-watch-toggle)
     (define-key map (kbd "C-c C-c p") #'typst-ts-mode-preview)
-    (define-key map (kbd "M-<left>") #'typst-ts-mode-shift-heading-left)
-    (define-key map (kbd "M-<right>") #'typst-ts-mode-shift-heading-right)
+    (define-key map (kbd "M-<left>") #'typst-ts-mode-heading-decrease)
+    (define-key map (kbd "M-<right>") #'typst-ts-mode-heading-increase)
     map))
 
 ;;;###autoload

Reply via email to