1 ;; Calculator for GNU Emacs, part II [calc-prog.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-prog () nil)
32 (defun calc-equal-to (arg)
35 (if (and (integerp arg) (> arg 2))
36 (calc-enter-result arg "eq" (cons 'calcFunc-eq (calc-top-list-n arg)))
37 (calc-binary-op "eq" 'calcFunc-eq arg)))
40 (defun calc-remove-equal (arg)
43 (calc-unary-op "rmeq" 'calcFunc-rmeq arg))
46 (defun calc-not-equal-to (arg)
49 (if (and (integerp arg) (> arg 2))
50 (calc-enter-result arg "neq" (cons 'calcFunc-neq (calc-top-list-n arg)))
51 (calc-binary-op "neq" 'calcFunc-neq arg)))
54 (defun calc-less-than (arg)
57 (calc-binary-op "lt" 'calcFunc-lt arg))
60 (defun calc-greater-than (arg)
63 (calc-binary-op "gt" 'calcFunc-gt arg))
66 (defun calc-less-equal (arg)
69 (calc-binary-op "leq" 'calcFunc-leq arg))
72 (defun calc-greater-equal (arg)
75 (calc-binary-op "geq" 'calcFunc-geq arg))
78 (defun calc-in-set (arg)
81 (calc-binary-op "in" 'calcFunc-in arg))
84 (defun calc-logical-and (arg)
87 (calc-binary-op "land" 'calcFunc-land arg 1))
90 (defun calc-logical-or (arg)
93 (calc-binary-op "lor" 'calcFunc-lor arg 0))
96 (defun calc-logical-not (arg)
99 (calc-unary-op "lnot" 'calcFunc-lnot arg))
102 (defun calc-logical-if ()
105 (calc-enter-result 3 "if" (cons 'calcFunc-if (calc-top-list-n 3))))
112 (defun calc-timing (n)
115 (calc-change-mode 'calc-timing n nil t)
116 (message (if calc-timing
117 "Reporting timing of slow commands in Trail."
118 "Not reporting timing of commands.")))
121 (defun calc-pass-errors ()
123 ;; The following two cases are for the new, optimizing byte compiler
124 ;; or the standard 18.57 byte compiler, respectively.
126 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 15)))
127 (or (memq (car-safe (car-safe place)) '(error xxxerror))
128 (setq place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 27)))
129 (or (memq (car (car place)) '(error xxxerror))
131 (setcar (car place) 'xxxerror))
132 (error (error "The calc-do function has been modified; unable to patch.")))
135 (defun calc-user-define ()
137 (message "Define user key: z-")
138 (let ((key (read-char)))
139 (if (= (calc-user-function-classify key) 0)
140 (error "Can't redefine \"?\" key"))
141 (let ((func (intern (completing-read (concat "Set key z "
148 (let* ((kmap (calc-user-key-map))
149 (old (assq key kmap)))
152 (setcdr kmap (cons (cons key func) (cdr kmap)))))))
155 (defun calc-user-undefine ()
157 (message "Undefine user key: z-")
158 (let ((key (read-char)))
159 (if (= (calc-user-function-classify key) 0)
160 (error "Can't undefine \"?\" key"))
161 (let* ((kmap (calc-user-key-map)))
162 (delq (or (assq key kmap)
163 (assq (upcase key) kmap)
164 (assq (downcase key) kmap)
165 (error "No such user key is defined"))
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 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 '(?\r ? ))) 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)))))))
253 (setq alist (read-from-minibuffer "Function argument list: "
255 (prin1-to-string arglist)
259 (and (not (calc-subsetp alist arglist))
261 "Okay for arguments that don't appear in formula to be ignored? "))))))
262 (setq is-symb (and alist
265 "Leave it symbolic for non-constant arguments? ")))
266 (setq alist (mapcar (function (lambda (x)
267 (or (cdr (assq x '((nil . arg-nil)
278 (list 'calc-enter-result
280 (let ((name (symbol-name (or func cmd))))
282 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
284 (math-match-substring name 1)))
287 (list 'calc-top-list-n
289 (put cmd 'calc-user-defn t)))
290 (let ((body (list 'math-normalize (calc-fix-user-formula form))))
295 (mapcar (function (lambda (v)
296 (list 'math-check-const v t)))
299 (put func 'calc-user-defn form)
300 (setq math-integral-cache-state nil)
302 (let* ((kmap (calc-user-key-map))
303 (old (assq key kmap)))
306 (setcdr kmap (cons (cons key cmd) (cdr kmap)))))))
310 (defun calc-default-formula-arglist (form)
312 (if (eq (car form) 'var)
313 (if (or (memq (nth 1 form) arglist)
314 (math-const-var form))
316 (setq arglist (cons (nth 1 form) arglist)))
317 (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))))
327 (defun calc-subsetp (a b)
329 (and (memq (car a) b)
330 (calc-subsetp (cdr a) b)))
333 (defun calc-fix-user-formula (f)
336 (cond ((and (eq (car f) 'var)
337 (memq (setq temp (or (cdr (assq (nth 1 f) '((nil . arg-nil)
342 ((or (math-constp f) (eq (car f) 'var))
344 ((and (eq (car f) 'calcFunc-eval)
346 (list 'let '((calc-simplify-mode nil))
347 (list 'math-normalize (calc-fix-user-formula (nth 1 f)))))
348 ((and (eq (car f) 'calcFunc-evalsimp)
350 (list 'math-simplify (calc-fix-user-formula (nth 1 f))))
351 ((and (eq (car f) 'calcFunc-evalextsimp)
353 (list 'math-simplify-extended
354 (calc-fix-user-formula (nth 1 f))))
357 (cons (list 'quote (car f))
358 (mapcar 'calc-fix-user-formula (cdr f)))))))
362 (defun calc-user-define-composition ()
365 (if (eq calc-language 'unform)
366 (error "Can't define formats for unformatted mode"))
367 (let* ((comp (calc-top 1))
368 (func (intern (completing-read "Define format for which function: "
369 obarray 'fboundp nil "calcFunc-")))
370 (comps (get func 'math-compose-forms))
374 (if (math-zerop comp)
375 (if (setq entry (assq calc-language comps))
376 (put func 'math-compose-forms (delq entry comps)))
377 (calc-default-formula-arglist comp)
378 (setq arglist (sort arglist 'string-lessp))
381 (setq alist (read-from-minibuffer "Composition argument list: "
383 (prin1-to-string arglist)
387 (and (not (calc-subsetp alist arglist))
389 "Okay for arguments that don't appear in formula to be invisible? "))))
390 (or (setq entry (assq calc-language comps))
391 (put func 'math-compose-forms
392 (cons (setq entry (list calc-language)) comps)))
393 (or (setq entry2 (assq (length alist) (cdr entry)))
395 (cons (setq entry2 (list (length alist))) (cdr entry))))
396 (setcdr entry2 (list 'lambda alist (calc-fix-user-formula comp))))
402 (defun calc-user-define-kbd-macro (arg)
405 (error "No keyboard macro defined"))
406 (message "Define last kbd macro on user key: z-")
407 (let ((key (read-char)))
408 (if (= (calc-user-function-classify key) 0)
409 (error "Can't redefine \"?\" key"))
410 (let ((cmd (intern (completing-read "Full name for new command: "
415 (if (or (and (>= key ?a)
422 (format "%03d" key)))))))
424 (not (let ((f (symbol-function cmd)))
427 (eq (car-safe (nth 3 f))
428 'calc-execute-kbd-macro)))))
429 (error "Function %s is already defined and not a keyboard macro"
431 (put cmd 'calc-user-defn t)
432 (fset cmd (if (< (prefix-numeric-value arg) 0)
437 (list 'calc-execute-kbd-macro
438 (vector (key-description last-kbd-macro)
441 (format "z%c" key)))))
442 (let* ((kmap (calc-user-key-map))
443 (old (assq key kmap)))
446 (setcdr kmap (cons (cons key cmd) (cdr kmap)))))))
450 (defun calc-edit-user-syntax ()
453 (let ((lang calc-language))
454 (calc-edit-mode (list 'calc-finish-user-syntax-edit (list 'quote lang))
456 (format "Editing %s-Mode Syntax Table"
457 (cond ((null lang) "Normal")
458 ((eq lang 'tex) "TeX")
459 (t (capitalize (symbol-name lang))))))
460 (calc-write-parse-table (cdr (assq lang calc-user-parse-tables))
462 (calc-show-edit-buffer)
465 (defun calc-finish-user-syntax-edit (lang)
466 (let ((tab (calc-read-parse-table calc-original-buffer lang))
467 (entry (assq lang calc-user-parse-tables)))
470 (car (setq calc-user-parse-tables
471 (cons (list lang) calc-user-parse-tables))))
474 (setq calc-user-parse-tables
475 (delq entry calc-user-parse-tables)))))
476 (switch-to-buffer calc-original-buffer)
479 (defun calc-write-parse-table (tab calc-lang)
482 (calc-write-parse-table-part (car (car p)))
484 (let ((math-format-hash-args t))
485 (math-format-flat-expr (cdr (car p)) 0))
490 (defun calc-write-parse-table-part (p)
492 (cond ((stringp (car p))
494 (if (and (string-match "\\`\\\\dots\\>" s)
495 (not (eq calc-lang 'tex)))
496 (setq s (concat ".." (substring s 5))))
497 (if (or (and (string-match
498 "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s)
499 (string-match "[^a-zA-Z0-9\\]" s))
500 (and (assoc s '((")") ("]") (">")))
502 (insert (prin1-to-string s) " ")
507 (insert "/" (int-to-string (car p))))
509 ((and (eq (car (car p)) '\?) (equal (car (nth 2 (car p))) "$$"))
510 (insert (car (nth 1 (car p))) " "))
513 (calc-write-parse-table-part (nth 1 (car p)))
514 (insert "}" (symbol-name (car (car p))))
516 (calc-write-parse-table-part (list (car (nth 2 (car p)))))
521 (defun calc-read-parse-table (calc-buf calc-lang)
524 (skip-chars-forward "\n\t ")
526 (if (looking-at "%%")
529 (p (calc-read-parse-table-part ":=[\n\t ]+" ":=")))
530 (or (stringp (car p))
531 (and (integerp (car p))
535 (error "Malformed syntax rule")))
538 (let* ((str (buffer-substring pos (point)))
540 (set-buffer calc-buf)
541 (let ((calc-user-parse-tables nil)
543 (math-expr-opers math-standard-opers)
544 (calc-hashes-used 0))
546 (if (string-match ",[ \t]*\\'" str)
547 (substring str 0 (match-beginning 0))
549 (if (eq (car-safe exp) 'error)
551 (goto-char (+ pos (nth 1 exp)))
552 (error (nth 2 exp))))
553 (setq tab (nconc tab (list (cons p exp)))))))))
557 (defun calc-fix-token-name (name &optional unquoted)
558 (cond ((string-match "\\`\\.\\." name)
559 (concat "\\dots" (substring name 2)))
560 ((and (equal name "{") (memq calc-lang '(tex eqn)))
562 ((and (equal name "}") (memq calc-lang '(tex eqn)))
564 ((and (equal name "&") (eq calc-lang 'tex))
567 (search-backward "#")
568 (error "Token '#' is reserved"))
569 ((and unquoted (string-match "#" name))
570 (error "Tokens containing '#' must be quoted"))
571 ((not (string-match "[^ ]" name))
572 (search-backward "\"" nil t)
573 (error "Blank tokens are not allowed"))
577 (defun calc-read-parse-table-part (term eterm)
581 (skip-chars-forward "\n\t ")
582 (if (eobp) (error "Expected '%s'" eterm))
583 (not (looking-at term)))
584 (cond ((looking-at "%%")
586 ((looking-at "{[\n\t ]")
588 (let ((p (calc-read-parse-table-part "}" "}")))
589 (or (looking-at "[+*?]")
590 (error "Expected '+', '*', or '?'"))
591 (let ((sym (intern (buffer-substring (point) (1+ (point))))))
593 (looking-at "[^\n\t ]*")
594 (let ((sep (buffer-substring (point) (match-end 0))))
595 (goto-char (match-end 0))
596 (and (eq sym '\?) (> (length sep) 0)
597 (not (equal sep "$")) (not (equal sep "."))
598 (error "Separator not allowed with { ... }?"))
599 (if (string-match "\\`\"" sep)
600 (setq sep (read-from-string sep)))
601 (setq sep (calc-fix-token-name sep))
602 (setq part (nconc part
604 (and (> (length sep) 0)
605 (cons sep p))))))))))
607 (error "Too many }'s"))
609 (setq quoted (calc-fix-token-name (read (current-buffer)))
610 part (nconc part (list quoted))))
611 ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]")
612 (setq part (nconc part (list (if (= (match-beginning 1)
617 (1+ (match-beginning 1))
619 (goto-char (match-end 0)))
620 ((looking-at ":=[\n\t ]")
621 (error "Misplaced ':='"))
623 (looking-at "[^\n\t ]*")
624 (let ((end (match-end 0)))
625 (setq part (nconc part (list (calc-fix-token-name
629 (goto-char (match-end 0))
630 (let ((len (length part)))
631 (while (and (> len 1)
632 (let ((last (nthcdr (setq len (1- len)) part)))
633 (and (assoc (car last) '((")") ("]") (">")))
634 (not (eq (car last) quoted))
636 (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")
650 (defun calc-user-define-edit (prefix)
651 (interactive "P") ; but no calc-wrapper!
652 (message "Edit definition of command: z-")
653 (let* ((key (read-char))
654 (def (or (assq key (calc-user-key-map))
655 (assq (upcase key) (calc-user-key-map))
656 (assq (downcase key) (calc-user-key-map))
657 (error "No command defined for that key")))
660 (setq cmd (symbol-function cmd)))
661 (cond ((or (stringp cmd)
663 (eq (car-safe (nth 3 cmd)) 'calc-execute-kbd-macro)))
664 (if (and (>= (prefix-numeric-value prefix) 0)
665 (fboundp 'edit-kbd-macro)
667 (eq major-mode 'calc-mode))
669 (if (and (< (window-width) (frame-width))
671 (let ((win (get-buffer-window (calc-trail-buffer))))
673 (delete-window win))))
674 (edit-kbd-macro (cdr def) prefix nil
677 (and calc-display-trail
679 (calc-trail-display 1 t)))))
682 (if (stringp (symbol-function cmd))
683 (symbol-function cmd)
684 (let ((mac (nth 1 (nth 3 (symbol-function
691 (if (stringp (symbol-function cmd))
693 (let ((mac (cdr (nth 3 (symbol-function
695 (if (vectorp (car mac))
698 (key-description new))
699 (aset (car mac) 1 new))
700 (setcar mac new))))))))
701 (let ((keys (progn (and (fboundp 'edit-kbd-macro)
702 (edit-kbd-macro nil))
703 (fboundp 'MacEdit-parse-keys))))
705 (calc-edit-mode (list 'calc-finish-macro-edit
713 (insert "Notations: RET, SPC, TAB, DEL, LFD, NUL"
714 ", C-xxx, M-xxx.\n\n")
716 (insert (if (stringp cmd)
717 (key-description cmd)
718 (if (vectorp (nth 1 (nth 3 cmd)))
719 (aref (nth 1 (nth 3 cmd)) 0)
720 (key-description (nth 1 (nth 3 cmd)))))
722 (if (>= (prog2 (forward-char -1)
726 (fill-region top (point))))
727 (insert "Press C-q to quote control characters like RET"
731 (if (vectorp (nth 1 (nth 3 cmd)))
732 (aref (nth 1 (nth 3 cmd)) 1)
733 (nth 1 (nth 3 cmd)))))))
734 (calc-show-edit-buffer)
735 (forward-line (if keys 2 1)))))
736 (t (let* ((func (calc-stack-command-p cmd))
739 (get func 'calc-user-defn))))
740 (if (and defn (calc-valid-formula-func func))
743 (calc-edit-mode (list 'calc-finish-formula-edit
745 (insert (math-showing-full-precision
746 (math-format-nice-expr defn (frame-width)))
748 (calc-show-edit-buffer))
749 (error "That command's definition cannot be edited"))))))
752 (defun calc-finish-macro-edit (def keys)
754 (if (and keys (looking-at "\n")) (forward-line 1))
755 (let* ((true-str (buffer-substring (point) (point-max)))
757 (if keys (setq str (MacEdit-parse-keys str)))
758 (if (symbolp (cdr def))
759 (if (stringp (symbol-function (cdr def)))
761 (let ((mac (cdr (nth 3 (symbol-function (cdr def))))))
762 (if (vectorp (car mac))
764 (aset (car mac) 0 (if keys true-str (key-description str)))
765 (aset (car mac) 1 str))
770 ;;; The following are hooks into the MacEdit package from macedit.el.
771 (put 'calc-execute-extended-command 'MacEdit-print
773 (setq macro-str (concat "\excalc-" macro-str))))
776 (put 'calcDigit-start 'MacEdit-print
778 (if calc-algebraic-mode
779 (calc-macro-edit-algebraic)
780 (MacEdit-unread-chars key-last)
784 (while (and (setq ch (MacEdit-read-char))
785 (or (and (>= ch ?0) (<= ch ?9))
786 (memq ch '(?\. ?e ?\_ ?n ?\: ?\# ?M
788 (and (memq ch '(?\' ?m ?s))
789 (string-match "[@oh]" str))
790 (and (or (and (>= ch ?a) (<= ch ?z))
791 (and (>= ch ?A) (<= ch ?Z)))
793 "^[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#"
795 (and (memq ch '(?\177 ?\C-h))
797 (and (memq ch '(?+ ?-))
799 (eq (aref str (1- (length str)))
801 (if (or (and (>= ch ?0) (<= ch ?9))
802 (and (or (not (memq ch '(?\177 ?\C-h)))
803 (<= (length str) min-bsp))
804 (setq min-bsp (1+ (length str)))))
805 (setq str (concat str (char-to-string ch)))
806 (setq str (substring str 0 -1))))
807 (if (memq ch '(? ?\n ?\r))
808 (setq str (concat str (char-to-string ch)))
809 (MacEdit-unread-chars ch))
811 (MacEdit-insert-string str)
815 (defun calc-macro-edit-algebraic ()
816 (MacEdit-unread-chars key-last)
820 (MacEdit-lookup-key calc-alg-ent-map)
821 (or (and (memq key-symbol '(self-insert-command
825 '(backward-delete-char
827 backward-delete-char-untabify))
829 (setq macro-str (substring macro-str (length key-str)))
830 (if (or (eq key-symbol 'self-insert-command)
831 (and (or (not (memq key-symbol '(backward-delete-char
833 backward-delete-char-untabify)))
834 (<= (length str) min-bsp))
835 (setq min-bsp (+ (length str) (length key-str)))))
836 (setq str (concat str key-str))
837 (setq str (substring str 0 -1))))
838 (if (memq key-last '(?\n ?\r))
839 (setq str (concat str key-str)
840 macro-str (substring macro-str (length key-str))))
841 (if (> (length str) 0)
844 (MacEdit-insert-string str)
847 (put 'calc-algebraic-entry 'MacEdit-print 'calc-macro-edit-algebraic)
848 (put 'calc-auto-algebraic-entry 'MacEdit-print 'calc-macro-edit-algebraic)
850 (defun calc-macro-edit-variable (&optional no-cmd)
852 (or no-cmd (insert (symbol-name key-symbol) "\n"))
853 (if (memq (MacEdit-peek-char) '(?\+ ?\- ?\* ?\/ ?\^ ?\|))
854 (setq str (char-to-string (MacEdit-read-char))))
855 (if (and (setq ch (MacEdit-peek-char))
856 (>= ch ?0) (<= ch ?9))
857 (insert "type \"" str
858 (char-to-string (MacEdit-read-char)) "\"\n")
859 (if (> (length str) 0)
860 (insert "type \"" str "\"\n"))
861 (MacEdit-read-argument)))
863 (put 'calc-store 'MacEdit-print 'calc-macro-edit-variable)
864 (put 'calc-store-into 'MacEdit-print 'calc-macro-edit-variable)
865 (put 'calc-store-neg 'MacEdit-print 'calc-macro-edit-variable)
866 (put 'calc-store-plus 'MacEdit-print 'calc-macro-edit-variable)
867 (put 'calc-store-minus 'MacEdit-print 'calc-macro-edit-variable)
868 (put 'calc-store-times 'MacEdit-print 'calc-macro-edit-variable)
869 (put 'calc-store-div 'MacEdit-print 'calc-macro-edit-variable)
870 (put 'calc-store-power 'MacEdit-print 'calc-macro-edit-variable)
871 (put 'calc-store-concat 'MacEdit-print 'calc-macro-edit-variable)
872 (put 'calc-store-inv 'MacEdit-print 'calc-macro-edit-variable)
873 (put 'calc-store-decr 'MacEdit-print 'calc-macro-edit-variable)
874 (put 'calc-store-incr 'MacEdit-print 'calc-macro-edit-variable)
875 (put 'calc-store-exchange 'MacEdit-print 'calc-macro-edit-variable)
876 (put 'calc-unstore 'MacEdit-print 'calc-macro-edit-variable)
877 (put 'calc-recall 'MacEdit-print 'calc-macro-edit-variable)
878 (put 'calc-let 'MacEdit-print 'calc-macro-edit-variable)
879 (put 'calc-permanent-variable 'MacEdit-print 'calc-macro-edit-variable)
881 (defun calc-macro-edit-variable-2 ()
882 (calc-macro-edit-variable)
883 (calc-macro-edit-variable t)
885 (put 'calc-copy-variable 'MacEdit-print 'calc-macro-edit-variable-2)
886 (put 'calc-declare-variable 'MacEdit-print 'calc-macro-edit-variable-2)
888 (defun calc-macro-edit-quick-digit ()
889 (insert "type \"" key-str "\" # " (symbol-name key-symbol) "\n")
891 (put 'calc-store-quick 'MacEdit-print 'calc-macro-edit-quick-digit)
892 (put 'calc-store-into-quick 'MacEdit-print 'calc-macro-edit-quick-digit)
893 (put 'calc-recall-quick 'MacEdit-print 'calc-macro-edit-quick-digit)
894 (put 'calc-select-part 'MacEdit-print 'calc-macro-edit-quick-digit)
895 (put 'calc-clean-num 'MacEdit-print 'calc-macro-edit-quick-digit)
898 (defun calc-finish-formula-edit (func)
899 (let ((buf (current-buffer))
900 (str (buffer-substring (point) (point-max)))
902 (body (calc-valid-formula-func func)))
903 (set-buffer calc-original-buffer)
904 (let ((val (math-read-expr str)))
905 (if (eq (car-safe val) 'error)
908 (goto-char (+ start (nth 1 val)))
909 (error (nth 2 val))))
911 (let ((alist (nth 1 (symbol-function func))))
912 (calc-fix-user-formula val)))
913 (put func 'calc-user-defn val)))
916 (defun calc-valid-formula-func (func)
917 (let ((def (symbol-function func)))
919 (eq (car def) 'lambda)
921 (setq def (cdr (cdr def)))
923 (not (eq (car (car def)) 'math-normalize)))
924 (setq def (cdr def)))
929 (defun calc-get-user-defn ()
932 (message "Get definition of command: z-")
933 (let* ((key (read-char))
934 (def (or (assq key (calc-user-key-map))
935 (assq (upcase key) (calc-user-key-map))
936 (assq (downcase key) (calc-user-key-map))
937 (error "No command defined for that key")))
940 (setq cmd (symbol-function cmd)))
942 (message "Keyboard macro: %s" cmd))
943 (t (let* ((func (calc-stack-command-p cmd))
946 (get func 'calc-user-defn))))
949 (and (calc-valid-formula-func func)
950 (setq defn (append '(calcFunc-lambda)
951 (mapcar 'math-build-var-name
952 (nth 1 (symbol-function
955 (calc-enter-result 0 "gdef" defn))
956 (error "That command is not defined by a formula")))))))
960 (defun calc-user-define-permanent ()
963 (message "Record in %s the command: z-" calc-settings-file)
964 (let* ((key (read-char))
965 (def (or (assq key (calc-user-key-map))
966 (assq (upcase key) (calc-user-key-map))
967 (assq (downcase key) (calc-user-key-map))
970 (intern (completing-read
971 (format "Record in %s the function: "
973 obarray 'fboundp nil "calcFunc-"))))
974 (error "No command defined for that key"))))
975 (set-buffer (find-file-noselect (substitute-in-file-name
976 calc-settings-file)))
977 (goto-char (point-max))
978 (let* ((cmd (cdr def))
979 (fcmd (and cmd (symbolp cmd) (symbol-function cmd)))
985 (insert "\n;;; Definition stored by Calc on " (current-time-string)
986 "\n(put 'calc-define '"
987 (if (symbolp cmd) (symbol-name cmd) (format "key%d" key))
990 (eq (car-safe fcmd) 'lambda)
991 (get cmd 'calc-user-defn))
993 (and (eq (car-safe (nth 3 fcmd)) 'calc-execute-kbd-macro)
994 (vectorp (nth 1 (nth 3 fcmd)))
995 (progn (and (fboundp 'edit-kbd-macro)
996 (edit-kbd-macro nil))
997 (fboundp 'MacEdit-parse-keys))
999 (aset (nth 1 (nth 3 fcmd)) 1 nil))
1000 (insert (setq str (prin1-to-string
1001 (cons 'defun (cons cmd (cdr fcmd)))))
1003 (or (and (string-match "\"" str) (not q-ok))
1004 (fill-region pt (point)))
1005 (indent-rigidly pt (point) 2)
1006 (delete-region pt (1+ pt))
1007 (insert " (put '" (symbol-name cmd)
1008 " 'calc-user-defn '"
1009 (prin1-to-string (get cmd 'calc-user-defn))
1011 (setq func (calc-stack-command-p cmd))
1012 (let ((ffunc (and func (symbolp func) (symbol-function func)))
1015 (eq (car-safe ffunc) 'lambda)
1016 (get func 'calc-user-defn)
1018 (insert (setq str (prin1-to-string
1019 (cons 'defun (cons func
1022 (or (and (string-match "\"" str) (not q-ok))
1023 (fill-region pt (point)))
1024 (indent-rigidly pt (point) 2)
1025 (delete-region pt (1+ pt))
1027 (insert "(put '" (symbol-name func)
1028 " 'calc-user-defn '"
1029 (prin1-to-string (get func 'calc-user-defn))
1031 (fill-region pt (point))
1032 (indent-rigidly pt (point) 2)
1033 (delete-region pt (1+ pt))))))
1035 (insert " (fset '" (prin1-to-string cmd)
1036 " " (prin1-to-string fcmd) ")\n")))
1037 (or func (setq func (and cmd (symbolp cmd) (fboundp cmd) cmd)))
1038 (if (get func 'math-compose-forms)
1040 (insert "(put '" (symbol-name cmd)
1041 " 'math-compose-forms '"
1042 (prin1-to-string (get func 'math-compose-forms))
1044 (fill-region pt (point))
1045 (indent-rigidly pt (point) 2)
1046 (delete-region pt (1+ pt))))
1048 (insert " (define-key calc-mode-map "
1049 (prin1-to-string (concat "z" (char-to-string key)))
1051 (prin1-to-string cmd)
1057 (defun calc-stack-command-p (cmd)
1058 (if (and cmd (symbolp cmd))
1060 (calc-stack-command-p (symbol-function cmd)))
1062 (eq (car cmd) 'lambda)
1063 (setq cmd (or (assq 'calc-wrapper cmd)
1064 (assq 'calc-slow-wrapper cmd)))
1065 (setq cmd (assq 'calc-enter-result cmd))
1066 (memq (car (nth 3 cmd)) '(cons list))
1067 (eq (car (nth 1 (nth 3 cmd))) 'quote)
1068 (nth 1 (nth 1 (nth 3 cmd)))))
1072 (defun calc-call-last-kbd-macro (arg)
1074 (and defining-kbd-macro
1075 (error "Can't execute anonymous macro while defining one"))
1077 (error "No kbd macro has been defined"))
1078 (calc-execute-kbd-macro last-kbd-macro arg)
1081 (defun calc-execute-kbd-macro (mac arg &rest prefix)
1082 (if (and (vectorp mac) (> (length mac) 0) (stringp (aref mac 0)))
1083 (setq mac (or (aref mac 1)
1084 (aset mac 1 (progn (and (fboundp 'edit-kbd-macro)
1085 (edit-kbd-macro nil))
1086 (MacEdit-parse-keys (aref mac 0)))))))
1087 (if (< (prefix-numeric-value arg) 0)
1088 (execute-kbd-macro mac (- (prefix-numeric-value arg)))
1089 (if calc-executing-macro
1090 (execute-kbd-macro mac arg)
1092 (let ((old-stack-whole (copy-sequence calc-stack))
1093 (old-stack-top calc-stack-top)
1094 (old-buffer-size (buffer-size))
1095 (old-refresh-count calc-refresh-count))
1097 (let ((calc-executing-macro mac))
1098 (execute-kbd-macro mac arg))
1099 (calc-select-buffer)
1100 (let ((new-stack (reverse calc-stack))
1101 (old-stack (reverse old-stack-whole)))
1102 (while (and new-stack old-stack
1103 (equal (car new-stack) (car old-stack)))
1104 (setq new-stack (cdr new-stack)
1105 old-stack (cdr old-stack)))
1106 (or (equal prefix '(nil))
1107 (calc-record-list (if (> (length new-stack) 1)
1108 (mapcar 'car new-stack)
1110 (or (car prefix) "kmac")))
1111 (calc-record-undo (list 'set 'saved-stack-top old-stack-top))
1113 (calc-record-undo (list 'pop 1 (mapcar 'car old-stack))))
1114 (let ((calc-stack old-stack-whole)
1116 (calc-cursor-stack-index (length old-stack)))
1117 (if (and (= old-buffer-size (buffer-size))
1118 (= old-refresh-count calc-refresh-count))
1119 (let ((buffer-read-only nil))
1120 (delete-region (point) (point-max))
1122 (calc-record-undo (list 'push 1))
1123 (insert (math-format-stack-value (car new-stack)) "\n")
1124 (setq new-stack (cdr new-stack)))
1125 (calc-renumber-stack))
1127 (calc-record-undo (list 'push 1))
1128 (setq new-stack (cdr new-stack)))
1130 (calc-record-undo (list 'set 'saved-stack-top 0))))))))
1133 (defun calc-push-list-in-macro (vals m sels)
1134 (let ((entry (list (car vals) 1 (car sels)))
1135 (mm (+ (or m 1) calc-stack-top)))
1137 (setcdr (nthcdr (- mm 2) calc-stack)
1138 (cons entry (nthcdr (1- mm) calc-stack)))
1139 (setq calc-stack (cons entry calc-stack))))
1142 (defun calc-pop-stack-in-macro (n mm)
1144 (setcdr (nthcdr (- mm 2) calc-stack)
1145 (nthcdr (+ n mm -1) calc-stack))
1146 (setq calc-stack (nthcdr n calc-stack)))
1150 (defun calc-kbd-if ()
1153 (let ((cond (calc-top-n 1)))
1155 (if (math-is-true cond)
1156 (if defining-kbd-macro
1157 (message "If true..."))
1158 (if defining-kbd-macro
1159 (message "Condition is false; skipping to Z: or Z] ..."))
1160 (calc-kbd-skip-to-else-if t))))
1163 (defun calc-kbd-else-if ()
1168 (defun calc-kbd-skip-to-else-if (else-okay)
1172 (setq ch (read-char))
1174 (error "Unterminated Z[ in keyboard macro"))
1177 (setq ch (read-char))
1179 (setq count (1+ count)))
1181 (setq count (1- count)))
1187 (keyboard-quit))))))
1188 (and defining-kbd-macro
1191 (message "End-if..."))))
1194 (defun calc-kbd-end-if ()
1196 (if defining-kbd-macro
1197 (message "End-if..."))
1200 (defun calc-kbd-else ()
1202 (if defining-kbd-macro
1203 (message "Else; skipping to Z] ..."))
1204 (calc-kbd-skip-to-else-if nil)
1208 (defun calc-kbd-repeat ()
1212 (setq count (math-trunc (calc-top-n 1)))
1213 (or (Math-integerp count)
1214 (error "Count must be an integer"))
1215 (if (Math-integer-negp count)
1217 (or (integerp count)
1218 (setq count 1000000))
1220 (calc-kbd-loop count))
1223 (defun calc-kbd-for (dir)
1227 (setq init (calc-top-n 2)
1228 final (calc-top-n 1))
1229 (or (and (math-anglep init) (math-anglep final))
1230 (error "Initial and final values must be real numbers"))
1232 (calc-kbd-loop nil init final (and dir (prefix-numeric-value dir))))
1235 (defun calc-kbd-loop (rpt-count &optional initial final dir)
1237 (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000))
1241 (open last-command-char)
1244 (or executing-kbd-macro
1245 (message "Reading loop body..."))
1247 (setq ch (read-char))
1249 (error "Unterminated Z%c in keyboard macro" open))
1252 (setq ch (read-char)
1253 body (concat body "Z" (char-to-string ch)))
1254 (cond ((memq ch '(?\< ?\( ?\{))
1255 (setq count (1+ count)))
1256 ((memq ch '(?\> ?\) ?\}))
1257 (setq count (1- count)))
1260 (setq parts (nconc parts (list (concat (substring body 0 -2)
1265 (setq body (concat body (char-to-string ch)))))
1266 (if (/= ch (cdr (assq open '( (?\< . ?\>) (?\( . ?\)) (?\{ . ?\}) ))))
1267 (error "Mismatched Z%c and Z%c in keyboard macro" open ch))
1268 (or executing-kbd-macro
1269 (message "Looping..."))
1270 (setq body (concat (substring body 0 -2) "Z]"))
1271 (and (not executing-kbd-macro)
1272 (= rpt-count 1000000)
1276 (message "Warning: Infinite loop! Not executing.")
1277 (setq rpt-count 0)))
1278 (or (not initial) dir
1279 (setq dir (math-compare final initial)))
1281 (while (> rpt-count 0)
1284 (if (cond ((eq dir 0) (Math-equal final counter))
1285 ((eq dir 1) (Math-lessp final counter))
1286 ((eq dir -1) (Math-lessp counter final)))
1288 (calc-push counter)))
1289 (while (and part (> rpt-count 0))
1290 (execute-kbd-macro (car part))
1291 (if (math-is-true (calc-top-n 1))
1293 (setq part (cdr part)))
1297 (execute-kbd-macro body)
1299 (let ((step (calc-top-n 1)))
1301 (setq counter (calcFunc-add counter step)))
1302 (setq rpt-count (1- rpt-count))))))))
1303 (or executing-kbd-macro
1304 (message "Looping...done")))
1307 (defun calc-kbd-end-repeat ()
1309 (error "Unbalanced Z> in keyboard macro")
1312 (defun calc-kbd-end-for ()
1314 (error "Unbalanced Z) in keyboard macro")
1317 (defun calc-kbd-end-loop ()
1319 (error "Unbalanced Z} in keyboard macro")
1322 (defun calc-kbd-break ()
1325 (let ((cond (calc-top-n 1)))
1327 (if (math-is-true cond)
1328 (error "Keyboard macro aborted."))))
1332 (defun calc-kbd-push (arg)
1335 (let* ((defs (and arg (> (prefix-numeric-value arg) 0)))
1336 (var-q0 (and (boundp 'var-q0) var-q0))
1337 (var-q1 (and (boundp 'var-q1) var-q1))
1338 (var-q2 (and (boundp 'var-q2) var-q2))
1339 (var-q3 (and (boundp 'var-q3) var-q3))
1340 (var-q4 (and (boundp 'var-q4) var-q4))
1341 (var-q5 (and (boundp 'var-q5) var-q5))
1342 (var-q6 (and (boundp 'var-q6) var-q6))
1343 (var-q7 (and (boundp 'var-q7) var-q7))
1344 (var-q8 (and (boundp 'var-q8) var-q8))
1345 (var-q9 (and (boundp 'var-q9) var-q9))
1346 (calc-internal-prec (if defs 12 calc-internal-prec))
1347 (calc-word-size (if defs 32 calc-word-size))
1348 (calc-angle-mode (if defs 'deg calc-angle-mode))
1349 (calc-simplify-mode (if defs nil calc-simplify-mode))
1350 (calc-algebraic-mode (if arg nil calc-algebraic-mode))
1351 (calc-incomplete-algebraic-mode (if arg nil
1352 calc-incomplete-algebraic-mode))
1353 (calc-symbolic-mode (if defs nil calc-symbolic-mode))
1354 (calc-matrix-mode (if defs nil calc-matrix-mode))
1355 (calc-prefer-frac (if defs nil calc-prefer-frac))
1356 (calc-complex-mode (if defs nil calc-complex-mode))
1357 (calc-infinite-mode (if defs nil calc-infinite-mode))
1361 (if (or executing-kbd-macro defining-kbd-macro)
1363 (if defining-kbd-macro
1364 (message "Reading body..."))
1366 (setq ch (read-char))
1368 (error "Unterminated Z` in keyboard macro"))
1371 (setq ch (read-char)
1372 body (concat body "Z" (char-to-string ch)))
1374 (setq count (1+ count)))
1376 (setq count (1- count)))
1379 (setq body (concat body (char-to-string ch)))))
1380 (if defining-kbd-macro
1381 (message "Reading body...done"))
1382 (let ((calc-kbd-push-level 0))
1383 (execute-kbd-macro (substring body 0 -2))))
1384 (let ((calc-kbd-push-level (1+ calc-kbd-push-level)))
1385 (message "Saving modes; type Z' to restore")
1386 (recursive-edit)))))
1388 (setq calc-kbd-push-level 0)
1390 (defun calc-kbd-pop ()
1392 (if (> calc-kbd-push-level 0)
1394 (message "Mode settings restored")
1395 (exit-recursive-edit))
1396 (error "Unbalanced Z' in keyboard macro"))
1400 (defun calc-kbd-report (msg)
1401 (interactive "sMessage: ")
1403 (let ((executing-kbd-macro nil)
1404 (defining-kbd-macro nil))
1405 (math-working msg (calc-top-n 1))))
1408 (defun calc-kbd-query (msg)
1409 (interactive "sPrompt: ")
1411 (let ((executing-kbd-macro nil)
1412 (defining-kbd-macro nil))
1413 (calc-alg-entry nil (and (not (equal msg "")) msg))))
1422 ;;;; Logical operations.
1424 (defun calcFunc-eq (a b &rest more)
1426 (let* ((args (cons a (cons b (copy-sequence more))))
1430 (while (and (cdr p) (not (eq res 0)))
1432 (while (and (setq p2 (cdr p2)) (not (eq res 0)))
1433 (setq res (math-two-eq (car p) (car p2)))
1435 (setcdr p (delq (car p2) (cdr p)))))
1440 (cons 'calcFunc-eq args)
1442 (or (math-two-eq a b)
1443 (if (and (or (math-looks-negp a) (math-zerop a))
1444 (or (math-looks-negp b) (math-zerop b)))
1445 (list 'calcFunc-eq (math-neg a) (math-neg b))
1446 (list 'calcFunc-eq a b))))
1449 (defun calcFunc-neq (a b &rest more)
1451 (let* ((args (cons a (cons b more)))
1456 (while (and (cdr p) (not (eq res 1)))
1458 (while (and (setq p2 (cdr p2)) (not (eq res 1)))
1459 (setq res (math-two-eq (car p) (car p2)))
1460 (or res (setq all nil)))
1466 (cons 'calcFunc-neq args))))
1467 (or (cdr (assq (math-two-eq a b) '((0 . 1) (1 . 0))))
1468 (if (and (or (math-looks-negp a) (math-zerop a))
1469 (or (math-looks-negp b) (math-zerop b)))
1470 (list 'calcFunc-neq (math-neg a) (math-neg b))
1471 (list 'calcFunc-neq a b))))
1474 (defun math-two-eq (a b)
1475 (if (eq (car-safe a) 'vec)
1476 (if (eq (car-safe b) 'vec)
1477 (if (= (length a) (length b))
1479 (while (and (setq a (cdr a) b (cdr b)) (not (eq res 0)))
1481 (setq res (math-two-eq (car a) (car b)))
1482 (if (eq (math-two-eq (car a) (car b)) 0)
1486 (if (Math-objectp b)
1489 (if (eq (car-safe b) 'vec)
1490 (if (Math-objectp a)
1493 (let ((res (math-compare a b)))
1496 (if (and (= res 2) (not (and (Math-scalarp a) (Math-scalarp b))))
1501 (defun calcFunc-lt (a b)
1502 (let ((res (math-compare a b)))
1506 (if (and (or (math-looks-negp a) (math-zerop a))
1507 (or (math-looks-negp b) (math-zerop b)))
1508 (list 'calcFunc-gt (math-neg a) (math-neg b))
1509 (list 'calcFunc-lt a b))
1513 (defun calcFunc-gt (a b)
1514 (let ((res (math-compare a b)))
1518 (if (and (or (math-looks-negp a) (math-zerop a))
1519 (or (math-looks-negp b) (math-zerop b)))
1520 (list 'calcFunc-lt (math-neg a) (math-neg b))
1521 (list 'calcFunc-gt a b))
1525 (defun calcFunc-leq (a b)
1526 (let ((res (math-compare a b)))
1530 (if (and (or (math-looks-negp a) (math-zerop a))
1531 (or (math-looks-negp b) (math-zerop b)))
1532 (list 'calcFunc-geq (math-neg a) (math-neg b))
1533 (list 'calcFunc-leq a b))
1537 (defun calcFunc-geq (a b)
1538 (let ((res (math-compare a b)))
1542 (if (and (or (math-looks-negp a) (math-zerop a))
1543 (or (math-looks-negp b) (math-zerop b)))
1544 (list 'calcFunc-leq (math-neg a) (math-neg b))
1545 (list 'calcFunc-geq a b))
1549 (defun calcFunc-rmeq (a)
1550 (if (math-vectorp a)
1551 (math-map-vec 'calcFunc-rmeq a)
1552 (if (assq (car-safe a) calc-tweak-eqn-table)
1553 (if (and (eq (car-safe (nth 2 a)) 'var)
1554 (math-objectp (nth 1 a)))
1557 (if (eq (car-safe a) 'calcFunc-assign)
1559 (if (eq (car-safe a) 'calcFunc-evalto)
1561 (list 'calcFunc-rmeq a)))))
1564 (defun calcFunc-land (a b)
1565 (cond ((Math-zerop a)
1573 (t (list 'calcFunc-land a b)))
1576 (defun calcFunc-lor (a b)
1577 (cond ((Math-zerop a)
1585 (t (list 'calcFunc-lor a b)))
1588 (defun calcFunc-lnot (a)
1591 (if (math-is-true a)
1593 (let ((op (and (= (length a) 3)
1594 (assq (car a) calc-tweak-eqn-table))))
1596 (cons (nth 2 op) (cdr a))
1597 (list 'calcFunc-lnot a)))))
1600 (defun calcFunc-if (c e1 e2)
1603 (if (and (math-is-true c) (not (Math-vectorp c)))
1605 (or (and (Math-vectorp c)
1607 (let ((ee1 (if (Math-vectorp e1)
1608 (if (= (length c) (length e1))
1610 (calc-record-why "*Dimension error" e1))
1612 (ee2 (if (Math-vectorp e2)
1613 (if (= (length c) (length e2))
1615 (calc-record-why "*Dimension error" e2))
1618 (cons 'vec (math-if-vector (cdr c) ee1 ee2)))))
1619 (list 'calcFunc-if c e1 e2))))
1622 (defun math-if-vector (c e1 e2)
1624 (cons (if (Math-zerop (car c)) (car e2) (car e1))
1625 (math-if-vector (cdr c)
1630 (defun math-normalize-logical-op (a)
1631 (or (and (eq (car a) 'calcFunc-if)
1633 (let ((a1 (math-normalize (nth 1 a))))
1635 (math-normalize (nth 3 a))
1636 (if (Math-numberp a1)
1637 (math-normalize (nth 2 a))
1638 (if (and (Math-vectorp (nth 1 a))
1639 (math-constp (nth 1 a)))
1640 (calcFunc-if (nth 1 a)
1641 (math-normalize (nth 2 a))
1642 (math-normalize (nth 3 a)))
1643 (let ((calc-simplify-mode 'none))
1644 (list 'calcFunc-if a1
1645 (math-normalize (nth 2 a))
1646 (math-normalize (nth 3 a)))))))))
1650 (defun calcFunc-in (a b)
1651 (or (and (eq (car-safe b) 'vec)
1653 (while (and (setq bb (cdr bb))
1654 (not (if (memq (car-safe (car bb)) '(vec intv))
1655 (eq (calcFunc-in a (car bb)) 1)
1656 (Math-equal a (car bb))))))
1657 (if bb 1 (and (math-constp a) (math-constp bb) 0))))
1658 (and (eq (car-safe b) 'intv)
1659 (let ((res (math-compare a (nth 2 b))) res2)
1663 (or (/= (nth 1 b) 2)
1664 (Math-lessp (nth 2 b) (nth 3 b))))
1665 (if (memq (nth 1 b) '(2 3)) 1 0))
1666 ((= (setq res2 (math-compare a (nth 3 b))) 1)
1669 (or (/= (nth 1 b) 1)
1670 (Math-lessp (nth 2 b) (nth 3 b))))
1671 (if (memq (nth 1 b) '(1 3)) 1 0))
1677 (and (Math-equal a b)
1679 (and (math-constp a) (math-constp b)
1681 (list 'calcFunc-in a b))
1684 (defun calcFunc-typeof (a)
1685 (cond ((Math-integerp a) 1)
1686 ((eq (car a) 'frac) 2)
1687 ((eq (car a) 'float) 3)
1688 ((eq (car a) 'hms) 4)
1689 ((eq (car a) 'cplx) 5)
1690 ((eq (car a) 'polar) 6)
1691 ((eq (car a) 'sdev) 7)
1692 ((eq (car a) 'intv) 8)
1693 ((eq (car a) 'mod) 9)
1694 ((eq (car a) 'date) (if (Math-integerp (nth 1 a)) 10 11))
1696 (if (memq (nth 2 a) '(var-inf var-uinf var-nan)) 12 100))
1697 ((eq (car a) 'vec) (if (math-matrixp a) 102 101))
1698 (t (math-calcFunc-to-var func)))
1701 (defun calcFunc-integer (a)
1702 (if (Math-integerp a)
1704 (if (Math-objvecp a)
1706 (list 'calcFunc-integer a)))
1709 (defun calcFunc-real (a)
1712 (if (Math-objvecp a)
1714 (list 'calcFunc-real a)))
1717 (defun calcFunc-constant (a)
1720 (if (Math-objvecp a)
1722 (list 'calcFunc-constant a)))
1725 (defun calcFunc-refers (a b)
1726 (if (math-expr-contains a b)
1728 (if (eq (car-safe a) 'var)
1729 (list 'calcFunc-refers a b)
1733 (defun calcFunc-negative (a)
1734 (if (math-looks-negp a)
1736 (if (or (math-zerop a)
1739 (list 'calcFunc-negative a)))
1742 (defun calcFunc-variable (a)
1743 (if (eq (car-safe a) 'var)
1745 (if (Math-objvecp a)
1747 (list 'calcFunc-variable a)))
1750 (defun calcFunc-nonvar (a)
1751 (if (eq (car-safe a) 'var)
1752 (list 'calcFunc-nonvar a)
1756 (defun calcFunc-istrue (a)
1757 (if (math-is-true a)
1765 ;;;; User-programmability.
1767 ;;; Compiling Lisp-like forms to use the math library.
1769 (defun math-do-defmath (func args body)
1771 (let* ((fname (intern (concat "calcFunc-" (symbol-name func))))
1772 (doc (if (stringp (car body)) (list (car body))))
1773 (clargs (mapcar 'math-clean-arg args))
1774 (body (math-define-function-body
1775 (if (stringp (car body)) (cdr body) body)
1778 (if (and (consp (car body))
1779 (eq (car (car body)) 'interactive))
1780 (let ((inter (car body)))
1781 (setq body (cdr body))
1782 (if (or (> (length inter) 2)
1783 (integerp (nth 1 inter)))
1784 (let ((hasprefix nil) (hasmulti nil))
1785 (if (stringp (nth 1 inter))
1787 (cond ((equal (nth 1 inter) "p")
1789 ((equal (nth 1 inter) "m")
1792 "Can't handle interactive code string \"%s\""
1794 (setq inter (cdr inter))))
1795 (if (not (integerp (nth 1 inter)))
1797 "Expected an integer in interactive specification"))
1798 (append (list 'defun
1799 (intern (concat "calc-"
1800 (symbol-name func)))
1801 (if (or hasprefix hasmulti)
1805 (if (or hasprefix hasmulti)
1806 '((interactive "P"))
1810 '(calc-slow-wrapper)
1817 (list 'prefix-numeric-value
1821 (list 'calc-enter-result
1822 (if hasmulti 'n (nth 1 inter))
1826 (list 'quote (list fname))
1827 (list 'calc-top-list-n
1836 'prefix-numeric-value
1840 (list 'calc-top-list-n
1843 (nth 1 inter)))))))))))
1844 (append (list 'defun
1845 (intern (concat "calc-" (symbol-name func)))
1850 (cons 'calc-wrapper body))))))
1851 (append (list 'defun fname clargs)
1853 (math-do-arg-list-check args nil nil)
1857 (defun math-clean-arg (arg)
1859 (math-clean-arg (nth 1 arg))
1863 (defun math-do-arg-check (arg var is-opt is-rest)
1865 (let ((chk (math-do-arg-check arg var nil nil)))
1869 (setq chk (list (cons 'progn chk)))
1872 (let* ((rest (math-do-arg-check (nth 1 arg) var is-opt is-rest))
1874 (qqual (list 'quote qual))
1875 (qual-name (symbol-name qual))
1876 (chk (intern (concat "math-check-" qual-name))))
1882 (list 'mapcar (list 'quote chk) var))
1883 (list 'setq var (list chk var)))))
1884 (if (fboundp (setq chk (intern (concat "math-" qual-name))))
1893 (list 'math-reject-arg
1898 (list 'math-reject-arg var qqual)))))
1899 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name)
1900 (fboundp (setq chk (intern
1902 (math-match-substring
1912 (list 'math-reject-arg
1917 (list 'math-reject-arg var qqual)))))
1918 (error "Unknown qualifier `%s'" qual-name)))))))
1921 (defun math-do-arg-list-check (args is-opt is-rest)
1922 (cond ((null args) nil)
1924 (append (math-do-arg-check (car args)
1925 (math-clean-arg (car args))
1927 (math-do-arg-list-check (cdr args) is-opt is-rest)))
1928 ((eq (car args) '&optional)
1929 (math-do-arg-list-check (cdr args) t nil))
1930 ((eq (car args) '&rest)
1931 (math-do-arg-list-check (cdr args) nil t))
1932 (t (math-do-arg-list-check (cdr args) is-opt is-rest)))
1935 (defconst math-prim-funcs
1936 '( (~= . math-nearly-equal)
1938 (lsh . calcFunc-lsh)
1939 (ash . calcFunc-ash)
1940 (logand . calcFunc-and)
1941 (logandc2 . calcFunc-diff)
1942 (logior . calcFunc-or)
1943 (logxor . calcFunc-xor)
1944 (lognot . calcFunc-not)
1945 (equal . equal) ; need to leave these ones alone!
1955 (defconst math-prim-vars
1958 (&optional . &optional)
1963 (defun math-define-function-body (body env)
1964 (let ((body (math-define-body body env)))
1965 (if (math-body-refers-to body 'math-return)
1966 (list (cons 'catch (cons '(quote math-return) body)))
1970 (defun math-define-body (body exp-env)
1971 (math-define-list body)
1974 (defun math-define-list (body &optional quote)
1977 ((and (eq (car body) ':)
1978 (stringp (nth 1 body)))
1979 (cons (let* ((math-read-expr-quotes t)
1980 (exp (math-read-plain-expr (nth 1 body) t)))
1981 (math-define-exp exp))
1982 (math-define-list (cdr (cdr body)))))
1984 (cons (cond ((consp (car body))
1985 (math-define-list (cdr body) t))
1988 (math-define-list (cdr body))))
1990 (cons (math-define-exp (car body))
1991 (math-define-list (cdr body)))))
1994 (defun math-define-exp (exp)
1996 (let ((func (car exp)))
1997 (cond ((memq func '(quote function))
1998 (if (and (consp (nth 1 exp))
1999 (eq (car (nth 1 exp)) 'lambda))
2001 (math-define-lambda (nth 1 exp) exp-env))
2003 ((memq func '(let let* for foreach))
2004 (let ((head (nth 1 exp))
2005 (body (cdr (cdr exp))))
2006 (if (memq func '(let let*))
2008 (setq func (cdr (assq func '((for . math-for)
2009 (foreach . math-foreach)))))
2010 (if (not (listp (car head)))
2011 (setq head (list head))))
2014 (cons (math-define-let head)
2015 (math-define-body body
2017 (math-define-let-env head)
2019 ((and (memq func '(setq setf))
2020 (math-complicated-lhs (cdr exp)))
2021 (if (> (length exp) 3)
2022 (cons 'progn (math-define-setf-list (cdr exp)))
2023 (math-define-setf (nth 1 exp) (nth 2 exp))))
2024 ((eq func 'condition-case)
2027 (math-define-body (cdr (cdr exp))
2032 (math-define-cond (cdr exp))))
2033 ((and (consp func) ; ('spam a b) == force use of plain spam
2034 (eq (car func) 'quote))
2035 (cons func (math-define-list (cdr exp))))
2037 (let ((args (math-define-list (cdr exp)))
2038 (prim (assq func math-prim-funcs)))
2040 (cons (cdr prim) args))
2042 (list 'eq (car args) '(quote float)))
2044 (math-define-binop 'math-add 0
2045 (car args) (cdr args)))
2047 (if (= (length args) 1)
2048 (cons 'math-neg args)
2049 (math-define-binop 'math-sub 0
2050 (car args) (cdr args))))
2052 (math-define-binop 'math-mul 1
2053 (car args) (cdr args)))
2055 (math-define-binop 'math-div 1
2056 (car args) (cdr args)))
2058 (math-define-binop 'math-min 0
2059 (car args) (cdr args)))
2061 (math-define-binop 'math-max 0
2062 (car args) (cdr args)))
2064 (if (and (math-numberp (nth 1 args))
2065 (math-zerop (nth 1 args)))
2066 (list 'math-negp (car args))
2067 (cons 'math-lessp args)))
2069 (if (and (math-numberp (nth 1 args))
2070 (math-zerop (nth 1 args)))
2071 (list 'math-posp (car args))
2072 (list 'math-lessp (nth 1 args) (nth 0 args))))
2075 (if (and (math-numberp (nth 1 args))
2076 (math-zerop (nth 1 args)))
2077 (list 'math-posp (car args))
2079 (nth 1 args) (nth 0 args)))))
2082 (if (and (math-numberp (nth 1 args))
2083 (math-zerop (nth 1 args)))
2084 (list 'math-negp (car args))
2085 (cons 'math-lessp args))))
2087 (if (and (math-numberp (nth 1 args))
2088 (math-zerop (nth 1 args)))
2089 (list 'math-zerop (nth 0 args))
2090 (if (and (integerp (nth 1 args))
2091 (/= (% (nth 1 args) 10) 0))
2092 (cons 'math-equal-int args)
2093 (cons 'math-equal args))))
2096 (if (and (math-numberp (nth 1 args))
2097 (math-zerop (nth 1 args)))
2098 (list 'math-zerop (nth 0 args))
2099 (if (and (integerp (nth 1 args))
2100 (/= (% (nth 1 args) 10) 0))
2101 (cons 'math-equal-int args)
2102 (cons 'math-equal args)))))
2104 (list 'math-add (car args) 1))
2106 (list 'math-add (car args) -1))
2107 ((eq func 'not) ; optimize (not (not x)) => x
2108 (if (eq (car-safe args) func)
2111 ((and (eq func 'elt) (cdr (cdr args)))
2112 (math-define-elt (car args) (cdr args)))
2115 (let* ((name (symbol-name func))
2116 (cfunc (intern (concat "calcFunc-" name)))
2117 (mfunc (intern (concat "math-" name))))
2118 (cond ((fboundp cfunc)
2123 (string-match "\\`calcFunc-.*" name))
2126 (cons cfunc args)))))))))
2127 (t (cons func args)))))
2129 (let ((prim (assq exp math-prim-vars))
2130 (name (symbol-name exp)))
2135 ((string-match "-" name)
2138 (intern (concat "var-" name))))))
2140 (if (or (<= exp -1000000) (>= exp 1000000))
2141 (list 'quote (math-normalize exp))
2146 (defun math-define-cond (forms)
2148 (cons (math-define-list (car forms))
2149 (math-define-cond (cdr forms))))
2152 (defun math-complicated-lhs (body)
2154 (or (not (symbolp (car body)))
2155 (math-complicated-lhs (cdr (cdr body)))))
2158 (defun math-define-setf-list (body)
2160 (cons (math-define-setf (nth 0 body) (nth 1 body))
2161 (math-define-setf-list (cdr (cdr body)))))
2164 (defun math-define-setf (place value)
2165 (setq place (math-define-exp place)
2166 value (math-define-exp value))
2167 (cond ((symbolp place)
2168 (list 'setq place value))
2169 ((eq (car-safe place) 'nth)
2170 (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value))
2171 ((eq (car-safe place) 'elt)
2172 (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value))
2173 ((eq (car-safe place) 'car)
2174 (list 'setcar (nth 1 place) value))
2175 ((eq (car-safe place) 'cdr)
2176 (list 'setcdr (nth 1 place) value))
2178 (error "Bad place form for setf: %s" place)))
2181 (defun math-define-binop (op ident arg1 rest)
2183 (math-define-binop op ident
2184 (list op arg1 (car rest))
2189 (defun math-define-let (vlist)
2191 (cons (if (consp (car vlist))
2192 (cons (car (car vlist))
2193 (math-define-list (cdr (car vlist))))
2195 (math-define-let (cdr vlist))))
2198 (defun math-define-let-env (vlist)
2200 (cons (if (consp (car vlist))
2203 (math-define-let-env (cdr vlist))))
2206 (defun math-define-lambda (exp exp-env)
2207 (nconc (list (nth 0 exp) ; 'lambda
2208 (nth 1 exp)) ; arg list
2209 (math-define-function-body (cdr (cdr exp))
2210 (append (nth 1 exp) exp-env)))
2213 (defun math-define-elt (seq idx)
2215 (math-define-elt (list 'elt seq (car idx)) (cdr idx))
2221 ;;; Useful programming macros.
2223 (defmacro math-while (head &rest body)
2224 (let ((body (cons 'while (cons head body))))
2225 (if (math-body-refers-to body 'math-break)
2226 (cons 'catch (cons '(quote math-break) (list body)))
2231 (defmacro math-for (head &rest body)
2232 (let ((body (if head
2233 (math-handle-for head body)
2234 (cons 'while (cons t body)))))
2235 (if (math-body-refers-to body 'math-break)
2236 (cons 'catch (cons '(quote math-break) (list body)))
2240 (defun math-handle-for (head body)
2241 (let* ((var (nth 0 (car head)))
2242 (init (nth 1 (car head)))
2243 (limit (nth 2 (car head)))
2244 (step (or (nth 3 (car head)) 1))
2245 (body (if (cdr head)
2246 (list (math-handle-for (cdr head) body))
2248 (all-ints (and (integerp init) (integerp limit) (integerp step)))
2249 (const-limit (or (integerp limit)
2250 (and (eq (car-safe limit) 'quote)
2251 (math-realp (nth 1 limit)))))
2252 (const-step (or (integerp step)
2253 (and (eq (car-safe step) 'quote)
2254 (math-realp (nth 1 step)))))
2255 (save-limit (if const-limit limit (make-symbol "<limit>")))
2256 (save-step (if const-step step (make-symbol "<step>"))))
2258 (cons (append (if const-limit nil (list (list save-limit limit)))
2259 (if const-step nil (list (list save-step step)))
2260 (list (list var init)))
2265 (list '<= var save-limit)
2266 (list '>= var save-limit))
2269 (if (or (math-posp step)
2298 (defmacro math-foreach (head &rest body)
2299 (let ((body (math-handle-foreach head body)))
2300 (if (math-body-refers-to body 'math-break)
2301 (cons 'catch (cons '(quote math-break) (list body)))
2306 (defun math-handle-foreach (head body)
2307 (let ((var (nth 0 (car head)))
2308 (data (nth 1 (car head)))
2309 (body (if (cdr head)
2310 (list (math-handle-foreach (cdr head) body))
2313 (cons (list (list var data))
2320 (list 'cdr var))))))))))
2324 (defun math-body-refers-to (body thing)
2325 (or (equal body thing)
2327 (or (math-body-refers-to (car body) thing)
2328 (math-body-refers-to (cdr body) thing))))
2331 (defun math-break (&optional value)
2332 (throw 'math-break value)
2335 (defun math-return (&optional value)
2336 (throw 'math-return value)
2343 (defun math-composite-inequalities (x op)
2344 (if (memq (nth 1 op) '(calcFunc-eq calcFunc-neq))
2345 (if (eq (car x) (nth 1 op))
2346 (append x (list (math-read-expr-level (nth 3 op))))
2347 (throw 'syntax "Syntax error"))
2350 (if (memq (nth 1 op) '(calcFunc-lt calcFunc-leq))
2351 (if (memq (car x) '(calcFunc-lt calcFunc-leq))
2353 (+ (if (eq (car x) 'calcFunc-leq) 2 0)
2354 (if (eq (nth 1 op) 'calcFunc-leq) 1 0))
2355 (nth 1 x) (math-read-expr-level (nth 3 op)))
2356 (throw 'syntax "Syntax error"))
2357 (if (memq (car x) '(calcFunc-gt calcFunc-geq))
2359 (+ (if (eq (nth 1 op) 'calcFunc-geq) 2 0)
2360 (if (eq (car x) 'calcFunc-geq) 1 0))
2361 (math-read-expr-level (nth 3 op)) (nth 1 x))
2362 (throw 'syntax "Syntax error")))))