: Learning GNU Emacs, 3rd Edition

11.5.4 Lisp Code for the Calculator Mode

11.5.4 Lisp Code for the Calculator Mode

Now you should be able to understand all of the code for the calculator mode. You will notice that there really isn't that much code at all! This is testimony to the power of Lisp and the versatility of built-in Emacs functions. Once you understand how this mode works, you should be ready to start rolling your own. Without any further ado, here is the code:

;; Calculator mode.
;; Supports the operators +, -, *, /, and % (remainder).
;; Commands: ;; c clear the stack
;; = print the value at the top of the stack
;; p print the entire stack contents
(defvar calc-mode-map nil
"Local keymap for calculator mode buffers.")
; set up the calculator mode keymap with
; C-j (linefeed) as "eval" key
(if calc-mode-map
(setq calc-mode-map (make-sparse-keymap))
(define-key calc-mode-map "C-j" 'calc-eval))
(defconst calc-number-regexp
"Regular expression for recognizing numbers.")
(defconst calc-operator-regexp "[-+*/%]"
"Regular expression for recognizing operators.")
(defconst calc-command-regexp "[c=ps]"
"Regular expression for recognizing commands.")
(defconst calc-whitespace "[ t]"
"Regular expression for recognizing whitespace.")
;; stack functions
(defun calc-push (num)
(if (numberp num)
(setq calc-stack (cons num calc-stack))))
(defun calc-top ()
(if (not calc-stack)
(error "stack empty.")
(car calc-stack)))
(defun calc-pop ()
(let ((val (calc-top)))
(if val
(setq calc-stack (cdr calc-stack)))
;; functions for user commands:
(defun calc-print-stack ()
"Print entire contents of stack, from top to bottom."
(if calc-stack
(insert "n")
(let ((stk calc-stack))
(while calc-stack
(insert (number-to-string (calc-pop)) " "))
(setq calc-stack stk)))
(error "stack empty.")))
(defun calc-clear-stack ()
"Clear the stack."
(setq calc-stack nil)
(message "stack cleared."))
(defun calc-command (tok)
"Given a command token, perform the appropriate action."
(cond ((equal tok "c")
((equal tok "=")
(insert "n" (number-to-string (calc-top))))
((equal tok "p")
(message (concat "invalid command: " tok)))))
(defun calc-operate (tok)
"Given an arithmetic operator (as string), pop two numbers
off the stack, perform operation tok (given as string), push
the result onto the stack."
(let ((op1 (calc-pop))
(op2 (calc-pop)))
(calc-push (funcall (read tok) op2 op1))))
(defun calc-push-number (tok)
"Given a number (as string), push it (as number)
onto the stack."
(calc-push (string-to-number tok)))
(defun calc-invalid-tok (tok)
(error (concat "Invalid token: " tok))
(defun calc-next-token ()
"Pick up the next token, based on regexp search.
As side effects, advance point one past the token,
and set name of function to use to process the token."
(let (tok)
(cond ((looking-at calc-number-regexp)
(goto-char (match-end 0))
(setq calc-proc-fun 'calc-push-number))
((looking-at calc-operator-regexp)
(forward-char 1)
(setq calc-proc-fun 'calc-operate))
((looking-at calc-command-regexp)
(forward-char 1)
(setq calc-proc-fun 'calc-command))
((looking-at ".")
(forward-char 1)
(setq calc-proc-fun 'calc-invalid-tok)))
;; pick up token and advance past it (and past whitespace)
(setq tok (buffer-substring (match-beginning 0) (point)))
(if (looking-at calc-whitespace)
(goto-char (match-end 0)))
(defun calc-eval ()
"Main evaluation function for calculator mode.
Process all tokens on an input line."
(while (not (eolp))
(let ((tok (calc-next-token)))
(funcall calc-proc-fun tok)))
(insert "n"))
(defun calc-mode ()
"Calculator mode, using H-P style postfix notation.
Understands the arithmetic operators +, -, *, / and %,
plus the following commands:
c clear stack
= print top of stack
p print entire stack contents (top to bottom)
Linefeed (C-j) is bound to an evaluation function that
will evaluate everything on the current line. No
whitespace is necessary, except to separate numbers."
(pop-to-buffer "*Calc*" nil)
(make-local-variable 'calc-stack)
(setq calc-stack nil)
(make-local-variable 'calc-proc-fun)
(setq major-mode 'calc-mode)
(setq mode-name "Calculator")
(use-local-map calc-mode-map))

The following are some possible extensions to the calculator mode, offered as exercises. If you try them, you will increase your understanding of the mode's code and Emacs Lisp programming in general.

Add an operator ^ for "power" (4 5 ^ evaluates to 1024). There is no built-in power function in Emacs Lisp, but you can use the built-in function expt.

Add support for octal (base 8) and/or hexadecimal (base 16) numbers. An octal number has a leading "0," and a hexadecimal has a leading "0x"; thus, 017 equals decimal 15, and 0x17 equals decimal 23.

Add operators + and * to add/multiply all of the numbers on the stack, not just the top two (e.g., 4 5 6 + evaluates to 15, and 4 5 6 * evaluates to 120).[82]

As an additional test of your knowledge of list handling in Lisp, complete the example (Example 5) from earlier in this chapter that searches compilation-error-regexp-alist for a match to a compiler error message. (Hint: make a copy of the list, then pick off the top element repeatedly until either a match is found or the list is exhausted.)

: 0.416. /Cache: 3 / 1