Function: math-defcache

math-defcache is a macro defined in calc-ext.el.gz.

Signature

(math-defcache NAME INIT FORM)

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-ext.el.gz
;;;; Caches.

(defmacro math-defcache (name init form)
  (declare (indent 2) (debug (symbolp sexp form)))
  (let ((cache-prec (intern (concat (symbol-name name) "-cache-prec")))
	(cache-val (intern (concat (symbol-name name) "-cache")))
	(last-prec (intern (concat (symbol-name name) "-last-prec")))
	(last-val (intern (concat (symbol-name name) "-last"))))
    `(progn
       ;; (defvar ,cache-prec ,(if init (math-numdigs (nth 1 init)) -100))
       (defvar ,cache-prec (cond
			    ((consp ,init) (math-numdigs (nth 1 ,init)))
			    (,init
			     (nth 1 (math-numdigs (eval ,init t))))
			    (t
			     -100)))
       (defvar ,cache-val (cond ((consp ,init) ,init)
				(,init (eval ,init t))
				(t ,init)))
       (defvar ,last-prec -100)
       (defvar ,last-val nil)
       (setq math-cache-list
	     (cons ',cache-prec
		   (cons ',last-prec
			 math-cache-list)))
       (defun ,name ()
	 (or (= ,last-prec calc-internal-prec)
	     (setq ,last-val
		   (math-normalize
		    (progn (or (>= ,cache-prec calc-internal-prec)
			       (setq ,cache-val
				     (let ((calc-internal-prec
					    (+ calc-internal-prec 4)))
				       ,form)
				     ,cache-prec (+ calc-internal-prec 2)))
			   ,cache-val))
		   ,last-prec calc-internal-prec))
	 ,last-val))))