Code :
(defun function_name (args) "documentation" ;optional (interactive optional-args) ;optional body)
The defun is a special form and it contains the arguments "documentation" as well as (interactive args) which are optional as indicated in the code above. The body may contain one or more expressions. Let's start with a simple example that uses the message function described here and here.
Code :
(defun hello() ;no arguments for hello (message "Hello World!"))
To execute your newly created function first execute the defun form above then execute (hello) as
Code :
(hello)
When you type in C-h f you get the hello function but no documentation. Let's document the function hello(),
Code :
(defun hello() "Prints Hello World into the echo area" (message "Hello World!"))
You want to make it interactive? Well, write it as
Code :
(defun hello() "Prints Hello World into the echo area" (interactive) (message "Hello World!"))
Now, with arguments
Code :
(defun hello-person(user) "Greets the user" (interactive) (message "Hello %s" user)) (hello-person user-full-name)
Finally, a neat little function (execute it and see)
Code :
(defun my-function() (goto-char (point-max)) (insert "\nhi There !")) (my-function)
0 comments:
Post a Comment