I have a pattern like this scattered all over my .emacs:
(defun my/function ()
(interactive)
(....stuff)
)
I would like to write a idefun
macro so I could write as following instead:
(idefun my/function ()
(... stuff)
)
So far, based on my previous ilambda macro post, I tried this business:
(defmacro idefun (name para &rest body)
`(defun ,name ,para (interactive) ,body))
(idefun my/idefun-test ()
(message "defun function"))
;I would like it to translate to:
;(defun my/idefun-test ()
; (interactive)
; (message "defun function")
; )
Now 'my/idefun-test' is listed under M-x, but when running it tells me 'invalid function'.
I have two questions/requests:
1) Does anyone know how to get the above to work?
2) Would it be possible to implement a version that conditionally inserts function documentation if the first item is a string? (like in the lambda post)I.e:
(idefun my/test () "function docu" (message "MEH"))
Should translate to:
(define my/test () "function docu" (interactive) (message "MEH"))
[EDIT SOLUTION]
NOTE: It is not reccomended to use a macro like this in a public package/library as it may lead to confusion. But for a personal .emacs it might just about prevent rsi.
Based on the answer below, I pieced together the following working macro:
(defmacro idefun (name para docOrBody &rest body)
(if (and (stringp docOrBody) body) ;if first arg is a string type
;then
`(defun ,name ,para
,docOrBody ;this is the function documentation string in this case.
(interactive)
,@body)
;else
`(defun ,name ,para
(interactive)
,docOrBody ,@body)) ;otherwise it is the body.
)
;-------
;Examples follow
;-------
(idefun my/id2 ()
(message "it works"))
(idefun my/id2-with-docu ()
"Function documentation"
(message "it works with docu"))
(idefun my/id2-args (a)
(message a))
(idefun my/hello-moto ()
"Function docu :-D!"
(message "Hello moto"))
(global-unset-key (kbd "<f8>"))
(global-set-key (kbd "<f8>") 'my/hello-moto)
;This is funny if read out loud.
(idefun u () (message "I am your father"))
(interactive)
is not worth it. Also those reading your code do know what(interactive)
means but have no idea whatidefun
does.(defun .. (interactive)
in my .emacs, time to consolidate things.