Elisp: Get Text Block

By Xah Lee. Date: .

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://ergoemacs.org/emacs/elisp_get_text_block.html'
Version 2020-06-10"
  (interactive)
  (let ($p1 $p2)
    (save-excursion
      (if (re-search-backward "\n[ \t]*\n" nil "move")
          (progn (re-search-forward "\n[ \t]*\n")
                 (setq $p1 (point)))
        (setq $p1 (point)))
      (if (re-search-forward "\n[ \t]*\n" nil "move")
          (progn (re-search-backward "\n[ \t]*\n")
                 (setq $p2 (point)))
        (setq $p2 (point))))
    (vector $p1 $p2)
    (message "%s" (vector $p1 $p2))))

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


Emacs Lisp

Basics

Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc