Function: markdown-ts-newline
markdown-ts-newline is an interactive and byte-compiled function
defined in markdown-ts-mode.el.gz.
Signature
(markdown-ts-newline)
Documentation
Insert a newline, continuing the current context.
Inside a list item, the new line is indented to the item's text column so the paragraph continues. A second RET on a blank continuation line removes the indentation, dropping point to column 0 so the tree-sitter grammar ends the list. Inside a block quote, the new line includes the quote prefix. Otherwise, insert a plain newline.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts-newline ()
"Insert a newline, continuing the current context.
Inside a list item, the new line is indented to the item's text
column so the paragraph continues. A second RET on a blank
continuation line removes the indentation, dropping point to
column 0 so the tree-sitter grammar ends the list.
Inside a block quote, the new line includes the quote prefix.
Otherwise, insert a plain newline."
(interactive)
(let* ((node (treesit-node-at
(save-excursion (back-to-indentation) (point))
'markdown))
;; Tree-sitter may report a node inside a block_quote even
;; when point is on a blank line past the quote (the node
;; span can extend beyond the `>' lines). Only treat the
;; line as inside a block quote if it actually starts with
;; a `>' marker.
(in-bq (and (treesit-parent-until node "\\`block_quote\\'")
(save-excursion
(beginning-of-line)
(looking-at-p "[ \t]*>"))))
(item (markdown-ts--list-item-at-point)))
(cond
(item
(let* ((col (markdown-ts--list-item-text-column item))
(bq-prefix (when in-bq (markdown-ts--block-quote-prefix)))
;; `col' is the absolute column of the item's text,
;; which inside a block quote includes the "> " prefix
;; width. Since we insert `bq-prefix' separately, we
;; must subtract its length to avoid doubling it.
(indent (- col (length (or bq-prefix ""))))
;; The current line is "blank" if it contains only
;; whitespace (or quote prefix + whitespace) and no
;; actual text from a previous RET's continuation indent.
(blank-line-p (save-excursion
(beginning-of-line)
(looking-at-p
"^[> \t]*$")))
;; Point is before the item's text (at or before the
;; marker). A plain newline is appropriate here, adding
;; continuation indent would shift the existing content
;; to the right.
(before-text-p (<= (current-column) col)))
(cond
(blank-line-p
;; The user pressed RET on an already-blank continuation
;; line. Instead of adding yet another indented blank
;; line, remove the indentation and insert a plain
;; newline. This drops point to column 0, which is the
;; only way the tree-sitter markdown grammar ends a list:
;; blank lines alone do NOT end a list, only
;; non-indented content does. So this gives the user a
;; natural "RET RET to exit the list" workflow.
(delete-region (line-beginning-position) (line-end-position))
(newline))
(before-text-p
;; Point is on or before the list marker; just insert a
;; plain newline so the item is pushed down unchanged.
(newline))
(t
(delete-horizontal-space)
(newline)
(when bq-prefix (insert bq-prefix))
(insert (make-string indent ?\s))))))
(in-bq
(let ((bq-prefix (markdown-ts--block-quote-prefix))
;; A line with only quote markers and whitespace (e.g.,
;; "> ") is "blank" inside the quote. RET here exits the
;; quote, same as RET on a blank continuation line exits a
;; list: remove the prefix and drop to column 0.
(blank-line-p (save-excursion
(beginning-of-line)
(looking-at-p "^[> \t]*$"))))
(if blank-line-p
(progn
(delete-region (line-beginning-position) (line-end-position))
(newline))
(delete-horizontal-space)
(newline)
(insert bq-prefix))))
;; Default: plain newline. This also handles empty list markers
;; (e.g., "- " with no text) that the grammar parses as ERROR
;; nodes rather than list_item nodes.
;;
;; When the current line is blank (whitespace only), it is a
;; continuation-indent line left by a previous RET that
;; tree-sitter no longer considers part of a list_item. Clear
;; the whitespace so no trailing spaces remain, and insert a
;; plain newline at column 0 to exit the list context.
(t
(when (save-excursion
(beginning-of-line)
(looking-at-p "^[ \t]+$"))
(delete-region (line-beginning-position) (line-end-position)))
(newline)))))