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.
29 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-equal-to (arg)
38 (if (and (integerp arg) (> arg 2))
39 (calc-enter-result arg "eq" (cons 'calcFunc-eq (calc-top-list-n arg)))
40 (calc-binary-op "eq" 'calcFunc-eq arg))))
42 (defun calc-remove-equal (arg)
45 (calc-unary-op "rmeq" 'calcFunc-rmeq arg)))
47 (defun calc-not-equal-to (arg)
50 (if (and (integerp arg) (> arg 2))
51 (calc-enter-result arg "neq" (cons 'calcFunc-neq (calc-top-list-n arg)))
52 (calc-binary-op "neq" 'calcFunc-neq arg))))
54 (defun calc-less-than (arg)
57 (calc-binary-op "lt" 'calcFunc-lt arg)))
59 (defun calc-greater-than (arg)
62 (calc-binary-op "gt" 'calcFunc-gt arg)))
64 (defun calc-less-equal (arg)
67 (calc-binary-op "leq" 'calcFunc-leq arg)))
69 (defun calc-greater-equal (arg)
72 (calc-binary-op "geq" 'calcFunc-geq arg)))
74 (defun calc-in-set (arg)
77 (calc-binary-op "in" 'calcFunc-in arg)))
79 (defun calc-logical-and (arg)
82 (calc-binary-op "land" 'calcFunc-land arg 1)))
84 (defun calc-logical-or (arg)
87 (calc-binary-op "lor" 'calcFunc-lor arg 0)))
89 (defun calc-logical-not (arg)
92 (calc-unary-op "lnot" 'calcFunc-lnot arg)))
94 (defun calc-logical-if ()
97 (calc-enter-result 3 "if" (cons 'calcFunc-if (calc-top-list-n 3)))))
103 (defun calc-timing (n)
106 (calc-change-mode 'calc-timing n nil t)
107 (message (if calc-timing
108 "Reporting timing of slow commands in Trail"
109 "Not reporting timing of commands"))))
111 (defun calc-pass-errors ()
113 ;; The following two cases are for the new, optimizing byte compiler
114 ;; or the standard 18.57 byte compiler, respectively.
116 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 15)))
117 (or (memq (car-safe (car-safe place)) '(error xxxerror))
118 (setq place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 27)))
119 (or (memq (car (car place)) '(error xxxerror))
121 (setcar (car place) 'xxxerror))
122 (error (error "The calc-do function has been modified; unable to patch"))))
124 (defun calc-user-define ()
126 (message "Define user key: z-")
127 (let ((key (read-char)))
128 (if (= (calc-user-function-classify key) 0)
129 (error "Can't redefine \"?\" key"))
130 (let ((func (intern (completing-read (concat "Set key z "
137 (let* ((kmap (calc-user-key-map))
138 (old (assq key kmap)))
141 (setcdr kmap (cons (cons key func) (cdr kmap))))))))
143 (defun calc-user-undefine ()
145 (message "Undefine user key: z-")
146 (let ((key (read-char)))
147 (if (= (calc-user-function-classify key) 0)
148 (error "Can't undefine \"?\" key"))
149 (let* ((kmap (calc-user-key-map)))
150 (delq (or (assq key kmap)
151 (assq (upcase key) kmap)
152 (assq (downcase key) kmap)
153 (error "No such user key is defined"))
157 ;; math-integral-cache-state is originally declared in calcalg2.el,
158 ;; it is used in calc-user-define-variable.
159 (defvar math-integral-cache-state)
161 ;; calc-user-formula-alist is local to calc-user-define-formula,
162 ;; calc-user-define-compostion and calc-finish-formula-edit,
163 ;; but is used by calc-fix-user-formula.
164 (defvar calc-user-formula-alist)
166 (defun calc-user-define-formula ()
169 (let* ((form (calc-top 1))
171 (is-lambda (and (eq (car-safe form) 'calcFunc-lambda)
172 (>= (length form) 2)))
173 odef key keyname cmd cmd-base cmd-base-default
174 func calc-user-formula-alist is-symb)
176 (setq arglist (mapcar (function (lambda (x) (nth 1 x)))
177 (nreverse (cdr (reverse (cdr form)))))
178 form (nth (1- (length form)) form))
179 (calc-default-formula-arglist form)
180 (setq arglist (sort arglist 'string-lessp)))
181 (message "Define user key: z-")
182 (setq key (read-char))
183 (if (= (calc-user-function-classify key) 0)
184 (error "Can't redefine \"?\" key"))
185 (setq key (and (not (memq key '(13 32))) key)
187 (if (or (and (<= ?0 key) (<= key ?9))
188 (and (<= ?a key) (<= key ?z))
189 (and (<= ?A key) (<= key ?Z)))
191 (format "%03d" key)))
192 odef (assq key (calc-user-key-map)))
194 (setq keyname (format "%05d" (abs (% (random) 10000)))))
197 (setq cmd-base-default (concat "User-" keyname))
198 (setq cmd (completing-read
199 (concat "Define M-x command name (default: calc-"
202 obarray 'commandp nil
203 (if (and odef (symbolp (cdr odef)))
204 (symbol-name (cdr odef))
206 (if (or (string-equal cmd "")
207 (string-equal cmd "calc-"))
208 (setq cmd (concat "calc-User-" keyname)))
209 (setq cmd-base (and (string-match "\\`calc-\\(.+\\)\\'" cmd)
210 (math-match-substring cmd 1)))
211 (setq cmd (intern cmd))
217 (if (get cmd 'calc-user-defn)
218 (concat "Replace previous definition for "
219 (symbol-name cmd) "? ")
220 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
223 (setq cmd-base-default
226 "\\`User-.+" cmd-base)
229 (substring cmd-base 5))
231 (concat "User" keyname)))
235 (concat "Define algebraic function name (default: "
236 cmd-base-default "): ")
237 (mapcar (lambda (x) (substring x 9))
238 (all-completions "calcFunc-"
242 (intern (concat "calcFunc-" x))))
245 (if (string-equal func "calcFunc-")
246 (intern (concat "calcFunc-" cmd-base-default))
254 (if (get func 'calc-user-defn)
255 (concat "Replace previous definition for "
256 (symbol-name func) "? ")
257 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
260 (setq func (intern (concat "calcFunc-User"
262 (and cmd (symbol-name cmd))
263 (format "%05d" (% (random) 10000)))))))
266 (setq calc-user-formula-alist arglist)
269 (setq calc-user-formula-alist
270 (read-from-minibuffer "Function argument list: "
272 (prin1-to-string arglist)
276 (and (not (calc-subsetp calc-user-formula-alist arglist))
278 "Okay for arguments that don't appear in formula to be ignored? "))))))
279 (setq is-symb (and calc-user-formula-alist
282 "Leave it symbolic for non-constant arguments? ")))
283 (setq calc-user-formula-alist
284 (mapcar (function (lambda (x)
285 (or (cdr (assq x '((nil . arg-nil)
287 x))) calc-user-formula-alist))
296 (list 'calc-enter-result
297 (length calc-user-formula-alist)
298 (let ((name (symbol-name (or func cmd))))
300 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
302 (math-match-substring name 1)))
305 (list 'calc-top-list-n
306 (length calc-user-formula-alist)))))))
307 (put cmd 'calc-user-defn t)))
308 (let ((body (list 'math-normalize (calc-fix-user-formula form))))
311 (list 'lambda calc-user-formula-alist)
313 (mapcar (function (lambda (v)
314 (list 'math-check-const v t)))
315 calc-user-formula-alist))
317 (put func 'calc-user-defn form)
318 (setq math-integral-cache-state nil)
320 (let* ((kmap (calc-user-key-map))
321 (old (assq key kmap)))
324 (setcdr kmap (cons (cons key cmd) (cdr kmap)))))))
327 (defun calc-default-formula-arglist (form)
329 (if (eq (car form) 'var)
330 (if (or (memq (nth 1 form) arglist)
331 (math-const-var form))
333 (setq arglist (cons (nth 1 form) arglist)))
334 (calc-default-formula-arglist-step (cdr form)))))
336 (defun calc-default-formula-arglist-step (l)
339 (calc-default-formula-arglist (car l))
340 (calc-default-formula-arglist-step (cdr l)))))
342 (defun calc-subsetp (a b)
344 (and (memq (car a) b)
345 (calc-subsetp (cdr a) b))))
347 (defun calc-fix-user-formula (f)
350 (cond ((and (eq (car f) 'var)
351 (memq (setq temp (or (cdr (assq (nth 1 f) '((nil . arg-nil)
354 calc-user-formula-alist))
356 ((or (math-constp f) (eq (car f) 'var))
358 ((and (eq (car f) 'calcFunc-eval)
360 (list 'let '((calc-simplify-mode nil))
361 (list 'math-normalize (calc-fix-user-formula (nth 1 f)))))
362 ((and (eq (car f) 'calcFunc-evalsimp)
364 (list 'math-simplify (calc-fix-user-formula (nth 1 f))))
365 ((and (eq (car f) 'calcFunc-evalextsimp)
367 (list 'math-simplify-extended
368 (calc-fix-user-formula (nth 1 f))))
371 (cons (list 'quote (car f))
372 (mapcar 'calc-fix-user-formula (cdr f)))))))
375 (defun calc-user-define-composition ()
378 (if (eq calc-language 'unform)
379 (error "Can't define formats for unformatted mode"))
380 (let* ((comp (calc-top 1))
383 (completing-read "Define format for which function: "
384 (mapcar (lambda (x) (substring x 9))
385 (all-completions "calcFunc-"
389 (intern (concat "calcFunc-" x))))))))
390 (comps (get func 'math-compose-forms))
393 (calc-user-formula-alist nil))
394 (if (math-zerop comp)
395 (if (setq entry (assq calc-language comps))
396 (put func 'math-compose-forms (delq entry comps)))
397 (calc-default-formula-arglist comp)
398 (setq arglist (sort arglist 'string-lessp))
401 (setq calc-user-formula-alist
402 (read-from-minibuffer "Composition argument list: "
404 (prin1-to-string arglist)
408 (and (not (calc-subsetp calc-user-formula-alist arglist))
410 "Okay for arguments that don't appear in formula to be invisible? "))))
411 (or (setq entry (assq calc-language comps))
412 (put func 'math-compose-forms
413 (cons (setq entry (list calc-language)) comps)))
414 (or (setq entry2 (assq (length calc-user-formula-alist) (cdr entry)))
417 (list (length calc-user-formula-alist))) (cdr entry))))
419 (list 'lambda calc-user-formula-alist (calc-fix-user-formula comp))))
424 (defun calc-user-define-kbd-macro (arg)
427 (error "No keyboard macro defined"))
428 (message "Define last kbd macro on user key: z-")
429 (let ((key (read-char)))
430 (if (= (calc-user-function-classify key) 0)
431 (error "Can't redefine \"?\" key"))
432 (let ((cmd (intern (completing-read "Full name for new command: "
437 (if (or (and (>= key ?a)
444 (format "%03d" key)))))))
446 (not (let ((f (symbol-function cmd)))
449 (eq (car-safe (nth 3 f))
450 'calc-execute-kbd-macro)))))
451 (error "Function %s is already defined and not a keyboard macro"
453 (put cmd 'calc-user-defn t)
454 (fset cmd (if (< (prefix-numeric-value arg) 0)
459 (list 'calc-execute-kbd-macro
460 (vector (key-description last-kbd-macro)
463 (format "z%c" key)))))
464 (let* ((kmap (calc-user-key-map))
465 (old (assq key kmap)))
468 (setcdr kmap (cons (cons key cmd) (cdr kmap))))))))
471 (defun calc-edit-user-syntax ()
474 (let ((lang calc-language))
475 (calc-edit-mode (list 'calc-finish-user-syntax-edit (list 'quote lang))
477 (format "Editing %s-Mode Syntax Table. "
478 (cond ((null lang) "Normal")
479 ((eq lang 'tex) "TeX")
480 (t (capitalize (symbol-name lang))))))
481 (calc-write-parse-table (cdr (assq lang calc-user-parse-tables))
483 (calc-show-edit-buffer))
485 (defvar calc-original-buffer)
487 (defun calc-finish-user-syntax-edit (lang)
488 (let ((tab (calc-read-parse-table calc-original-buffer lang))
489 (entry (assq lang calc-user-parse-tables)))
492 (car (setq calc-user-parse-tables
493 (cons (list lang) calc-user-parse-tables))))
496 (setq calc-user-parse-tables
497 (delq entry calc-user-parse-tables)))))
498 (switch-to-buffer calc-original-buffer))
500 ;; The variable calc-lang is local to calc-write-parse-table, but is
501 ;; used by calc-write-parse-table-part which is called by
502 ;; calc-write-parse-table. The variable is also local to
503 ;; calc-read-parse-table, but is used by calc-fix-token-name which
504 ;; is called (indirectly) by calc-read-parse-table.
507 (defun calc-write-parse-table (tab calc-lang)
510 (calc-write-parse-table-part (car (car p)))
512 (let ((math-format-hash-args t))
513 (math-format-flat-expr (cdr (car p)) 0))
517 (defun calc-write-parse-table-part (p)
519 (cond ((stringp (car p))
521 (if (and (string-match "\\`\\\\dots\\>" s)
522 (not (eq calc-lang 'tex)))
523 (setq s (concat ".." (substring s 5))))
524 (if (or (and (string-match
525 "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s)
526 (string-match "[^a-zA-Z0-9\\]" s))
527 (and (assoc s '((")") ("]") (">")))
529 (insert (prin1-to-string s) " ")
534 (insert "/" (int-to-string (car p))))
536 ((and (eq (car (car p)) '\?) (equal (car (nth 2 (car p))) "$$"))
537 (insert (car (nth 1 (car p))) " "))
540 (calc-write-parse-table-part (nth 1 (car p)))
541 (insert "}" (symbol-name (car (car p))))
543 (calc-write-parse-table-part (list (car (nth 2 (car p)))))
547 (defun calc-read-parse-table (calc-buf calc-lang)
550 (skip-chars-forward "\n\t ")
552 (if (looking-at "%%")
555 (p (calc-read-parse-table-part ":=[\n\t ]+" ":=")))
556 (or (stringp (car p))
557 (and (integerp (car p))
561 (error "Malformed syntax rule")))
564 (let* ((str (buffer-substring pos (point)))
566 (set-buffer calc-buf)
567 (let ((calc-user-parse-tables nil)
569 (math-expr-opers math-standard-opers)
570 (calc-hashes-used 0))
572 (if (string-match ",[ \t]*\\'" str)
573 (substring str 0 (match-beginning 0))
575 (if (eq (car-safe exp) 'error)
577 (goto-char (+ pos (nth 1 exp)))
578 (error (nth 2 exp))))
579 (setq tab (nconc tab (list (cons p exp)))))))))
582 (defun calc-fix-token-name (name &optional unquoted)
583 (cond ((string-match "\\`\\.\\." name)
584 (concat "\\dots" (substring name 2)))
585 ((and (equal name "{") (memq calc-lang '(tex eqn)))
587 ((and (equal name "}") (memq calc-lang '(tex eqn)))
589 ((and (equal name "&") (eq calc-lang 'tex))
592 (search-backward "#")
593 (error "Token '#' is reserved"))
594 ((and unquoted (string-match "#" name))
595 (error "Tokens containing '#' must be quoted"))
596 ((not (string-match "[^ ]" name))
597 (search-backward "\"" nil t)
598 (error "Blank tokens are not allowed"))
601 (defun calc-read-parse-table-part (term eterm)
605 (skip-chars-forward "\n\t ")
606 (if (eobp) (error "Expected '%s'" eterm))
607 (not (looking-at term)))
608 (cond ((looking-at "%%")
610 ((looking-at "{[\n\t ]")
612 (let ((p (calc-read-parse-table-part "}" "}")))
613 (or (looking-at "[+*?]")
614 (error "Expected '+', '*', or '?'"))
615 (let ((sym (intern (buffer-substring (point) (1+ (point))))))
617 (looking-at "[^\n\t ]*")
618 (let ((sep (buffer-substring (point) (match-end 0))))
619 (goto-char (match-end 0))
620 (and (eq sym '\?) (> (length sep) 0)
621 (not (equal sep "$")) (not (equal sep "."))
622 (error "Separator not allowed with { ... }?"))
623 (if (string-match "\\`\"" sep)
624 (setq sep (read-from-string sep)))
625 (setq sep (calc-fix-token-name sep))
626 (setq part (nconc part
628 (and (> (length sep) 0)
629 (cons sep p))))))))))
631 (error "Too many }'s"))
633 (setq quoted (calc-fix-token-name (read (current-buffer)))
634 part (nconc part (list quoted))))
635 ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]")
636 (setq part (nconc part (list (if (= (match-beginning 1)
641 (1+ (match-beginning 1))
643 (goto-char (match-end 0)))
644 ((looking-at ":=[\n\t ]")
645 (error "Misplaced ':='"))
647 (looking-at "[^\n\t ]*")
648 (let ((end (match-end 0)))
649 (setq part (nconc part (list (calc-fix-token-name
653 (goto-char (match-end 0))
654 (let ((len (length part)))
655 (while (and (> len 1)
656 (let ((last (nthcdr (setq len (1- len)) part)))
657 (and (assoc (car last) '((")") ("]") (">")))
658 (not (eq (car last) quoted))
660 (list '\? (list (car last)) '("$$"))))))))
663 (defun calc-user-define-invocation ()
666 (error "No keyboard macro defined"))
667 (setq calc-invocation-macro last-kbd-macro)
668 (message "Use `M-# Z' to invoke this macro"))
670 (defun calc-user-define-edit ()
671 (interactive) ; but no calc-wrapper!
672 (message "Edit definition of command: z-")
673 (let* ((key (read-char))
674 (def (or (assq key (calc-user-key-map))
675 (assq (upcase key) (calc-user-key-map))
676 (assq (downcase key) (calc-user-key-map))
677 (error "No command defined for that key")))
680 (setq cmdname (symbol-name cmd))
681 (setq cmd (symbol-function cmd)))
682 (cond ((or (stringp cmd)
684 (eq (car-safe (nth 3 cmd)) 'calc-execute-kbd-macro)))
685 (let* ((mac (elt (nth 1 (nth 3 cmd)) 1))
686 (str (edmacro-format-keys mac t))
687 (kys (nth 3 (nth 3 cmd))))
689 (list 'calc-edit-macro-finish-edit cmdname kys)
691 "Editing keyboard macro (%s, bound to %s).\n"
692 "Original keys: %s \n")
693 cmdname kys (elt (nth 1 (nth 3 cmd)) 0)))
695 (calc-edit-format-macro-buffer)
696 (calc-show-edit-buffer)))
697 (t (let* ((func (calc-stack-command-p cmd))
700 (get func 'calc-user-defn)))
701 (kys (concat "z" (char-to-string (car def))))
702 (intcmd (symbol-name (cdr def)))
703 (algcmd (substring (symbol-name func) 9)))
704 (if (and defn (calc-valid-formula-func func))
708 (list 'calc-finish-formula-edit (list 'quote func))
710 (format "Editing formula (%s, %s, bound to %s).\n"
712 (insert (math-showing-full-precision
713 (math-format-nice-expr defn (frame-width)))
715 (calc-show-edit-buffer))
716 (error "That command's definition cannot be edited")))))))
718 ;; Formatting the macro buffer
720 (defun calc-edit-macro-repeats ()
721 (goto-char calc-edit-top)
723 (re-search-forward "^\\([0-9]+\\)\\*" nil t)
724 (setq num (string-to-int (match-string 1)))
725 (setq line (buffer-substring (point) (line-end-position)))
726 (goto-char (line-beginning-position))
730 (setq num (1- num)))))
732 (defun calc-edit-macro-adjust-buffer ()
733 (calc-edit-macro-repeats)
734 (goto-char calc-edit-top)
735 (while (re-search-forward "^RET$" nil t)
737 (goto-char calc-edit-top)
738 (while (and (re-search-forward "^$" nil t)
739 (not (= (point) (point-max))))
742 (defun calc-edit-macro-command ()
743 "Return the command on the current line in a Calc macro editing buffer."
744 (let ((beg (line-beginning-position))
746 (if (search-forward ";;" (line-end-position) 1)
748 (skip-chars-backward " \t")
750 (buffer-substring beg end)))
752 (defun calc-edit-macro-command-type ()
753 "Return the type of command on the current line in a Calc macro editing buffer."
754 (let ((beg (save-excursion
755 (if (search-forward ";;" (line-end-position) t)
757 (skip-chars-forward " \t")
760 (goto-char (line-end-position))
761 (skip-chars-backward " \t")
764 (buffer-substring beg end)
767 (defun calc-edit-macro-combine-alg-ent ()
768 "Put an entire algebraic entry on a single line."
769 (let ((line (calc-edit-macro-command))
770 (type (calc-edit-macro-command-type))
773 (goto-char (line-beginning-position))
775 (setq curline (calc-edit-macro-command))
777 (not (string-equal "RET" curline))
778 (not (setq match (string-match "<return>" curline))))
779 (setq line (concat line curline))
781 (setq curline (calc-edit-macro-command)))
784 (setq line (concat line (substring curline 0 match))))
785 (setq line (replace-regexp-in-string "SPC" " SPC "
786 (replace-regexp-in-string " " "" line)))
787 (insert line "\t\t\t")
788 (if (> (current-column) 24)
790 (insert ";; " type "\n")
792 (insert "RET\t\t\t;; calc-enter\n"))))
794 (defun calc-edit-macro-combine-ext-command ()
795 "Put an entire extended command on a single line."
796 (let ((cmdbeg (calc-edit-macro-command))
798 (type (calc-edit-macro-command-type))
801 (goto-char (line-beginning-position))
803 (setq curline (calc-edit-macro-command))
805 (not (string-equal "RET" curline))
806 (not (setq match (string-match "<return>" curline))))
807 (setq line (concat line curline))
809 (setq curline (calc-edit-macro-command)))
812 (setq line (concat line (substring curline 0 match))))
813 (setq line (replace-regexp-in-string " " "" line))
814 (insert cmdbeg " " line "\t\t\t")
815 (if (> (current-column) 24)
817 (insert ";; " type "\n")
819 (insert "RET\t\t\t;; calc-enter\n"))))
821 (defun calc-edit-macro-combine-var-name ()
822 "Put an entire variable name on a single line."
823 (let ((line (calc-edit-macro-command))
826 (goto-char (line-beginning-position))
828 (if (member line '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
829 (insert line "\t\t\t;; calc quick variable\n")
830 (setq curline (calc-edit-macro-command))
832 (not (string-equal "RET" curline))
833 (not (setq match (string-match "<return>" curline))))
834 (setq line (concat line curline))
836 (setq curline (calc-edit-macro-command)))
839 (setq line (concat line (substring curline 0 match))))
840 (setq line (replace-regexp-in-string " " "" line))
841 (insert line "\t\t\t")
842 (if (> (current-column) 24)
844 (insert ";; calc variable\n")
846 (insert "RET\t\t\t;; calc-enter\n")))))
848 (defun calc-edit-macro-combine-digits ()
849 "Put an entire sequence of digits on a single line."
850 (let ((line (calc-edit-macro-command))
852 (goto-char (line-beginning-position))
854 (while (string-equal (calc-edit-macro-command-type) "calcDigit-start")
855 (setq line (concat line (calc-edit-macro-command)))
857 (insert line "\t\t\t")
858 (if (> (current-column) 24)
860 (insert ";; calc digits\n")))
862 (defun calc-edit-format-macro-buffer ()
863 "Rewrite the Calc macro editing buffer."
864 (calc-edit-macro-adjust-buffer)
865 (goto-char calc-edit-top)
866 (let ((type (calc-edit-macro-command-type)))
867 (while (not (string-equal type ""))
870 (string-equal type "calc-algebraic-entry")
871 (string-equal type "calc-auto-algebraic-entry"))
872 (calc-edit-macro-combine-alg-ent))
873 ((string-equal type "calc-execute-extended-command")
874 (calc-edit-macro-combine-ext-command))
875 ((string-equal type "calcDigit-start")
876 (calc-edit-macro-combine-digits))
878 (string-equal type "calc-store")
879 (string-equal type "calc-store-into")
880 (string-equal type "calc-store-neg")
881 (string-equal type "calc-store-plus")
882 (string-equal type "calc-store-minus")
883 (string-equal type "calc-store-div")
884 (string-equal type "calc-store-times")
885 (string-equal type "calc-store-power")
886 (string-equal type "calc-store-concat")
887 (string-equal type "calc-store-inv")
888 (string-equal type "calc-store-dec")
889 (string-equal type "calc-store-incr")
890 (string-equal type "calc-store-exchange")
891 (string-equal type "calc-unstore")
892 (string-equal type "calc-recall")
893 (string-equal type "calc-let")
894 (string-equal type "calc-permanent-variable"))
896 (calc-edit-macro-combine-var-name))
898 (string-equal type "calc-copy-variable")
899 (string-equal type "calc-declare-variable"))
901 (calc-edit-macro-combine-var-name)
902 (calc-edit-macro-combine-var-name))
903 (t (forward-line 1)))
904 (setq type (calc-edit-macro-command-type))))
905 (goto-char calc-edit-top))
907 ;; Finish editing the macro
909 (defun calc-edit-macro-pre-finish-edit ()
910 (goto-char calc-edit-top)
911 (while (re-search-forward "\\(^\\| \\)RET\\($\\|\t\\| \\)" nil t)
912 (search-backward "RET")
914 (insert "<return>")))
916 (defvar calc-edit-top)
917 (defun calc-edit-macro-finish-edit (cmdname key)
918 "Finish editing a Calc macro.
919 Redefine the corresponding command."
921 (let ((cmd (intern cmdname)))
922 (calc-edit-macro-pre-finish-edit)
923 (let* ((str (buffer-substring calc-edit-top (point-max)))
924 (mac (edmacro-parse-keys str t)))
925 (if (= (length mac) 0)
930 (list 'calc-execute-kbd-macro
931 (vector (key-description mac)
935 (defun calc-finish-formula-edit (func)
936 (let ((buf (current-buffer))
937 (str (buffer-substring calc-edit-top (point-max)))
939 (body (calc-valid-formula-func func)))
940 (set-buffer calc-original-buffer)
941 (let ((val (math-read-expr str)))
942 (if (eq (car-safe val) 'error)
945 (goto-char (+ start (nth 1 val)))
946 (error (nth 2 val))))
948 (let ((calc-user-formula-alist (nth 1 (symbol-function func))))
949 (calc-fix-user-formula val)))
950 (put func 'calc-user-defn val))))
952 (defun calc-valid-formula-func (func)
953 (let ((def (symbol-function func)))
955 (eq (car def) 'lambda)
957 (setq def (cdr (cdr def)))
959 (not (eq (car (car def)) 'math-normalize)))
960 (setq def (cdr def)))
964 (defun calc-get-user-defn ()
967 (message "Get definition of command: z-")
968 (let* ((key (read-char))
969 (def (or (assq key (calc-user-key-map))
970 (assq (upcase key) (calc-user-key-map))
971 (assq (downcase key) (calc-user-key-map))
972 (error "No command defined for that key")))
975 (setq cmd (symbol-function cmd)))
977 (message "Keyboard macro: %s" cmd))
978 (t (let* ((func (calc-stack-command-p cmd))
981 (get func 'calc-user-defn))))
984 (and (calc-valid-formula-func func)
985 (setq defn (append '(calcFunc-lambda)
986 (mapcar 'math-build-var-name
987 (nth 1 (symbol-function
990 (calc-enter-result 0 "gdef" defn))
991 (error "That command is not defined by a formula"))))))))
994 (defun calc-user-define-permanent ()
997 (message "Record in %s the command: z-" calc-settings-file)
998 (let* ((key (read-char))
999 (def (or (assq key (calc-user-key-map))
1000 (assq (upcase key) (calc-user-key-map))
1001 (assq (downcase key) (calc-user-key-map))
1007 (format "Record in %s the algebraic function: "
1009 (mapcar (lambda (x) (substring x 9))
1010 (all-completions "calcFunc-"
1014 (intern (concat "calcFunc-" x))))
1018 (intern (completing-read
1019 (format "Record in %s the command: "
1021 obarray 'fboundp nil "calc-"))))
1022 (error "No command defined for that key"))))
1023 (set-buffer (find-file-noselect (substitute-in-file-name
1024 calc-settings-file)))
1025 (goto-char (point-max))
1026 (let* ((cmd (cdr def))
1027 (fcmd (and cmd (symbolp cmd) (symbol-function cmd)))
1033 (insert "\n;;; Definition stored by Calc on " (current-time-string)
1034 "\n(put 'calc-define '"
1035 (if (symbolp cmd) (symbol-name cmd) (format "key%d" key))
1038 (eq (car-safe fcmd) 'lambda)
1039 (get cmd 'calc-user-defn))
1041 (and (eq (car-safe (nth 3 fcmd)) 'calc-execute-kbd-macro)
1042 (vectorp (nth 1 (nth 3 fcmd)))
1043 (progn (and (fboundp 'edit-kbd-macro)
1044 (edit-kbd-macro nil))
1045 (fboundp 'edmacro-parse-keys))
1047 (aset (nth 1 (nth 3 fcmd)) 1 nil))
1048 (insert (setq str (prin1-to-string
1049 (cons 'defun (cons cmd (cdr fcmd)))))
1051 (or (and (string-match "\"" str) (not q-ok))
1052 (fill-region pt (point)))
1053 (indent-rigidly pt (point) 2)
1054 (delete-region pt (1+ pt))
1055 (insert " (put '" (symbol-name cmd)
1056 " 'calc-user-defn '"
1057 (prin1-to-string (get cmd 'calc-user-defn))
1059 (setq func (calc-stack-command-p cmd))
1060 (let ((ffunc (and func (symbolp func) (symbol-function func)))
1063 (eq (car-safe ffunc) 'lambda)
1064 (get func 'calc-user-defn)
1066 (insert (setq str (prin1-to-string
1067 (cons 'defun (cons func
1070 (or (and (string-match "\"" str) (not q-ok))
1071 (fill-region pt (point)))
1072 (indent-rigidly pt (point) 2)
1073 (delete-region pt (1+ pt))
1075 (insert "(put '" (symbol-name func)
1076 " 'calc-user-defn '"
1077 (prin1-to-string (get func 'calc-user-defn))
1079 (fill-region pt (point))
1080 (indent-rigidly pt (point) 2)
1081 (delete-region pt (1+ pt))))))
1083 (insert " (fset '" (prin1-to-string cmd)
1084 " " (prin1-to-string fcmd) ")\n")))
1085 (or func (setq func (and cmd (symbolp cmd) (fboundp cmd) cmd)))
1086 (if (get func 'math-compose-forms)
1088 (insert "(put '" (symbol-name cmd)
1089 " 'math-compose-forms '"
1090 (prin1-to-string (get func 'math-compose-forms))
1092 (fill-region pt (point))
1093 (indent-rigidly pt (point) 2)
1094 (delete-region pt (1+ pt))))
1096 (insert " (define-key calc-mode-map "
1097 (prin1-to-string (concat "z" (char-to-string key)))
1099 (prin1-to-string cmd)
1104 (defun calc-stack-command-p (cmd)
1105 (if (and cmd (symbolp cmd))
1107 (calc-stack-command-p (symbol-function cmd)))
1109 (eq (car cmd) 'lambda)
1110 (setq cmd (or (assq 'calc-wrapper cmd)
1111 (assq 'calc-slow-wrapper cmd)))
1112 (setq cmd (assq 'calc-enter-result cmd))
1113 (memq (car (nth 3 cmd)) '(cons list))
1114 (eq (car (nth 1 (nth 3 cmd))) 'quote)
1115 (nth 1 (nth 1 (nth 3 cmd))))))
1118 (defun calc-call-last-kbd-macro (arg)
1120 (and defining-kbd-macro
1121 (error "Can't execute anonymous macro while defining one"))
1123 (error "No kbd macro has been defined"))
1124 (calc-execute-kbd-macro last-kbd-macro arg))
1126 (defun calc-execute-kbd-macro (mac arg &rest prefix)
1127 (if (and (vectorp mac) (> (length mac) 0) (stringp (aref mac 0)))
1128 (setq mac (or (aref mac 1)
1129 (aset mac 1 (progn (and (fboundp 'edit-kbd-macro)
1130 (edit-kbd-macro nil))
1131 (edmacro-parse-keys (aref mac 0)))))))
1132 (if (< (prefix-numeric-value arg) 0)
1133 (execute-kbd-macro mac (- (prefix-numeric-value arg)))
1134 (if calc-executing-macro
1135 (execute-kbd-macro mac arg)
1137 (let ((old-stack-whole (copy-sequence calc-stack))
1138 (old-stack-top calc-stack-top)
1139 (old-buffer-size (buffer-size))
1140 (old-refresh-count calc-refresh-count))
1142 (let ((calc-executing-macro mac))
1143 (execute-kbd-macro mac arg))
1144 (calc-select-buffer)
1145 (let ((new-stack (reverse calc-stack))
1146 (old-stack (reverse old-stack-whole)))
1147 (while (and new-stack old-stack
1148 (equal (car new-stack) (car old-stack)))
1149 (setq new-stack (cdr new-stack)
1150 old-stack (cdr old-stack)))
1151 (or (equal prefix '(nil))
1152 (calc-record-list (if (> (length new-stack) 1)
1153 (mapcar 'car new-stack)
1155 (or (car prefix) "kmac")))
1156 (calc-record-undo (list 'set 'saved-stack-top old-stack-top))
1158 (calc-record-undo (list 'pop 1 (mapcar 'car old-stack))))
1159 (let ((calc-stack old-stack-whole)
1161 (calc-cursor-stack-index (length old-stack)))
1162 (if (and (= old-buffer-size (buffer-size))
1163 (= old-refresh-count calc-refresh-count))
1164 (let ((buffer-read-only nil))
1165 (delete-region (point) (point-max))
1167 (calc-record-undo (list 'push 1))
1168 (insert (math-format-stack-value (car new-stack)) "\n")
1169 (setq new-stack (cdr new-stack)))
1170 (calc-renumber-stack))
1172 (calc-record-undo (list 'push 1))
1173 (setq new-stack (cdr new-stack)))
1175 (calc-record-undo (list 'set 'saved-stack-top 0)))))))))
1177 (defun calc-push-list-in-macro (vals m sels)
1178 (let ((entry (list (car vals) 1 (car sels)))
1179 (mm (+ (or m 1) calc-stack-top)))
1181 (setcdr (nthcdr (- mm 2) calc-stack)
1182 (cons entry (nthcdr (1- mm) calc-stack)))
1183 (setq calc-stack (cons entry calc-stack)))))
1185 (defun calc-pop-stack-in-macro (n mm)
1187 (setcdr (nthcdr (- mm 2) calc-stack)
1188 (nthcdr (+ n mm -1) calc-stack))
1189 (setq calc-stack (nthcdr n calc-stack))))
1192 (defun calc-kbd-if ()
1195 (let ((cond (calc-top-n 1)))
1197 (if (math-is-true cond)
1198 (if defining-kbd-macro
1199 (message "If true.."))
1200 (if defining-kbd-macro
1201 (message "Condition is false; skipping to Z: or Z] ..."))
1202 (calc-kbd-skip-to-else-if t)))))
1204 (defun calc-kbd-else-if ()
1208 (defun calc-kbd-skip-to-else-if (else-okay)
1212 (setq ch (read-char))
1214 (error "Unterminated Z[ in keyboard macro"))
1217 (setq ch (read-char))
1219 (setq count (1+ count)))
1221 (setq count (1- count)))
1227 (keyboard-quit))))))
1228 (and defining-kbd-macro
1231 (message "End-if...")))))
1233 (defun calc-kbd-end-if ()
1235 (if defining-kbd-macro
1236 (message "End-if...")))
1238 (defun calc-kbd-else ()
1240 (if defining-kbd-macro
1241 (message "Else; skipping to Z] ..."))
1242 (calc-kbd-skip-to-else-if nil))
1245 (defun calc-kbd-repeat ()
1249 (setq count (math-trunc (calc-top-n 1)))
1250 (or (Math-integerp count)
1251 (error "Count must be an integer"))
1252 (if (Math-integer-negp count)
1254 (or (integerp count)
1255 (setq count 1000000))
1257 (calc-kbd-loop count)))
1259 (defun calc-kbd-for (dir)
1263 (setq init (calc-top-n 2)
1264 final (calc-top-n 1))
1265 (or (and (math-anglep init) (math-anglep final))
1266 (error "Initial and final values must be real numbers"))
1268 (calc-kbd-loop nil init final (and dir (prefix-numeric-value dir)))))
1270 (defun calc-kbd-loop (rpt-count &optional initial final dir)
1272 (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000))
1276 (open last-command-char)
1279 (or executing-kbd-macro
1280 (message "Reading loop body..."))
1282 (setq ch (read-char))
1284 (error "Unterminated Z%c in keyboard macro" open))
1287 (setq ch (read-char)
1288 body (concat body "Z" (char-to-string ch)))
1289 (cond ((memq ch '(?\< ?\( ?\{))
1290 (setq count (1+ count)))
1291 ((memq ch '(?\> ?\) ?\}))
1292 (setq count (1- count)))
1295 (setq parts (nconc parts (list (concat (substring body 0 -2)
1300 (setq body (concat body (char-to-string ch)))))
1301 (if (/= ch (cdr (assq open '( (?\< . ?\>) (?\( . ?\)) (?\{ . ?\}) ))))
1302 (error "Mismatched Z%c and Z%c in keyboard macro" open ch))
1303 (or executing-kbd-macro
1304 (message "Looping..."))
1305 (setq body (concat (substring body 0 -2) "Z]"))
1306 (and (not executing-kbd-macro)
1307 (= rpt-count 1000000)
1311 (message "Warning: Infinite loop! Not executing")
1312 (setq rpt-count 0)))
1313 (or (not initial) dir
1314 (setq dir (math-compare final initial)))
1316 (while (> rpt-count 0)
1319 (if (cond ((eq dir 0) (Math-equal final counter))
1320 ((eq dir 1) (Math-lessp final counter))
1321 ((eq dir -1) (Math-lessp counter final)))
1323 (calc-push counter)))
1324 (while (and part (> rpt-count 0))
1325 (execute-kbd-macro (car part))
1326 (if (math-is-true (calc-top-n 1))
1328 (setq part (cdr part)))
1332 (execute-kbd-macro body)
1334 (let ((step (calc-top-n 1)))
1336 (setq counter (calcFunc-add counter step)))
1337 (setq rpt-count (1- rpt-count))))))))
1338 (or executing-kbd-macro
1339 (message "Looping...done"))))
1341 (defun calc-kbd-end-repeat ()
1343 (error "Unbalanced Z> in keyboard macro"))
1345 (defun calc-kbd-end-for ()
1347 (error "Unbalanced Z) in keyboard macro"))
1349 (defun calc-kbd-end-loop ()
1351 (error "Unbalanced Z} in keyboard macro"))
1353 (defun calc-kbd-break ()
1356 (let ((cond (calc-top-n 1)))
1358 (if (math-is-true cond)
1359 (error "Keyboard macro aborted")))))
1362 (defvar calc-kbd-push-level 0)
1364 ;; The variables var-q0 through var-q9 are the "quick" variables.
1376 (defun calc-kbd-push (arg)
1379 (let* ((defs (and arg (> (prefix-numeric-value arg) 0)))
1390 (calc-internal-prec (if defs 12 calc-internal-prec))
1391 (calc-word-size (if defs 32 calc-word-size))
1392 (calc-angle-mode (if defs 'deg calc-angle-mode))
1393 (calc-simplify-mode (if defs nil calc-simplify-mode))
1394 (calc-algebraic-mode (if arg nil calc-algebraic-mode))
1395 (calc-incomplete-algebraic-mode (if arg nil
1396 calc-incomplete-algebraic-mode))
1397 (calc-symbolic-mode (if defs nil calc-symbolic-mode))
1398 (calc-matrix-mode (if defs nil calc-matrix-mode))
1399 (calc-prefer-frac (if defs nil calc-prefer-frac))
1400 (calc-complex-mode (if defs nil calc-complex-mode))
1401 (calc-infinite-mode (if defs nil calc-infinite-mode))
1405 (if (or executing-kbd-macro defining-kbd-macro)
1407 (if defining-kbd-macro
1408 (message "Reading body..."))
1410 (setq ch (read-char))
1412 (error "Unterminated Z` in keyboard macro"))
1415 (setq ch (read-char)
1416 body (concat body "Z" (char-to-string ch)))
1418 (setq count (1+ count)))
1420 (setq count (1- count)))
1423 (setq body (concat body (char-to-string ch)))))
1424 (if defining-kbd-macro
1425 (message "Reading body...done"))
1426 (let ((calc-kbd-push-level 0))
1427 (execute-kbd-macro (substring body 0 -2))))
1428 (let ((calc-kbd-push-level (1+ calc-kbd-push-level)))
1429 (message "Saving modes; type Z' to restore")
1430 (recursive-edit))))))
1432 (defun calc-kbd-pop ()
1434 (if (> calc-kbd-push-level 0)
1436 (message "Mode settings restored")
1437 (exit-recursive-edit))
1438 (error "Unbalanced Z' in keyboard macro")))
1441 (defun calc-kbd-report (msg)
1442 (interactive "sMessage: ")
1444 (math-working msg (calc-top-n 1))))
1446 (defun calc-kbd-query (msg)
1447 (interactive "sPrompt: ")
1449 (calc-alg-entry nil (and (not (equal msg "")) msg))))
1451 ;;;; Logical operations.
1453 (defun calcFunc-eq (a b &rest more)
1455 (let* ((args (cons a (cons b (copy-sequence more))))
1459 (while (and (cdr p) (not (eq res 0)))
1461 (while (and (setq p2 (cdr p2)) (not (eq res 0)))
1462 (setq res (math-two-eq (car p) (car p2)))
1464 (setcdr p (delq (car p2) (cdr p)))))
1469 (cons 'calcFunc-eq args)
1471 (or (math-two-eq 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-eq (math-neg a) (math-neg b))
1475 (list 'calcFunc-eq a b)))))
1477 (defun calcFunc-neq (a b &rest more)
1479 (let* ((args (cons a (cons b more)))
1484 (while (and (cdr p) (not (eq res 1)))
1486 (while (and (setq p2 (cdr p2)) (not (eq res 1)))
1487 (setq res (math-two-eq (car p) (car p2)))
1488 (or res (setq all nil)))
1494 (cons 'calcFunc-neq args))))
1495 (or (cdr (assq (math-two-eq a b) '((0 . 1) (1 . 0))))
1496 (if (and (or (math-looks-negp a) (math-zerop a))
1497 (or (math-looks-negp b) (math-zerop b)))
1498 (list 'calcFunc-neq (math-neg a) (math-neg b))
1499 (list 'calcFunc-neq a b)))))
1501 (defun math-two-eq (a b)
1502 (if (eq (car-safe a) 'vec)
1503 (if (eq (car-safe b) 'vec)
1504 (if (= (length a) (length b))
1506 (while (and (setq a (cdr a) b (cdr b)) (not (eq res 0)))
1508 (setq res (math-two-eq (car a) (car b)))
1509 (if (eq (math-two-eq (car a) (car b)) 0)
1513 (if (Math-objectp b)
1516 (if (eq (car-safe b) 'vec)
1517 (if (Math-objectp a)
1520 (let ((res (math-compare a b)))
1523 (if (and (= res 2) (not (and (Math-scalarp a) (Math-scalarp b))))
1527 (defun calcFunc-lt (a b)
1528 (let ((res (math-compare a b)))
1532 (if (and (or (math-looks-negp a) (math-zerop a))
1533 (or (math-looks-negp b) (math-zerop b)))
1534 (list 'calcFunc-gt (math-neg a) (math-neg b))
1535 (list 'calcFunc-lt a b))
1538 (defun calcFunc-gt (a b)
1539 (let ((res (math-compare a b)))
1543 (if (and (or (math-looks-negp a) (math-zerop a))
1544 (or (math-looks-negp b) (math-zerop b)))
1545 (list 'calcFunc-lt (math-neg a) (math-neg b))
1546 (list 'calcFunc-gt a b))
1549 (defun calcFunc-leq (a b)
1550 (let ((res (math-compare a b)))
1554 (if (and (or (math-looks-negp a) (math-zerop a))
1555 (or (math-looks-negp b) (math-zerop b)))
1556 (list 'calcFunc-geq (math-neg a) (math-neg b))
1557 (list 'calcFunc-leq a b))
1560 (defun calcFunc-geq (a b)
1561 (let ((res (math-compare a b)))
1565 (if (and (or (math-looks-negp a) (math-zerop a))
1566 (or (math-looks-negp b) (math-zerop b)))
1567 (list 'calcFunc-leq (math-neg a) (math-neg b))
1568 (list 'calcFunc-geq a b))
1571 (defun calcFunc-rmeq (a)
1572 (if (math-vectorp a)
1573 (math-map-vec 'calcFunc-rmeq a)
1574 (if (assq (car-safe a) calc-tweak-eqn-table)
1575 (if (and (eq (car-safe (nth 2 a)) 'var)
1576 (math-objectp (nth 1 a)))
1579 (if (eq (car-safe a) 'calcFunc-assign)
1581 (if (eq (car-safe a) 'calcFunc-evalto)
1583 (list 'calcFunc-rmeq a))))))
1585 (defun calcFunc-land (a b)
1586 (cond ((Math-zerop a)
1594 (t (list 'calcFunc-land a b))))
1596 (defun calcFunc-lor (a b)
1597 (cond ((Math-zerop a)
1605 (t (list 'calcFunc-lor a b))))
1607 (defun calcFunc-lnot (a)
1610 (if (math-is-true a)
1612 (let ((op (and (= (length a) 3)
1613 (assq (car a) calc-tweak-eqn-table))))
1615 (cons (nth 2 op) (cdr a))
1616 (list 'calcFunc-lnot a))))))
1618 (defun calcFunc-if (c e1 e2)
1621 (if (and (math-is-true c) (not (Math-vectorp c)))
1623 (or (and (Math-vectorp c)
1625 (let ((ee1 (if (Math-vectorp e1)
1626 (if (= (length c) (length e1))
1628 (calc-record-why "*Dimension error" e1))
1630 (ee2 (if (Math-vectorp e2)
1631 (if (= (length c) (length e2))
1633 (calc-record-why "*Dimension error" e2))
1636 (cons 'vec (math-if-vector (cdr c) ee1 ee2)))))
1637 (list 'calcFunc-if c e1 e2)))))
1639 (defun math-if-vector (c e1 e2)
1641 (cons (if (Math-zerop (car c)) (car e2) (car e1))
1642 (math-if-vector (cdr c)
1644 (or (cdr e2) e2)))))
1646 (defun math-normalize-logical-op (a)
1647 (or (and (eq (car a) 'calcFunc-if)
1649 (let ((a1 (math-normalize (nth 1 a))))
1651 (math-normalize (nth 3 a))
1652 (if (Math-numberp a1)
1653 (math-normalize (nth 2 a))
1654 (if (and (Math-vectorp (nth 1 a))
1655 (math-constp (nth 1 a)))
1656 (calcFunc-if (nth 1 a)
1657 (math-normalize (nth 2 a))
1658 (math-normalize (nth 3 a)))
1659 (let ((calc-simplify-mode 'none))
1660 (list 'calcFunc-if a1
1661 (math-normalize (nth 2 a))
1662 (math-normalize (nth 3 a)))))))))
1665 (defun calcFunc-in (a b)
1666 (or (and (eq (car-safe b) 'vec)
1668 (while (and (setq bb (cdr bb))
1669 (not (if (memq (car-safe (car bb)) '(vec intv))
1670 (eq (calcFunc-in a (car bb)) 1)
1671 (Math-equal a (car bb))))))
1672 (if bb 1 (and (math-constp a) (math-constp bb) 0))))
1673 (and (eq (car-safe b) 'intv)
1674 (let ((res (math-compare a (nth 2 b))) res2)
1678 (or (/= (nth 1 b) 2)
1679 (Math-lessp (nth 2 b) (nth 3 b))))
1680 (if (memq (nth 1 b) '(2 3)) 1 0))
1681 ((= (setq res2 (math-compare a (nth 3 b))) 1)
1684 (or (/= (nth 1 b) 1)
1685 (Math-lessp (nth 2 b) (nth 3 b))))
1686 (if (memq (nth 1 b) '(1 3)) 1 0))
1692 (and (Math-equal a b)
1694 (and (math-constp a) (math-constp b)
1696 (list 'calcFunc-in a b)))
1698 (defun calcFunc-typeof (a)
1699 (cond ((Math-integerp a) 1)
1700 ((eq (car a) 'frac) 2)
1701 ((eq (car a) 'float) 3)
1702 ((eq (car a) 'hms) 4)
1703 ((eq (car a) 'cplx) 5)
1704 ((eq (car a) 'polar) 6)
1705 ((eq (car a) 'sdev) 7)
1706 ((eq (car a) 'intv) 8)
1707 ((eq (car a) 'mod) 9)
1708 ((eq (car a) 'date) (if (Math-integerp (nth 1 a)) 10 11))
1710 (if (memq (nth 2 a) '(var-inf var-uinf var-nan)) 12 100))
1711 ((eq (car a) 'vec) (if (math-matrixp a) 102 101))
1712 (t (math-calcFunc-to-var (car a)))))
1714 (defun calcFunc-integer (a)
1715 (if (Math-integerp a)
1717 (if (Math-objvecp a)
1719 (list 'calcFunc-integer a))))
1721 (defun calcFunc-real (a)
1724 (if (Math-objvecp a)
1726 (list 'calcFunc-real a))))
1728 (defun calcFunc-constant (a)
1731 (if (Math-objvecp a)
1733 (list 'calcFunc-constant a))))
1735 (defun calcFunc-refers (a b)
1736 (if (math-expr-contains a b)
1738 (if (eq (car-safe a) 'var)
1739 (list 'calcFunc-refers a b)
1742 (defun calcFunc-negative (a)
1743 (if (math-looks-negp a)
1745 (if (or (math-zerop a)
1748 (list 'calcFunc-negative a))))
1750 (defun calcFunc-variable (a)
1751 (if (eq (car-safe a) 'var)
1753 (if (Math-objvecp a)
1755 (list 'calcFunc-variable a))))
1757 (defun calcFunc-nonvar (a)
1758 (if (eq (car-safe a) 'var)
1759 (list 'calcFunc-nonvar a)
1762 (defun calcFunc-istrue (a)
1763 (if (math-is-true a)
1769 ;;;; User-programmability.
1771 ;;; Compiling Lisp-like forms to use the math library.
1773 (defun math-do-defmath (func args body)
1774 (require 'calc-macs)
1775 (let* ((fname (intern (concat "calcFunc-" (symbol-name func))))
1776 (doc (if (stringp (car body)) (list (car body))))
1777 (clargs (mapcar 'math-clean-arg args))
1778 (body (math-define-function-body
1779 (if (stringp (car body)) (cdr body) body)
1782 (if (and (consp (car body))
1783 (eq (car (car body)) 'interactive))
1784 (let ((inter (car body)))
1785 (setq body (cdr body))
1786 (if (or (> (length inter) 2)
1787 (integerp (nth 1 inter)))
1788 (let ((hasprefix nil) (hasmulti nil))
1789 (if (stringp (nth 1 inter))
1791 (cond ((equal (nth 1 inter) "p")
1793 ((equal (nth 1 inter) "m")
1796 "Can't handle interactive code string \"%s\""
1798 (setq inter (cdr inter))))
1799 (if (not (integerp (nth 1 inter)))
1801 "Expected an integer in interactive specification"))
1802 (append (list 'defun
1803 (intern (concat "calc-"
1804 (symbol-name func)))
1805 (if (or hasprefix hasmulti)
1809 (if (or hasprefix hasmulti)
1810 '((interactive "P"))
1814 '(calc-slow-wrapper)
1821 (list 'prefix-numeric-value
1825 (list 'calc-enter-result
1826 (if hasmulti 'n (nth 1 inter))
1830 (list 'quote (list fname))
1831 (list 'calc-top-list-n
1840 'prefix-numeric-value
1844 (list 'calc-top-list-n
1847 (nth 1 inter)))))))))))
1848 (append (list 'defun
1849 (intern (concat "calc-" (symbol-name func)))
1854 (cons 'calc-wrapper body))))))
1855 (append (list 'defun fname clargs)
1857 (math-do-arg-list-check args nil nil)
1860 (defun math-clean-arg (arg)
1862 (math-clean-arg (nth 1 arg))
1865 (defun math-do-arg-check (arg var is-opt is-rest)
1867 (let ((chk (math-do-arg-check arg var nil nil)))
1871 (setq chk (list (cons 'progn chk)))
1874 (let* ((rest (math-do-arg-check (nth 1 arg) var is-opt is-rest))
1876 (qqual (list 'quote qual))
1877 (qual-name (symbol-name qual))
1878 (chk (intern (concat "math-check-" qual-name))))
1884 (list 'mapcar (list 'quote chk) var))
1885 (list 'setq var (list chk var)))))
1886 (if (fboundp (setq chk (intern (concat "math-" qual-name))))
1895 (list 'math-reject-arg
1900 (list 'math-reject-arg var qqual)))))
1901 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name)
1902 (fboundp (setq chk (intern
1904 (math-match-substring
1914 (list 'math-reject-arg
1919 (list 'math-reject-arg var qqual)))))
1920 (error "Unknown qualifier `%s'" qual-name))))))))
1922 (defun math-do-arg-list-check (args is-opt is-rest)
1923 (cond ((null args) nil)
1925 (append (math-do-arg-check (car args)
1926 (math-clean-arg (car args))
1928 (math-do-arg-list-check (cdr args) is-opt is-rest)))
1929 ((eq (car args) '&optional)
1930 (math-do-arg-list-check (cdr args) t nil))
1931 ((eq (car args) '&rest)
1932 (math-do-arg-list-check (cdr args) nil t))
1933 (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!
1954 (defconst math-prim-vars
1957 (&optional . &optional)
1961 (defun math-define-function-body (body env)
1962 (let ((body (math-define-body body env)))
1963 (if (math-body-refers-to body 'math-return)
1964 (list (cons 'catch (cons '(quote math-return) body)))
1967 ;; The variable math-exp-env is local to math-define-body, but is
1968 ;; used by math-define-exp, which is called (indirectly) by
1969 ;; by math-define-body.
1970 (defvar math-exp-env)
1972 (defun math-define-body (body math-exp-env)
1973 (math-define-list body))
1975 (defun math-define-list (body &optional quote)
1978 ((and (eq (car body) ':)
1979 (stringp (nth 1 body)))
1980 (cons (let* ((math-read-expr-quotes t)
1981 (exp (math-read-plain-expr (nth 1 body) t)))
1982 (math-define-exp exp))
1983 (math-define-list (cdr (cdr body)))))
1985 (cons (cond ((consp (car body))
1986 (math-define-list (cdr body) t))
1989 (math-define-list (cdr body))))
1991 (cons (math-define-exp (car body))
1992 (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) math-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 (math-define-list (cdr exp))))))) ;;args
2129 (let ((prim (assq exp math-prim-vars))
2130 (name (symbol-name exp)))
2133 ((memq exp math-exp-env)
2135 ((string-match "-" name)
2138 (intern (concat "var-" name))))))
2140 (if (or (<= exp -1000000) (>= exp 1000000))
2141 (list 'quote (math-normalize exp))
2145 (defun math-define-cond (forms)
2147 (cons (math-define-list (car forms))
2148 (math-define-cond (cdr forms)))))
2150 (defun math-complicated-lhs (body)
2152 (or (not (symbolp (car body)))
2153 (math-complicated-lhs (cdr (cdr body))))))
2155 (defun math-define-setf-list (body)
2157 (cons (math-define-setf (nth 0 body) (nth 1 body))
2158 (math-define-setf-list (cdr (cdr body))))))
2160 (defun math-define-setf (place value)
2161 (setq place (math-define-exp place)
2162 value (math-define-exp value))
2163 (cond ((symbolp place)
2164 (list 'setq place value))
2165 ((eq (car-safe place) 'nth)
2166 (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value))
2167 ((eq (car-safe place) 'elt)
2168 (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value))
2169 ((eq (car-safe place) 'car)
2170 (list 'setcar (nth 1 place) value))
2171 ((eq (car-safe place) 'cdr)
2172 (list 'setcdr (nth 1 place) value))
2174 (error "Bad place form for setf: %s" place))))
2176 (defun math-define-binop (op ident arg1 rest)
2178 (math-define-binop op ident
2179 (list op arg1 (car rest))
2183 (defun math-define-let (vlist)
2185 (cons (if (consp (car vlist))
2186 (cons (car (car vlist))
2187 (math-define-list (cdr (car vlist))))
2189 (math-define-let (cdr vlist)))))
2191 (defun math-define-let-env (vlist)
2193 (cons (if (consp (car vlist))
2196 (math-define-let-env (cdr vlist)))))
2198 (defun math-define-lambda (exp exp-env)
2199 (nconc (list (nth 0 exp) ; 'lambda
2200 (nth 1 exp)) ; arg list
2201 (math-define-function-body (cdr (cdr exp))
2202 (append (nth 1 exp) exp-env))))
2204 (defun math-define-elt (seq idx)
2206 (math-define-elt (list 'elt seq (car idx)) (cdr idx))
2211 ;;; Useful programming macros.
2213 (defmacro math-while (head &rest body)
2214 (let ((body (cons 'while (cons head body))))
2215 (if (math-body-refers-to body 'math-break)
2216 (cons 'catch (cons '(quote math-break) (list body)))
2218 ;; (put 'math-while 'lisp-indent-hook 1)
2220 (defmacro math-for (head &rest body)
2221 (let ((body (if head
2222 (math-handle-for head body)
2223 (cons 'while (cons t body)))))
2224 (if (math-body-refers-to body 'math-break)
2225 (cons 'catch (cons '(quote math-break) (list body)))
2227 ;; (put 'math-for 'lisp-indent-hook 1)
2229 (defun math-handle-for (head body)
2230 (let* ((var (nth 0 (car head)))
2231 (init (nth 1 (car head)))
2232 (limit (nth 2 (car head)))
2233 (step (or (nth 3 (car head)) 1))
2234 (body (if (cdr head)
2235 (list (math-handle-for (cdr head) body))
2237 (all-ints (and (integerp init) (integerp limit) (integerp step)))
2238 (const-limit (or (integerp limit)
2239 (and (eq (car-safe limit) 'quote)
2240 (math-realp (nth 1 limit)))))
2241 (const-step (or (integerp step)
2242 (and (eq (car-safe step) 'quote)
2243 (math-realp (nth 1 step)))))
2244 (save-limit (if const-limit limit (make-symbol "<limit>")))
2245 (save-step (if const-step step (make-symbol "<step>"))))
2247 (cons (append (if const-limit nil (list (list save-limit limit)))
2248 (if const-step nil (list (list save-step step)))
2249 (list (list var init)))
2254 (list '<= var save-limit)
2255 (list '>= var save-limit))
2258 (if (or (math-posp step)
2283 save-step)))))))))))
2285 (defmacro math-foreach (head &rest body)
2286 (let ((body (math-handle-foreach head body)))
2287 (if (math-body-refers-to body 'math-break)
2288 (cons 'catch (cons '(quote math-break) (list body)))
2290 ;; (put 'math-foreach 'lisp-indent-hook 1)
2292 (defun math-handle-foreach (head body)
2293 (let ((var (nth 0 (car head)))
2294 (data (nth 1 (car head)))
2295 (body (if (cdr head)
2296 (list (math-handle-foreach (cdr head) body))
2299 (cons (list (list var data))
2306 (list 'cdr var)))))))))))
2309 (defun math-body-refers-to (body thing)
2310 (or (equal body thing)
2312 (or (math-body-refers-to (car body) thing)
2313 (math-body-refers-to (cdr body) thing)))))
2315 (defun math-break (&optional value)
2316 (throw 'math-break value))
2318 (defun math-return (&optional value)
2319 (throw 'math-return value))
2325 (defun math-composite-inequalities (x op)
2326 (if (memq (nth 1 op) '(calcFunc-eq calcFunc-neq))
2327 (if (eq (car x) (nth 1 op))
2328 (append x (list (math-read-expr-level (nth 3 op))))
2329 (throw 'syntax "Syntax error"))
2332 (if (memq (nth 1 op) '(calcFunc-lt calcFunc-leq))
2333 (if (memq (car x) '(calcFunc-lt calcFunc-leq))
2335 (+ (if (eq (car x) 'calcFunc-leq) 2 0)
2336 (if (eq (nth 1 op) 'calcFunc-leq) 1 0))
2337 (nth 1 x) (math-read-expr-level (nth 3 op)))
2338 (throw 'syntax "Syntax error"))
2339 (if (memq (car x) '(calcFunc-gt calcFunc-geq))
2341 (+ (if (eq (nth 1 op) 'calcFunc-geq) 2 0)
2342 (if (eq (car x) 'calcFunc-geq) 1 0))
2343 (math-read-expr-level (nth 3 op)) (nth 1 x))
2344 (throw 'syntax "Syntax error"))))))
2346 (provide 'calc-prog)
2348 ;;; arch-tag: 4c5a183b-c9e5-4632-bb3f-e41a764518b0
2349 ;;; calc-prog.el ends here