[OS X Emacs] new+question

Jack Repenning jrepenning at collab.net
Thu Dec 16 13:36:59 EST 2010

On Dec 16, 2010, at 12:10 AM, Marinos Koutsomichalis wrote:

> How do I pass a text selection as an argument to a lisp function 
> (defun test-function (word) "print word to minibuffer" 
> 	(interactive)
> 	(message "%s"  word)
> 	)

What you want is something like this:

(defun test-function (sel-start sel-end) "print word to minibuffer" 
	(interactive "r")
	(message "%s"  (buffer-substring sel-start sel-end))

Some "fishing lessons" on getting to such answers:

"Ctrl-h f" is your friend! Plant your cursor in the word "interactive" and give it a try. You get the help for the function "interactive," which is the key here.

If, as will be true in this case, you don't see quite what you expected, read the rest and ponder how it could. As you see, in this case I used the "region" (region is the name for the end points of what you called the "selection"). Since "region" is a pair of numbers, you need something to turn it into the actual text, and that's "buffer-substring".

If you really want to be strict about it, you don't need to "pass" the region/selection anywhere--everyone knows it, by the names "point" and "mark". But taking the end points as arguments is better, because it allows your function to be used in other ways as well -- I can call

  (test-function some-other-spot another-spot-near-that-one)

and get the buffer-substring mentioned printed, even if it's not actually the selection.

Oh ... and, welcome to Emacs!

More information about the MacOSX-Emacs mailing list