Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the region (selection) programmatically in Emacs Lisp?

Tags:

I need to access the selection in Emacs buffer. I have found this article How do I access the contents of the current region in Emacs Lisp? and it helps me a lot.

But there is a problem. The first time I select (highlight) a region, it works okay, but when I press C-g, and move cursor normally to another place without highlighting any chars, I got a string from last mark to the current point while I expect an empty one.

Actually I need to implement a function which will return the current selection (highlighted) as a string, or empty string if nothing is highlighted. The following code may express me more clearly.

 (defun get-search-term ()   (interactive)   (let (         (selection (buffer-substring-no-properties (region-beginning) (region-end))))     (if (= (length selection) 0)         (message "empty string")       (message selection))))  

Any suggestions? Thanks a lot!

like image 982
sailing Avatar asked May 15 '12 04:05

sailing


People also ask

How do I lisp in emacs?

In a fresh Emacs window, type ESC-x lisp-interaction-mode . That will turn your buffer into a LISP terminal; pressing Ctrl+j will feed the s-expression that your cursor (called "point" in Emacs manuals' jargon) stands right behind to LISP, and will print the result.

What does #' mean in Emacs Lisp?

#'... is short-hand for (function ...) which is simply a variant of '... / (quote ...) that also hints to the byte-compiler that it can compile the quoted form as a function.

Is Emacs Lisp the same as Lisp?

Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.


1 Answers

"r" specification of interactive is dumb. You're seeing why.

(defun get-search-term (beg end)   "message region or \"empty string\" if none highlighted"   (interactive (if (use-region-p)                    (list (region-beginning) (region-end))                  (list (point-min) (point-min))))   (let ((selection (buffer-substring-no-properties beg end)))     (if (= (length selection) 0)         (message "empty string")       (message selection)))) 

I don't mean "dumb" as in stupid and not useful; just that it doesn't care about whether the mark is active or not. I think it predates transient-mark-mode.

EDIT: Using (point-min) twice above makes the code harder to understand when re-reading. Here is a better implementation:

(defun get-search-term (beg end)   "message region or \"empty string\" if none highlighted"   (interactive (if (use-region-p)                    (list (region-beginning) (region-end))                  (list nil nil)))   (message "%s" (if (and beg end)                     (buffer-substring-no-properties beg end)                   "empty string"))) 
like image 177
event_jr Avatar answered Oct 30 '22 07:10

event_jr