Next: , Up: Defining Functions   [Contents][Index]


4.1 Using DEFUN

Although the full syntax of a function declaration is discussed in the SXEmacs internals manual in greater depth, what follows is a brief description of how to define and implement a new Lisp primitive in a module. This is done using the DEFUN macro. Here is a small example:

DEFUN("my-function", Fmy_function, 1, 1, "FFile name: ", /*
Sample Emacs primitive function.

The specified FILE is frobnicated before it is fnozzled.
*/
    (file))
{
        char *filename;

        if (NILP(file))
                return Qnil;

        filename = (char *)XSTRING_DATA(file);
        frob(filename);
        return Qt;
}

The first argument is the name of the function as it will appear to the Lisp reader. This must be provided as a string. The second argument is the name of the actual C function that will be created. This is typically the Lisp function name with a preceding capital F, with hyphens converted to underscores. This must be a valid C function name. Next come the minimum and maximum number of arguments, respectively. This is used to ensure that the correct number of arguments are passed to the function. Next is the interactive definition. If this function is meant to be run by a user interactively, then you need to specify the argument types and prompts in this string. Please consult the SXEmacs Lisp manual for more details. Next comes a C comment that is the documentation for this function. Last comes the list of function argument names, if any.