: Learning GNU Emacs, 3rd Edition

11.3.3 A Treasure Trove of Examples

11.3.3 A Treasure Trove of Examples

As mentioned above, the full auto-mode-alist has a lot more entries and documentation than fit in this book. The compile.el module in which it is defined also contains functions that use it. One of the best ways to learn how to use Emacs Lisp (as well as discovering things you might not have even realized you can do) is to browse through the implementations of standard modules that are similar to what you're trying to achieve, or that are simply interesting. But how do you find them?

The manual way is to look at the value of the variable load-path. This is the variable Emacs consults when it needs to load a library file itself, so any library you're looking for must be in one of these directories. (This variable is discussed further in the final section of this chapter.) The problem, as you will see if you look at the current value of the variable, is that it contains a large number of directories for you to wade through, which would be pretty tedious each time you're curious about a library. (An easy way to see the variable's value is through Help's "Describe variable" feature, C-h v.)

One of the authors wrote the command listed in Example 11-1 to address this problem and uses it regularly to easily snoop on the source files that make much of Emacs run. If you don't want to type this entire function into your .emacs by hand, you can download it from this book's web site, http://www.oreilly.com/catalog/gnu3.

Example11-1.find-library-file

(defun find-library-file (library)
"Takes a single argument LIBRARY, being a library file to search for.
Searches for LIBRARY directly (in case relative to current directory,
or absolute) and then searches directories in load-path in order. It
will test LIBRARY with no added extension, then with .el, and finally
with .elc. If a file is found in the search, it is visited. If none
is found, an error is signaled. Note that order of extension searching
is reversed from that of the load function."
(interactive "sFind library file: ")
(let ((path (cons "" load-path)) exact match elc test found)
(while (and (not match) path)
(setq test (concat (car path) "/" library)
match (if (condition-case nil
(file-readable-p test)
(error nil))
test)
path (cdr path)))
(setq path (cons "" load-path))
(or match
(while (and (not elc) path)
(setq test (concat (car path) "/" library ".elc")
elc (if (condition-case nil
(file-readable-p test)
(error nil))
test)
path (cdr path))))
(setq path (cons "" load-path))
(while (and (not match) path)
(setq test (concat (car path) "/" library ".el")
match (if (condition-case nil
(file-readable-p test)
(error nil))
test)
path (cdr path)))
(setq found (or match elc))
(if found
(progn
(find-file found)
(and match elc
(message "(library file %s exists)" elc)
(sit-for 1))
(message "Found library file %s" found))
(error "Library file "%s" not found." library))))

Once this command is defined, you can visit any library's implementation by typing M-x find-library file Enter librarynameEnter. If you use it as often as this author does, you too may find it worth binding to a key sequence. We won't present a detailed discussion of how this function works because it goes a bit deeper than this chapter, but if you're curious about what some of the functions do, you can put your cursor in the function name in a Lisp buffer and use the Help system's "Describe function" (C-h f) feature to get more information about it.

If you find that most of the time when you ask for a library, you end up with a file containing a lot of cryptic numeric codes and no comments, check if the filename ends in .elc. If that is usually what you end up with, it means that only the byte-compiled versions of the libraries (see the discussion at the end of this chapter) have been installed on your system. Ask your system administrator if you can get the source installed; that's an important part of being able to learn and tweak the Emacs Lisp environment.


: 0.343. /Cache: 3 / 1