Elisp: 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.)

(defun xah-get-pos-of-block ()
  "Return the begin end positions of current text block.
Return value is a `vector'.
Text block is group of lines separated by blank lines.

URL `http://xahlee.info/emacs/emacs/elisp_get_text_block.html'
Created: 2024-03-23
Version: 2024-10-07"
  (let (xbeg xend (xp (point)))
    (save-excursion
      (setq xbeg (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point)))
      (goto-char xp)
      (setq xend (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point))))
    (vector xbeg xend)))

(defun xah-get-pos-of-block-or ()
  "Return the begin end positions of current text block or active region.
Return value is a `vector'.

URL `http://xahlee.info/emacs/emacs/elisp_get_text_block.html'
Created: 2024-03-23
Version: 2024-03-23"
  (if (region-active-p)
      (vector (region-beginning) (region-end))
    (xah-get-pos-of-block)))

Elisp, text processing functions