I need to get selection as a string in my Emacs Lisp function.
You can select blocks of text in Emacs just as you would in most other environments. You could, for example, drag your mouse over a region. You could also hold down the Shift key and use arrow keys.
Place the cursor on either side of the word, then hold the shift key down and hold the alt/option key down, and then use the left or right arrow.
Richard Stallman chose Lisp as the extension language for his rewrite of Emacs (the original used Text Editor and Corrector (TECO) as its extension language) because of its powerful features, including the ability to treat functions as data.
Selected text is called region in Emacs universe. See How do I access the contents of the current region in Emacs Lisp?
The accepted answer pointed me to the right answer. I want to leave this piece of code for more ELisp beginners like me. regionp
contains the "selection" (known as region in ELisp) as a variable in the ELisp function. The if
condition checks if the region is active.
(defun get-selected-text (start end)
(interactive "r")
(if (use-region-p)
(let ((regionp (buffer-substring start end)))
(message regionp))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With