I have never written an emacs function before and was wondering if anyone could help me get started. I would like to have a function that takes a highlighted region parses it (by ",") then evaluates each chunk with another function already built into emacs.
The highlighted code may look something like this: x <- function(w=NULL,y=1,z=20){}
(r code), and I would like to scrape out w=NULL
, y=1
, and z=20
then pass each one a function already included with emacs. Any suggestions on how to get started?
A lisp function is defined using
defun
(you really should read the elisp intro, it will save you a lot of time - "a pint of sweat saves a gallon of blood").To turn a mere function into an interactive command (which can be called using M-x or bound to a key), you use
interactive
.To pass the region (selection) to the function, you use the
"r"
code:Now, copy the form above to the
*scratch*
emacs buffer, place the point (cursor) on a function, say,mapc
orsplit-string
, then hit C-h f RET and you will see a*Help*
buffer explaining what the function does.You can evaluate the function definition by hitting C-M-x while the point is on it (don't forget to replace
the-emacs-function-you-want-to-call-on-each-arg
with something meaningful), and then test is by selectingw=NULL,y=1,z=20
and hitting M-x my-command RET.Incidentally, C-h f my-command RET will now show
Operate on each word in the region
in the*Help*
buffer.