Function: markdown-ts-renumber-list

markdown-ts-renumber-list is an interactive and byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts-renumber-list &optional START)

Documentation

Renumber the ordered list at point.

Numbers are assigned sequentially starting from the first item's number. With a prefix argument START, start numbering from that value instead. Does nothing for unordered lists.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts-renumber-list (&optional start)
  "Renumber the ordered list at point.
Numbers are assigned sequentially starting from the first item's
number.  With a prefix argument START, start numbering from that
value instead.  Does nothing for unordered lists."
  (interactive "P")
  (when-let* ((item (markdown-ts--list-item-at-point))
              (list-node (treesit-node-parent item)))
    (when (equal (treesit-node-type list-node) "list")
      (let* ((children (treesit-node-children list-node))
             (items (seq-filter (lambda (n)
                                  (equal (treesit-node-type n) "list_item"))
                                children))
             (first-marker (treesit-node-child (car items) 0))
             (marker-type (treesit-node-type first-marker)))
        (when (member marker-type '("list_marker_dot" "list_marker_parenthesis"))
          (let* ((base (if start
                           (prefix-numeric-value start)
                         (string-to-number
                          (treesit-node-text first-marker t))))
                 (count (length items))
                 ;; Start from the last number and work backwards so
                 ;; that earlier buffer positions remain valid.
                 (num (+ base count -1)))
            (dolist (it (reverse items))
              (let* ((marker (treesit-node-child it 0))
                     (marker-end (treesit-node-end marker))
                     (bol (save-excursion
                            (goto-char (treesit-node-start marker))
                            (line-beginning-position)))
                     ;; Full text from bol to end of marker includes
                     ;; any leading whitespace, the number, and the
                     ;; separator (e.g., ". " or ".\t").
                     (full-text (buffer-substring-no-properties
                                 bol marker-end))
                     (indent-str
                      (if (string-match
                           "\\`\\([[:blank:]]*\\)[0-9]" full-text)
                          (match-string 1 full-text)
                        ""))
                     (suffix-str
                      (if (string-match
                           "\\`[[:blank:]]*[0-9]+\\(.*\\)\\'" full-text)
                          (match-string 1 full-text)
                        ". "))
                     (new-text (concat indent-str
                                       (number-to-string num)
                                       suffix-str)))
                (save-excursion
                  (delete-region bol marker-end)
                  (goto-char bol)
                  (insert new-text)))
              (cl-decf num))
            (message "Renumbered %d items" count)))))))