1 ;;; calc-prog.el --- user programmability functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-prog () nil)
38 (defun calc-equal-to (arg)
41 (if (and (integerp arg) (> arg 2))
42 (calc-enter-result arg "eq" (cons 'calcFunc-eq (calc-top-list-n arg)))
43 (calc-binary-op "eq" 'calcFunc-eq arg))))
45 (defun calc-remove-equal (arg)
48 (calc-unary-op "rmeq" 'calcFunc-rmeq arg)))
50 (defun calc-not-equal-to (arg)
53 (if (and (integerp arg) (> arg 2))
54 (calc-enter-result arg "neq" (cons 'calcFunc-neq (calc-top-list-n arg)))
55 (calc-binary-op "neq" 'calcFunc-neq arg))))
57 (defun calc-less-than (arg)
60 (calc-binary-op "lt" 'calcFunc-lt arg)))
62 (defun calc-greater-than (arg)
65 (calc-binary-op "gt" 'calcFunc-gt arg)))
67 (defun calc-less-equal (arg)
70 (calc-binary-op "leq" 'calcFunc-leq arg)))
72 (defun calc-greater-equal (arg)
75 (calc-binary-op "geq" 'calcFunc-geq arg)))
77 (defun calc-in-set (arg)
80 (calc-binary-op "in" 'calcFunc-in arg)))
82 (defun calc-logical-and (arg)
85 (calc-binary-op "land" 'calcFunc-land arg 1)))
87 (defun calc-logical-or (arg)
90 (calc-binary-op "lor" 'calcFunc-lor arg 0)))
92 (defun calc-logical-not (arg)
95 (calc-unary-op "lnot" 'calcFunc-lnot arg)))
97 (defun calc-logical-if ()
100 (calc-enter-result 3 "if" (cons 'calcFunc-if (calc-top-list-n 3)))))
106 (defun calc-timing (n)
109 (calc-change-mode 'calc-timing n nil t)
110 (message (if calc-timing
111 "Reporting timing of slow commands in Trail"
112 "Not reporting timing of commands"))))
114 (defun calc-pass-errors ()
116 ;; The following two cases are for the new, optimizing byte compiler
117 ;; or the standard 18.57 byte compiler, respectively.
119 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 15)))
120 (or (memq (car-safe (car-safe place)) '(error xxxerror))
121 (setq place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 27)))
122 (or (memq (car (car place)) '(error xxxerror))
124 (setcar (car place) 'xxxerror))
125 (error (error "The calc-do function has been modified; unable to patch"))))
127 (defun calc-user-define ()
129 (message "Define user key: z-")
130 (let ((key (read-char)))
131 (if (= (calc-user-function-classify key) 0)
132 (error "Can't redefine \"?\" key"))
133 (let ((func (intern (completing-read (concat "Set key z "
140 (let* ((kmap (calc-user-key-map))
141 (old (assq key kmap)))
144 (setcdr kmap (cons (cons key func) (cdr kmap))))))))
146 (defun calc-user-undefine ()
148 (message "Undefine user key: z-")
149 (let ((key (read-char)))
150 (if (= (calc-user-function-classify key) 0)
151 (error "Can't undefine \"?\" key"))
152 (let* ((kmap (calc-user-key-map)))
153 (delq (or (assq key kmap)
154 (assq (upcase key) kmap)
155 (assq (downcase key) kmap)
156 (error "No such user key is defined"))
160 ;; math-integral-cache-state is originally declared in calcalg2.el,
161 ;; it is used in calc-user-define-variable.
162 (defvar math-integral-cache-state)
164 ;; calc-user-formula-alist is local to calc-user-define-formula,
165 ;; calc-user-define-compostion and calc-finish-formula-edit,
166 ;; but is used by calc-fix-user-formula.
167 (defvar calc-user-formula-alist)
169 (defun calc-user-define-formula ()
172 (let* ((form (calc-top 1))
174 (is-lambda (and (eq (car-safe form) 'calcFunc-lambda)
175 (>= (length form) 2)))
176 odef key keyname cmd cmd-base func calc-user-formula-alist is-symb)
178 (setq arglist (mapcar (function (lambda (x) (nth 1 x)))
179 (nreverse (cdr (reverse (cdr form)))))
180 form (nth (1- (length form)) form))
181 (calc-default-formula-arglist form)
182 (setq arglist (sort arglist 'string-lessp)))
183 (message "Define user key: z-")
184 (setq key (read-char))
185 (if (= (calc-user-function-classify key) 0)
186 (error "Can't redefine \"?\" key"))
187 (setq key (and (not (memq key '(13 32))) key)
189 (if (or (and (<= ?0 key) (<= key ?9))
190 (and (<= ?a key) (<= key ?z))
191 (and (<= ?A key) (<= key ?Z)))
193 (format "%03d" key)))
194 odef (assq key (calc-user-key-map)))
197 (setq cmd (completing-read "Define M-x command name: "
198 obarray 'commandp nil
199 (if (and odef (symbolp (cdr odef)))
200 (symbol-name (cdr odef))
202 cmd-base (and (string-match "\\`calc-\\(.+\\)\\'" cmd)
203 (math-match-substring cmd 1))
204 cmd (and (not (or (string-equal cmd "")
205 (string-equal cmd "calc-")))
212 (if (get cmd 'calc-user-defn)
213 (concat "Replace previous definition for "
214 (symbol-name cmd) "? ")
215 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
216 (if (and key (not cmd))
217 (setq cmd (intern (concat "calc-User-" keyname))))
220 (setq func (completing-read "Define algebraic function name: "
225 "\\`User-.+" cmd-base)
228 (substring cmd-base 5))
231 func (and (not (or (string-equal func "")
232 (string-equal func "calcFunc-")))
240 (if (get func 'calc-user-defn)
241 (concat "Replace previous definition for "
242 (symbol-name func) "? ")
243 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
245 (setq func (intern (concat "calcFunc-User"
247 (and cmd (symbol-name cmd))
248 (format "%05d" (% (random) 10000)))))))
250 (setq calc-user-formula-alist arglist)
253 (setq calc-user-formula-alist
254 (read-from-minibuffer "Function argument list: "
256 (prin1-to-string arglist)
260 (and (not (calc-subsetp calc-user-formula-alist arglist))
262 "Okay for arguments that don't appear in formula to be ignored? "))))))
263 (setq is-symb (and calc-user-formula-alist
266 "Leave it symbolic for non-constant arguments? ")))
267 (setq calc-user-formula-alist
268 (mapcar (function (lambda (x)
269 (or (cdr (assq x '((nil . arg-nil)
271 x))) calc-user-formula-alist))
280 (list 'calc-enter-result
281 (length calc-user-formula-alist)
282 (let ((name (symbol-name (or func cmd))))
284 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
286 (math-match-substring name 1)))
289 (list 'calc-top-list-n
290 (length calc-user-formula-alist)))))))
291 (put cmd 'calc-user-defn t)))
292 (let ((body (list 'math-normalize (calc-fix-user-formula form))))
295 (list 'lambda calc-user-formula-alist)
297 (mapcar (function (lambda (v)
298 (list 'math-check-const v t)))
299 calc-user-formula-alist))
301 (put func 'calc-user-defn form)
302 (setq math-integral-cache-state nil)
304 (let* ((kmap (calc-user-key-map))
305 (old (assq key kmap)))
308 (setcdr kmap (cons (cons key cmd) (cdr kmap)))))))
311 (defun calc-default-formula-arglist (form)
313 (if (eq (car form) 'var)
314 (if (or (memq (nth 1 form) arglist)
315 (math-const-var form))
317 (setq arglist (cons (nth 1 form) arglist)))
318 (calc-default-formula-arglist-step (cdr form)))))
320 (defun calc-default-formula-arglist-step (l)
323 (calc-default-formula-arglist (car l))
324 (calc-default-formula-arglist-step (cdr l)))))
326 (defun calc-subsetp (a b)
328 (and (memq (car a) b)
329 (calc-subsetp (cdr a) b))))
331 (defun calc-fix-user-formula (f)
334 (cond ((and (eq (car f) 'var)
335 (memq (setq temp (or (cdr (assq (nth 1 f) '((nil . arg-nil)
338 calc-user-formula-alist))
340 ((or (math-constp f) (eq (car f) 'var))
342 ((and (eq (car f) 'calcFunc-eval)
344 (list 'let '((calc-simplify-mode nil))
345 (list 'math-normalize (calc-fix-user-formula (nth 1 f)))))
346 ((and (eq (car f) 'calcFunc-evalsimp)
348 (list 'math-simplify (calc-fix-user-formula (nth 1 f))))
349 ((and (eq (car f) 'calcFunc-evalextsimp)
351 (list 'math-simplify-extended
352 (calc-fix-user-formula (nth 1 f))))
355 (cons (list 'quote (car f))
356 (mapcar 'calc-fix-user-formula (cdr f)))))))
359 (defun calc-user-define-composition ()
362 (if (eq calc-language 'unform)
363 (error "Can't define formats for unformatted mode"))
364 (let* ((comp (calc-top 1))
365 (func (intern (completing-read "Define format for which function: "
366 obarray 'fboundp nil "calcFunc-")))
367 (comps (get func 'math-compose-forms))
370 (calc-user-formula-alist nil))
371 (if (math-zerop comp)
372 (if (setq entry (assq calc-language comps))
373 (put func 'math-compose-forms (delq entry comps)))
374 (calc-default-formula-arglist comp)
375 (setq arglist (sort arglist 'string-lessp))
378 (setq calc-user-formula-alist
379 (read-from-minibuffer "Composition argument list: "
381 (prin1-to-string arglist)
385 (and (not (calc-subsetp calc-user-formula-alist arglist))
387 "Okay for arguments that don't appear in formula to be invisible? "))))
388 (or (setq entry (assq calc-language comps))
389 (put func 'math-compose-forms
390 (cons (setq entry (list calc-language)) comps)))
391 (or (setq entry2 (assq (length calc-user-formula-alist) (cdr entry)))
394 (list (length calc-user-formula-alist))) (cdr entry))))
396 (list 'lambda calc-user-formula-alist (calc-fix-user-formula comp))))
401 (defun calc-user-define-kbd-macro (arg)
404 (error "No keyboard macro defined"))
405 (message "Define last kbd macro on user key: z-")
406 (let ((key (read-char)))
407 (if (= (calc-user-function-classify key) 0)
408 (error "Can't redefine \"?\" key"))
409 (let ((cmd (intern (completing-read "Full name for new command: "
414 (if (or (and (>= key ?a)
421 (format "%03d" key)))))))
423 (not (let ((f (symbol-function cmd)))
426 (eq (car-safe (nth 3 f))
427 'calc-execute-kbd-macro)))))
428 (error "Function %s is already defined and not a keyboard macro"
430 (put cmd 'calc-user-defn t)
431 (fset cmd (if (< (prefix-numeric-value arg) 0)
436 (list 'calc-execute-kbd-macro
437 (vector (key-description last-kbd-macro)
440 (format "z%c" key)))))
441 (let* ((kmap (calc-user-key-map))
442 (old (assq key kmap)))
445 (setcdr kmap (cons (cons key cmd) (cdr kmap))))))))
448 (defun calc-edit-user-syntax ()
451 (let ((lang calc-language))
452 (calc-edit-mode (list 'calc-finish-user-syntax-edit (list 'quote lang))
454 (format "Editing %s-Mode Syntax Table"
455 (cond ((null lang) "Normal")
456 ((eq lang 'tex) "TeX")
457 (t (capitalize (symbol-name lang))))))
458 (calc-write-parse-table (cdr (assq lang calc-user-parse-tables))
460 (calc-show-edit-buffer))
462 (defvar calc-original-buffer)
464 (defun calc-finish-user-syntax-edit (lang)
465 (let ((tab (calc-read-parse-table calc-original-buffer lang))
466 (entry (assq lang calc-user-parse-tables)))
469 (car (setq calc-user-parse-tables
470 (cons (list lang) calc-user-parse-tables))))
473 (setq calc-user-parse-tables
474 (delq entry calc-user-parse-tables)))))
475 (switch-to-buffer calc-original-buffer))
477 ;; The variable calc-lang is local to calc-write-parse-table, but is
478 ;; used by calc-write-parse-table-part which is called by
479 ;; calc-write-parse-table. The variable is also local to
480 ;; calc-read-parse-table, but is used by calc-fix-token-name which
481 ;; is called (indirectly) by calc-read-parse-table.
484 (defun calc-write-parse-table (tab calc-lang)
487 (calc-write-parse-table-part (car (car p)))
489 (let ((math-format-hash-args t))
490 (math-format-flat-expr (cdr (car p)) 0))
494 (defun calc-write-parse-table-part (p)
496 (cond ((stringp (car p))
498 (if (and (string-match "\\`\\\\dots\\>" s)
499 (not (eq calc-lang 'tex)))
500 (setq s (concat ".." (substring s 5))))
501 (if (or (and (string-match
502 "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s)
503 (string-match "[^a-zA-Z0-9\\]" s))
504 (and (assoc s '((")") ("]") (">")))
506 (insert (prin1-to-string s) " ")
511 (insert "/" (int-to-string (car p))))
513 ((and (eq (car (car p)) '\?) (equal (car (nth 2 (car p))) "$$"))
514 (insert (car (nth 1 (car p))) " "))
517 (calc-write-parse-table-part (nth 1 (car p)))
518 (insert "}" (symbol-name (car (car p))))
520 (calc-write-parse-table-part (list (car (nth 2 (car p)))))
524 (defun calc-read-parse-table (calc-buf calc-lang)
527 (skip-chars-forward "\n\t ")
529 (if (looking-at "%%")
532 (p (calc-read-parse-table-part ":=[\n\t ]+" ":=")))
533 (or (stringp (car p))
534 (and (integerp (car p))
538 (error "Malformed syntax rule")))
541 (let* ((str (buffer-substring pos (point)))
543 (set-buffer calc-buf)
544 (let ((calc-user-parse-tables nil)
546 (math-expr-opers math-standard-opers)
547 (calc-hashes-used 0))
549 (if (string-match ",[ \t]*\\'" str)
550 (substring str 0 (match-beginning 0))
552 (if (eq (car-safe exp) 'error)
554 (goto-char (+ pos (nth 1 exp)))
555 (error (nth 2 exp))))
556 (setq tab (nconc tab (list (cons p exp)))))))))
559 (defun calc-fix-token-name (name &optional unquoted)
560 (cond ((string-match "\\`\\.\\." name)
561 (concat "\\dots" (substring name 2)))
562 ((and (equal name "{") (memq calc-lang '(tex eqn)))
564 ((and (equal name "}") (memq calc-lang '(tex eqn)))
566 ((and (equal name "&") (eq calc-lang 'tex))
569 (search-backward "#")
570 (error "Token '#' is reserved"))
571 ((and unquoted (string-match "#" name))
572 (error "Tokens containing '#' must be quoted"))
573 ((not (string-match "[^ ]" name))
574 (search-backward "\"" nil t)
575 (error "Blank tokens are not allowed"))
578 (defun calc-read-parse-table-part (term eterm)
582 (skip-chars-forward "\n\t ")
583 (if (eobp) (error "Expected '%s'" eterm))
584 (not (looking-at term)))
585 (cond ((looking-at "%%")
587 ((looking-at "{[\n\t ]")
589 (let ((p (calc-read-parse-table-part "}" "}")))
590 (or (looking-at "[+*?]")
591 (error "Expected '+', '*', or '?'"))
592 (let ((sym (intern (buffer-substring (point) (1+ (point))))))
594 (looking-at "[^\n\t ]*")
595 (let ((sep (buffer-substring (point) (match-end 0))))
596 (goto-char (match-end 0))
597 (and (eq sym '\?) (> (length sep) 0)
598 (not (equal sep "$")) (not (equal sep "."))
599 (error "Separator not allowed with { ... }?"))
600 (if (string-match "\\`\"" sep)
601 (setq sep (read-from-string sep)))
602 (setq sep (calc-fix-token-name sep))
603 (setq part (nconc part
605 (and (> (length sep) 0)
606 (cons sep p))))))))))
608 (error "Too many }'s"))
610 (setq quoted (calc-fix-token-name (read (current-buffer)))
611 part (nconc part (list quoted))))
612 ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]")
613 (setq part (nconc part (list (if (= (match-beginning 1)
618 (1+ (match-beginning 1))
620 (goto-char (match-end 0)))
621 ((looking-at ":=[\n\t ]")
622 (error "Misplaced ':='"))
624 (looking-at "[^\n\t ]*")
625 (let ((end (match-end 0)))
626 (setq part (nconc part (list (calc-fix-token-name
630 (goto-char (match-end 0))
631 (let ((len (length part)))
632 (while (and (> len 1)
633 (let ((last (nthcdr (setq len (1- len)) part)))
634 (and (assoc (car last) '((")") ("]") (">")))
635 (not (eq (car last) quoted))
637 (list '\? (list (car last)) '("$$"))))))))
641 (defun calc-user-define-invocation ()
644 (error "No keyboard macro defined"))
645 (setq calc-invocation-macro last-kbd-macro)
646 (message "Use `M-# Z' to invoke this macro"))
649 (defun calc-user-define-edit (prefix)
650 (interactive "P") ; but no calc-wrapper!
651 (message "Edit definition of command: z-")
652 (let* ((key (read-char))
653 (def (or (assq key (calc-user-key-map))
654 (assq (upcase key) (calc-user-key-map))
655 (assq (downcase key) (calc-user-key-map))
656 (error "No command defined for that key")))
659 (setq cmd (symbol-function cmd)))
660 (cond ((or (stringp cmd)
662 (eq (car-safe (nth 3 cmd)) 'calc-execute-kbd-macro)))
663 (if (and (>= (prefix-numeric-value prefix) 0)
664 (fboundp 'edit-kbd-macro)
666 (eq major-mode 'calc-mode))
668 (if (and (< (window-width) (frame-width))
670 (let ((win (get-buffer-window (calc-trail-buffer))))
672 (delete-window win))))
673 (edit-kbd-macro (cdr def) prefix nil
676 (and calc-display-trail
678 (calc-trail-display 1 t)))))
681 (if (stringp (symbol-function cmd))
682 (symbol-function cmd)
683 (let ((mac (nth 1 (nth 3 (symbol-function
690 (if (stringp (symbol-function cmd))
692 (let ((mac (cdr (nth 3 (symbol-function
694 (if (vectorp (car mac))
697 (key-description new))
698 (aset (car mac) 1 new))
699 (setcar mac new))))))))
700 (let ((keys (progn (and (fboundp 'edit-kbd-macro)
701 (edit-kbd-macro nil))
702 (fboundp 'MacEdit-parse-keys))))
704 (calc-edit-mode (list 'calc-finish-macro-edit
712 (insert "Notations: RET, SPC, TAB, DEL, LFD, NUL"
713 ", C-xxx, M-xxx.\n\n")
715 (insert (if (stringp cmd)
716 (key-description cmd)
717 (if (vectorp (nth 1 (nth 3 cmd)))
718 (aref (nth 1 (nth 3 cmd)) 0)
719 (key-description (nth 1 (nth 3 cmd)))))
721 (if (>= (prog2 (forward-char -1)
725 (fill-region top (point))))
726 (insert "Press C-q to quote control characters like RET"
730 (if (vectorp (nth 1 (nth 3 cmd)))
731 (aref (nth 1 (nth 3 cmd)) 1)
732 (nth 1 (nth 3 cmd)))))))
733 (calc-show-edit-buffer)
734 (forward-line (if keys 2 1)))))
735 (t (let* ((func (calc-stack-command-p cmd))
738 (get func 'calc-user-defn))))
739 (if (and defn (calc-valid-formula-func func))
742 (calc-edit-mode (list 'calc-finish-formula-edit
744 (insert (math-showing-full-precision
745 (math-format-nice-expr defn (frame-width)))
747 (calc-show-edit-buffer))
748 (error "That command's definition cannot be edited")))))))
750 (defun calc-finish-macro-edit (def keys)
752 (if (and keys (looking-at "\n")) (forward-line 1))
753 (let* ((true-str (buffer-substring (point) (point-max)))
755 (if keys (setq str (MacEdit-parse-keys str)))
756 (if (symbolp (cdr def))
757 (if (stringp (symbol-function (cdr def)))
759 (let ((mac (cdr (nth 3 (symbol-function (cdr def))))))
760 (if (vectorp (car mac))
762 (aset (car mac) 0 (if keys true-str (key-description str)))
763 (aset (car mac) 1 str))
767 ;;; The following are hooks into the MacEdit package from macedit.el.
768 (put 'calc-execute-extended-command 'MacEdit-print
770 (setq macro-str (concat "\excalc-" macro-str)))))
772 (put 'calcDigit-start 'MacEdit-print
774 (if calc-algebraic-mode
775 (calc-macro-edit-algebraic)
776 (MacEdit-unread-chars key-last)
780 (while (and (setq ch (MacEdit-read-char))
781 (or (and (>= ch ?0) (<= ch ?9))
782 (memq ch '(?\. ?e ?\_ ?n ?\: ?\# ?M
784 (and (memq ch '(?\' ?m ?s))
785 (string-match "[@oh]" str))
786 (and (or (and (>= ch ?a) (<= ch ?z))
787 (and (>= ch ?A) (<= ch ?Z)))
789 "^[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#"
791 (and (memq ch '(?\177 ?\C-h))
793 (and (memq ch '(?+ ?-))
795 (eq (aref str (1- (length str)))
797 (if (or (and (>= ch ?0) (<= ch ?9))
798 (and (or (not (memq ch '(?\177 ?\C-h)))
799 (<= (length str) min-bsp))
800 (setq min-bsp (1+ (length str)))))
801 (setq str (concat str (char-to-string ch)))
802 (setq str (substring str 0 -1))))
803 (if (memq ch '(32 10 13))
804 (setq str (concat str (char-to-string ch)))
805 (MacEdit-unread-chars ch))
807 (MacEdit-insert-string str)
810 (defun calc-macro-edit-algebraic ()
811 (MacEdit-unread-chars key-last)
815 (MacEdit-lookup-key calc-alg-ent-map)
816 (or (and (memq key-symbol '(self-insert-command
820 '(backward-delete-char
822 backward-delete-char-untabify))
824 (setq macro-str (substring macro-str (length key-str)))
825 (if (or (eq key-symbol 'self-insert-command)
826 (and (or (not (memq key-symbol '(backward-delete-char
828 backward-delete-char-untabify)))
829 (<= (length str) min-bsp))
830 (setq min-bsp (+ (length str) (length key-str)))))
831 (setq str (concat str key-str))
832 (setq str (substring str 0 -1))))
833 (if (memq key-last '(10 13))
834 (setq str (concat str key-str)
835 macro-str (substring macro-str (length key-str))))
836 (if (> (length str) 0)
839 (MacEdit-insert-string str)
841 (put 'calc-algebraic-entry 'MacEdit-print 'calc-macro-edit-algebraic)
842 (put 'calc-auto-algebraic-entry 'MacEdit-print 'calc-macro-edit-algebraic)
844 (defun calc-macro-edit-variable (&optional no-cmd)
846 (or no-cmd (insert (symbol-name key-symbol) "\n"))
847 (if (memq (MacEdit-peek-char) '(?\+ ?\- ?\* ?\/ ?^ ?\|))
848 (setq str (char-to-string (MacEdit-read-char))))
849 (if (and (setq ch (MacEdit-peek-char))
850 (>= ch ?0) (<= ch ?9))
851 (insert "type \"" str
852 (char-to-string (MacEdit-read-char)) "\"\n")
853 (if (> (length str) 0)
854 (insert "type \"" str "\"\n"))
855 (MacEdit-read-argument))))
856 (put 'calc-store 'MacEdit-print 'calc-macro-edit-variable)
857 (put 'calc-store-into 'MacEdit-print 'calc-macro-edit-variable)
858 (put 'calc-store-neg 'MacEdit-print 'calc-macro-edit-variable)
859 (put 'calc-store-plus 'MacEdit-print 'calc-macro-edit-variable)
860 (put 'calc-store-minus 'MacEdit-print 'calc-macro-edit-variable)
861 (put 'calc-store-times 'MacEdit-print 'calc-macro-edit-variable)
862 (put 'calc-store-div 'MacEdit-print 'calc-macro-edit-variable)
863 (put 'calc-store-power 'MacEdit-print 'calc-macro-edit-variable)
864 (put 'calc-store-concat 'MacEdit-print 'calc-macro-edit-variable)
865 (put 'calc-store-inv 'MacEdit-print 'calc-macro-edit-variable)
866 (put 'calc-store-decr 'MacEdit-print 'calc-macro-edit-variable)
867 (put 'calc-store-incr 'MacEdit-print 'calc-macro-edit-variable)
868 (put 'calc-store-exchange 'MacEdit-print 'calc-macro-edit-variable)
869 (put 'calc-unstore 'MacEdit-print 'calc-macro-edit-variable)
870 (put 'calc-recall 'MacEdit-print 'calc-macro-edit-variable)
871 (put 'calc-let 'MacEdit-print 'calc-macro-edit-variable)
872 (put 'calc-permanent-variable 'MacEdit-print 'calc-macro-edit-variable)
874 (defun calc-macro-edit-variable-2 ()
875 (calc-macro-edit-variable)
876 (calc-macro-edit-variable t))
877 (put 'calc-copy-variable 'MacEdit-print 'calc-macro-edit-variable-2)
878 (put 'calc-declare-variable 'MacEdit-print 'calc-macro-edit-variable-2)
880 (defun calc-macro-edit-quick-digit ()
881 (insert "type \"" key-str "\" # " (symbol-name key-symbol) "\n"))
882 (put 'calc-store-quick 'MacEdit-print 'calc-macro-edit-quick-digit)
883 (put 'calc-store-into-quick 'MacEdit-print 'calc-macro-edit-quick-digit)
884 (put 'calc-recall-quick 'MacEdit-print 'calc-macro-edit-quick-digit)
885 (put 'calc-select-part 'MacEdit-print 'calc-macro-edit-quick-digit)
886 (put 'calc-clean-num 'MacEdit-print 'calc-macro-edit-quick-digit)
889 (defun calc-finish-formula-edit (func)
890 (let ((buf (current-buffer))
891 (str (buffer-substring (point) (point-max)))
893 (body (calc-valid-formula-func func)))
894 (set-buffer calc-original-buffer)
895 (let ((val (math-read-expr str)))
896 (if (eq (car-safe val) 'error)
899 (goto-char (+ start (nth 1 val)))
900 (error (nth 2 val))))
902 (let ((calc-user-formula-alist (nth 1 (symbol-function func))))
903 (calc-fix-user-formula val)))
904 (put func 'calc-user-defn val))))
906 (defun calc-valid-formula-func (func)
907 (let ((def (symbol-function func)))
909 (eq (car def) 'lambda)
911 (setq def (cdr (cdr def)))
913 (not (eq (car (car def)) 'math-normalize)))
914 (setq def (cdr def)))
918 (defun calc-get-user-defn ()
921 (message "Get definition of command: z-")
922 (let* ((key (read-char))
923 (def (or (assq key (calc-user-key-map))
924 (assq (upcase key) (calc-user-key-map))
925 (assq (downcase key) (calc-user-key-map))
926 (error "No command defined for that key")))
929 (setq cmd (symbol-function cmd)))
931 (message "Keyboard macro: %s" cmd))
932 (t (let* ((func (calc-stack-command-p cmd))
935 (get func 'calc-user-defn))))
938 (and (calc-valid-formula-func func)
939 (setq defn (append '(calcFunc-lambda)
940 (mapcar 'math-build-var-name
941 (nth 1 (symbol-function
944 (calc-enter-result 0 "gdef" defn))
945 (error "That command is not defined by a formula"))))))))
948 (defun calc-user-define-permanent ()
951 (message "Record in %s the command: z-" calc-settings-file)
952 (let* ((key (read-char))
953 (def (or (assq key (calc-user-key-map))
954 (assq (upcase key) (calc-user-key-map))
955 (assq (downcase key) (calc-user-key-map))
958 (intern (completing-read
959 (format "Record in %s the function: "
961 obarray 'fboundp nil "calcFunc-"))))
962 (error "No command defined for that key"))))
963 (set-buffer (find-file-noselect (substitute-in-file-name
964 calc-settings-file)))
965 (goto-char (point-max))
966 (let* ((cmd (cdr def))
967 (fcmd (and cmd (symbolp cmd) (symbol-function cmd)))
973 (insert "\n;;; Definition stored by Calc on " (current-time-string)
974 "\n(put 'calc-define '"
975 (if (symbolp cmd) (symbol-name cmd) (format "key%d" key))
978 (eq (car-safe fcmd) 'lambda)
979 (get cmd 'calc-user-defn))
981 (and (eq (car-safe (nth 3 fcmd)) 'calc-execute-kbd-macro)
982 (vectorp (nth 1 (nth 3 fcmd)))
983 (progn (and (fboundp 'edit-kbd-macro)
984 (edit-kbd-macro nil))
985 (fboundp 'MacEdit-parse-keys))
987 (aset (nth 1 (nth 3 fcmd)) 1 nil))
988 (insert (setq str (prin1-to-string
989 (cons 'defun (cons cmd (cdr fcmd)))))
991 (or (and (string-match "\"" str) (not q-ok))
992 (fill-region pt (point)))
993 (indent-rigidly pt (point) 2)
994 (delete-region pt (1+ pt))
995 (insert " (put '" (symbol-name cmd)
997 (prin1-to-string (get cmd 'calc-user-defn))
999 (setq func (calc-stack-command-p cmd))
1000 (let ((ffunc (and func (symbolp func) (symbol-function func)))
1003 (eq (car-safe ffunc) 'lambda)
1004 (get func 'calc-user-defn)
1006 (insert (setq str (prin1-to-string
1007 (cons 'defun (cons func
1010 (or (and (string-match "\"" str) (not q-ok))
1011 (fill-region pt (point)))
1012 (indent-rigidly pt (point) 2)
1013 (delete-region pt (1+ pt))
1015 (insert "(put '" (symbol-name func)
1016 " 'calc-user-defn '"
1017 (prin1-to-string (get func 'calc-user-defn))
1019 (fill-region pt (point))
1020 (indent-rigidly pt (point) 2)
1021 (delete-region pt (1+ pt))))))
1023 (insert " (fset '" (prin1-to-string cmd)
1024 " " (prin1-to-string fcmd) ")\n")))
1025 (or func (setq func (and cmd (symbolp cmd) (fboundp cmd) cmd)))
1026 (if (get func 'math-compose-forms)
1028 (insert "(put '" (symbol-name cmd)
1029 " 'math-compose-forms '"
1030 (prin1-to-string (get func 'math-compose-forms))
1032 (fill-region pt (point))
1033 (indent-rigidly pt (point) 2)
1034 (delete-region pt (1+ pt))))
1036 (insert " (define-key calc-mode-map "
1037 (prin1-to-string (concat "z" (char-to-string key)))
1039 (prin1-to-string cmd)
1044 (defun calc-stack-command-p (cmd)
1045 (if (and cmd (symbolp cmd))
1047 (calc-stack-command-p (symbol-function cmd)))
1049 (eq (car cmd) 'lambda)
1050 (setq cmd (or (assq 'calc-wrapper cmd)
1051 (assq 'calc-slow-wrapper cmd)))
1052 (setq cmd (assq 'calc-enter-result cmd))
1053 (memq (car (nth 3 cmd)) '(cons list))
1054 (eq (car (nth 1 (nth 3 cmd))) 'quote)
1055 (nth 1 (nth 1 (nth 3 cmd))))))
1058 (defun calc-call-last-kbd-macro (arg)
1060 (and defining-kbd-macro
1061 (error "Can't execute anonymous macro while defining one"))
1063 (error "No kbd macro has been defined"))
1064 (calc-execute-kbd-macro last-kbd-macro arg))
1066 (defun calc-execute-kbd-macro (mac arg &rest prefix)
1067 (if (and (vectorp mac) (> (length mac) 0) (stringp (aref mac 0)))
1068 (setq mac (or (aref mac 1)
1069 (aset mac 1 (progn (and (fboundp 'edit-kbd-macro)
1070 (edit-kbd-macro nil))
1071 (MacEdit-parse-keys (aref mac 0)))))))
1072 (if (< (prefix-numeric-value arg) 0)
1073 (execute-kbd-macro mac (- (prefix-numeric-value arg)))
1074 (if calc-executing-macro
1075 (execute-kbd-macro mac arg)
1077 (let ((old-stack-whole (copy-sequence calc-stack))
1078 (old-stack-top calc-stack-top)
1079 (old-buffer-size (buffer-size))
1080 (old-refresh-count calc-refresh-count))
1082 (let ((calc-executing-macro mac))
1083 (execute-kbd-macro mac arg))
1084 (calc-select-buffer)
1085 (let ((new-stack (reverse calc-stack))
1086 (old-stack (reverse old-stack-whole)))
1087 (while (and new-stack old-stack
1088 (equal (car new-stack) (car old-stack)))
1089 (setq new-stack (cdr new-stack)
1090 old-stack (cdr old-stack)))
1091 (or (equal prefix '(nil))
1092 (calc-record-list (if (> (length new-stack) 1)
1093 (mapcar 'car new-stack)
1095 (or (car prefix) "kmac")))
1096 (calc-record-undo (list 'set 'saved-stack-top old-stack-top))
1098 (calc-record-undo (list 'pop 1 (mapcar 'car old-stack))))
1099 (let ((calc-stack old-stack-whole)
1101 (calc-cursor-stack-index (length old-stack)))
1102 (if (and (= old-buffer-size (buffer-size))
1103 (= old-refresh-count calc-refresh-count))
1104 (let ((buffer-read-only nil))
1105 (delete-region (point) (point-max))
1107 (calc-record-undo (list 'push 1))
1108 (insert (math-format-stack-value (car new-stack)) "\n")
1109 (setq new-stack (cdr new-stack)))
1110 (calc-renumber-stack))
1112 (calc-record-undo (list 'push 1))
1113 (setq new-stack (cdr new-stack)))
1115 (calc-record-undo (list 'set 'saved-stack-top 0)))))))))
1117 (defun calc-push-list-in-macro (vals m sels)
1118 (let ((entry (list (car vals) 1 (car sels)))
1119 (mm (+ (or m 1) calc-stack-top)))
1121 (setcdr (nthcdr (- mm 2) calc-stack)
1122 (cons entry (nthcdr (1- mm) calc-stack)))
1123 (setq calc-stack (cons entry calc-stack)))))
1125 (defun calc-pop-stack-in-macro (n mm)
1127 (setcdr (nthcdr (- mm 2) calc-stack)
1128 (nthcdr (+ n mm -1) calc-stack))
1129 (setq calc-stack (nthcdr n calc-stack))))
1132 (defun calc-kbd-if ()
1135 (let ((cond (calc-top-n 1)))
1137 (if (math-is-true cond)
1138 (if defining-kbd-macro
1139 (message "If true.."))
1140 (if defining-kbd-macro
1141 (message "Condition is false; skipping to Z: or Z] ..."))
1142 (calc-kbd-skip-to-else-if t)))))
1144 (defun calc-kbd-else-if ()
1148 (defun calc-kbd-skip-to-else-if (else-okay)
1152 (setq ch (read-char))
1154 (error "Unterminated Z[ in keyboard macro"))
1157 (setq ch (read-char))
1159 (setq count (1+ count)))
1161 (setq count (1- count)))
1167 (keyboard-quit))))))
1168 (and defining-kbd-macro
1171 (message "End-if...")))))
1173 (defun calc-kbd-end-if ()
1175 (if defining-kbd-macro
1176 (message "End-if...")))
1178 (defun calc-kbd-else ()
1180 (if defining-kbd-macro
1181 (message "Else; skipping to Z] ..."))
1182 (calc-kbd-skip-to-else-if nil))
1185 (defun calc-kbd-repeat ()
1189 (setq count (math-trunc (calc-top-n 1)))
1190 (or (Math-integerp count)
1191 (error "Count must be an integer"))
1192 (if (Math-integer-negp count)
1194 (or (integerp count)
1195 (setq count 1000000))
1197 (calc-kbd-loop count)))
1199 (defun calc-kbd-for (dir)
1203 (setq init (calc-top-n 2)
1204 final (calc-top-n 1))
1205 (or (and (math-anglep init) (math-anglep final))
1206 (error "Initial and final values must be real numbers"))
1208 (calc-kbd-loop nil init final (and dir (prefix-numeric-value dir)))))
1210 (defun calc-kbd-loop (rpt-count &optional initial final dir)
1212 (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000))
1216 (open last-command-char)
1219 (or executing-kbd-macro
1220 (message "Reading loop body..."))
1222 (setq ch (read-char))
1224 (error "Unterminated Z%c in keyboard macro" open))
1227 (setq ch (read-char)
1228 body (concat body "Z" (char-to-string ch)))
1229 (cond ((memq ch '(?\< ?\( ?\{))
1230 (setq count (1+ count)))
1231 ((memq ch '(?\> ?\) ?\}))
1232 (setq count (1- count)))
1235 (setq parts (nconc parts (list (concat (substring body 0 -2)
1240 (setq body (concat body (char-to-string ch)))))
1241 (if (/= ch (cdr (assq open '( (?\< . ?\>) (?\( . ?\)) (?\{ . ?\}) ))))
1242 (error "Mismatched Z%c and Z%c in keyboard macro" open ch))
1243 (or executing-kbd-macro
1244 (message "Looping..."))
1245 (setq body (concat (substring body 0 -2) "Z]"))
1246 (and (not executing-kbd-macro)
1247 (= rpt-count 1000000)
1251 (message "Warning: Infinite loop! Not executing")
1252 (setq rpt-count 0)))
1253 (or (not initial) dir
1254 (setq dir (math-compare final initial)))
1256 (while (> rpt-count 0)
1259 (if (cond ((eq dir 0) (Math-equal final counter))
1260 ((eq dir 1) (Math-lessp final counter))
1261 ((eq dir -1) (Math-lessp counter final)))
1263 (calc-push counter)))
1264 (while (and part (> rpt-count 0))
1265 (execute-kbd-macro (car part))
1266 (if (math-is-true (calc-top-n 1))
1268 (setq part (cdr part)))
1272 (execute-kbd-macro body)
1274 (let ((step (calc-top-n 1)))
1276 (setq counter (calcFunc-add counter step)))
1277 (setq rpt-count (1- rpt-count))))))))
1278 (or executing-kbd-macro
1279 (message "Looping...done"))))
1281 (defun calc-kbd-end-repeat ()
1283 (error "Unbalanced Z> in keyboard macro"))
1285 (defun calc-kbd-end-for ()
1287 (error "Unbalanced Z) in keyboard macro"))
1289 (defun calc-kbd-end-loop ()
1291 (error "Unbalanced Z} in keyboard macro"))
1293 (defun calc-kbd-break ()
1296 (let ((cond (calc-top-n 1)))
1298 (if (math-is-true cond)
1299 (error "Keyboard macro aborted")))))
1302 (defvar calc-kbd-push-level 0)
1304 ;; The variables var-q0 through var-q9 are the "quick" variables.
1316 (defun calc-kbd-push (arg)
1319 (let* ((defs (and arg (> (prefix-numeric-value arg) 0)))
1330 (calc-internal-prec (if defs 12 calc-internal-prec))
1331 (calc-word-size (if defs 32 calc-word-size))
1332 (calc-angle-mode (if defs 'deg calc-angle-mode))
1333 (calc-simplify-mode (if defs nil calc-simplify-mode))
1334 (calc-algebraic-mode (if arg nil calc-algebraic-mode))
1335 (calc-incomplete-algebraic-mode (if arg nil
1336 calc-incomplete-algebraic-mode))
1337 (calc-symbolic-mode (if defs nil calc-symbolic-mode))
1338 (calc-matrix-mode (if defs nil calc-matrix-mode))
1339 (calc-prefer-frac (if defs nil calc-prefer-frac))
1340 (calc-complex-mode (if defs nil calc-complex-mode))
1341 (calc-infinite-mode (if defs nil calc-infinite-mode))
1345 (if (or executing-kbd-macro defining-kbd-macro)
1347 (if defining-kbd-macro
1348 (message "Reading body..."))
1350 (setq ch (read-char))
1352 (error "Unterminated Z` in keyboard macro"))
1355 (setq ch (read-char)
1356 body (concat body "Z" (char-to-string ch)))
1358 (setq count (1+ count)))
1360 (setq count (1- count)))
1363 (setq body (concat body (char-to-string ch)))))
1364 (if defining-kbd-macro
1365 (message "Reading body...done"))
1366 (let ((calc-kbd-push-level 0))
1367 (execute-kbd-macro (substring body 0 -2))))
1368 (let ((calc-kbd-push-level (1+ calc-kbd-push-level)))
1369 (message "Saving modes; type Z' to restore")
1370 (recursive-edit))))))
1372 (defun calc-kbd-pop ()
1374 (if (> calc-kbd-push-level 0)
1376 (message "Mode settings restored")
1377 (exit-recursive-edit))
1378 (error "Unbalanced Z' in keyboard macro")))
1381 (defun calc-kbd-report (msg)
1382 (interactive "sMessage: ")
1384 (math-working msg (calc-top-n 1))))
1386 (defun calc-kbd-query (msg)
1387 (interactive "sPrompt: ")
1389 (calc-alg-entry nil (and (not (equal msg "")) msg))))
1391 ;;;; Logical operations.
1393 (defun calcFunc-eq (a b &rest more)
1395 (let* ((args (cons a (cons b (copy-sequence more))))
1399 (while (and (cdr p) (not (eq res 0)))
1401 (while (and (setq p2 (cdr p2)) (not (eq res 0)))
1402 (setq res (math-two-eq (car p) (car p2)))
1404 (setcdr p (delq (car p2) (cdr p)))))
1409 (cons 'calcFunc-eq args)
1411 (or (math-two-eq a b)
1412 (if (and (or (math-looks-negp a) (math-zerop a))
1413 (or (math-looks-negp b) (math-zerop b)))
1414 (list 'calcFunc-eq (math-neg a) (math-neg b))
1415 (list 'calcFunc-eq a b)))))
1417 (defun calcFunc-neq (a b &rest more)
1419 (let* ((args (cons a (cons b more)))
1424 (while (and (cdr p) (not (eq res 1)))
1426 (while (and (setq p2 (cdr p2)) (not (eq res 1)))
1427 (setq res (math-two-eq (car p) (car p2)))
1428 (or res (setq all nil)))
1434 (cons 'calcFunc-neq args))))
1435 (or (cdr (assq (math-two-eq a b) '((0 . 1) (1 . 0))))
1436 (if (and (or (math-looks-negp a) (math-zerop a))
1437 (or (math-looks-negp b) (math-zerop b)))
1438 (list 'calcFunc-neq (math-neg a) (math-neg b))
1439 (list 'calcFunc-neq a b)))))
1441 (defun math-two-eq (a b)
1442 (if (eq (car-safe a) 'vec)
1443 (if (eq (car-safe b) 'vec)
1444 (if (= (length a) (length b))
1446 (while (and (setq a (cdr a) b (cdr b)) (not (eq res 0)))
1448 (setq res (math-two-eq (car a) (car b)))
1449 (if (eq (math-two-eq (car a) (car b)) 0)
1453 (if (Math-objectp b)
1456 (if (eq (car-safe b) 'vec)
1457 (if (Math-objectp a)
1460 (let ((res (math-compare a b)))
1463 (if (and (= res 2) (not (and (Math-scalarp a) (Math-scalarp b))))
1467 (defun calcFunc-lt (a b)
1468 (let ((res (math-compare a b)))
1472 (if (and (or (math-looks-negp a) (math-zerop a))
1473 (or (math-looks-negp b) (math-zerop b)))
1474 (list 'calcFunc-gt (math-neg a) (math-neg b))
1475 (list 'calcFunc-lt a b))
1478 (defun calcFunc-gt (a b)
1479 (let ((res (math-compare a b)))
1483 (if (and (or (math-looks-negp a) (math-zerop a))
1484 (or (math-looks-negp b) (math-zerop b)))
1485 (list 'calcFunc-lt (math-neg a) (math-neg b))
1486 (list 'calcFunc-gt a b))
1489 (defun calcFunc-leq (a b)
1490 (let ((res (math-compare a b)))
1494 (if (and (or (math-looks-negp a) (math-zerop a))
1495 (or (math-looks-negp b) (math-zerop b)))
1496 (list 'calcFunc-geq (math-neg a) (math-neg b))
1497 (list 'calcFunc-leq a b))
1500 (defun calcFunc-geq (a b)
1501 (let ((res (math-compare a b)))
1505 (if (and (or (math-looks-negp a) (math-zerop a))
1506 (or (math-looks-negp b) (math-zerop b)))
1507 (list 'calcFunc-leq (math-neg a) (math-neg b))
1508 (list 'calcFunc-geq a b))
1511 (defun calcFunc-rmeq (a)
1512 (if (math-vectorp a)
1513 (math-map-vec 'calcFunc-rmeq a)
1514 (if (assq (car-safe a) calc-tweak-eqn-table)
1515 (if (and (eq (car-safe (nth 2 a)) 'var)
1516 (math-objectp (nth 1 a)))
1519 (if (eq (car-safe a) 'calcFunc-assign)
1521 (if (eq (car-safe a) 'calcFunc-evalto)
1523 (list 'calcFunc-rmeq a))))))
1525 (defun calcFunc-land (a b)
1526 (cond ((Math-zerop a)
1534 (t (list 'calcFunc-land a b))))
1536 (defun calcFunc-lor (a b)
1537 (cond ((Math-zerop a)
1545 (t (list 'calcFunc-lor a b))))
1547 (defun calcFunc-lnot (a)
1550 (if (math-is-true a)
1552 (let ((op (and (= (length a) 3)
1553 (assq (car a) calc-tweak-eqn-table))))
1555 (cons (nth 2 op) (cdr a))
1556 (list 'calcFunc-lnot a))))))
1558 (defun calcFunc-if (c e1 e2)
1561 (if (and (math-is-true c) (not (Math-vectorp c)))
1563 (or (and (Math-vectorp c)
1565 (let ((ee1 (if (Math-vectorp e1)
1566 (if (= (length c) (length e1))
1568 (calc-record-why "*Dimension error" e1))
1570 (ee2 (if (Math-vectorp e2)
1571 (if (= (length c) (length e2))
1573 (calc-record-why "*Dimension error" e2))
1576 (cons 'vec (math-if-vector (cdr c) ee1 ee2)))))
1577 (list 'calcFunc-if c e1 e2)))))
1579 (defun math-if-vector (c e1 e2)
1581 (cons (if (Math-zerop (car c)) (car e2) (car e1))
1582 (math-if-vector (cdr c)
1584 (or (cdr e2) e2)))))
1586 (defun math-normalize-logical-op (a)
1587 (or (and (eq (car a) 'calcFunc-if)
1589 (let ((a1 (math-normalize (nth 1 a))))
1591 (math-normalize (nth 3 a))
1592 (if (Math-numberp a1)
1593 (math-normalize (nth 2 a))
1594 (if (and (Math-vectorp (nth 1 a))
1595 (math-constp (nth 1 a)))
1596 (calcFunc-if (nth 1 a)
1597 (math-normalize (nth 2 a))
1598 (math-normalize (nth 3 a)))
1599 (let ((calc-simplify-mode 'none))
1600 (list 'calcFunc-if a1
1601 (math-normalize (nth 2 a))
1602 (math-normalize (nth 3 a)))))))))
1605 (defun calcFunc-in (a b)
1606 (or (and (eq (car-safe b) 'vec)
1608 (while (and (setq bb (cdr bb))
1609 (not (if (memq (car-safe (car bb)) '(vec intv))
1610 (eq (calcFunc-in a (car bb)) 1)
1611 (Math-equal a (car bb))))))
1612 (if bb 1 (and (math-constp a) (math-constp bb) 0))))
1613 (and (eq (car-safe b) 'intv)
1614 (let ((res (math-compare a (nth 2 b))) res2)
1618 (or (/= (nth 1 b) 2)
1619 (Math-lessp (nth 2 b) (nth 3 b))))
1620 (if (memq (nth 1 b) '(2 3)) 1 0))
1621 ((= (setq res2 (math-compare a (nth 3 b))) 1)
1624 (or (/= (nth 1 b) 1)
1625 (Math-lessp (nth 2 b) (nth 3 b))))
1626 (if (memq (nth 1 b) '(1 3)) 1 0))
1632 (and (Math-equal a b)
1634 (and (math-constp a) (math-constp b)
1636 (list 'calcFunc-in a b)))
1638 (defun calcFunc-typeof (a)
1639 (cond ((Math-integerp a) 1)
1640 ((eq (car a) 'frac) 2)
1641 ((eq (car a) 'float) 3)
1642 ((eq (car a) 'hms) 4)
1643 ((eq (car a) 'cplx) 5)
1644 ((eq (car a) 'polar) 6)
1645 ((eq (car a) 'sdev) 7)
1646 ((eq (car a) 'intv) 8)
1647 ((eq (car a) 'mod) 9)
1648 ((eq (car a) 'date) (if (Math-integerp (nth 1 a)) 10 11))
1650 (if (memq (nth 2 a) '(var-inf var-uinf var-nan)) 12 100))
1651 ((eq (car a) 'vec) (if (math-matrixp a) 102 101))
1652 (t (math-calcFunc-to-var (car a)))))
1654 (defun calcFunc-integer (a)
1655 (if (Math-integerp a)
1657 (if (Math-objvecp a)
1659 (list 'calcFunc-integer a))))
1661 (defun calcFunc-real (a)
1664 (if (Math-objvecp a)
1666 (list 'calcFunc-real a))))
1668 (defun calcFunc-constant (a)
1671 (if (Math-objvecp a)
1673 (list 'calcFunc-constant a))))
1675 (defun calcFunc-refers (a b)
1676 (if (math-expr-contains a b)
1678 (if (eq (car-safe a) 'var)
1679 (list 'calcFunc-refers a b)
1682 (defun calcFunc-negative (a)
1683 (if (math-looks-negp a)
1685 (if (or (math-zerop a)
1688 (list 'calcFunc-negative a))))
1690 (defun calcFunc-variable (a)
1691 (if (eq (car-safe a) 'var)
1693 (if (Math-objvecp a)
1695 (list 'calcFunc-variable a))))
1697 (defun calcFunc-nonvar (a)
1698 (if (eq (car-safe a) 'var)
1699 (list 'calcFunc-nonvar a)
1702 (defun calcFunc-istrue (a)
1703 (if (math-is-true a)
1709 ;;;; User-programmability.
1711 ;;; Compiling Lisp-like forms to use the math library.
1713 (defun math-do-defmath (func args body)
1714 (require 'calc-macs)
1715 (let* ((fname (intern (concat "calcFunc-" (symbol-name func))))
1716 (doc (if (stringp (car body)) (list (car body))))
1717 (clargs (mapcar 'math-clean-arg args))
1718 (body (math-define-function-body
1719 (if (stringp (car body)) (cdr body) body)
1722 (if (and (consp (car body))
1723 (eq (car (car body)) 'interactive))
1724 (let ((inter (car body)))
1725 (setq body (cdr body))
1726 (if (or (> (length inter) 2)
1727 (integerp (nth 1 inter)))
1728 (let ((hasprefix nil) (hasmulti nil))
1729 (if (stringp (nth 1 inter))
1731 (cond ((equal (nth 1 inter) "p")
1733 ((equal (nth 1 inter) "m")
1736 "Can't handle interactive code string \"%s\""
1738 (setq inter (cdr inter))))
1739 (if (not (integerp (nth 1 inter)))
1741 "Expected an integer in interactive specification"))
1742 (append (list 'defun
1743 (intern (concat "calc-"
1744 (symbol-name func)))
1745 (if (or hasprefix hasmulti)
1749 (if (or hasprefix hasmulti)
1750 '((interactive "P"))
1754 '(calc-slow-wrapper)
1761 (list 'prefix-numeric-value
1765 (list 'calc-enter-result
1766 (if hasmulti 'n (nth 1 inter))
1770 (list 'quote (list fname))
1771 (list 'calc-top-list-n
1780 'prefix-numeric-value
1784 (list 'calc-top-list-n
1787 (nth 1 inter)))))))))))
1788 (append (list 'defun
1789 (intern (concat "calc-" (symbol-name func)))
1794 (cons 'calc-wrapper body))))))
1795 (append (list 'defun fname clargs)
1797 (math-do-arg-list-check args nil nil)
1800 (defun math-clean-arg (arg)
1802 (math-clean-arg (nth 1 arg))
1805 (defun math-do-arg-check (arg var is-opt is-rest)
1807 (let ((chk (math-do-arg-check arg var nil nil)))
1811 (setq chk (list (cons 'progn chk)))
1814 (let* ((rest (math-do-arg-check (nth 1 arg) var is-opt is-rest))
1816 (qqual (list 'quote qual))
1817 (qual-name (symbol-name qual))
1818 (chk (intern (concat "math-check-" qual-name))))
1824 (list 'mapcar (list 'quote chk) var))
1825 (list 'setq var (list chk var)))))
1826 (if (fboundp (setq chk (intern (concat "math-" qual-name))))
1835 (list 'math-reject-arg
1840 (list 'math-reject-arg var qqual)))))
1841 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name)
1842 (fboundp (setq chk (intern
1844 (math-match-substring
1854 (list 'math-reject-arg
1859 (list 'math-reject-arg var qqual)))))
1860 (error "Unknown qualifier `%s'" qual-name))))))))
1862 (defun math-do-arg-list-check (args is-opt is-rest)
1863 (cond ((null args) nil)
1865 (append (math-do-arg-check (car args)
1866 (math-clean-arg (car args))
1868 (math-do-arg-list-check (cdr args) is-opt is-rest)))
1869 ((eq (car args) '&optional)
1870 (math-do-arg-list-check (cdr args) t nil))
1871 ((eq (car args) '&rest)
1872 (math-do-arg-list-check (cdr args) nil t))
1873 (t (math-do-arg-list-check (cdr args) is-opt is-rest))))
1875 (defconst math-prim-funcs
1876 '( (~= . math-nearly-equal)
1878 (lsh . calcFunc-lsh)
1879 (ash . calcFunc-ash)
1880 (logand . calcFunc-and)
1881 (logandc2 . calcFunc-diff)
1882 (logior . calcFunc-or)
1883 (logxor . calcFunc-xor)
1884 (lognot . calcFunc-not)
1885 (equal . equal) ; need to leave these ones alone!
1894 (defconst math-prim-vars
1897 (&optional . &optional)
1901 (defun math-define-function-body (body env)
1902 (let ((body (math-define-body body env)))
1903 (if (math-body-refers-to body 'math-return)
1904 (list (cons 'catch (cons '(quote math-return) body)))
1907 ;; The variable math-exp-env is local to math-define-body, but is
1908 ;; used by math-define-exp, which is called (indirectly) by
1909 ;; by math-define-body.
1910 (defvar math-exp-env)
1912 (defun math-define-body (body math-exp-env)
1913 (math-define-list body))
1915 (defun math-define-list (body &optional quote)
1918 ((and (eq (car body) ':)
1919 (stringp (nth 1 body)))
1920 (cons (let* ((math-read-expr-quotes t)
1921 (exp (math-read-plain-expr (nth 1 body) t)))
1922 (math-define-exp exp))
1923 (math-define-list (cdr (cdr body)))))
1925 (cons (cond ((consp (car body))
1926 (math-define-list (cdr body) t))
1929 (math-define-list (cdr body))))
1931 (cons (math-define-exp (car body))
1932 (math-define-list (cdr body))))))
1934 (defun math-define-exp (exp)
1936 (let ((func (car exp)))
1937 (cond ((memq func '(quote function))
1938 (if (and (consp (nth 1 exp))
1939 (eq (car (nth 1 exp)) 'lambda))
1941 (math-define-lambda (nth 1 exp) math-exp-env))
1943 ((memq func '(let let* for foreach))
1944 (let ((head (nth 1 exp))
1945 (body (cdr (cdr exp))))
1946 (if (memq func '(let let*))
1948 (setq func (cdr (assq func '((for . math-for)
1949 (foreach . math-foreach)))))
1950 (if (not (listp (car head)))
1951 (setq head (list head))))
1954 (cons (math-define-let head)
1955 (math-define-body body
1957 (math-define-let-env head)
1959 ((and (memq func '(setq setf))
1960 (math-complicated-lhs (cdr exp)))
1961 (if (> (length exp) 3)
1962 (cons 'progn (math-define-setf-list (cdr exp)))
1963 (math-define-setf (nth 1 exp) (nth 2 exp))))
1964 ((eq func 'condition-case)
1967 (math-define-body (cdr (cdr exp))
1972 (math-define-cond (cdr exp))))
1973 ((and (consp func) ; ('spam a b) == force use of plain spam
1974 (eq (car func) 'quote))
1975 (cons func (math-define-list (cdr exp))))
1977 (let ((args (math-define-list (cdr exp)))
1978 (prim (assq func math-prim-funcs)))
1980 (cons (cdr prim) args))
1982 (list 'eq (car args) '(quote float)))
1984 (math-define-binop 'math-add 0
1985 (car args) (cdr args)))
1987 (if (= (length args) 1)
1988 (cons 'math-neg args)
1989 (math-define-binop 'math-sub 0
1990 (car args) (cdr args))))
1992 (math-define-binop 'math-mul 1
1993 (car args) (cdr args)))
1995 (math-define-binop 'math-div 1
1996 (car args) (cdr args)))
1998 (math-define-binop 'math-min 0
1999 (car args) (cdr args)))
2001 (math-define-binop 'math-max 0
2002 (car args) (cdr args)))
2004 (if (and (math-numberp (nth 1 args))
2005 (math-zerop (nth 1 args)))
2006 (list 'math-negp (car args))
2007 (cons 'math-lessp args)))
2009 (if (and (math-numberp (nth 1 args))
2010 (math-zerop (nth 1 args)))
2011 (list 'math-posp (car args))
2012 (list 'math-lessp (nth 1 args) (nth 0 args))))
2015 (if (and (math-numberp (nth 1 args))
2016 (math-zerop (nth 1 args)))
2017 (list 'math-posp (car args))
2019 (nth 1 args) (nth 0 args)))))
2022 (if (and (math-numberp (nth 1 args))
2023 (math-zerop (nth 1 args)))
2024 (list 'math-negp (car args))
2025 (cons 'math-lessp args))))
2027 (if (and (math-numberp (nth 1 args))
2028 (math-zerop (nth 1 args)))
2029 (list 'math-zerop (nth 0 args))
2030 (if (and (integerp (nth 1 args))
2031 (/= (% (nth 1 args) 10) 0))
2032 (cons 'math-equal-int args)
2033 (cons 'math-equal args))))
2036 (if (and (math-numberp (nth 1 args))
2037 (math-zerop (nth 1 args)))
2038 (list 'math-zerop (nth 0 args))
2039 (if (and (integerp (nth 1 args))
2040 (/= (% (nth 1 args) 10) 0))
2041 (cons 'math-equal-int args)
2042 (cons 'math-equal args)))))
2044 (list 'math-add (car args) 1))
2046 (list 'math-add (car args) -1))
2047 ((eq func 'not) ; optimize (not (not x)) => x
2048 (if (eq (car-safe args) func)
2051 ((and (eq func 'elt) (cdr (cdr args)))
2052 (math-define-elt (car args) (cdr args)))
2055 (let* ((name (symbol-name func))
2056 (cfunc (intern (concat "calcFunc-" name)))
2057 (mfunc (intern (concat "math-" name))))
2058 (cond ((fboundp cfunc)
2063 (string-match "\\`calcFunc-.*" name))
2066 (cons cfunc args)))))))))
2067 (t (cons func (math-define-list (cdr exp))))))) ;;args
2069 (let ((prim (assq exp math-prim-vars))
2070 (name (symbol-name exp)))
2073 ((memq exp math-exp-env)
2075 ((string-match "-" name)
2078 (intern (concat "var-" name))))))
2080 (if (or (<= exp -1000000) (>= exp 1000000))
2081 (list 'quote (math-normalize exp))
2085 (defun math-define-cond (forms)
2087 (cons (math-define-list (car forms))
2088 (math-define-cond (cdr forms)))))
2090 (defun math-complicated-lhs (body)
2092 (or (not (symbolp (car body)))
2093 (math-complicated-lhs (cdr (cdr body))))))
2095 (defun math-define-setf-list (body)
2097 (cons (math-define-setf (nth 0 body) (nth 1 body))
2098 (math-define-setf-list (cdr (cdr body))))))
2100 (defun math-define-setf (place value)
2101 (setq place (math-define-exp place)
2102 value (math-define-exp value))
2103 (cond ((symbolp place)
2104 (list 'setq place value))
2105 ((eq (car-safe place) 'nth)
2106 (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value))
2107 ((eq (car-safe place) 'elt)
2108 (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value))
2109 ((eq (car-safe place) 'car)
2110 (list 'setcar (nth 1 place) value))
2111 ((eq (car-safe place) 'cdr)
2112 (list 'setcdr (nth 1 place) value))
2114 (error "Bad place form for setf: %s" place))))
2116 (defun math-define-binop (op ident arg1 rest)
2118 (math-define-binop op ident
2119 (list op arg1 (car rest))
2123 (defun math-define-let (vlist)
2125 (cons (if (consp (car vlist))
2126 (cons (car (car vlist))
2127 (math-define-list (cdr (car vlist))))
2129 (math-define-let (cdr vlist)))))
2131 (defun math-define-let-env (vlist)
2133 (cons (if (consp (car vlist))
2136 (math-define-let-env (cdr vlist)))))
2138 (defun math-define-lambda (exp exp-env)
2139 (nconc (list (nth 0 exp) ; 'lambda
2140 (nth 1 exp)) ; arg list
2141 (math-define-function-body (cdr (cdr exp))
2142 (append (nth 1 exp) exp-env))))
2144 (defun math-define-elt (seq idx)
2146 (math-define-elt (list 'elt seq (car idx)) (cdr idx))
2151 ;;; Useful programming macros.
2153 (defmacro math-while (head &rest body)
2154 (let ((body (cons 'while (cons head body))))
2155 (if (math-body-refers-to body 'math-break)
2156 (cons 'catch (cons '(quote math-break) (list body)))
2158 ;; (put 'math-while 'lisp-indent-hook 1)
2160 (defmacro math-for (head &rest body)
2161 (let ((body (if head
2162 (math-handle-for head body)
2163 (cons 'while (cons t body)))))
2164 (if (math-body-refers-to body 'math-break)
2165 (cons 'catch (cons '(quote math-break) (list body)))
2167 ;; (put 'math-for 'lisp-indent-hook 1)
2169 (defun math-handle-for (head body)
2170 (let* ((var (nth 0 (car head)))
2171 (init (nth 1 (car head)))
2172 (limit (nth 2 (car head)))
2173 (step (or (nth 3 (car head)) 1))
2174 (body (if (cdr head)
2175 (list (math-handle-for (cdr head) body))
2177 (all-ints (and (integerp init) (integerp limit) (integerp step)))
2178 (const-limit (or (integerp limit)
2179 (and (eq (car-safe limit) 'quote)
2180 (math-realp (nth 1 limit)))))
2181 (const-step (or (integerp step)
2182 (and (eq (car-safe step) 'quote)
2183 (math-realp (nth 1 step)))))
2184 (save-limit (if const-limit limit (make-symbol "<limit>")))
2185 (save-step (if const-step step (make-symbol "<step>"))))
2187 (cons (append (if const-limit nil (list (list save-limit limit)))
2188 (if const-step nil (list (list save-step step)))
2189 (list (list var init)))
2194 (list '<= var save-limit)
2195 (list '>= var save-limit))
2198 (if (or (math-posp step)
2223 save-step)))))))))))
2225 (defmacro math-foreach (head &rest body)
2226 (let ((body (math-handle-foreach head body)))
2227 (if (math-body-refers-to body 'math-break)
2228 (cons 'catch (cons '(quote math-break) (list body)))
2230 ;; (put 'math-foreach 'lisp-indent-hook 1)
2232 (defun math-handle-foreach (head body)
2233 (let ((var (nth 0 (car head)))
2234 (data (nth 1 (car head)))
2235 (body (if (cdr head)
2236 (list (math-handle-foreach (cdr head) body))
2239 (cons (list (list var data))
2246 (list 'cdr var)))))))))))
2249 (defun math-body-refers-to (body thing)
2250 (or (equal body thing)
2252 (or (math-body-refers-to (car body) thing)
2253 (math-body-refers-to (cdr body) thing)))))
2255 (defun math-break (&optional value)
2256 (throw 'math-break value))
2258 (defun math-return (&optional value)
2259 (throw 'math-return value))
2265 (defun math-composite-inequalities (x op)
2266 (if (memq (nth 1 op) '(calcFunc-eq calcFunc-neq))
2267 (if (eq (car x) (nth 1 op))
2268 (append x (list (math-read-expr-level (nth 3 op))))
2269 (throw 'syntax "Syntax error"))
2272 (if (memq (nth 1 op) '(calcFunc-lt calcFunc-leq))
2273 (if (memq (car x) '(calcFunc-lt calcFunc-leq))
2275 (+ (if (eq (car x) 'calcFunc-leq) 2 0)
2276 (if (eq (nth 1 op) 'calcFunc-leq) 1 0))
2277 (nth 1 x) (math-read-expr-level (nth 3 op)))
2278 (throw 'syntax "Syntax error"))
2279 (if (memq (car x) '(calcFunc-gt calcFunc-geq))
2281 (+ (if (eq (nth 1 op) 'calcFunc-geq) 2 0)
2282 (if (eq (car x) 'calcFunc-geq) 1 0))
2283 (math-read-expr-level (nth 3 op)) (nth 1 x))
2284 (throw 'syntax "Syntax error"))))))
2286 ;;; arch-tag: 4c5a183b-c9e5-4632-bb3f-e41a764518b0
2287 ;;; calc-prog.el ends here