Function: markdown-ts--fill-forward-paragraph

markdown-ts--fill-forward-paragraph is a byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts--fill-forward-paragraph ARG)

Documentation

Move forward by ARG paragraphs, respecting Markdown structure.

List items are treated as individual paragraphs. Blocks matched by markdown-ts--fill-unfillable-block-query are skipped: both forward and backward motion move to the end of the block, so fill-region sees a zero-length region and leaves the block unfilled.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts--fill-forward-paragraph (arg)
  "Move forward by ARG paragraphs, respecting Markdown structure.
List items are treated as individual paragraphs.  Blocks matched
by `markdown-ts--fill-unfillable-block-query' are skipped: both
forward and backward motion move to the end of the block, so
`fill-region' sees a zero-length region and leaves the block
unfilled."
  (let ((direction (if (> arg 0) 1 -1))
        (count (abs arg))
        (moved 0))
    (dotimes (_ count)
      ;; For backward motion, skip back over whitespace to find
      ;; the item we are leaving, not the next one.
      (let* ((pos (if (< direction 0)
                      (save-excursion
                        (skip-chars-backward " \t\n")
                        (max (point-min) (1- (point))))
                    (point)))
             (block (car (treesit-query-capture
                          (treesit-buffer-root-node 'markdown)
                          markdown-ts--fill-unfillable-block-query
                          pos (1+ pos))))
             (indented-pos (save-excursion
                             (goto-char pos)
                             (back-to-indentation)
                             (point)))
             (node (treesit-node-at indented-pos 'markdown))
             (item (treesit-parent-until node "\\`list_item\\'")))
        ;; When moving forward from whitespace between list items,
        ;; skip to the next non-blank position and check again.
        (when (and (not item) (not block) (> direction 0))
          (let ((next-pos (save-excursion
                            (skip-chars-forward " \t\n")
                            (point))))
            (setq node (treesit-node-at next-pos 'markdown))
            (setq item (treesit-parent-until node "\\`list_item\\'"))))
        (cond
         ;; Inside an unfillable block: skip over it entirely.
         (block
          (goto-char (treesit-node-end (cdr block)))
          (setq moved (1+ moved)))
         ;; Inside a list item: treat as paragraph boundary.
         (item
          (if (> direction 0)
              (goto-char (treesit-node-end item))
            (goto-char (treesit-node-start item)))
          (setq moved (1+ moved)))
         ;; Default: use standard paragraph motion.
         (t
          (forward-paragraph direction)
          (setq moved (1+ moved))))))
    ;; Return the number of paragraphs left to move (0 = all done).
    (- count moved)))