Function: markdown-ts--move-subtree-up-or-down

markdown-ts--move-subtree-up-or-down is a byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts--move-subtree-up-or-down UP)

Documentation

Move the current section subtree.

If UP is non-nil, move past the previous sibling; otherwise move down. Preserves the folding state of both sections.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts--move-subtree-up-or-down (up)
  "Move the current section subtree.
If UP is non-nil, move past the previous sibling; otherwise move down.
Preserves the folding state of both sections."
  (when-let* ((section (markdown-ts--section-at-point))
              (sibling (if up
                           (treesit-node-prev-sibling section)
                         (treesit-node-next-sibling section))))
    (when (equal (treesit-node-type sibling) "section")
      (let* ((first (if up sibling section))
             (second (if up section sibling))
             (first-folded (markdown-ts--section-folded-p first))
             (second-folded (markdown-ts--section-folded-p second))
             (first-start (treesit-node-start first))
             (second-start (treesit-node-start second))
             (second-end (treesit-node-end second))
             ;; Extract separator between items and trailing whitespace.
             (first-raw (buffer-substring first-start second-start))
             (first-text (string-trim-right first-raw))
             (separator (substring first-raw (length first-text)))
             (second-raw (buffer-substring second-start second-end))
             (second-text (string-trim-right second-raw))
             (trailing (substring second-raw (length second-text)))
             (line-offset (- (line-number-at-pos (point))
                             (line-number-at-pos (treesit-node-start section)))))
        (delete-region first-start second-end)
        (goto-char first-start)
        (insert second-text separator first-text trailing)
        ;; Restore folding state
        (save-excursion
          (goto-char first-start)
          (when (outline-on-heading-p)
            (if second-folded
                (outline-hide-subtree)
              (outline-show-subtree)))
          (outline-next-heading)
          (when (outline-on-heading-p)
            (if first-folded
                (outline-hide-subtree)
              (outline-show-subtree))))
        (goto-char (if up
                       first-start
                     (+ first-start (length second-text)
                        (length separator))))
        (with-suppressed-warnings ((interactive-only next-line))
          (next-line line-offset))))))