Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 22: Learning Lisp Basics

Previous HourNext Hour

Sections in this Hour:

 

Assigning Variables

A variable is a container that can hold a value for later retrieval. You have already seen numerous examples of variables: load-path, next-line-add-newlines, and abbrev-file-name to name a few.

Some of the variables can be made buffer-local. That is, their values are different from buffer to buffer. This has the advantage that different major modes can configure Emacs in specific ways. A good example of this are the variables comment-start and comment-end. These variables are used by the filling commands (see Hour 12, "Visible Editing Utilities," for a description of these commands).

When a major mode is started in a given buffer, it makes these two variables buffer-local and sets them to their specific values for the given major mode. Making the variables buffer-local ensures that the variable is configured only for the given buffer and that the variable is not changed in any of the other buffers, which might be in a different major mode.

When a variable is made buffer-local, changes made to it in the given buffer only have an effect in that buffer, not in any of the other buffers. Still a global variable might exist that all the other buffers share (including new ones).

Some variables can be made buffer-local in such a way that new buffers created get a local copy of it. The copy they get is initialized from a default value. An example of such a variable is fill-column. Depending on your intention and whether the variable is buffer-local or not you can use either setq or setq-default. The possible combinations is shown in Table 22.1.

Table 22.1  Functions to Use to Set Variables (setq or setq-default)

Intention

Set Global Value

Set Local Value

The variable is not buffer- local

This is the most common situation. Here you can simply use setq. You can, however, also use the function setq-default. Thus, it is possible to use this function all the time, when you want to set the global value for a variable.

In this situation, the variable is not a buffer-local value. To make a buffer-local variable, you must use the function make-local-variable. Given that it is the variable next-line-add-newlines, which you want to make buffer-local and set to nil, use this code: (make-local-variable 'next-line-add newlines) (setq next-line-add newlines nil)

The variable is buffer- local

To set the global value for a buffer-local variable, you must use the function setq-default.

To set a local value for a buffer-local buffer simply use the function setq. To set a variable local for a given buffer, you must either press M-:.nd execute the commands described previously, or insert the command in a hook (hooks are described in Hour 24, "Installing Emacs Add-Ons"). In short, hooks are commands executed when a major mode, for example, starts.


If you, for example, want to set the global value for the variable fill-column, you must use setq-default, because this variable is buffer-local.

Configuring Options for a Single File

Some configurations might be local to a given file rather than a given major mode. Examples of these kinds of configurations are

This can be obtained by inserting a few lines at the bottom of the given file. These look like


01  /* Local Variables:  */ 
02  /* variable1: value1 */ 
03  /* variable2: value2 */ 
04  /* End:              */ 

In line 1, Local Variables: and End: must be there as is, to ensure that Emacs recognizes these lines as instructions to it. Also in line 1, notice the slashes and asterisks. All the lines must start and end with the same text as this line. These are the comment characters for a C file. variable1 in line 2 is the name of the variable. And the value for this variable is value1, also shown in line 2.

The following is an example where the abbreviation file and the fill-column are set: in a HTML file (enclose a comment in <!-- and -->).


<!-- Local Variables: --> 
<!-- fill-column: 100 --> 
<!-- abbrev-file-name: "~/WWW/.abbrevs" --> 
<!-- End: --> 

Executing Code

In the previous examples, loading a file was also mentioned as one of the things you could do with this feature. In general, any kind of Lisp code can be executed. Simply insert a line starting with eval: and include the Lisp expression after that, as can be seen in the following example:


<!-- Local Variables: --> 
<!-- fill-column: 100 --> 
<!-- eval: (load-file "~/Emacs/Macros/chapter-macros.el") --> 
<!-- End: --> 

The function load-file reads in a Lisp file and evaluates it.

Caution - Now that you have learned this feature, please think carefully before you insert text such as the preceding in your files, especially if someone else is supposed to edit the files too. If your configuration is a matter of personal taste, it might be a better idea to customize the options using hooks on major modes instead.


When Emacs loads a file with local variables, it asks whether you really want this region evaluated. This is to avoid reading in someone else's file, where the file contains code in the eval section that does something you do not like (such as erasing your hard disk). You can ask Emacs to skip this security check if you never edit other people's files. This is done by inserting the following line into your .emacs file:


(setq enable-local-eval t)

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 22: Learning Lisp Basics

Previous HourNext Hour

Sections in this Hour: