Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 22: Learning Lisp Basics

Previous HourNext Hour

Sections in this Hour:

 

Hour 22
Learning Lisp Basics

Emacs is extended using a variant of Lisp called Emacs Lisp, or elisp for short. Emacs can be extended almost limitlessly using elisp. Think about dired, discussed in Hour 16, "Interfacing with the System". dired has very little in common with the original task of Emacs, namely editing text files; still, dired is built purely on Emacs, written using only Lisp.

You also use elisp yourself, when configuring minor things in your .emacs file. You can, for example, see Lisp expressions such as the following in the .emacs file:


(setq next-line-add-newlines nil)

The focus in this hour is on customizing Emacs using your .emacs file, not on topics specific to writing larger extensions. This hour teaches you enough about Lisp to understand and edit your .emacs file. You should not, however, expect to be able to build larger extensions to Emacs, such as dired.

This hour assumes no knowledge of programming, so if you have never programmed a single line of code before, you should still get through. The hour does, however, draw parallels to other programming languages to make it easier for those who have programmed before.

The Purpose of Lisp


You might very well ask yourself Why do I need such a complex system as Lisp to configure things in the .emacs file? The answer is simple: This is to give you enough power to do whatever you want! An example might clarify my point.

Imagine that you had only a very simple syntax with which you could configure things in your .emacs file. The syntax could for example resemble


option1: value1 
option2: value2 
option3: value3 
... 

How would you, using this syntax, tell Emacs that an option should have one value in one major mode, and another value in another major mode? You might suggest that you make sections in the configuration file for each major mode like:


major-mode1: 
option1: value1 
option2: value2 
option3: value3 
... 
 
major-mode2: 
... 

Okay...how would you then avoid having to specify the same value twice for all major modes, if the value was mode independent? How would you specify a value based on both major and minor mode? one based on the computer you were logged in to? the capabilities of your monitor? the major/minor modes? whether you use GNU Emacs or XEmacs? whether you use version 19 or version 20? and on and on....

If you have a good answer to this question, I'll bet that your answer is a full-blown programming language. Lisp is a full-blown programming language, so why reinvent the wheel?

Evaluation in Lisp

In a programming language such as C or Pascal, you would write the addition of three numbers with subsequent assignments to a variable in the following way:


a := 10 + 20 + 30

In Lisp this is written like


(setq a (+ 10 (+ 20 30)))

The syntax is different, but what is done is, in fact, the same.

Note - The reason for the difference in syntax is that Lisp is a much older language than Pascal. Parsing the preceding Pascal expression was very expensive in the ancient days when Lisp was invented; furthermore, the methods you know today for parsing weren't invented at that time.


The Lisp expression is evaluated from the inside out. That is, first the numbers 20 and 30 are added and the result, 50, is added to 10. Finally, the result of this is assigned to the variable named a.

Note - The preceding statement couldn't be more wrong, but it's a nice way to think about it! What actually happens is that the Lisp interpreter sees the outermost parentheses and concludes that this is something for it to evaluate. The first element of this list is regarded as a function, and the remaining elements are regarded as arguments to this function. Before the Lisp interpreter executes the function, all its arguments are evaluated. In this round of evaluation, the result of 10+20+30 is calculated.


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 22: Learning Lisp Basics

Previous HourNext Hour

Sections in this Hour: