Previous Table of Contents Next


Next, evaluate add-37 for Edebug with C-x X d, and again call symbol- function. You will see the calls to edebug-before and edebug-after inserted in the definition, along with other expressions.

After you have instrumented add-37 by evaluating it with C-x X d, you can evaluate an expression such as

   (add-37 -7)

When you do this, you are popped to the source for the definition, and an arrow appears on the line that contains the next expression to be evaluated. Point moves to the beginning of that expression.

The source buffer looks like this:

  ---------- Buffer: *scratch* ----------
   (defun add-37 (number)
   “Add 37 to NUMBER.”
   (interactive “p”)
  =>[special character] (message “The new number is:    %d!” (+ 37 nmber)))
  ---------- Buffer: *scratch* ----------

The definition is in the *scratch* buffer; [special character] indicates the position of point.

When you press the spacebar, point moves to the next expression the Lisp interpreter will try to evaluate. Eventually (or in this example, rather quickly), it reaches the error.

In addition to stepping through an evaluation and stopping before and after each expression, you can use Edebug to set breakpoints, trace slowly or quickly, display expression results and evaluate expressions as if outside Edebug, automatically reevaluate a list of expressions and display their results each time Edebug updates the display, stop when an error occurs, display a backtrace, specify argument evaluation for macros and defining forms, and obtain rudimentary coverage testing and frequency counts.

Edebug is described in the section “Edebug” in The GNU Emacs Lisp Reference Manual.

2.20. Backups and Auto-Saving

GNU Emacs can keep either a single backup file or a series of numbered backup files for each file you edit. This means you can go back to earlier versions of files.

Also, from time to time Emacs saves all the files you are visiting without being asked. This is called auto-saving; it means you do not lose more than a limited amount of work if your system crashes.

By default, Emacs saves your files every 300 keystrokes, or after around 30 seconds of idle time. You can examine your auto-saved files like any other file, or use the M-x recover-file command to restore them.

2.21. Evaluating or Loading a Whole File

To evaluate, or load, a whole file, use the M-x load-file command. This interactive function reads a filename using Emacs’s minibuffer and then evaluates the complete contents of that file as Lisp.14 Incidentally, you can evaluate a whole buffer with the M-x eval-current-buffer command and evaluate a region within a buffer with the M-x eval-region command.


14The M-x load-file command reads a file as found on disk, not the text in an Emacs buffer; when you are writing code, be sure to save your edited text so it becomes part of the file on disk.

Files of Emacs Lisp are kept in libraries, that is, in directories whose pathname is part of Emacs’s load-path. You can easily load such files using the M-x load-library command. Moreover, programs can load such files by calling load-library, or load, two functions that evaluate whole files.

load-path is an Emacs variable like any other variable. You can examine it using C-h v (describe-variable) or set it with setq. Often, you will specify an additional directory by consing the path of that directory onto its existing load-path, like this:

   (setq load-path (cons “∼/emacs” load-path))

This expression prepends the emacs directory in your home directory to the existing load-path. (People often keep personally interesting .el files in the emacs subdirectory of their home directory.)

2.22. Byte Compilation

GNU Emacs possesses a program that converts humanly readable Emacs Lisp expressions into an unreadable form that the byte-code interpreter can run more quickly. Byte code is not native code, specific to a particular type of machine, but can run on any machine; it runs faster than humanly readable Emacs Lisp, but more slowly than true compiled code.

Although you can byte-compile an individual function or macro definition, more often you byte-compile whole files of Emacs Lisp, using the byte-compile-file or byte-recompile-directory commands. These commands create new files, with .elc, rather than .el, extensions.

The M-x load-library command, when given a name without an extension, automatically looks for a file with an .elc extension first, and loads that file rather than a file with an .el extension. If the .el file has a newer date, it prints a warning because it is likely that somebody made changes to the .el file and forgot to recompile it.

See the sections “Libraries of Lisp Code for Emacs” in The GNU Emacs Manual, and “The Compilation Functions” in The GNU Emacs Lisp Reference Manual.

2.23. Your .emacs Initialization File

GNU Emacs is customizable and extensible. This means you can change how Emacs work and add to it. You can place your own customizations and small extensions in a file in your home directory called .emacs. This file is loaded when you start your Emacs; it is loaded after the sitewide initialization file, site-start.el, but before another sitewide initialization file default.el. This means your customizations and extensions will override whatever is in site-start.el, but will be overridden by default.el.

Your .emacs file consists of expressions in Emacs Lisp. Here is an example—first, a comment, after the semicolons; second, a setq expression; and third, an add-hook expression:

   ;;; Text mode and Auto Fill mode
   ; The next two lines put Emacs into Text mode
   ; and Auto Fill mode, and are for writers who
   ; want to start writing prose rather than code.

   (setq default-major-mode ‘text-mode)
   (add-hook ‘text-mode-hook ‘turn-on-auto-fill)

As a result of these customizations, when Emacs sees a file, it places that file in Text mode, unless it is told otherwise (by, for example, a .c extension to the file name that indicates C mode).

And every time Emacs turns on Text mode, Emacs runs commands hooked onto Text mode, so in this case, Emacs also turns on Auto Fill mode.

Here is a simple extension to Emacs that moves the line point is on to the top of the window when you press the F9 function key:

   ;;; Line to top of window;
   ;;; replace three keystroke sequence  C-u 0 C-l
   (defun line-to-top-of-window ()
     “Move the line point is on to top of window.”
     (interactive)
     (recenter 0))

   (global-set-key [f9] ‘line-to-top-of-window)

These examples show how you can use Emacs Lisp to set values in your initialization file and write simple extensions.

And because Emacs Lisp is a complete programming language, you can go much further: You can write calendars, calculators, spreadsheets, flashcards, Conway’s Game of Life, and Towers of Hanoi, not to mention C mode, Fortran mode, Gnus mode, Mail mode, and Telnet mode, all in Emacs Lisp.


Previous Table of Contents Next