Emacs Lisp: Get Text Block

By Xah Lee. Date: . Last updated: .

Often, you want a interactive command to work on a text block, without user having to manually select it. (text block is group of lines separated by empty lines, similar to a paragraph.)

Commands working on text block is especially useful in programing language source code.

Here's command that gets the beginning and ending positions of text block the cursor is in.

(defun get-text-block-positions ()
  "Return a vector [begin end] of text block,
 Return a vector [begin end] that's the begin and end positions of text block the cursor is in.
Text block is group of lines separated by blank lines.

URL `http://xahlee.info/emacs/emacs/elisp_get_text_block.html'
Version 2020-06-10 2022-01-21"
  (interactive)
  (let ($p1 $p2)
    (save-excursion
      (if (re-search-backward "\n[ \t]*\n" nil "move")
          (progn (goto-char (match-end 0))
                 (setq $p1 (point)))
        (setq $p1 (point)))
      (if (re-search-forward "\n[ \t]*\n" nil "move")
          (progn (goto-char (match-beginning 0))
                 (setq $p2 (point)))
        (setq $p2 (point))))
    (message "%s" (vector $p1 $p2))
    (vector $p1 $p2)))
ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Practical Elisp ⭐

Writing Command

Writing Script