This chapter starts with an introduction to the aspects of Lisp that resemble common programming languages like Java and Perl. These features are enough to enable you to write many Emacs commands. Then we deal with how to interface Lisp code with Emacs so that the functions you write can become Emacs commands. We will see various built-in Lisp functions that are useful for writing your own Emacs commands, including those that use regular expressions; we give an explanation of regular expressions that extends the introduction in Chapter 3 Chapter 3. Search and Replace The commands we discussed in the first two chapters are enough to get you started, but they're certainly not enough to do any serious editing. If you're using Emacs for anything longer than a few paragraphs, you'll want the support this chapter describes. In this chapter, we cover the various ways that Emacs lets you search for and replace text. Emacs provides the traditional search and replace facilities you would expect in any editor; it also provides several important variants, including incremental searches, regular expression searches, and query-replace. We also cover spell-checking here, because it is a type of replacement (errors are sought and replaced with corrections). Finally, we cover word abbreviation mode; this feature is a type of automatic replacement that can be a real timesaver.
and is oriented toward Lisp programming. We then return to the basics of Lisp for a little while, covering the unique features of the language that have to do with lists, and show how this chapter's concepts fit together by presenting a file template system you can install and use in your own programming or writing projects.
Finally we show you how to program a simple major mode, illustrating that this "summit" of Emacs Lisp programming isn't so hard to scale. After that, you will see how easy it is to customize Emacs's built-in major modes without having to change (or even look at) the code that implements them. We finish the chapter by describing how to build your own library of Lisp packages.
11.1 Introduction to Lisp
You may have heard of Lisp as a language for artificial intelligence (AI). If you aren't into AI, don't worry. Lisp may have an unusual syntax, but many of its basic features are just like those of more conventional languages you may have seen, such as Java or Perl. We emphasize such features in this chapter. After introducing the basic Lisp concepts, we proceed by building up various example functions that you can actually use in Emacs. In order to try out the examples, you should be familiar with Emacs Lisp mode and Lisp interaction mode, which were discussed in Chapter 9.
11.1.1 Basic Lisp Entities
The basic elements in Lisp you need to be familiar with are functions, variables, and atoms. Functions are the only program units in Lisp; they cover the notions of procedures, subroutines, programs, and even operators in other languages.
Functions are defined as lists of the above entities, usually as lists of calls to other, existing functions. All functions have return values (as with Perl functions and non-void Java methods); a function's return value is simply the value of the last item in the list, usually the value returned by the last function called. A function call within another function is equivalent to a statement in other languages, and we use statement interchangeably with function call in this chapter. Here is the syntax for function:
( function-name argument1 argument2 ...)
which is equivalent to this:
method_name ( argument1, argument2, ...);
in Java. This syntax is used for all functions, including those equivalent to arithmetic or comparison operators in other languages. For example, in order to add 2 and 4 in Java or Perl, you would use the expression 2 + 4, whereas in Lisp you would use the following:
(+ 2 4)
Similarly, where you would use 4 >= 2 (greater than or equal to), the Lisp equivalent is:
(>= 4 2)
Variables in Lisp are similar to those in any other language, except that they do not have types . A Lisp variable can assume any type of value (values themselves do have types, but variables don't impose restrictions on what they can hold).
Atoms are values of any type, including integers, floating point (real) numbers, characters, strings, Boolean truth values, symbols, and special Emacs types such as buffers, windows, and processes. The syntax for various kinds of atoms is:
• Integersare what you would expect: signed whole numbers in the range -2 27to 2 27- 1.
• Floating point numbersare real numbers that you can represent with decimal points and scientific notation (with lowercase "e" for the power of 10). For example, the number 5489 can be written 5489, 5.489e3, 548.9e1, and so on.
• Charactersare preceded by a question mark, for example, ?a
. Esc, Newline, and Tabare abbreviated \e
, \n
, and \t
respectively; other control characters are denoted with the prefix \C-
, so that (for example) C-ais denoted as ?\C-a
. [75]
• Stringsare surrounded by double quotes; quote marks and backslashes within strings need to be preceded by a backslash. For example, " Jane said, \"See Dick run.\"
" is a legal string. Strings can be split across multiple lines without any special syntax. Everything until the closing quote, including all the line breaks, is part of the string value.
• Booleansuse t
for true and nil
for false, though most of the time, if a Boolean value is expected, any non- nil
value is assumed to mean true. nil
is also used as a null or nonvalue in various situations, as we will see.
• Symbolsare names of things in Lisp, for example, names of variables or functions. Sometimes it is important to refer to the name of something instead of its value, and this is done by preceding the name with a single quote ('). For example, the define-keyfunction, described in Chapter 10, uses the name of the command (as a symbol) rather than the command itself.
A simple example that ties many of these basic Lisp concepts together is the function setq. [76]As you may have figured out from previous chapters, setqis a way of assigning values to variables, as in
(setq auto-save-interval 800)
Notice that setqis a function, unlike in other languages in which special syntax such as =
or :=
is used for assignment. setqtakes two arguments: a variable name and a value. In this example, the variable auto-save-interval(the number of keystrokes between auto-saves) is set to the value 800
.
setqcan actually be used to assign values to multiple variables, as in
(setq thisvar thisvalue
thatvar thatvalue
theothervar theothervalue )
The return value of setqis simply the last value assigned, in this case theothervalue
. You can set the values of variables in other ways, as we'll see, but setqis the most widely applicable.
11.1.2 Defining Functions
Now it's time for an example of a simple function definition. Start Emacs without any arguments; this puts you into the *scratch*
buffer, an empty buffer in Lisp interaction mode (see Chapter 9), so that you can actually try this and subsequent examples.
Читать дальше