branch: elpa/typst-ts-mode commit ebc84e4f651e619da6add5365b49a04bc5fb1902 Author: Huan Thieu Nguyen <nguyenthieuh...@gmail.com> Commit: Huan Thieu Nguyen <nguyenthieuh...@gmail.com>
chore: only use typst-ts as prefix #42 Also remove own swap function for built-in transpose-regions. --- typst-ts-compile.el | 8 +-- typst-ts-core.el | 6 +- typst-ts-editing.el | 188 +++++++++++++++++++++----------------------------- typst-ts-mode.el | 152 ++++++++++++++++++++-------------------- typst-ts-symbols.el | 4 +- typst-ts-transient.el | 2 +- typst-ts-variables.el | 16 ++--- 7 files changed, 172 insertions(+), 204 deletions(-) diff --git a/typst-ts-compile.el b/typst-ts-compile.el index b070b78eef..2acb77e8ca 100644 --- a/typst-ts-compile.el +++ b/typst-ts-compile.el @@ -41,7 +41,7 @@ When using a prefix argument or the optional argument PREVIEW, (interactive "P") (when preview (add-hook 'compilation-finish-functions - (typst-ts-mode-compile-and-preview--compilation-finish-function + (typst-ts-compile-and-preview--compilation-finish-function (current-buffer)))) (run-hooks typst-ts-compile-before-compilation-hook) @@ -72,7 +72,7 @@ no compiled pdf file when CHECK is non-nil." res)))) -(defun typst-ts-mode-compile-and-preview--compilation-finish-function (cur-buffer) +(defun typst-ts-compile-and-preview--compilation-finish-function (cur-buffer) "For `typst-ts-compile-and-preview' and `compilation-finish-functions'. CUR-BUFFER: original typst buffer, in case user set `display-buffer-alist' option for compilation buffer to switch to compilation @@ -81,7 +81,7 @@ buffer before compilation." (unwind-protect (typst-ts-preview cur-buffer) (remove-hook 'compilation-finish-functions - (typst-ts-mode-compile-and-preview--compilation-finish-function cur-buffer))))) + (typst-ts-compile-and-preview--compilation-finish-function cur-buffer))))) ;;;###autoload (defun typst-ts-compile-and-preview () @@ -95,7 +95,7 @@ Assuming the compile output file name is in default style." "Preview the typst document output. If BUFFER is passed, preview its output, otherwise use current buffer." (interactive) - (funcall typst-ts-mode-preview-function (typst-ts-compile-get-result-pdf-filename buffer))) + (funcall typst-ts-preview-function (typst-ts-compile-get-result-pdf-filename buffer))) (defvar typst-ts-compilation-mode-error (cons (rx bol "error:" (+ not-newline) "\n" (+ blank) "┌─ " diff --git a/typst-ts-core.el b/typst-ts-core.el index b5ab461e23..adc4653004 100644 --- a/typst-ts-core.el +++ b/typst-ts-core.el @@ -27,7 +27,7 @@ ;; code from Emacs binary (declare-function treesit-parser-list "treesit" t t) -(defcustom typst-ts-mode-indent-offset 4 +(defcustom typst-ts-indent-offset 4 "Number of spaces for each indentation step in `typst-ts-mode'." :type 'natnum :group 'typst-ts) @@ -37,7 +37,7 @@ ;; TODO file a bug '("block" "content" "group" "math" "_math_group")) -(defconst typst-ts-mode--container-node-types-regexp +(defconst typst-ts--container-node-types-regexp (regexp-opt typst-ts-core--container-node-types) "Container node types regexp.") @@ -113,7 +113,7 @@ NODE TYPE INCLUDE-NODE see `treesit-parent-until'." (let ((node-type (treesit-node-type node))) (or (and same-context (string-match-p - typst-ts-mode--container-node-types-regexp node-type)) + typst-ts--container-node-types-regexp node-type)) (string-match-p type node-type)))) include-node))) (when (and matched-node diff --git a/typst-ts-editing.el b/typst-ts-editing.el index aa03c5a369..f69d968cff 100644 --- a/typst-ts-editing.el +++ b/typst-ts-editing.el @@ -28,27 +28,27 @@ (require 'typst-ts-symbols) (require 'seq) -(defun typst-ts-mode-heading-up () +(defun typst-ts-editing-heading-up () "Switch the current heading with the heading above." (interactive) (call-interactively #'outline-move-subtree-up)) -(defun typst-ts-mode-heading-down () +(defun typst-ts-editing-heading-down () "Switch the current heading with the heading below." (interactive) (call-interactively #'outline-move-subtree-down)) -(defun typst-ts-mode-heading-left () +(defun typst-ts-editing-heading-left () "Increase the heading level." (interactive) (call-interactively #'outline-promote)) -(defun typst-ts-mode-heading-right () +(defun typst-ts-editing-heading-right () "Decrease heading level." (interactive) (call-interactively #'outline-demote)) -(defun typst-ts-mode-heading--at-point-p () +(defun typst-ts-editing-heading--at-point-p () "Whether the current line is a heading. Return the heading node when yes otherwise nil." (let ((node (treesit-node-parent @@ -60,7 +60,7 @@ Return the heading node when yes otherwise nil." node nil))) -(defun typst-ts-mode-grid-cell--index (cell grid-cells amount-of-columns) +(defun typst-ts-editing-grid-cell--index (cell grid-cells amount-of-columns) "Return a list in form of (row-index column-index) of CELL in GRID-CELLS. AMOUNT-OF-COLUMNS specifies how many columns one row has. Indeces are given in 0 index." @@ -68,17 +68,17 @@ Indeces are given in 0 index." (list (/ index amount-of-columns) (mod index amount-of-columns)))) -(defun typts-ts-mode-grid-row--move (direction) +(defun typst-ts-grid-row--move (direction) "Move grid row at point depending on DIRECTION up/down. DIRECTION is one of following symbols: `up', `down'." (let (to-switch current grid grid-cells row-index rows amount-of-columns cell) - (seq-setq (grid cell grid-cells) (typst-ts-mode-grid-cell--at-point-p)) + (seq-setq (grid cell grid-cells) (typst-ts-editing-grid-cell--at-point-p)) (unless (and grid cell) (user-error "Not inside a grid with rows")) - (setq amount-of-columns (typst-ts-mode-grid--column-number grid)) + (setq amount-of-columns (typst-ts-editing-grid--column-number grid)) (setq row-index - (car (typst-ts-mode-grid-cell--index + (car (typst-ts-editing-grid-cell--index cell grid-cells amount-of-columns))) (setq rows (seq-partition grid-cells amount-of-columns)) (setq current (seq-elt rows row-index)) @@ -100,9 +100,9 @@ DIRECTION is one of following symbols: (end1 (treesit-node-end (car (last current)))) (start2 (treesit-node-start (car to-switch))) (end2 (treesit-node-end (car (last to-switch))))) - (typst-ts-mode--swap-regions start1 end1 start2 end2)))) + (transpose-regions start1 end1 start2 end2)))) -(defun typst-ts-mode-grid-cell--move (direction) +(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'. @@ -110,7 +110,7 @@ DIRECTION is one of following symbols: 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-mode-grid-cell--at-point-p)) + (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 @@ -128,19 +128,19 @@ Up/down means moving the cell to another row while keeping the column index." (treesit-node-next-sibling (treesit-node-next-sibling cell))) ((or 'up 'down) (let ((amount-of-columns - (typst-ts-mode-grid--column-number grid)) + (typst-ts-editing-grid--column-number grid)) (select-cell (lambda (row column) (seq-elt (seq-elt (seq-partition grid-cells - (typst-ts-mode-grid--column-number grid)) + (typst-ts-editing-grid--column-number grid)) row) column))) row column) (seq-setq (row column) - (typst-ts-mode-grid-cell--index + (typst-ts-editing-grid-cell--index cell grid-cells amount-of-columns)) (if (eq direction 'up) (progn @@ -157,10 +157,10 @@ Up/down means moving the cell to another row while keeping the column index." (string= "(" (treesit-node-text to-switch)) (string= ")" (treesit-node-text to-switch))) (user-error "There is no cell in the %s direction" direction)) - (typst-ts-mode--swap-regions (treesit-node-start cell) (treesit-node-end cell) - (treesit-node-start to-switch) (treesit-node-end to-switch)))) + (transpose-regions (treesit-node-start cell) (treesit-node-end cell) + (treesit-node-start to-switch) (treesit-node-end to-switch)))) -(defun typst-ts-mode-grid--at-point-p () +(defun typst-ts-editing-grid--at-point-p () "Whether the current point is on a grid/table. Return the call node if yes, otherwise return nil." (treesit-parent-until @@ -175,14 +175,14 @@ Return the call node if yes, otherwise return nil." (string= "table.header" ident))))) t)) -(defun typst-ts-mode-grid-cell--at-point-p () +(defun typst-ts-editing-grid-cell--at-point-p () "Whether the current point is on a grid cell or not. Return a list (grid-node cell-node grid-cells) if yes, otherwise return nil." ;; A grid cell is a node inside a grid node that is not a tagged node. (let* ((node (treesit-node-at (point))) (node-begin (treesit-node-start node)) (node-end (treesit-node-end node)) - (inside-grid-p (typst-ts-mode-grid--at-point-p)) + (inside-grid-p (typst-ts-editing-grid--at-point-p)) (grid-cells (treesit-filter-child ;; the child number 1 is the argument list @@ -211,7 +211,7 @@ Return a list (grid-node cell-node grid-cells) if yes, otherwise return nil." (when (and inside-grid-p cell-at-point) (list inside-grid-p cell-at-point grid-cells)))) -(defun typst-ts-mode-grid--column-number (node) +(defun typst-ts-editing-grid--column-number (node) "Return the number of columns the grid has. NODE must be a call node with ident being grid or table. When there is no columns field or the semantic meaning makes no sense return 1." @@ -252,7 +252,7 @@ When there is no columns field or the semantic meaning makes no sense return 1." (t (setq column-number 1))) column-number)) -(defun typst-ts-mode-item--at-point-p () +(defun typst-ts-editing-item--at-point-p () "Return item node when point is on item. Otherwise nil." (treesit-parent-until @@ -264,7 +264,7 @@ Otherwise nil." (lambda (x) (string= (treesit-node-type x) "item")))) -(defun typst-ts-mode-item--with-siblings () +(defun typst-ts-editing-item--with-siblings () "Return (prev current next numbered-p) items. The last item in the last tells you if the list is numbered (t) or not (nil). @@ -277,7 +277,7 @@ Being a different item type does not count as sibling, ex: - bar When point is not on an item node return nil." - (when-let* ((node (typst-ts-mode-item--at-point-p)) + (when-let* ((node (typst-ts-editing-item--at-point-p)) (get-item-type (lambda (x) (treesit-node-text (treesit-node-child x 0)))) (item-type (funcall get-item-type node)) @@ -301,39 +301,7 @@ When point is not on an item node return nil." (funcall only-if (treesit-node-next-sibling node)) node-numbered-p))))) -(defun typst-ts-mode--swap-regions (start1 end1 start2 end2) - "Swap region between START1 and END1 with region between START2 and END2. -START1 END1 is the region where the point should be after swapping." - (let ((text1 (buffer-substring start1 end1)) - (text2 (buffer-substring start2 end2)) - (marker1-start (make-marker)) - (marker1-end (make-marker)) - (marker2-start (make-marker)) - (marker2-end (make-marker)) - (point (point))) - (set-marker marker1-start start1) - (set-marker marker1-end end1) - (set-marker marker2-start start2) - (set-marker marker2-end end2) - (delete-region marker1-start marker1-end) - (delete-region marker2-start marker2-end) - - (goto-char marker1-start) - (insert text2) - - (goto-char marker2-start) - (insert text1) - ;; move point to original position if possible - (when (and (<= start1 point) - (>= end1 point)) - (forward-char (- point end1))) - ;; clean markers - (set-marker marker1-start nil) - (set-marker marker1-end nil) - (set-marker marker2-start nil) - (set-marker marker2-end nil))) - -(defun typst-ts-mode-item--move (direction) +(defun typst-ts-editing-item--move (direction) "Move item node up or down (swap). DIRECTION should be `up' or `down'." (let* ( previous current next swap-with numbered-p @@ -345,7 +313,7 @@ DIRECTION should be `up' or `down'." (setq swap-with next)) (_ (error "%s is not one of: `up' `down'" direction)))))) (seq-setq (previous current next numbered-p) - (typst-ts-mode-item--with-siblings)) + (typst-ts-editing-item--with-siblings)) (unless current (error "Point is not on an item")) (funcall bind) @@ -361,70 +329,70 @@ DIRECTION should be `up' or `down'." (other-begin (treesit-node-start number2)) (other-end (treesit-node-end number2))) (save-excursion - (typst-ts-mode--swap-regions current-begin current-end - other-begin other-end)))) + (transpose-regions current-begin current-end + other-begin other-end)))) ;; the nodes must be reinitialized (seq-setq (previous current next numbered-p) - (typst-ts-mode-item--with-siblings)) + (typst-ts-editing-item--with-siblings)) (funcall bind) (let ((current-begin (treesit-node-start current)) (current-end (treesit-node-end current)) (other-begin (treesit-node-start swap-with)) (other-end (treesit-node-end swap-with)) (column (current-column))) - (typst-ts-mode--swap-regions current-begin current-end - other-begin other-end) + (transpose-regions current-begin current-end + other-begin other-end) (move-to-column column)))) -(defun typst-ts-mode-item-up () +(defun typst-ts-editing-item-up () "Move the item at point up." (interactive) - (typst-ts-mode-item--move 'up)) + (typst-ts-editing-item--move 'up)) -(defun typst-ts-mode-item-down () +(defun typst-ts-editing-item-down () "Move the item at point down." (interactive) - (typst-ts-mode-item--move 'down)) + (typst-ts-editing-item--move 'down)) -(defun typst-ts-mode-meta-up () - "See `typst-ts-mode-meta--dwim'." +(defun typst-ts-editing-meta-up () + "See `typst-ts-editing-meta--dwim'." (interactive) - (call-interactively (typst-ts-mode-meta--dwim 'up))) + (call-interactively (typst-ts-editing-meta--dwim 'up))) -(defun typst-ts-mode-meta-down () - "See `typst-ts-mode-meta--dwim'." +(defun typst-ts-editing-meta-down () + "See `typst-ts-editing-meta--dwim'." (interactive) - (call-interactively (typst-ts-mode-meta--dwim 'down))) + (call-interactively (typst-ts-editing-meta--dwim 'down))) -(defun typst-ts-mode-meta-left () - "See `typst-ts-mode-meta--dwim'." +(defun typst-ts-editing-meta-left () + "See `typst-ts-editing-meta--dwim'." (interactive) - (call-interactively (typst-ts-mode-meta--dwim 'left))) + (call-interactively (typst-ts-editing-meta--dwim 'left))) -(defun typst-ts-mode-meta-right () - "See `typst-ts-mode-meta--dwim'." +(defun typst-ts-editing-meta-right () + "See `typst-ts-editing-meta--dwim'." (interactive) - (call-interactively (typst-ts-mode-meta--dwim 'right))) + (call-interactively (typst-ts-editing-meta--dwim 'right))) -(defun typst-ts-mode-meta--dwim (direction) +(defun typst-ts-editing-meta--dwim (direction) "Return function depending on the context with meta key + DIRECTION. When point is at heading: -`left': `typst-ts-mode-heading-decrease', -`right': `typst-ts-mode-heading-increase', -`up': `typst-ts-mode-heading-up', -`down': `typst-ts-mode-heading-down'. +`left': `typst-ts-editing-heading-decrease', +`right': `typst-ts-editing-heading-increase', +`up': `typst-ts-editing-heading-up', +`down': `typst-ts-editing-heading-down'. When point is at item list: -`up': `typst-ts-mode-item-up' -`down': `typst-ts-mode-item-down' +`up': `typst-ts-editing-item-up' +`down': `typst-ts-editing-item-down' When there is no relevant action to do it will return the relevant function in the `GLOBAL-MAP' (example: `right-word')." - (let* ((prefix "typst-ts-mode-") + (let* ((prefix "typst-ts-editing-") (mid (cond - ((typst-ts-mode-heading--at-point-p) "heading") - ((and (typst-ts-mode-item--at-point-p) + ((typst-ts-editing-heading--at-point-p) "heading") + ((and (typst-ts-editing-item--at-point-p) ;; does not exist, maybe will exist at some point (not (or (eq 'left direction) (eq 'right direction)))) @@ -447,7 +415,7 @@ the `GLOBAL-MAP' (example: `right-word')." (concat "\\[" prefix "meta" end "]"))) (intern-soft (concat prefix mid end))))) -(defun typst-ts-mode-meta-return (&optional arg) +(defun typst-ts-editing-meta-return (&optional arg) "Depending on context, insert a heading or insert an item. The new heading is created after the ending of current heading. Using ARG argument will ignore the context and it will insert a heading instead." @@ -458,22 +426,22 @@ Using ARG argument will ignore the context and it will insert a heading instead. (string= "item" (treesit-node-type node))))) (node (typst-ts-core-get-parent-of-node-at-bol-nonwhite))) (cond - (arg (typst-ts-mode-insert--heading nil)) + (arg (typst-ts-editing-insert--heading nil)) (item-node - (typst-ts-mode-insert--item item-node)) + (typst-ts-editing-insert--item item-node)) (t - (typst-ts-mode-insert--heading node))))) + (typst-ts-editing-insert--heading node))))) -(defun typst-ts-mode-return (&optional arg) +(defun typst-ts-editing-return (&optional arg) "RET behavior depending context. -Can be turned off by setting `typst-ts-mode-electric-return' to nil. +Can be turned off by setting `typst-ts-electric-return' to nil. When point is on end of line of a list item with content, it will insert a list item without content on the next line. When point is on a list item without content, it will delete the list item. -When using prefix argument ARG, `typst-ts-mode-electric-return' is nil, +When using prefix argument ARG, `typst-ts-electric-return' is nil, or no special context, call global RET function" (interactive "P") (let ((default-call @@ -492,14 +460,14 @@ When using prefix argument ARG, `typst-ts-mode-electric-return' is nil, (typst-ts-core-get-parent-of-node-at-bol-nonwhite) "item" t t))) (cond - ((or (not typst-ts-mode-electric-return) arg) (funcall default-call)) + ((or (not typst-ts-electric-return) arg) (funcall default-call)) ((and node (eolp)) (if (> (treesit-node-child-count node) 1) - (typst-ts-mode-insert--item node) + (typst-ts-editing-insert--item node) (delete-region (treesit-node-start node) (treesit-node-end node)))) (t (funcall default-call))))) -(defun typst-ts-mode-insert--item (node) +(defun typst-ts-editing-insert--item (node) "Insert an item after NODE. NODE must be an item node! This function respects indentation." @@ -518,7 +486,7 @@ This function respects indentation." (concat (number-to-string (1+ item-number)) ".")) " "))) -(defun typst-ts-mode-insert--heading (node) +(defun typst-ts-editing-insert--heading (node) "Insert a heading after the section that NODE is part of. When there is no section it will insert a heading below point." (let* ((section @@ -558,7 +526,7 @@ When there is no section it will insert a heading below point." (typst-ts-core-line-bol-nonwhite-pos)) offset)))))) -(defun typst-ts-mode-cycle (&optional _arg) +(defun typst-ts-editing-cycle (&optional _arg) "Cycle." (interactive "P") (let (node) @@ -610,13 +578,13 @@ When there is no section it will insert a heading below point." (offset (- cur-item-node-start-column prev-item-node-start-column))) - (if (>= offset typst-ts-mode-indent-offset) + (if (>= offset typst-ts-editing-indent-offset) (typst-ts-editing--indent-item-node-lines cur-item-node - (- (+ offset typst-ts-mode-indent-offset))) + (- (+ offset typst-ts-editing-indent-offset))) (typst-ts-editing--indent-item-node-lines cur-item-node - (- typst-ts-mode-indent-offset (abs offset))))) + (- typst-ts-editing-indent-offset (abs offset))))) 'success))))) ;; execute default action if not successful @@ -645,15 +613,15 @@ When there is no section it will insert a heading below point." (adaptive-fill-mode (null fill-prefix))) (when fill-prefix (do-auto-fill))))) -(defun typst-ts-mode-symbol-picker () - "Insert elements from `typst-ts-mode-symbol-alist' `typst-ts-mode-emoji-alist'. +(defun typst-ts-editing-symbol-picker () + "Insert elements from `typst-ts-editing-symbol-alist' `typst-ts-editing-emoji-alist'. In markup mode, it will prefix the selection with \"#\" and its corresponding module (\"sym.\", \"emoji.\"). In math mode, symbols do not need a \"#\" prefix and \"sym.\" prefix. In code mode, the selection needs to be prefixed with the module." (interactive) - (let* ((all-symbols (append typst-ts-mode-symbol-alist typst-ts-mode-emoji-alist)) + (let* ((all-symbols (append typst-ts-editing-symbol-alist typst-ts-editing-emoji-alist)) (completion-extra-properties '(:annotation-function (lambda (key) @@ -673,8 +641,8 @@ In code mode, the selection needs to be prefixed with the module." "code") (string= (treesit-node-type x) "content"))))) - (is-symbol-p (assoc value typst-ts-mode-symbol-alist)) - (is-emoji-p (assoc value typst-ts-mode-emoji-alist)) + (is-symbol-p (assoc value typst-ts-editing-symbol-alist)) + (is-emoji-p (assoc value typst-ts-editing-emoji-alist)) (to-insert value)) (cond ((string= (treesit-node-type inside-code) "code") diff --git a/typst-ts-mode.el b/typst-ts-mode.el index 6e1ac85dbb..d21867798d 100644 --- a/typst-ts-mode.el +++ b/typst-ts-mode.el @@ -49,7 +49,7 @@ ;; ============================================================================== ;; TODO typst has three modes (namely 'markup', 'code' and 'math') ;; Currently only add common settings to syntax table -(defvar typst-ts-mode-syntax-table +(defvar typst-ts-syntax-table (let ((st (make-syntax-table))) ;; comment (modify-syntax-entry ?/ ". 124b" st) @@ -57,7 +57,7 @@ (modify-syntax-entry ?\n "> b" st) st)) -(defvar typst-ts-mode-font-lock-settings +(defvar typst-ts-font-lock-settings (treesit-font-lock-rules :language 'typst :feature 'comment @@ -114,8 +114,8 @@ "```" @typst-ts-markup-rawblock-indicator-face (ident) :? @typst-ts-markup-rawblock-lang-face ;; NOTE let embedded language fontify blob - ,@(if typst-ts-mode-enable-raw-blocks-highlight - '((blob) @typst-ts-mode-highlight-raw-block-fn) + ,@(if typst-ts-enable-raw-blocks-highlight + '((blob) @typst-ts-highlight-raw-block-fn) '((blob) @typst-ts-markup-rawblock-blob-face)) "```" @typst-ts-markup-rawblock-indicator-face) (label) @typst-ts-markup-label-face ; TODO more precise highlight (upstream) @@ -197,7 +197,7 @@ :override 'append '((attach ["^" "_"] @typst-ts-script-char-face - (_) @typst-ts-mode-render-math-scripts-fn)) + (_) @typst-ts-render-math-scripts-fn)) :language 'typst :feature 'math-extended @@ -208,10 +208,10 @@ "Font lock rules for `typst-ts-mode'. If you want to enable/disable specific font lock feature, please change -`treesit-font-lock-level' or modify `typst-ts-mode-font-lock-feature-list'.") +`treesit-font-lock-level' or modify `typst-ts-font-lock-feature-list'.") ;; modified from Auctex -(defun typst-ts-mode-unfontify-region (beg end &rest _ignored) +(defun typst-ts-unfontify-region (beg end &rest _ignored) "Unfontify region from BEG to END." (font-lock-default-unfontify-region beg end) (while (< beg end) @@ -223,13 +223,13 @@ If you want to enable/disable specific font lock feature, please change (setq beg next)))) -(defun typst-ts-mode-render-math-scripts-fn (node _override _start _end) +(defun typst-ts-render-math-scripts-fn (node _override _start _end) (let* ((ns (treesit-node-start node)) (ne (treesit-node-end node)) (prev-node-text (treesit-node-text (treesit-node-prev-sibling node))) (display-properties (pcase prev-node-text - ("_" (car typst-ts-mode-math-script-display)) - ("^" (cdr typst-ts-mode-math-script-display)))) + ("_" (car typst-ts-math-script-display)) + ("^" (cdr typst-ts-math-script-display)))) (face (pcase prev-node-text ("_" 'typst-ts-subscript-face) ("^" 'typst-ts-superscript-face)))) @@ -242,8 +242,8 @@ If you want to enable/disable specific font lock feature, please change ns ne face))) -(defun typst-ts-mode-highlight-raw-block-fn (blob-node _override _start _end) - "A function used in function `typst-ts-mode-font-lock-rules'. +(defun typst-ts-highlight-raw-block-fn (blob-node _override _start _end) + "A function used in function `typst-ts-font-lock-rules'. This function assign `typst-ts-markup-rawblock-blob-face' to those raw block whose language cannot be found or be loaded. BLOB-NODE." @@ -266,14 +266,14 @@ BLOB-NODE." (typst-ts-els-fontify-raw-block lang-mode bns bne) (put-text-property bns bne 'face 'typst-ts-markup-rawblock-blob-face)))) -(defconst typst-ts-mode-font-lock-feature-list +(defconst typst-ts-font-lock-feature-list '((comment common) (markup-basic code-basic math-basic) (markup-standard code-standard math-standard) (code-extended math-extended))) -(defun typst-ts-mode-indent--grand-parent-bol (_node parent _bol) +(defun typst-ts-indent--grand-parent-bol (_node parent _bol) "Return the grand parent beginning of line position. NODE, PARENT and BOL see `treesit-simple-indent-rules'." (save-excursion @@ -281,7 +281,7 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." (back-to-indentation) (point))) -(defun typst-ts-mode-indent--no-node-section-container-p (node parent _bol) +(defun typst-ts-indent--no-node-section-container-p (node parent _bol) "Whether the current structure is nil -> parbreak -> container -> section. NODE, PARENT and BOL see `treesit-simple-indent-rules'." (unless node @@ -291,10 +291,10 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." (ggp-node-type (treesit-node-type (treesit-node-parent gp-node)))) (and (equal "parbreak" parent-type) - (string-match-p typst-ts-mode--container-node-types-regexp gp-node-type) + (string-match-p typst-ts--container-node-types-regexp gp-node-type) (equal "section" ggp-node-type))))) -(defvar typst-ts-mode-indent-rules +(defvar typst-ts-indent-rules ;; debug tips: ;; use `typst-ts/util/setup-indent-debug-environment' function in `side/utils.el' ;; it basically does these (with some extra trivial stuffs): @@ -305,11 +305,11 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." ;; `indentation-test.typ' file is used for testing indentation. ;; no-node situation: often in insert mode > hit return at the line ending - ;; `typst-ts-mode-indent-line-function' is created for handling end of buffer + ;; `typst-ts-indent-line-function' is created for handling end of buffer ;; edge cases ;; Note electric-pair-mode will auto insert newline character when condition meets - ;; see `typst-ts-mode-electric-pair-open-newline-between-pairs-psif' + ;; see `typst-ts-electric-pair-open-newline-between-pairs-psif' ;; It may be better to turn off `electric-pair-open-newline-between-pairs' `((typst ;; ((lambda (node parent bol) ; NOTE @@ -323,7 +323,7 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." ((parent-is "source_file") column-0 0) ((n-p-gp ,(regexp-opt '(")" "]" "}" "$")) - ,typst-ts-mode--container-node-types-regexp + ,typst-ts--container-node-types-regexp nil) parent-bol 0) @@ -331,18 +331,18 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." ;; math align, example: ;; sum_(k=0)^n k ;; &= 1 + ... + n \ - ((node-is "align") parent-bol typst-ts-mode-indent-offset) + ((node-is "align") parent-bol typst-ts-indent-offset) ;; code field, example: ;; "a b c" ;; .split(" ") - ((n-p-gp "." "field" nil) parent-bol typst-ts-mode-indent-offset) + ((n-p-gp "." "field" nil) parent-bol typst-ts-indent-offset) ((parent-is "comment") prev-adaptive-prefix 0) ;; item - child item ((and (node-is "item") (parent-is "item")) parent-bol - typst-ts-mode-indent-offset) + typst-ts-indent-offset) ;; multi-line item ;; - #[hi] foo @@ -350,22 +350,22 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." ;; my try with `prev-adaptive-prefix' failed even after set the ;; `adaptive-fill-regexp' ((match nil "item" nil 2 nil) - typst-ts-mode--indentation-multiline-item-get-anchor 0) + typst-ts--indentation-multiline-item-get-anchor 0) ;; item - new item content should follow its previous line's indentation ;; level ;; e.g. ;; - hi | <- return (newline command) ((and no-node - typst-ts-mode--indentation-prev-line-is-item-p + typst-ts--indentation-prev-line-is-item-p ;; not in container ;; example: ;; - hi ;; hi #[ ;; - hello | <- return ;; ] - typst-ts-mode--indentation-editing-not-inside-code-container-p) - typst-ts-mode--indentation-multiline-item-get-anchor_ 0) + typst-ts--indentation-editing-not-inside-code-container-p) + typst-ts--indentation-multiline-item-get-anchor_ 0) ;; raw block ;; whether normally or in insertion, the current node is always nil... @@ -376,16 +376,16 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." parent-bol 0) ;; inside container && container is direct child of "section" (headline) - (typst-ts-mode-indent--no-node-section-container-p + (typst-ts-indent--no-node-section-container-p great-grand-parent 0) - ((n-p-gp nil ,typst-ts-mode--container-node-types-regexp "section") + ((n-p-gp nil ,typst-ts--container-node-types-regexp "section") grand-parent 0) ;; inside container - ((and no-node (n-p-gp nil "parbreak" ,typst-ts-mode--container-node-types-regexp)) - typst-ts-mode-indent--grand-parent-bol typst-ts-mode-indent-offset) - ((parent-is ,typst-ts-mode--container-node-types-regexp) - parent-bol typst-ts-mode-indent-offset) + ((and no-node (n-p-gp nil "parbreak" ,typst-ts--container-node-types-regexp)) + typst-ts-indent--grand-parent-bol typst-ts-indent-offset) + ((parent-is ,typst-ts--container-node-types-regexp) + parent-bol typst-ts-indent-offset) (no-node parent-bol 0) @@ -395,11 +395,11 @@ NODE, PARENT and BOL see `treesit-simple-indent-rules'." (catch-all prev-line 0))) "Tree-sitter indent rules for `typst-ts-mode'.") -(defun typst-ts-mode--indentation-multiline-item-get-anchor (_node parent _bol) +(defun typst-ts--indentation-multiline-item-get-anchor (_node parent _bol) "Return the start of second child of PARENT." (treesit-node-start (treesit-node-child parent 1))) -(defun typst-ts-mode--indentation-multiline-item-get-anchor_ (_node _parent _bol) +(defun typst-ts--indentation-multiline-item-get-anchor_ (_node _parent _bol) "Return the start of second child of the current item. This function is meant to be used when user hits a return key." (treesit-node-start @@ -413,7 +413,7 @@ This function is meant to be used when user hits a return key." "item" t) 1))) -(defun typst-ts-mode--indentation-prev-line-is-item-p (_node _parent _bol) +(defun typst-ts--indentation-prev-line-is-item-p (_node _parent _bol) (save-excursion (forward-line -1) (back-to-indentation) @@ -421,7 +421,7 @@ This function is meant to be used when user hits a return key." (treesit-node-at (point)) "item" t))) -(defun typst-ts-mode--indentation-editing-not-inside-code-container-p +(defun typst-ts--indentation-editing-not-inside-code-container-p (node parent _bol) "NODE, PARENT and BOL see `treesit-simple-indent-rules'." (let* ((gp-node (treesit-node-parent parent)) @@ -433,7 +433,7 @@ This function is meant to be used when user hits a return key." (equal (treesit-node-type ggp-node) "code"))))) -(defun typst-ts-mode-comment-setup() +(defun typst-ts-comment-setup() "Setup comment related stuffs for `typst-ts-mode'." ;; stolen from `c-ts-common-comment-setup' (setq-local comment-start "// ") @@ -446,7 +446,7 @@ This function is meant to be used when user hits a return key." (group (or (syntax comment-end) (seq (+ "*") "/")))))) -(defun typst-ts-mode--imenu-function-defintion-p (node) +(defun typst-ts--imenu-function-defintion-p (node) "Whether NODE is a function defintion node." (let* ((parent-node (treesit-node-parent node)) (grandparent-node (treesit-node-parent parent-node))) @@ -455,23 +455,23 @@ This function is meant to be used when user hits a return key." (equal (treesit-node-field-name parent-node) "pattern") (equal (treesit-node-type grandparent-node) "let")))) -(defun typst-ts-mode--imenu-name-function (node) +(defun typst-ts--imenu-name-function (node) "Generate name of NODE for displaying in Imenu." (treesit-node-text node)) ;; outline-minor-mode -(defconst typst-ts-mode-outline-regexp "^[[:space:]]*\\(=+\\)" +(defconst typst-ts-outline-regexp "^[[:space:]]*\\(=+\\)" "Regexp identifying Typst header.") -(defconst typst-ts-mode-outline-heading-alist +(defconst typst-ts-outline-heading-alist '(("=" . 1) ("==" . 2) ("===" . 3) ("====" . 4) ("=====" . 5) ("======" . 6)) "See `outline-heading-alist'.") -(defun typst-ts-mode-outline-level () +(defun typst-ts-outline-level () "Return the level of the heading at point." (save-excursion (end-of-line) - (if (re-search-backward typst-ts-mode-outline-regexp nil t) + (if (re-search-backward typst-ts-outline-regexp nil t) (- (match-end 1) (match-beginning 1)) 0))) @@ -483,23 +483,23 @@ This function is meant to be used when user hits a return key." "C-c C-w" #'typst-ts-watch-mode "C-c C-p" #'typst-ts-preview - "M-<left>" #'typst-ts-mode-meta-left - "M-<right>" #'typst-ts-mode-meta-right - "M-<down>" #'typst-ts-mode-meta-down - "M-<up>" #'typst-ts-mode-meta-up - "M-<return>" #'typst-ts-mode-meta-return + "M-<left>" #'typst-ts-editing-meta-left + "M-<right>" #'typst-ts-editing-meta-right + "M-<down>" #'typst-ts-editing-meta-down + "M-<up>" #'typst-ts-editing-meta-up + "M-<return>" #'typst-ts-editing-meta-return ;; don't bind <return> ;; Binding a command to "<return>" is generally a bug. ;; Emacs will first look for a binding for `return` and if it finds one ;; it'll use it in preference to a binding for `RET`, regardless of the ;; relative precedence of the keymaps involved. - "TAB" #'typst-ts-mode-cycle - "RET" #'typst-ts-mode-return + "TAB" #'typst-ts-editing-cycle + "RET" #'typst-ts-editing-return "C-c '" #'typst-ts-edit-indirect) -(defun typst-ts-mode-indent-line-function () +(defun typst-ts-indent-line-function () "A simple wrapper of `treesit-indent' for handle indentation edge cases. It is useful to handle end of buffer situation (please turn on `whitespace-mode' to see that it's actually end of buffer). Basically, if we are at the end of @@ -512,7 +512,7 @@ nil and parbreak." (backward-char)) (treesit-indent)) -(defun typst-ts-mode-electric-pair-open-newline-between-pairs-psif () +(defun typst-ts-electric-pair-open-newline-between-pairs-psif () "Custom version of `electric-pair-open-newline-between-pairs-psif'. It provide the ability to automatically open a new line for '$' character." (when (and (if (functionp electric-pair-open-newline-between-pairs) @@ -528,15 +528,15 @@ It provide the ability to automatically open a new line for '$' character." (and (eq cb ?\$) (eq ca ?\$))))) (save-excursion (newline 1 t)))) -(defun typst-ts-mode-check-grammar-version () +(defun typst-ts-check-grammar-version () "Check typst tree sitter grammar version. May not be correct(modified time can be the download time, copied time, etc.), but it does help prevent some error cases." - (when typst-ts-mode-grammar-location - (let ((min-time (time-convert typst-ts-mode--grammar-minimum-version-timestamp nil)) + (when typst-ts-grammar-location + (let ((min-time (time-convert typst-ts--grammar-minimum-version-timestamp nil)) (mod-time (file-attribute-modification-time - (file-attributes typst-ts-mode-grammar-location)))) + (file-attributes typst-ts-grammar-location)))) (when (time-less-p mod-time min-time) (message (propertize @@ -544,8 +544,8 @@ but it does help prevent some error cases." typst tree sitter grammar (at least %s)!" (current-time-string min-time)) 'face '(:weight bold :foreground "firebrick"))))))) -(defun typst-ts-mode-after-hook-function () - "Run after all hooks in `typst-ts-mode-hook'." +(defun typst-ts-after-hook-function () + "Run after all hooks in `typst-ts-hook'." ;; patch `electric-pair-post-self-insert-function' function (when electric-pair-mode ;; add-function :override buffer-locally doesn't work, so we do this... @@ -553,7 +553,7 @@ typst tree sitter grammar (at least %s)!" (current-time-string min-time)) (remove-hook 'post-self-insert-hook 'electric-pair-post-self-insert-function t) (add-hook 'post-self-insert-hook - #'typst-ts-mode-electric-pair-open-newline-between-pairs-psif + #'typst-ts-electric-pair-open-newline-between-pairs-psif t)) ;; Set Compile Command @@ -572,20 +572,20 @@ typst tree sitter grammar (at least %s)!" (current-time-string min-time)) ;; FIXME ;; necessary for - ;; `typst-ts-mode-cycle'(`typst-ts-editing--indent-item-node-lines') + ;; `typst-ts-cycle'(`typst-ts-editing--indent-item-node-lines') ;; since it calculate offset based on character ;; (maybe also some indentation rules) (indent-tabs-mode -1) - (typst-ts-mode-check-grammar-version)) + (typst-ts-check-grammar-version)) ;;;###autoload (define-derived-mode typst-ts-mode text-mode "Typst" "Major mode for editing Typst, powered by tree-sitter." :group 'typst - :syntax-table typst-ts-mode-syntax-table + :syntax-table typst-ts-syntax-table :after-hook - (typst-ts-mode-after-hook-function) + (typst-ts-after-hook-function) (unless (treesit-ready-p 'typst) (user-error "Tree-sitter for Typst isn't available")) @@ -593,7 +593,7 @@ typst tree sitter grammar (at least %s)!" (current-time-string min-time)) (setq-local treesit-primary-parser (treesit-parser-create 'typst)) ;; Comments. - (typst-ts-mode-comment-setup) + (typst-ts-comment-setup) ;; Electric (setq-local @@ -607,11 +607,11 @@ typst tree sitter grammar (at least %s)!" (current-time-string min-time)) (?\$ . ?\$))) ;; Font Lock - (setq-local treesit-font-lock-settings typst-ts-mode-font-lock-settings) - (setq-local treesit-font-lock-feature-list typst-ts-mode-font-lock-feature-list) + (setq-local treesit-font-lock-settings typst-ts-font-lock-settings) + (setq-local treesit-font-lock-feature-list typst-ts-font-lock-feature-list) ;; Indentation - (setq-local treesit-simple-indent-rules typst-ts-mode-indent-rules) + (setq-local treesit-simple-indent-rules typst-ts-indent-rules) ;; Imenu (setq-local treesit-simple-imenu-settings @@ -623,9 +623,9 @@ typst tree sitter grammar (at least %s)!" (current-time-string min-time)) ;; regexp string) when you use default settings for outline ;; (outline from imenu) see `treesit-major-mode-setup' and ;; `treesit-outline-predicate' - `(("Functions" typst-ts-mode--imenu-function-defintion-p nil - typst-ts-mode--imenu-name-function) - ("Headings" "^heading$" nil typst-ts-mode--imenu-name-function))) + `(("Functions" typst-ts--imenu-function-defintion-p nil + typst-ts--imenu-name-function) + ("Headings" "^heading$" nil typst-ts--imenu-name-function))) (setq-local treesit-defun-type-regexp (regexp-opt '("let" "math"))) @@ -639,9 +639,9 @@ typst tree sitter grammar (at least %s)!" (current-time-string min-time)) (if nil ; (>= emacs-major-version 30) ;; FIXME maybe it's a upstream bug. Circle top-level section will cycle all the content below (setq treesit-outline-predicate (regexp-opt '("section" "source_file"))) - (setq-local outline-regexp typst-ts-mode-outline-regexp) - (setq-local outline-level #'typst-ts-mode-outline-level)) - (setq-local outline-heading-alist typst-ts-mode-outline-heading-alist) + (setq-local outline-regexp typst-ts-outline-regexp) + (setq-local outline-level #'typst-ts-outline-level)) + (setq-local outline-heading-alist typst-ts-outline-heading-alist) ;; auto fill function @@ -649,8 +649,8 @@ typst tree sitter grammar (at least %s)!" (current-time-string min-time)) (treesit-major-mode-setup) - (setq-local font-lock-unfontify-region-function #'typst-ts-mode-unfontify-region) - (setq-local indent-line-function #'typst-ts-mode-indent-line-function) + (setq-local font-lock-unfontify-region-function #'typst-ts-unfontify-region) + (setq-local indent-line-function #'typst-ts-indent-line-function) (add-hook 'typst-ts-mode-hook (lambda () (setq-local edit-indirect-guess-mode-function #'typst-ts-edit-indirect--guess-mode)))) diff --git a/typst-ts-symbols.el b/typst-ts-symbols.el index f19b5e2519..1e39313aaa 100644 --- a/typst-ts-symbols.el +++ b/typst-ts-symbols.el @@ -19,7 +19,7 @@ ;;; Code: -(defcustom typst-ts-mode-symbol-alist +(defcustom typst-ts-symbol-alist '(("wj" . "wjoin") ("zwj" . "zwj") ("zwnj" . "zwnj") ("zws" . "zwsp") ("lrm" . "") ("rlm" . "") ("space" . "␣") ("space.nobreak" . "nbsp") ("space.nobreak.narrow" . " ") @@ -374,7 +374,7 @@ https://typst.app/docs/reference/symbols/sym/" (cons (string :tag "Name") (string :tag "Symbol")))) -(defcustom typst-ts-mode-emoji-alist +(defcustom typst-ts-emoji-alist '(("abacus" . "🧮") ("abc" . "🔤") ("abcd" . "🔡") diff --git a/typst-ts-transient.el b/typst-ts-transient.el index 53f03cf7dd..0945ef71b6 100644 --- a/typst-ts-transient.el +++ b/typst-ts-transient.el @@ -26,7 +26,7 @@ (require 'typst-ts-misc-commands) (transient-define-prefix typst-ts-tmenu () - "Typst-ts-mode transient menu." + "`typst-ts-mode' transient menu." [["Compile" ("c" "compile & preview" typst-ts-compile-and-preview) ("C" "compile" typst-ts-compile) diff --git a/typst-ts-variables.el b/typst-ts-variables.el index 5d99585819..87b98b5c14 100644 --- a/typst-ts-variables.el +++ b/typst-ts-variables.el @@ -28,30 +28,30 @@ :group 'languages) -(defcustom typst-ts-mode-grammar-location nil +(defcustom typst-ts-grammar-location nil "Specify typst tree sitter grammar file location. This is used for grammar minimum version check. The modified time of the grammar file is used for comparing. -This variable is used in `typst-ts-mode-check-grammar-version'." +This variable is used in `typst-ts-check-grammar-version'." :type '(choice (string :tag "typst tree sitter grammar file location") (const :tag "Don't enable grammar version check" nil))) ;; `git log -n1 --date=raw` or `git log -n1 --format="%at"` -(defvar typst-ts-mode--grammar-minimum-version-timestamp 1713791627 +(defvar typst-ts--grammar-minimum-version-timestamp 1713791627 "Timestamp for the minimum supported typst tree sitter grammar version.") -(defcustom typst-ts-mode-enable-raw-blocks-highlight nil +(defcustom typst-ts-enable-raw-blocks-highlight nil "Whether to enable raw block highlighting. NOTE this option must be set before the first loading(opening typst file)" :type 'boolean) -(defcustom typst-ts-mode-electric-return t - "Whether `typst-ts-mode-return' auto inserts list items or not." +(defcustom typst-ts-electric-return t + "Whether `typst-ts-return' auto inserts list items or not." :type 'boolean) ;; code from Auctex -(defcustom typst-ts-mode-math-script-display '((raise -0.5) . (raise 0.5)) +(defcustom typst-ts-math-script-display '((raise -0.5) . (raise 0.5)) "Display specification for subscript and superscript content. The car is used for subscript, the cdr is used for superscripts." :type '(cons (choice (sexp :tag "Subscript form") @@ -86,7 +86,7 @@ The compile options will be passed to the end of :group 'typst-ts) -(defcustom typst-ts-mode-preview-function 'browse-url +(defcustom typst-ts-preview-function 'browse-url "Function that opens PDF documents for preview." :type 'function :group 'typst-ts)