Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 22: Learning Lisp Basics

Previous HourNext Hour

Sections in this Hour:

 

Function Definitions


A function is a collection of commands that is given a name. When the function is executed, each command is executed in turn. You have already seen numerous functions--namely those that you can invoke by pressing M-x and typing a name (which in fact is the function name).

Functions can take a number of arguments. These arguments are used to configure the behavior of the function. An example is the function that is used to switch to another window (called other-window and bound to C-x o. This function takes an argument that tells it how many windows to skip and which direction to move.

The functions you need to write to configure Emacs do not require any arguments, but it's worth it for you to know the existence of arguments, because some of the functions you need to invoke require arguments. You have already seen how to invoke a function, but I haven't been concrete about that matter until now.

A function is invoked by typing its name at the first position within a set of parentheses. The elements at the subsequent positions are arguments to the function. Thus to invoke other-window with an argument of -1, use (other-window -1).

The following is an example of a function that switches buffer in the backward direction:


01  (defun my-switch-buffer () 
02    "Like switch-buffer but in the opposite direction" 
03    (interactive "") 
04    (other-window -1) 
05  ) 

In line 1 of this code, defun is a keyword to indicate a function definition. my-switch-buffer is the name of the new function. The parentheses in line 1 contain the arguments to the function (none in this case). Line 2 provides an optional description of the function (see it with C-h f. And (interactive "") in line 3 is an interactive specification.

The interactive specification is used to tell Emacs that the function can be invoked by pressing M-x and typing its name, or by binding it to a key. Without this line you can call the function only from within another function or from a hook. The string given as argument defines how Emacs should obtain the argument to the function, when it is called interactively. But as this function takes no arguments, this string is empty.

With the previous function in your .emacs file you can now press M-x and type my-switch-buffer, or bind the function to a key.

It is beyond the scope of this book to tell you the whole story about function definitions, so I'll focus on what you really need to know in order to configure Emacs. You need to define functions that you can attach to hooks. Hook functions do not need an interactive part and take no arguments. As an example of a hook function, the following is a function that turns on auto-fill mode and sets the fill column to 100:


(defun my-auto-fill-hook-function () 
  (setq fill-column 100) 
  (turn-on-auto-fill) 
) 

This function can now be added to the hook for c-mode (called c-mode-hook) with the following code:


(add-hook 'c-mode-hook 'my-auto-fill-hook-function)

Hooks are discussed in more detail in Hour 24.

Note - Please note the quote in front of my-auto-fill-hook-function; without this quote Lisp thinks that my-auto-fill-hook-function is a variable that it needs to find a value for in its dictionary. With the quote, Lisp knows that you are stating a name of a function.

Anonymous Functions

With the previous method for defining hooks, you often find that you need a new name for a function, with the only reason that the function needs a name is so that you can give it to the add-hook declaration. With an anonymous function you can save time and avoid having to find a name for the function. Anonymous functions are defined with the keyword lambda instead of the keyword defun, as can be seen in the following example:


(add-hook 'c-mode-hook 
  (lambda () 
    (setq fill-column 100) 
    (turn-on-auto-fill) 
)) 

Note - If you think that the lambda function is an extra complexity, you might be right. I told you about lambda functions only because you most likely will see the preceding method as the preferred one in many other sources.

Lambda functions are in fact very powerful when you use Lisp for writing real programs--that is, in contrast to configuring minor things in Emacs.


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 22: Learning Lisp Basics

Previous HourNext Hour

Sections in this Hour: