Making variables local to the mode is also desirable so that they are known only within a buffer that is running the mode. [80]To do this, use the make-local-variablefunction, as in:
(make-local-variable 'calc-stack)
Notice that the name of the variable, not its value, is needed; therefore a single quote precedes the variable name, turning it into a symbol.
Finally, various major modes use special buffers that are not attached to files. For example, the C-x C-b(for list-buffers) command creates a buffer called *Buffer List*
. To create a buffer in a new window, use the pop-to-bufferfunction, as in:
(pop-to-buffer "*Calc*")
There are a couple of useful variations on pop-to-buffer. We won't use them in our mode example, but they are handy in other circumstances.
switch-to-buffer
Same as the C-x bcommand covered in Chapter 4; can also be used with a buffer name argument in Lisp.
set-buffer
Used only within Lisp code to designate the buffer used for editing; the best function to use for creating a temporary "work" buffer within a Lisp function.
11.5.2 More Lisp Basics: Lists
A Reverse Polish Notation calculator uses a data structure called a stack . Think of a stack as being similar to a spring-loaded dish stack in a cafeteria. When you enter a number into a RPN calculator, you push it onto the stack. When you apply an operator such as plus or minus, you pop the top two numbers off the stack, add or subtract them, and push the result back on the stack.
The list , a fundamental concept of Lisp, is a natural for implementing stacks. The list is the main concept that sets Lisp apart from other programming languages. It is a data structure that has two parts: the head and tail . These are known in Lisp jargon, for purely historical reasons, as carand cdrrespectively. Think of these terms as "the first thing in the list" and "the rest of the list." The functions carand cdr, when given a list argument, return the head and tail of it, respectively. [81]Two functions are often used for making lists. cons(construct) takes two arguments, which become the head and tail of the list respectively. listtakes a list of elements and makes them into a list. For example, this:
(list 2 3 4 5)
makes a list of the numbers from 2 to 5, and this:
(cons 1 (list 2 3 4 5))
makes a list of the numbers from 1 to 5. carapplied to that list would return 1
, while cdrwould return the list (2 3 4 5)
.
These concepts are important because stacks, such as that used in the calculator mode, are easily implemented as lists. To push the value of x onto the stack calc-stack, we can just say this:
(setq calc-stack (cons x calc-stack))
If we want to get at the value at the top of the stack, the following returns that value:
(car calc-stack)
To pop the top value off the stack, we say this:
(setq calc-stack (cdr calc-stack))
Bear in mind that the elements of a list can be anything, including other lists. (This is why a list is called a recursive data structure.) In fact (ready to be confused?) just about everything in Lisp that is not an atom is a list. This includes functions, which are basically lists of function name, arguments, and expressions to be evaluated. The idea of functions as lists will come in handy very soon.
11.5.3 The Calculator Mode
The complete Lisp code for the calculator mode appears at the end of this section; you should refer to it while reading the following explanation. If you download or type the code in, you can use the calculator by typing M-x calc-mode Enter. You will be put in the buffer *Calc*
. You can type a line of numbers and operators and then type C-jto evaluate the line. Table 11-7lists the three commands in calculator mode.
Table 11-7. Calculator mode commands
Command |
Action |
= |
Print the value at the top of the stack. |
p |
Print the entire stack contents. |
c |
Clear the stack. |
Blank spaces are not necessary, except to separate numbers. For example, typing this:
4 17*6-=
followed by C-j, evaluates (4 * 17) - 6 and causes the result, 62, to be printed.
The heart of the code for the calculator mode is the functions calc-evaland calc-next-token. (See the code at the end of this section for these.) calc-evalis bound to C-jin Calculator mode. Starting at the beginning of the line preceding C-j, it calls calc-next-tokento grab each token (number, operator, or command letter) in the line and evaluate it.
calc-next-tokenuses a condconstruct to see if there is a number, operator, or command letter at point by using the regular expressions calc-number-regexp, calc-operator-regexp, and calc-command-regexp. According to which regular expression was matched, it sets the variable calc-proc-funto the name (symbol) of the function that should be run (either calc-push-number, calc-operate, or calc-command), and it sets tok
to the result of the regular expression match.
In calc-eval, we see where the idea of a function as a list comes in. The funcallfunction reflects the fact that there is little difference between code and data in Lisp. We can put together a list consisting of a symbol and a bunch of expressions and evaluate it as a function, using the symbol as the function name and the expressions as arguments; this is what funcalldoes. In this case, the following:
(funcall calc-proc-fun tok)
treats the symbol value of calc-proc-funas the name of the function to be called and calls it with the argument tok
. Then the function does one of three things:
• If the token is a number, calc-push-numberpushes the number onto the stack.
• If the token is an operator, calc-operateperforms the operation on the top two numbers on the stack (see below).
• If the token is a command, calc-commandperforms the appropriate command.
The function calc-operatetakes the idea of functions as lists of data a step further by converting the token from the user directly into a function (an arithmetic operator). This step is accomplished by the function read, which takes a character string and converts it into a symbol. Thus, calc-operateuses funcalland readin combination as follows:
(defun calc-operate (tok)
(let ((op1 (calc-pop))
(op2 (calc-pop)))
(calc-push (funcall (read tok) op2 op1))))
This function takes the name of an arithmetic operator (as a string) as its argument. As we saw earlier, the string tok
is a token extracted from the *Calc*
buffer, in this case, an arithmetic operator such as +
or *
. The calc-operatefunction pops the top two arguments off the stack by using the popfunction, which is similar to the use of cdr
earlier. readconverts the token to a symbol, and thus to the name of an arithmetic function. So, if the operator is +
, then funcallis called as here:
Читать дальше