Elisp: Get Text Block 💠
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 `list'. 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: 2025-03-25" (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)))) (list 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 `list'. URL `http://xahlee.info/emacs/emacs/elisp_get_text_block.html' Created: 2024-03-23 Version: 2024-03-23" (if (region-active-p) (list (region-beginning) (region-end)) (xah-get-pos-of-block)))
Inline Version
;; get positions of block (seq-setq (xbeg xend) (list (save-excursion (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point))) (save-excursion (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point)))))
;; get positions of block or selection (seq-setq (xbeg xend) (if (region-active-p) (list (region-beginning) (region-end)) (list (save-excursion (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point))) (save-excursion (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point))))))
Emacs Lisp, text processing functions
- Elisp: Cursor Position Functions
- Elisp: Move Cursor
- Elisp: Text Editing Functions
- Elisp: Search Text
- Elisp: Find Replace Text in Buffer
- Elisp: Mark, Region, Active Region
- Elisp: Cut Copy Paste, kill-ring
- Elisp: Get Buffer String
- Elisp: Functions on Line
- Elisp: thing-at-point
- Elisp: Get Text Block 💠
- Elisp: Save narrow-to-region