[OS X Emacs] new+question
marinos at agxivatein.com
Thu Dec 16 16:32:00 EST 2010
I' m gonna try this tonight to code a small snippet I need - hopefully with success..
On 16 Δεκ 2010, at 8:36 μ.μ., Jack Repenning wrote:
> 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"
>> (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!
> MacOSX-Emacs mailing list
> MacOSX-Emacs at email.esm.psu.edu
> List Archives: http://dir.gmane.org/gmane.emacs.macintosh.osx
More information about the MacOSX-Emacs