Computer Languages A to Z: Elisp

Gary L. Ratliff Sr. (eronstuc)

Lisp was one of the earliest computer languages invented. It appeared in the late 50's and stood for List Processing. It was used in artificial intelligence and was very powerful for regular programming. In the mid 70's a graduate engineering student enrolled in MIT and worked in the MIT AI lab. His name was Richard Matthew Stallman, and programmers preferred to be called hackers in those days before the word acquired its current evil connotation.

A very early full screen editor was TECO (Text Editor and Corrector which was then in use in the AI Lab. Many of the hackers had written macros for this editor which extended its capabilities. Stallman wrote many and then cataloged them and this set of macros was placed on the computer. Because no program on this system started with the letter E, this group of macros for text editing came to be known as Emacs. The features of the program continued and the size of the program and number of its features grew. The core of the current Emacs editor is written in C and the program features name extensions which are loaded into memory when they are asked for. These extensions are written in a dialect of Lisp called Elisp by old hands or more properly Emacs Lisp.

Getting Your Computer to Understand Elisp

The editor which comes standard with the KDE version of PCLINUXOS 2009-1 is kwrite. I just installed the GNOME 2009-2 version and its default editor is gedit. This was verified while the initial boot from the CD was being installed into the most unused partition of the main system. Gedit and Kwrite are very similar. However, by default you will not have the emacs editor installed. By going to synaptic and doing a search for emacs, you will be able to have this editor installed on your system.

Here we see that the items we desire to install from emacs have been selected and installed by using "apply" now they are marked as having been sucessfully installed into the system.

Here from the screen shot you can see that I also elected to install the documentation for emacs as well as the elisp source code and support for the X11 version of emacs. There is also another version of emacs named Xemacs. For the purposes of this article we will only install the emacs versions.

Now you will run the emacs program from: More Applications; Editors; emacs on the menu. Once in the emacs editor click on the Buffers Menu item and select:

Scratch. You will notice a message to the effect that this buffer is for notes or Lisp evaluation. The one thing to note about Lisp and its dialects is that you will learn to type the left and right parentheses keys very often. So after the three lines of text at the beginning of the buffer enter the following lines of elisp code:

(+ 1 1)
(+ 2 1)
(- 4 2)
(* 8 4)
(/ 20 5)
(+ 1 2 3 4 5 6 7 8 9)
(* 1 2 3 4 5 6 7 8 9)

The scratch buffer from Emacs has had some simple forms entered. The item marked by the current location of the cursor has been evaluated by pressing down the Control key and tapping x and then e. Note the answer appears in the small window at the bottom of the scratch buffer.

Now move your cursor to the space to the right of the right parenthesis in the line: "(+ 1 1)". With the cursor resting in this position hold down the Control key and tap the x key and then the e key. At the bottom of the window another small window should appear which has the number 2 in it. So this has answered the math question: "What is 1 + 1?" Also in the discussion on Dylan it was noted that the decision was made to abandon the prefix notation of the Scheme dialect of lisp in favor of the infix notation. However, Elisp like the many other Lisp dialects uses prefix or Reverse Polish Notation. The operator comes before the arguments. Now by simply pressing the down arrow the cursor should be properly located to be at the next problem.

By again holding the Control key and tapping x and e in succession you should note a 3 appear in the window. Dropping down to the next line reveals 2 and then 32 and then 4. These answer the questions: 2+1 =; 4 – 2 =; 8 * 4 =; and 20 / 5 =.

Now if upon reaching the last mentioned problem you see 5 instead of the correct answer 4 it means that you just pressed the down arrow and your cursor is not outside the right parenthesis.

If in a similar manner you pressed the down arrow to the next problem it would be resting on the number 3 and the number 2 would appear in the answer box as that is the number to the immediate left of the 3. By moving to outside the right parenthesis and again request that the function be evaluated you see the answer 49 appear. This is the Ʃ i = 1 to 9. And the answer to the last problem will appear as 362880 which is the Π i = 1 to 9 (the summation and successive multiplication of those integers respectively.)

So it appears that we know how to write simple math functions. In Lisp these are known as forms. So how would we enter simple text? Or how would we tackle the infamous "Hello World!" program? For this next set let us close and then restart emacs. Now enter the following forms for evaluation:

(message "Hello World!")
(message "My name is %s" "Gary")
(message "I am %d years old" 66)
(message "%s had a little lamb and took him to school when she was only %d years old" "Mary" 7)
(message "The first letter letter in my name is: %c " 71)

So in C one would say: "printf("Hello World!\n); while in Dylan it would be written: format-out("Hello World!\n"); Here we see the simpler elisp method. Also notice that the format specifications are the same in Elisp as in C however, notice no , separator is used in the Elisp notation. Also the character is an integer and as such takes a numeric argument in Elisp while in C this argument could have been written as 'G' for a single character. Doing this in Elisp will invoke the debugger. Now the text of the debugger will stay on the screen so this would be a good time to quit emacs and restart the editor.

Now we shall explore how some comparisons are made using Elisp. Enter the following forms into a fresh start of emacs:

(if (< 5 4) (message "yes"))
(if (< 5 4) (message "yes") (message "no"))
(if (> 5 4) (message "yes") (message "no"))
(if (= 5 5) (message "yes") (message "no"))
(not (if (= 5 5) (message "yes") (message "no")))

Now use the Control x e to evaluate these forms and you should see these answers appear in the window at the bottom of the scratch buffer: nil, no, yes, yes, nil. In C these would be the familiar if then and if then else expressions. Now if a value is false then nil is used. (Later we will learn that it also will be used for and empty list.) While if it is true then the symbol t will be used. In the first form as there is no else so the answer is false and the system reports this by showing nil. The second form presents the same question and there is a message to be displayed if the answer is false. So here the answer no is used.

In the third form we change the condition so that it is true and the answer yes if given. Now in Lisp there is no != as there is in C. So to obtain that result the logic of the expression is reversed by surrounding it with the not symbol as shown. And for this the answer is nil.

Now "didn't you mention that LISP stood for List Processing?" you ask. So our next examples show some lists:

(list ())
(list '(a ()))
(list '(a b c))
(list '("a" "b" "c"))

Now when we ask for these to be evaluated we see these answers: (nil) ((a nil)) ((a b c)) and (("a" "b" "c")). Here we see examples of the empty list being noted by nil. Also because these are lists we see this clearly indicated by the surrounding parentheses.

This is just an introduction to the many features of Elisp. I am certain that you might like to explore this fascinating language further. The creator of Emacs: RMS along with many others wrote the definitive reference for the current and many past versions of Emacs. This is available in many forms and the pdf format version of this is 1017 pages long! This assumes that the reader is currently familiar with the operations of the Emacs editor. There is a similar reference manual available for the Emacs editor itself.

The full reference manual notes that people who are just now learning the language might find the text of: An Introduction to Programming in Emacs Lisp Third Edition more useful for them. Both of these may easily be found by entering Emacs Lisp in the Google search box on the Firefox browser.

A form from Xah's Emacs Lisp Tutorial highlighted and ready to copy using the browsers Edit menu.

Another easy introduction to Elisp is available from xahlee.org and is listed in the google search engine as: Xah's Emacs Lisp Tutorial.

One final tip as you are reading these tutorials you may wish to try out the lisp forms. Have the text of the article you are reading in one screen and that of the Emacs editor in another. Highlight the text you wish to use and the Copy it then go to the emacs editor on the scratch buffer and paste it into the buffer. Now you may execute the code as mentioned in this article.

Here the form appears in the scratch buffer of Emacs ready to be evaluated. This is pasted into the scratch buffer using the Edit menu and the Paste command.