1 ;;; calc-prog.el --- user programmability functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
33 ;; Declare functions which are defined elsewhere.
34 (declare-function edmacro-format-keys
"edmacro" (macro &optional verbose
))
35 (declare-function edmacro-parse-keys
"edmacro" (string &optional need-vector
))
36 (declare-function math-read-expr-level
"calc-aent" (exp-prec &optional exp-term
))
39 (defun calc-equal-to (arg)
42 (if (and (integerp arg
) (> arg
2))
43 (calc-enter-result arg
"eq" (cons 'calcFunc-eq
(calc-top-list-n arg
)))
44 (calc-binary-op "eq" 'calcFunc-eq arg
))))
46 (defun calc-remove-equal (arg)
49 (calc-unary-op "rmeq" 'calcFunc-rmeq arg
)))
51 (defun calc-not-equal-to (arg)
54 (if (and (integerp arg
) (> arg
2))
55 (calc-enter-result arg
"neq" (cons 'calcFunc-neq
(calc-top-list-n arg
)))
56 (calc-binary-op "neq" 'calcFunc-neq arg
))))
58 (defun calc-less-than (arg)
61 (calc-binary-op "lt" 'calcFunc-lt arg
)))
63 (defun calc-greater-than (arg)
66 (calc-binary-op "gt" 'calcFunc-gt arg
)))
68 (defun calc-less-equal (arg)
71 (calc-binary-op "leq" 'calcFunc-leq arg
)))
73 (defun calc-greater-equal (arg)
76 (calc-binary-op "geq" 'calcFunc-geq arg
)))
78 (defun calc-in-set (arg)
81 (calc-binary-op "in" 'calcFunc-in arg
)))
83 (defun calc-logical-and (arg)
86 (calc-binary-op "land" 'calcFunc-land arg
1)))
88 (defun calc-logical-or (arg)
91 (calc-binary-op "lor" 'calcFunc-lor arg
0)))
93 (defun calc-logical-not (arg)
96 (calc-unary-op "lnot" 'calcFunc-lnot arg
)))
98 (defun calc-logical-if ()
101 (calc-enter-result 3 "if" (cons 'calcFunc-if
(calc-top-list-n 3)))))
107 (defun calc-timing (n)
110 (calc-change-mode 'calc-timing n nil t
)
111 (message (if calc-timing
112 "Reporting timing of slow commands in Trail"
113 "Not reporting timing of commands"))))
115 (defun calc-pass-errors ()
117 ;; The following two cases are for the new, optimizing byte compiler
118 ;; or the standard 18.57 byte compiler, respectively.
120 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do
))) 15)))
121 (or (memq (car-safe (car-safe place
)) '(error xxxerror
))
122 (setq place
(aref (nth 2 (nth 2 (symbol-function 'calc-do
))) 27)))
123 (or (memq (car (car place
)) '(error xxxerror
))
125 (setcar (car place
) 'xxxerror
))
126 (error (error "The calc-do function has been modified; unable to patch"))))
128 (defun calc-user-define ()
130 (message "Define user key: z-")
131 (let ((key (read-char)))
132 (if (= (calc-user-function-classify key
) 0)
133 (error "Can't redefine \"?\" key"))
134 (let ((func (intern (completing-read (concat "Set key z "
141 (let* ((kmap (calc-user-key-map))
142 (old (assq key kmap
)))
145 (setcdr kmap
(cons (cons key func
) (cdr kmap
))))))))
147 (defun calc-user-undefine ()
149 (message "Undefine user key: z-")
150 (let ((key (read-char)))
151 (if (= (calc-user-function-classify key
) 0)
152 (error "Can't undefine \"?\" key"))
153 (let* ((kmap (calc-user-key-map)))
154 (delq (or (assq key kmap
)
155 (assq (upcase key
) kmap
)
156 (assq (downcase key
) kmap
)
157 (error "No such user key is defined"))
161 ;; math-integral-cache-state is originally declared in calcalg2.el,
162 ;; it is used in calc-user-define-variable.
163 (defvar math-integral-cache-state
)
165 ;; calc-user-formula-alist is local to calc-user-define-formula,
166 ;; calc-user-define-composition and calc-finish-formula-edit,
167 ;; but is used by calc-fix-user-formula.
168 (defvar calc-user-formula-alist
)
170 (defun calc-user-define-formula ()
173 (let* ((form (calc-top 1))
175 (is-lambda (and (eq (car-safe form
) 'calcFunc-lambda
)
176 (>= (length form
) 2)))
177 odef key keyname cmd cmd-base cmd-base-default
178 func calc-user-formula-alist is-symb
)
180 (setq arglist
(mapcar (function (lambda (x) (nth 1 x
)))
181 (nreverse (cdr (reverse (cdr form
)))))
182 form
(nth (1- (length form
)) form
))
183 (calc-default-formula-arglist form
)
184 (setq arglist
(sort arglist
'string-lessp
)))
185 (message "Define user key: z-")
186 (setq key
(read-char))
187 (if (= (calc-user-function-classify key
) 0)
188 (error "Can't redefine \"?\" key"))
189 (setq key
(and (not (memq key
'(13 32))) key
)
191 (if (or (and (<= ?
0 key
) (<= key ?
9))
192 (and (<= ?a key
) (<= key ?z
))
193 (and (<= ?A key
) (<= key ?Z
)))
195 (format "%03d" key
)))
196 odef
(assq key
(calc-user-key-map)))
198 (setq keyname
(format "%05d" (abs (%
(random) 10000)))))
201 (setq cmd-base-default
(concat "User-" keyname
))
202 (setq cmd
(completing-read
203 (concat "Define M-x command name (default calc-"
206 obarray
'commandp nil
207 (if (and odef
(symbolp (cdr odef
)))
208 (symbol-name (cdr odef
))
210 (if (or (string-equal cmd
"")
211 (string-equal cmd
"calc-"))
212 (setq cmd
(concat "calc-User-" keyname
)))
213 (setq cmd-base
(and (string-match "\\`calc-\\(.+\\)\\'" cmd
)
214 (math-match-substring cmd
1)))
215 (setq cmd
(intern cmd
))
221 (if (get cmd
'calc-user-defn
)
222 (concat "Replace previous definition for "
223 (symbol-name cmd
) "? ")
224 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
227 (setq cmd-base-default
230 "\\`User-.+" cmd-base
)
233 (substring cmd-base
5))
235 (concat "User" keyname
)))
239 (concat "Define algebraic function name (default "
240 cmd-base-default
"): ")
241 (mapcar (lambda (x) (substring x
9))
242 (all-completions "calcFunc-"
246 (intern (concat "calcFunc-" x
))))
249 (if (string-equal func
"calcFunc-")
250 (intern (concat "calcFunc-" cmd-base-default
))
258 (if (get func
'calc-user-defn
)
259 (concat "Replace previous definition for "
260 (symbol-name func
) "? ")
261 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
264 (setq func
(intern (concat "calcFunc-User"
266 (and cmd
(symbol-name cmd
))
267 (format "%05d" (%
(random) 10000)))))))
270 (setq calc-user-formula-alist arglist
)
273 (setq calc-user-formula-alist
274 (read-from-minibuffer "Function argument list: "
276 (prin1-to-string arglist
)
280 (and (not (calc-subsetp calc-user-formula-alist arglist
))
282 "Okay for arguments that don't appear in formula to be ignored? "))))))
283 (setq is-symb
(and calc-user-formula-alist
286 "Leave it symbolic for non-constant arguments? ")))
287 (setq calc-user-formula-alist
288 (mapcar (function (lambda (x)
289 (or (cdr (assq x
'((nil . arg-nil
)
291 x
))) calc-user-formula-alist
))
300 (list 'calc-enter-result
301 (length calc-user-formula-alist
)
302 (let ((name (symbol-name (or func cmd
))))
304 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
306 (math-match-substring name
1)))
309 (list 'calc-top-list-n
310 (length calc-user-formula-alist
)))))))
311 (put cmd
'calc-user-defn t
)))
312 (let ((body (list 'math-normalize
(calc-fix-user-formula form
))))
315 (list 'lambda calc-user-formula-alist
)
317 (mapcar (function (lambda (v)
318 (list 'math-check-const v t
)))
319 calc-user-formula-alist
))
321 (put func
'calc-user-defn form
)
322 (setq math-integral-cache-state nil
)
324 (let* ((kmap (calc-user-key-map))
325 (old (assq key kmap
)))
328 (setcdr kmap
(cons (cons key cmd
) (cdr kmap
)))))))
331 (defun calc-default-formula-arglist (form)
333 (if (eq (car form
) 'var
)
334 (if (or (memq (nth 1 form
) arglist
)
335 (math-const-var form
))
337 (setq arglist
(cons (nth 1 form
) arglist
)))
338 (calc-default-formula-arglist-step (cdr form
)))))
340 (defun calc-default-formula-arglist-step (l)
343 (calc-default-formula-arglist (car l
))
344 (calc-default-formula-arglist-step (cdr l
)))))
346 (defun calc-subsetp (a b
)
348 (and (memq (car a
) b
)
349 (calc-subsetp (cdr a
) b
))))
351 (defun calc-fix-user-formula (f)
354 (cond ((and (eq (car f
) 'var
)
355 (memq (setq temp
(or (cdr (assq (nth 1 f
) '((nil . arg-nil
)
358 calc-user-formula-alist
))
360 ((or (math-constp f
) (eq (car f
) 'var
))
362 ((and (eq (car f
) 'calcFunc-eval
)
364 (list 'let
'((calc-simplify-mode nil
))
365 (list 'math-normalize
(calc-fix-user-formula (nth 1 f
)))))
366 ((and (eq (car f
) 'calcFunc-evalsimp
)
368 (list 'math-simplify
(calc-fix-user-formula (nth 1 f
))))
369 ((and (eq (car f
) 'calcFunc-evalextsimp
)
371 (list 'math-simplify-extended
372 (calc-fix-user-formula (nth 1 f
))))
375 (cons (list 'quote
(car f
))
376 (mapcar 'calc-fix-user-formula
(cdr f
)))))))
379 (defun calc-user-define-composition ()
382 (if (eq calc-language
'unform
)
383 (error "Can't define formats for unformatted mode"))
384 (let* ((comp (calc-top 1))
387 (completing-read "Define format for which function: "
388 (mapcar (lambda (x) (substring x
9))
389 (all-completions "calcFunc-"
393 (intern (concat "calcFunc-" x
))))))))
394 (comps (get func
'math-compose-forms
))
397 (calc-user-formula-alist nil
))
398 (if (math-zerop comp
)
399 (if (setq entry
(assq calc-language comps
))
400 (put func
'math-compose-forms
(delq entry comps
)))
401 (calc-default-formula-arglist comp
)
402 (setq arglist
(sort arglist
'string-lessp
))
405 (setq calc-user-formula-alist
406 (read-from-minibuffer "Composition argument list: "
408 (prin1-to-string arglist
)
412 (and (not (calc-subsetp calc-user-formula-alist arglist
))
414 "Okay for arguments that don't appear in formula to be invisible? "))))
415 (or (setq entry
(assq calc-language comps
))
416 (put func
'math-compose-forms
417 (cons (setq entry
(list calc-language
)) comps
)))
418 (or (setq entry2
(assq (length calc-user-formula-alist
) (cdr entry
)))
421 (list (length calc-user-formula-alist
))) (cdr entry
))))
423 (list 'lambda calc-user-formula-alist
(calc-fix-user-formula comp
))))
428 (defun calc-user-define-kbd-macro (arg)
431 (error "No keyboard macro defined"))
432 (message "Define last kbd macro on user key: z-")
433 (let ((key (read-char)))
434 (if (= (calc-user-function-classify key
) 0)
435 (error "Can't redefine \"?\" key"))
436 (let ((cmd (intern (completing-read "Full name for new command: "
441 (if (or (and (>= key ?a
)
448 (format "%03d" key
)))))))
450 (not (let ((f (symbol-function cmd
)))
453 (eq (car-safe (nth 3 f
))
454 'calc-execute-kbd-macro
)))))
455 (error "Function %s is already defined and not a keyboard macro"
457 (put cmd
'calc-user-defn t
)
458 (fset cmd
(if (< (prefix-numeric-value arg
) 0)
463 (list 'calc-execute-kbd-macro
464 (vector (key-description last-kbd-macro
)
467 (format "z%c" key
)))))
468 (let* ((kmap (calc-user-key-map))
469 (old (assq key kmap
)))
472 (setcdr kmap
(cons (cons key cmd
) (cdr kmap
))))))))
475 (defun calc-edit-user-syntax ()
478 (let ((lang calc-language
))
479 (calc-edit-mode (list 'calc-finish-user-syntax-edit
(list 'quote lang
))
481 (format "Editing %s-Mode Syntax Table. "
482 (cond ((null lang
) "Normal")
483 ((eq lang
'tex
) "TeX")
484 ((eq lang
'latex
) "LaTeX")
485 (t (capitalize (symbol-name lang
))))))
486 (calc-write-parse-table (cdr (assq lang calc-user-parse-tables
))
488 (calc-show-edit-buffer))
490 (defvar calc-original-buffer
)
492 (defun calc-finish-user-syntax-edit (lang)
493 (let ((tab (calc-read-parse-table calc-original-buffer lang
))
494 (entry (assq lang calc-user-parse-tables
)))
497 (car (setq calc-user-parse-tables
498 (cons (list lang
) calc-user-parse-tables
))))
501 (setq calc-user-parse-tables
502 (delq entry calc-user-parse-tables
)))))
503 (switch-to-buffer calc-original-buffer
))
505 ;; The variable calc-lang is local to calc-write-parse-table, but is
506 ;; used by calc-write-parse-table-part which is called by
507 ;; calc-write-parse-table. The variable is also local to
508 ;; calc-read-parse-table, but is used by calc-fix-token-name which
509 ;; is called (indirectly) by calc-read-parse-table.
512 (defun calc-write-parse-table (tab calc-lang
)
515 (calc-write-parse-table-part (car (car p
)))
517 (let ((math-format-hash-args t
))
518 (math-format-flat-expr (cdr (car p
)) 0))
522 (defun calc-write-parse-table-part (p)
524 (cond ((stringp (car p
))
526 (if (and (string-match "\\`\\\\dots\\>" s
)
527 (not (memq calc-lang
'(tex latex
))))
528 (setq s
(concat ".." (substring s
5))))
529 (if (or (and (string-match
530 "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s
)
531 (string-match "[^a-zA-Z0-9\\]" s
))
532 (and (assoc s
'((")") ("]") (">")))
534 (insert (prin1-to-string s
) " ")
539 (insert "/" (int-to-string (car p
))))
541 ((and (eq (car (car p
)) '\?) (equal (car (nth 2 (car p
))) "$$"))
542 (insert (car (nth 1 (car p
))) " "))
545 (calc-write-parse-table-part (nth 1 (car p
)))
546 (insert "}" (symbol-name (car (car p
))))
548 (calc-write-parse-table-part (list (car (nth 2 (car p
)))))
552 (defun calc-read-parse-table (calc-buf calc-lang
)
555 (skip-chars-forward "\n\t ")
557 (if (looking-at "%%")
560 (p (calc-read-parse-table-part ":=[\n\t ]+" ":=")))
561 (or (stringp (car p
))
562 (and (integerp (car p
))
566 (error "Malformed syntax rule")))
569 (let* ((str (buffer-substring pos
(point)))
571 (set-buffer calc-buf
)
572 (let ((calc-user-parse-tables nil
)
574 (math-expr-opers (math-standard-ops))
575 (calc-hashes-used 0))
577 (if (string-match ",[ \t]*\\'" str
)
578 (substring str
0 (match-beginning 0))
580 (if (eq (car-safe exp
) 'error
)
582 (goto-char (+ pos
(nth 1 exp
)))
583 (error (nth 2 exp
))))
584 (setq tab
(nconc tab
(list (cons p exp
)))))))))
587 (defun calc-fix-token-name (name &optional unquoted
)
588 (cond ((string-match "\\`\\.\\." name
)
589 (concat "\\dots" (substring name
2)))
590 ((and (equal name
"{") (memq calc-lang
'(tex latex eqn
)))
592 ((and (equal name
"}") (memq calc-lang
'(tex latex eqn
)))
594 ((and (equal name
"&") (memq calc-lang
'(tex latex
)))
597 (search-backward "#")
598 (error "Token '#' is reserved"))
599 ((and unquoted
(string-match "#" name
))
600 (error "Tokens containing '#' must be quoted"))
601 ((not (string-match "[^ ]" name
))
602 (search-backward "\"" nil t
)
603 (error "Blank tokens are not allowed"))
606 (defun calc-read-parse-table-part (term eterm
)
610 (skip-chars-forward "\n\t ")
611 (if (eobp) (error "Expected '%s'" eterm
))
612 (not (looking-at term
)))
613 (cond ((looking-at "%%")
615 ((looking-at "{[\n\t ]")
617 (let ((p (calc-read-parse-table-part "}" "}")))
618 (or (looking-at "[+*?]")
619 (error "Expected '+', '*', or '?'"))
620 (let ((sym (intern (buffer-substring (point) (1+ (point))))))
622 (looking-at "[^\n\t ]*")
623 (let ((sep (buffer-substring (point) (match-end 0))))
624 (goto-char (match-end 0))
625 (and (eq sym
'\?) (> (length sep
) 0)
626 (not (equal sep
"$")) (not (equal sep
"."))
627 (error "Separator not allowed with { ... }?"))
628 (if (string-match "\\`\"" sep
)
629 (setq sep
(read-from-string sep
)))
630 (setq sep
(calc-fix-token-name sep
))
631 (setq part
(nconc part
633 (and (> (length sep
) 0)
634 (cons sep p
))))))))))
636 (error "Too many }'s"))
638 (setq quoted
(calc-fix-token-name (read (current-buffer)))
639 part
(nconc part
(list quoted
))))
640 ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]")
641 (setq part
(nconc part
(list (if (= (match-beginning 1)
646 (1+ (match-beginning 1))
648 (goto-char (match-end 0)))
649 ((looking-at ":=[\n\t ]")
650 (error "Misplaced ':='"))
652 (looking-at "[^\n\t ]*")
653 (let ((end (match-end 0)))
654 (setq part
(nconc part
(list (calc-fix-token-name
658 (goto-char (match-end 0))
659 (let ((len (length part
)))
660 (while (and (> len
1)
661 (let ((last (nthcdr (setq len
(1- len
)) part
)))
662 (and (assoc (car last
) '((")") ("]") (">")))
663 (not (eq (car last
) quoted
))
665 (list '\? (list (car last
)) '("$$"))))))))
668 (defun calc-user-define-invocation ()
671 (error "No keyboard macro defined"))
672 (setq calc-invocation-macro last-kbd-macro
)
673 (message "Use `C-x * Z' to invoke this macro"))
675 (defun calc-user-define-edit ()
676 (interactive) ; but no calc-wrapper!
677 (message "Edit definition of command: z-")
680 (def (or (assq key
(calc-user-key-map))
681 (assq (upcase key
) (calc-user-key-map))
682 (assq (downcase key
) (calc-user-key-map))
683 (error "No command defined for that key")))
686 (setq cmdname
(symbol-name cmd
))
687 (setq cmd
(symbol-function cmd
)))
688 (cond ((or (stringp cmd
)
690 (eq (car-safe (nth 3 cmd
)) 'calc-execute-kbd-macro
)))
691 (let* ((mac (elt (nth 1 (nth 3 cmd
)) 1))
692 (str (edmacro-format-keys mac t
))
693 (kys (nth 3 (nth 3 cmd
))))
695 (list 'calc-edit-macro-finish-edit cmdname kys
)
697 "Editing keyboard macro (%s, bound to %s).\n"
698 "Original keys: %s \n")
699 cmdname kys
(elt (nth 1 (nth 3 cmd
)) 0)))
701 (calc-edit-format-macro-buffer)
702 (calc-show-edit-buffer)))
703 (t (let* ((func (calc-stack-command-p cmd
))
706 (get func
'calc-user-defn
)))
707 (kys (concat "z" (char-to-string (car def
))))
708 (intcmd (symbol-name (cdr def
)))
709 (algcmd (if func
(substring (symbol-name func
) 9) "")))
710 (if (and defn
(calc-valid-formula-func func
))
711 (let ((niceexpr (math-format-nice-expr defn
(frame-width))))
714 (list 'calc-finish-formula-edit
(list 'quote func
))
717 "Editing formula (%s, %s, bound to %s).\n"
718 "Original formula: %s\n")
719 intcmd algcmd kys niceexpr
))
720 (insert (math-showing-full-precision
723 (calc-show-edit-buffer))
724 (error "That command's definition cannot be edited")))))))
726 ;; Formatting the macro buffer
728 (defvar calc-edit-top
)
730 (defun calc-edit-macro-repeats ()
731 (goto-char calc-edit-top
)
733 (re-search-forward "^\\([0-9]+\\)\\*" nil t
)
734 (let ((num (string-to-number (match-string 1)))
735 (line (buffer-substring (point) (line-end-position))))
736 (goto-char (line-beginning-position))
740 (setq num
(1- num
))))))
742 (defun calc-edit-macro-adjust-buffer ()
743 (calc-edit-macro-repeats)
744 (goto-char calc-edit-top
)
745 (while (re-search-forward "^RET$" nil t
)
747 (goto-char calc-edit-top
)
748 (while (and (re-search-forward "^$" nil t
)
749 (not (= (point) (point-max))))
752 (defun calc-edit-macro-command ()
753 "Return the command on the current line in a Calc macro editing buffer."
754 (let ((beg (line-beginning-position))
756 (if (search-forward ";;" (line-end-position) 1)
758 (skip-chars-backward " \t")
760 (buffer-substring beg end
)))
762 (defun calc-edit-macro-command-type ()
763 "Return the type of command on the current line in a Calc macro editing buffer."
764 (let ((beg (save-excursion
765 (if (search-forward ";;" (line-end-position) t
)
767 (skip-chars-forward " \t")
770 (goto-char (line-end-position))
771 (skip-chars-backward " \t")
774 (buffer-substring beg end
)
777 (defun calc-edit-macro-combine-alg-ent ()
778 "Put an entire algebraic entry on a single line."
779 (let ((line (calc-edit-macro-command))
780 (type (calc-edit-macro-command-type))
783 (goto-char (line-beginning-position))
785 (setq curline
(calc-edit-macro-command))
787 (not (string-equal "RET" curline
))
788 (not (setq match
(string-match "<return>" curline
))))
789 (setq line
(concat line curline
))
791 (setq curline
(calc-edit-macro-command)))
794 (setq line
(concat line
(substring curline
0 match
))))
795 (setq line
(replace-regexp-in-string "SPC" " SPC "
796 (replace-regexp-in-string " " "" line
)))
797 (insert line
"\t\t\t")
798 (if (> (current-column) 24)
800 (insert ";; " type
"\n")
802 (insert "RET\t\t\t;; calc-enter\n"))))
804 (defun calc-edit-macro-combine-ext-command ()
805 "Put an entire extended command on a single line."
806 (let ((cmdbeg (calc-edit-macro-command))
808 (type (calc-edit-macro-command-type))
811 (goto-char (line-beginning-position))
813 (setq curline
(calc-edit-macro-command))
815 (not (string-equal "RET" curline
))
816 (not (setq match
(string-match "<return>" curline
))))
817 (setq line
(concat line curline
))
819 (setq curline
(calc-edit-macro-command)))
822 (setq line
(concat line
(substring curline
0 match
))))
823 (setq line
(replace-regexp-in-string " " "" line
))
824 (insert cmdbeg
" " line
"\t\t\t")
825 (if (> (current-column) 24)
827 (insert ";; " type
"\n")
829 (insert "RET\t\t\t;; calc-enter\n"))))
831 (defun calc-edit-macro-combine-var-name ()
832 "Put an entire variable name on a single line."
833 (let ((line (calc-edit-macro-command))
836 (goto-char (line-beginning-position))
838 (if (member line
'("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
839 (insert line
"\t\t\t;; calc quick variable\n")
840 (setq curline
(calc-edit-macro-command))
842 (not (string-equal "RET" curline
))
843 (not (setq match
(string-match "<return>" curline
))))
844 (setq line
(concat line curline
))
846 (setq curline
(calc-edit-macro-command)))
849 (setq line
(concat line
(substring curline
0 match
))))
850 (setq line
(replace-regexp-in-string " " "" line
))
851 (insert line
"\t\t\t")
852 (if (> (current-column) 24)
854 (insert ";; calc variable\n")
856 (insert "RET\t\t\t;; calc-enter\n")))))
858 (defun calc-edit-macro-combine-digits ()
859 "Put an entire sequence of digits on a single line."
860 (let ((line (calc-edit-macro-command))
862 (goto-char (line-beginning-position))
864 (while (string-equal (calc-edit-macro-command-type) "calcDigit-start")
865 (setq line
(concat line
(calc-edit-macro-command)))
867 (insert line
"\t\t\t")
868 (if (> (current-column) 24)
870 (insert ";; calc digits\n")))
872 (defun calc-edit-format-macro-buffer ()
873 "Rewrite the Calc macro editing buffer."
874 (calc-edit-macro-adjust-buffer)
875 (goto-char calc-edit-top
)
876 (let ((type (calc-edit-macro-command-type)))
877 (while (not (string-equal type
""))
880 (string-equal type
"calc-algebraic-entry")
881 (string-equal type
"calc-auto-algebraic-entry"))
882 (calc-edit-macro-combine-alg-ent))
883 ((string-equal type
"calc-execute-extended-command")
884 (calc-edit-macro-combine-ext-command))
885 ((string-equal type
"calcDigit-start")
886 (calc-edit-macro-combine-digits))
888 (string-equal type
"calc-store")
889 (string-equal type
"calc-store-into")
890 (string-equal type
"calc-store-neg")
891 (string-equal type
"calc-store-plus")
892 (string-equal type
"calc-store-minus")
893 (string-equal type
"calc-store-div")
894 (string-equal type
"calc-store-times")
895 (string-equal type
"calc-store-power")
896 (string-equal type
"calc-store-concat")
897 (string-equal type
"calc-store-inv")
898 (string-equal type
"calc-store-dec")
899 (string-equal type
"calc-store-incr")
900 (string-equal type
"calc-store-exchange")
901 (string-equal type
"calc-unstore")
902 (string-equal type
"calc-recall")
903 (string-equal type
"calc-let")
904 (string-equal type
"calc-permanent-variable"))
906 (calc-edit-macro-combine-var-name))
908 (string-equal type
"calc-copy-variable")
909 (string-equal type
"calc-copy-special-constant")
910 (string-equal type
"calc-declare-variable"))
912 (calc-edit-macro-combine-var-name)
913 (calc-edit-macro-combine-var-name))
914 (t (forward-line 1)))
915 (setq type
(calc-edit-macro-command-type))))
916 (goto-char calc-edit-top
))
918 ;; Finish editing the macro
920 (defun calc-edit-macro-pre-finish-edit ()
921 (goto-char calc-edit-top
)
922 (while (re-search-forward "\\(^\\| \\)RET\\($\\|\t\\| \\)" nil t
)
923 (search-backward "RET")
925 (insert "<return>")))
927 (defun calc-edit-macro-finish-edit (cmdname key
)
928 "Finish editing a Calc macro.
929 Redefine the corresponding command."
931 (let ((cmd (intern cmdname
)))
932 (calc-edit-macro-pre-finish-edit)
933 (let* ((str (buffer-substring calc-edit-top
(point-max)))
934 (mac (edmacro-parse-keys str t
)))
935 (if (= (length mac
) 0)
940 (list 'calc-execute-kbd-macro
941 (vector (key-description mac
)
945 (defun calc-finish-formula-edit (func)
946 (let ((buf (current-buffer))
947 (str (buffer-substring calc-edit-top
(point-max)))
949 (body (calc-valid-formula-func func
)))
950 (set-buffer calc-original-buffer
)
951 (let ((val (math-read-expr str
)))
952 (if (eq (car-safe val
) 'error
)
955 (goto-char (+ start
(nth 1 val
)))
956 (error (nth 2 val
))))
958 (let ((calc-user-formula-alist (nth 1 (symbol-function func
))))
959 (calc-fix-user-formula val
)))
960 (put func
'calc-user-defn val
))))
962 (defun calc-valid-formula-func (func)
963 (let ((def (symbol-function func
)))
965 (eq (car def
) 'lambda
)
967 (setq def
(cdr (cdr def
)))
969 (not (eq (car (car def
)) 'math-normalize
)))
970 (setq def
(cdr def
)))
974 (defun calc-get-user-defn ()
977 (message "Get definition of command: z-")
978 (let* ((key (read-char))
979 (def (or (assq key
(calc-user-key-map))
980 (assq (upcase key
) (calc-user-key-map))
981 (assq (downcase key
) (calc-user-key-map))
982 (error "No command defined for that key")))
985 (setq cmd
(symbol-function cmd
)))
987 (message "Keyboard macro: %s" cmd
))
988 (t (let* ((func (calc-stack-command-p cmd
))
991 (get func
'calc-user-defn
))))
994 (and (calc-valid-formula-func func
)
995 (setq defn
(append '(calcFunc-lambda)
996 (mapcar 'math-build-var-name
997 (nth 1 (symbol-function
1000 (calc-enter-result 0 "gdef" defn
))
1001 (error "That command is not defined by a formula"))))))))
1004 (defun calc-user-define-permanent ()
1007 (message "Record in %s the command: z-" calc-settings-file
)
1008 (let* ((key (read-char))
1009 (def (or (assq key
(calc-user-key-map))
1010 (assq (upcase key
) (calc-user-key-map))
1011 (assq (downcase key
) (calc-user-key-map))
1017 (format "Record in %s the algebraic function: "
1019 (mapcar (lambda (x) (substring x
9))
1020 (all-completions "calcFunc-"
1024 (intern (concat "calcFunc-" x
))))
1028 (intern (completing-read
1029 (format "Record in %s the command: "
1031 obarray
'fboundp nil
"calc-"))))
1032 (error "No command defined for that key"))))
1033 (set-buffer (find-file-noselect (substitute-in-file-name
1034 calc-settings-file
)))
1035 (goto-char (point-max))
1036 (let* ((cmd (cdr def
))
1037 (fcmd (and cmd
(symbolp cmd
) (symbol-function cmd
)))
1043 (insert "\n;;; Definition stored by Calc on " (current-time-string)
1044 "\n(put 'calc-define '"
1045 (if (symbolp cmd
) (symbol-name cmd
) (format "key%d" key
))
1048 (eq (car-safe fcmd
) 'lambda
)
1049 (get cmd
'calc-user-defn
))
1051 (and (eq (car-safe (nth 3 fcmd
)) 'calc-execute-kbd-macro
)
1052 (vectorp (nth 1 (nth 3 fcmd
)))
1053 (progn (and (fboundp 'edit-kbd-macro
)
1054 (edit-kbd-macro nil
))
1055 (fboundp 'edmacro-parse-keys
))
1057 (aset (nth 1 (nth 3 fcmd
)) 1 nil
))
1058 (insert (setq str
(prin1-to-string
1059 (cons 'defun
(cons cmd
(cdr fcmd
)))))
1061 (or (and (string-match "\"" str
) (not q-ok
))
1062 (fill-region pt
(point)))
1063 (indent-rigidly pt
(point) 2)
1064 (delete-region pt
(1+ pt
))
1065 (insert " (put '" (symbol-name cmd
)
1066 " 'calc-user-defn '"
1067 (prin1-to-string (get cmd
'calc-user-defn
))
1069 (setq func
(calc-stack-command-p cmd
))
1070 (let ((ffunc (and func
(symbolp func
) (symbol-function func
)))
1073 (eq (car-safe ffunc
) 'lambda
)
1074 (get func
'calc-user-defn
)
1076 (insert (setq str
(prin1-to-string
1077 (cons 'defun
(cons func
1080 (or (and (string-match "\"" str
) (not q-ok
))
1081 (fill-region pt
(point)))
1082 (indent-rigidly pt
(point) 2)
1083 (delete-region pt
(1+ pt
))
1085 (insert "(put '" (symbol-name func
)
1086 " 'calc-user-defn '"
1087 (prin1-to-string (get func
'calc-user-defn
))
1089 (fill-region pt
(point))
1090 (indent-rigidly pt
(point) 2)
1091 (delete-region pt
(1+ pt
))))))
1093 (insert " (fset '" (prin1-to-string cmd
)
1094 " " (prin1-to-string fcmd
) ")\n")))
1095 (or func
(setq func
(and cmd
(symbolp cmd
) (fboundp cmd
) cmd
)))
1096 (if (get func
'math-compose-forms
)
1098 (insert "(put '" (symbol-name cmd
)
1099 " 'math-compose-forms '"
1100 (prin1-to-string (get func
'math-compose-forms
))
1102 (fill-region pt
(point))
1103 (indent-rigidly pt
(point) 2)
1104 (delete-region pt
(1+ pt
))))
1106 (insert " (define-key calc-mode-map "
1107 (prin1-to-string (concat "z" (char-to-string key
)))
1109 (prin1-to-string cmd
)
1114 (defun calc-stack-command-p (cmd)
1115 (if (and cmd
(symbolp cmd
))
1117 (calc-stack-command-p (symbol-function cmd
)))
1119 (eq (car cmd
) 'lambda
)
1120 (setq cmd
(or (assq 'calc-wrapper cmd
)
1121 (assq 'calc-slow-wrapper cmd
)))
1122 (setq cmd
(assq 'calc-enter-result cmd
))
1123 (memq (car (nth 3 cmd
)) '(cons list
))
1124 (eq (car (nth 1 (nth 3 cmd
))) 'quote
)
1125 (nth 1 (nth 1 (nth 3 cmd
))))))
1128 (defun calc-call-last-kbd-macro (arg)
1130 (and defining-kbd-macro
1131 (error "Can't execute anonymous macro while defining one"))
1133 (error "No kbd macro has been defined"))
1134 (calc-execute-kbd-macro last-kbd-macro arg
))
1136 (defun calc-execute-kbd-macro (mac arg
&rest prefix
)
1137 (if calc-keep-args-flag
1139 (if (and (vectorp mac
) (> (length mac
) 0) (stringp (aref mac
0)))
1140 (setq mac
(or (aref mac
1)
1141 (aset mac
1 (progn (and (fboundp 'edit-kbd-macro
)
1142 (edit-kbd-macro nil
))
1143 (edmacro-parse-keys (aref mac
0)))))))
1144 (if (< (prefix-numeric-value arg
) 0)
1145 (execute-kbd-macro mac
(- (prefix-numeric-value arg
)))
1146 (if calc-executing-macro
1147 (execute-kbd-macro mac arg
)
1149 (let ((old-stack-whole (copy-sequence calc-stack
))
1150 (old-stack-top calc-stack-top
)
1151 (old-buffer-size (buffer-size))
1152 (old-refresh-count calc-refresh-count
))
1154 (let ((calc-executing-macro mac
))
1155 (execute-kbd-macro mac arg
))
1156 (calc-select-buffer)
1157 (let ((new-stack (reverse calc-stack
))
1158 (old-stack (reverse old-stack-whole
)))
1159 (while (and new-stack old-stack
1160 (equal (car new-stack
) (car old-stack
)))
1161 (setq new-stack
(cdr new-stack
)
1162 old-stack
(cdr old-stack
)))
1163 (or (equal prefix
'(nil))
1164 (calc-record-list (if (> (length new-stack
) 1)
1165 (mapcar 'car new-stack
)
1167 (or (car prefix
) "kmac")))
1168 (calc-record-undo (list 'set
'saved-stack-top old-stack-top
))
1170 (calc-record-undo (list 'pop
1 (mapcar 'car old-stack
))))
1171 (let ((calc-stack old-stack-whole
)
1173 (calc-cursor-stack-index (length old-stack
)))
1174 (if (and (= old-buffer-size
(buffer-size))
1175 (= old-refresh-count calc-refresh-count
))
1176 (let ((buffer-read-only nil
))
1177 (delete-region (point) (point-max))
1179 (calc-record-undo (list 'push
1))
1180 (insert (math-format-stack-value (car new-stack
)) "\n")
1181 (setq new-stack
(cdr new-stack
)))
1182 (calc-renumber-stack))
1184 (calc-record-undo (list 'push
1))
1185 (setq new-stack
(cdr new-stack
)))
1187 (calc-record-undo (list 'set
'saved-stack-top
0)))))))))
1189 (defun calc-push-list-in-macro (vals m sels
)
1190 (let ((entry (list (car vals
) 1 (car sels
)))
1191 (mm (+ (or m
1) calc-stack-top
)))
1193 (setcdr (nthcdr (- mm
2) calc-stack
)
1194 (cons entry
(nthcdr (1- mm
) calc-stack
)))
1195 (setq calc-stack
(cons entry calc-stack
)))))
1197 (defun calc-pop-stack-in-macro (n mm
)
1199 (setcdr (nthcdr (- mm
2) calc-stack
)
1200 (nthcdr (+ n mm -
1) calc-stack
))
1201 (setq calc-stack
(nthcdr n calc-stack
))))
1204 (defun calc-kbd-if ()
1207 (let ((cond (calc-top-n 1)))
1209 (if (math-is-true cond
)
1210 (if defining-kbd-macro
1211 (message "If true.."))
1212 (if defining-kbd-macro
1213 (message "Condition is false; skipping to Z: or Z] ..."))
1214 (calc-kbd-skip-to-else-if t
)))))
1216 (defun calc-kbd-else-if ()
1220 (defun calc-kbd-skip-to-else-if (else-okay)
1224 (setq ch
(read-char))
1226 (error "Unterminated Z[ in keyboard macro"))
1229 (setq ch
(read-char))
1231 (setq count
(1+ count
)))
1233 (setq count
(1- count
)))
1239 (keyboard-quit))))))
1240 (and defining-kbd-macro
1243 (message "End-if...")))))
1245 (defun calc-kbd-end-if ()
1247 (if defining-kbd-macro
1248 (message "End-if...")))
1250 (defun calc-kbd-else ()
1252 (if defining-kbd-macro
1253 (message "Else; skipping to Z] ..."))
1254 (calc-kbd-skip-to-else-if nil
))
1257 (defun calc-kbd-repeat ()
1261 (setq count
(math-trunc (calc-top-n 1)))
1262 (or (Math-integerp count
)
1263 (error "Count must be an integer"))
1264 (if (Math-integer-negp count
)
1266 (or (integerp count
)
1267 (setq count
1000000))
1269 (calc-kbd-loop count
)))
1271 (defun calc-kbd-for (dir)
1275 (setq init
(calc-top-n 2)
1276 final
(calc-top-n 1))
1277 (or (and (math-anglep init
) (math-anglep final
))
1278 (error "Initial and final values must be real numbers"))
1280 (calc-kbd-loop nil init final
(and dir
(prefix-numeric-value dir
)))))
1282 (defun calc-kbd-loop (rpt-count &optional initial final dir
)
1284 (setq rpt-count
(if rpt-count
(prefix-numeric-value rpt-count
) 1000000))
1288 (open last-command-char
)
1291 (or executing-kbd-macro
1292 (message "Reading loop body..."))
1294 (setq ch
(read-char))
1296 (error "Unterminated Z%c in keyboard macro" open
))
1299 (setq ch
(read-char)
1300 body
(concat body
"Z" (char-to-string ch
)))
1301 (cond ((memq ch
'(?\
< ?\
( ?\
{))
1302 (setq count
(1+ count
)))
1303 ((memq ch
'(?\
> ?\
) ?\
}))
1304 (setq count
(1- count
)))
1307 (setq parts
(nconc parts
(list (concat (substring body
0 -
2)
1312 (setq body
(concat body
(char-to-string ch
)))))
1313 (if (/= ch
(cdr (assq open
'( (?\
< . ?\
>) (?\
( . ?\
)) (?\
{ . ?\
}) ))))
1314 (error "Mismatched Z%c and Z%c in keyboard macro" open ch
))
1315 (or executing-kbd-macro
1316 (message "Looping..."))
1317 (setq body
(concat (substring body
0 -
2) "Z]"))
1318 (and (not executing-kbd-macro
)
1319 (= rpt-count
1000000)
1323 (message "Warning: Infinite loop! Not executing")
1324 (setq rpt-count
0)))
1325 (or (not initial
) dir
1326 (setq dir
(math-compare final initial
)))
1328 (while (> rpt-count
0)
1331 (if (cond ((eq dir
0) (Math-equal final counter
))
1332 ((eq dir
1) (Math-lessp final counter
))
1333 ((eq dir -
1) (Math-lessp counter final
)))
1335 (calc-push counter
)))
1336 (while (and part
(> rpt-count
0))
1337 (execute-kbd-macro (car part
))
1338 (if (math-is-true (calc-top-n 1))
1340 (setq part
(cdr part
)))
1344 (execute-kbd-macro body
)
1346 (let ((step (calc-top-n 1)))
1348 (setq counter
(calcFunc-add counter step
)))
1349 (setq rpt-count
(1- rpt-count
))))))))
1350 (or executing-kbd-macro
1351 (message "Looping...done"))))
1353 (defun calc-kbd-end-repeat ()
1355 (error "Unbalanced Z> in keyboard macro"))
1357 (defun calc-kbd-end-for ()
1359 (error "Unbalanced Z) in keyboard macro"))
1361 (defun calc-kbd-end-loop ()
1363 (error "Unbalanced Z} in keyboard macro"))
1365 (defun calc-kbd-break ()
1368 (let ((cond (calc-top-n 1)))
1370 (if (math-is-true cond
)
1371 (error "Keyboard macro aborted")))))
1374 (defvar calc-kbd-push-level
0)
1376 ;; The variables var-q0 through var-q9 are the "quick" variables.
1388 (defun calc-kbd-push (arg)
1391 (let* ((defs (and arg
(> (prefix-numeric-value arg
) 0)))
1402 (calc-internal-prec (if defs
12 calc-internal-prec
))
1403 (calc-word-size (if defs
32 calc-word-size
))
1404 (calc-angle-mode (if defs
'deg calc-angle-mode
))
1405 (calc-simplify-mode (if defs nil calc-simplify-mode
))
1406 (calc-algebraic-mode (if arg nil calc-algebraic-mode
))
1407 (calc-incomplete-algebraic-mode (if arg nil
1408 calc-incomplete-algebraic-mode
))
1409 (calc-symbolic-mode (if defs nil calc-symbolic-mode
))
1410 (calc-matrix-mode (if defs nil calc-matrix-mode
))
1411 (calc-prefer-frac (if defs nil calc-prefer-frac
))
1412 (calc-complex-mode (if defs nil calc-complex-mode
))
1413 (calc-infinite-mode (if defs nil calc-infinite-mode
))
1417 (if (or executing-kbd-macro defining-kbd-macro
)
1419 (if defining-kbd-macro
1420 (message "Reading body..."))
1422 (setq ch
(read-char))
1424 (error "Unterminated Z` in keyboard macro"))
1427 (setq ch
(read-char)
1428 body
(concat body
"Z" (char-to-string ch
)))
1430 (setq count
(1+ count
)))
1432 (setq count
(1- count
)))
1435 (setq body
(concat body
(char-to-string ch
)))))
1436 (if defining-kbd-macro
1437 (message "Reading body...done"))
1438 (let ((calc-kbd-push-level 0))
1439 (execute-kbd-macro (substring body
0 -
2))))
1440 (let ((calc-kbd-push-level (1+ calc-kbd-push-level
)))
1441 (message "Saving modes; type Z' to restore")
1442 (recursive-edit))))))
1444 (defun calc-kbd-pop ()
1446 (if (> calc-kbd-push-level
0)
1448 (message "Mode settings restored")
1449 (exit-recursive-edit))
1450 (error "Unbalanced Z' in keyboard macro")))
1453 ;; (defun calc-kbd-report (msg)
1454 ;; (interactive "sMessage: ")
1456 ;; (math-working msg (calc-top-n 1))))
1458 (defun calc-kbd-query ()
1460 (let ((defining-kbd-macro nil
)
1461 (executing-kbd-macro nil
)
1463 (if (not (eq (car-safe msg
) 'vec
))
1464 (error "No prompt string provided")
1465 (setq msg
(math-vector-to-string msg
))
1468 (calc-alg-entry nil
(and (not (equal msg
"")) msg
))))))
1470 ;;;; Logical operations.
1472 (defun calcFunc-eq (a b
&rest more
)
1474 (let* ((args (cons a
(cons b
(copy-sequence more
))))
1478 (while (and (cdr p
) (not (eq res
0)))
1480 (while (and (setq p2
(cdr p2
)) (not (eq res
0)))
1481 (setq res
(math-two-eq (car p
) (car p2
)))
1483 (setcdr p
(delq (car p2
) (cdr p
)))))
1488 (cons 'calcFunc-eq args
)
1490 (or (math-two-eq a b
)
1491 (if (and (or (math-looks-negp a
) (math-zerop a
))
1492 (or (math-looks-negp b
) (math-zerop b
)))
1493 (list 'calcFunc-eq
(math-neg a
) (math-neg b
))
1494 (list 'calcFunc-eq a b
)))))
1496 (defun calcFunc-neq (a b
&rest more
)
1498 (let* ((args (cons a
(cons b more
)))
1503 (while (and (cdr p
) (not (eq res
1)))
1505 (while (and (setq p2
(cdr p2
)) (not (eq res
1)))
1506 (setq res
(math-two-eq (car p
) (car p2
)))
1507 (or res
(setq all nil
)))
1513 (cons 'calcFunc-neq args
))))
1514 (or (cdr (assq (math-two-eq a b
) '((0 .
1) (1 .
0))))
1515 (if (and (or (math-looks-negp a
) (math-zerop a
))
1516 (or (math-looks-negp b
) (math-zerop b
)))
1517 (list 'calcFunc-neq
(math-neg a
) (math-neg b
))
1518 (list 'calcFunc-neq a b
)))))
1520 (defun math-two-eq (a b
)
1521 (if (eq (car-safe a
) 'vec
)
1522 (if (eq (car-safe b
) 'vec
)
1523 (if (= (length a
) (length b
))
1525 (while (and (setq a
(cdr a
) b
(cdr b
)) (not (eq res
0)))
1527 (setq res
(math-two-eq (car a
) (car b
)))
1528 (if (eq (math-two-eq (car a
) (car b
)) 0)
1532 (if (Math-objectp b
)
1535 (if (eq (car-safe b
) 'vec
)
1536 (if (Math-objectp a
)
1539 (let ((res (math-compare a b
)))
1542 (if (and (= res
2) (not (and (Math-scalarp a
) (Math-scalarp b
))))
1546 (defun calcFunc-lt (a b
)
1547 (let ((res (math-compare a b
)))
1551 (if (and (or (math-looks-negp a
) (math-zerop a
))
1552 (or (math-looks-negp b
) (math-zerop b
)))
1553 (list 'calcFunc-gt
(math-neg a
) (math-neg b
))
1554 (list 'calcFunc-lt a b
))
1557 (defun calcFunc-gt (a b
)
1558 (let ((res (math-compare a b
)))
1562 (if (and (or (math-looks-negp a
) (math-zerop a
))
1563 (or (math-looks-negp b
) (math-zerop b
)))
1564 (list 'calcFunc-lt
(math-neg a
) (math-neg b
))
1565 (list 'calcFunc-gt a b
))
1568 (defun calcFunc-leq (a b
)
1569 (let ((res (math-compare a b
)))
1573 (if (and (or (math-looks-negp a
) (math-zerop a
))
1574 (or (math-looks-negp b
) (math-zerop b
)))
1575 (list 'calcFunc-geq
(math-neg a
) (math-neg b
))
1576 (list 'calcFunc-leq a b
))
1579 (defun calcFunc-geq (a b
)
1580 (let ((res (math-compare a b
)))
1584 (if (and (or (math-looks-negp a
) (math-zerop a
))
1585 (or (math-looks-negp b
) (math-zerop b
)))
1586 (list 'calcFunc-leq
(math-neg a
) (math-neg b
))
1587 (list 'calcFunc-geq a b
))
1590 (defun calcFunc-rmeq (a)
1591 (if (math-vectorp a
)
1592 (math-map-vec 'calcFunc-rmeq a
)
1593 (if (assq (car-safe a
) calc-tweak-eqn-table
)
1594 (if (and (eq (car-safe (nth 2 a
)) 'var
)
1595 (math-objectp (nth 1 a
)))
1598 (if (eq (car-safe a
) 'calcFunc-assign
)
1600 (if (eq (car-safe a
) 'calcFunc-evalto
)
1602 (list 'calcFunc-rmeq a
))))))
1604 (defun calcFunc-land (a b
)
1605 (cond ((Math-zerop a
)
1613 (t (list 'calcFunc-land a b
))))
1615 (defun calcFunc-lor (a b
)
1616 (cond ((Math-zerop a
)
1624 (t (list 'calcFunc-lor a b
))))
1626 (defun calcFunc-lnot (a)
1629 (if (math-is-true a
)
1631 (let ((op (and (= (length a
) 3)
1632 (assq (car a
) calc-tweak-eqn-table
))))
1634 (cons (nth 2 op
) (cdr a
))
1635 (list 'calcFunc-lnot a
))))))
1637 (defun calcFunc-if (c e1 e2
)
1640 (if (and (math-is-true c
) (not (Math-vectorp c
)))
1642 (or (and (Math-vectorp c
)
1644 (let ((ee1 (if (Math-vectorp e1
)
1645 (if (= (length c
) (length e1
))
1647 (calc-record-why "*Dimension error" e1
))
1649 (ee2 (if (Math-vectorp e2
)
1650 (if (= (length c
) (length e2
))
1652 (calc-record-why "*Dimension error" e2
))
1655 (cons 'vec
(math-if-vector (cdr c
) ee1 ee2
)))))
1656 (list 'calcFunc-if c e1 e2
)))))
1658 (defun math-if-vector (c e1 e2
)
1660 (cons (if (Math-zerop (car c
)) (car e2
) (car e1
))
1661 (math-if-vector (cdr c
)
1663 (or (cdr e2
) e2
)))))
1665 (defun math-normalize-logical-op (a)
1666 (or (and (eq (car a
) 'calcFunc-if
)
1668 (let ((a1 (math-normalize (nth 1 a
))))
1670 (math-normalize (nth 3 a
))
1671 (if (Math-numberp a1
)
1672 (math-normalize (nth 2 a
))
1673 (if (and (Math-vectorp (nth 1 a
))
1674 (math-constp (nth 1 a
)))
1675 (calcFunc-if (nth 1 a
)
1676 (math-normalize (nth 2 a
))
1677 (math-normalize (nth 3 a
)))
1678 (let ((calc-simplify-mode 'none
))
1679 (list 'calcFunc-if a1
1680 (math-normalize (nth 2 a
))
1681 (math-normalize (nth 3 a
)))))))))
1684 (defun calcFunc-in (a b
)
1685 (or (and (eq (car-safe b
) 'vec
)
1687 (while (and (setq bb
(cdr bb
))
1688 (not (if (memq (car-safe (car bb
)) '(vec intv
))
1689 (eq (calcFunc-in a
(car bb
)) 1)
1690 (Math-equal a
(car bb
))))))
1691 (if bb
1 (and (math-constp a
) (math-constp bb
) 0))))
1692 (and (eq (car-safe b
) 'intv
)
1693 (let ((res (math-compare a
(nth 2 b
))) res2
)
1697 (or (/= (nth 1 b
) 2)
1698 (Math-lessp (nth 2 b
) (nth 3 b
))))
1699 (if (memq (nth 1 b
) '(2 3)) 1 0))
1700 ((= (setq res2
(math-compare a
(nth 3 b
))) 1)
1703 (or (/= (nth 1 b
) 1)
1704 (Math-lessp (nth 2 b
) (nth 3 b
))))
1705 (if (memq (nth 1 b
) '(1 3)) 1 0))
1711 (and (Math-equal a b
)
1713 (and (math-constp a
) (math-constp b
)
1715 (list 'calcFunc-in a b
)))
1717 (defun calcFunc-typeof (a)
1718 (cond ((Math-integerp a
) 1)
1719 ((eq (car a
) 'frac
) 2)
1720 ((eq (car a
) 'float
) 3)
1721 ((eq (car a
) 'hms
) 4)
1722 ((eq (car a
) 'cplx
) 5)
1723 ((eq (car a
) 'polar
) 6)
1724 ((eq (car a
) 'sdev
) 7)
1725 ((eq (car a
) 'intv
) 8)
1726 ((eq (car a
) 'mod
) 9)
1727 ((eq (car a
) 'date
) (if (Math-integerp (nth 1 a
)) 10 11))
1729 (if (memq (nth 2 a
) '(var-inf var-uinf var-nan
)) 12 100))
1730 ((eq (car a
) 'vec
) (if (math-matrixp a
) 102 101))
1731 (t (math-calcFunc-to-var (car a
)))))
1733 (defun calcFunc-integer (a)
1734 (if (Math-integerp a
)
1736 (if (Math-objvecp a
)
1738 (list 'calcFunc-integer a
))))
1740 (defun calcFunc-real (a)
1743 (if (Math-objvecp a
)
1745 (list 'calcFunc-real a
))))
1747 (defun calcFunc-constant (a)
1750 (if (Math-objvecp a
)
1752 (list 'calcFunc-constant a
))))
1754 (defun calcFunc-refers (a b
)
1755 (if (math-expr-contains a b
)
1757 (if (eq (car-safe a
) 'var
)
1758 (list 'calcFunc-refers a b
)
1761 (defun calcFunc-negative (a)
1762 (if (math-looks-negp a
)
1764 (if (or (math-zerop a
)
1767 (list 'calcFunc-negative a
))))
1769 (defun calcFunc-variable (a)
1770 (if (eq (car-safe a
) 'var
)
1772 (if (Math-objvecp a
)
1774 (list 'calcFunc-variable a
))))
1776 (defun calcFunc-nonvar (a)
1777 (if (eq (car-safe a
) 'var
)
1778 (list 'calcFunc-nonvar a
)
1781 (defun calcFunc-istrue (a)
1782 (if (math-is-true a
)
1788 ;;;; User-programmability.
1790 ;;; Compiling Lisp-like forms to use the math library.
1792 (defun math-do-defmath (func args body
)
1793 (require 'calc-macs
)
1794 (let* ((fname (intern (concat "calcFunc-" (symbol-name func
))))
1795 (doc (if (stringp (car body
)) (list (car body
))))
1796 (clargs (mapcar 'math-clean-arg args
))
1797 (body (math-define-function-body
1798 (if (stringp (car body
)) (cdr body
) body
)
1801 (if (and (consp (car body
))
1802 (eq (car (car body
)) 'interactive
))
1803 (let ((inter (car body
)))
1804 (setq body
(cdr body
))
1805 (if (or (> (length inter
) 2)
1806 (integerp (nth 1 inter
)))
1807 (let ((hasprefix nil
) (hasmulti nil
))
1808 (if (stringp (nth 1 inter
))
1810 (cond ((equal (nth 1 inter
) "p")
1812 ((equal (nth 1 inter
) "m")
1815 "Can't handle interactive code string \"%s\""
1817 (setq inter
(cdr inter
))))
1818 (if (not (integerp (nth 1 inter
)))
1820 "Expected an integer in interactive specification"))
1821 (append (list 'defun
1822 (intern (concat "calc-"
1823 (symbol-name func
)))
1824 (if (or hasprefix hasmulti
)
1828 (if (or hasprefix hasmulti
)
1829 '((interactive "P"))
1833 '(calc-slow-wrapper)
1840 (list 'prefix-numeric-value
1844 (list 'calc-enter-result
1845 (if hasmulti
'n
(nth 1 inter
))
1849 (list 'quote
(list fname
))
1850 (list 'calc-top-list-n
1859 'prefix-numeric-value
1863 (list 'calc-top-list-n
1866 (nth 1 inter
)))))))))))
1867 (append (list 'defun
1868 (intern (concat "calc-" (symbol-name func
)))
1873 (cons 'calc-wrapper body
))))))
1874 (append (list 'defun
fname clargs
)
1876 (math-do-arg-list-check args nil nil
)
1879 (defun math-clean-arg (arg)
1881 (math-clean-arg (nth 1 arg
))
1884 (defun math-do-arg-check (arg var is-opt is-rest
)
1886 (let ((chk (math-do-arg-check arg var nil nil
)))
1890 (setq chk
(list (cons 'progn chk
)))
1893 (let* ((rest (math-do-arg-check (nth 1 arg
) var is-opt is-rest
))
1895 (qqual (list 'quote qual
))
1896 (qual-name (symbol-name qual
))
1897 (chk (intern (concat "math-check-" qual-name
))))
1903 (list 'mapcar
(list 'quote chk
) var
))
1904 (list 'setq var
(list chk var
)))))
1905 (if (fboundp (setq chk
(intern (concat "math-" qual-name
))))
1914 (list 'math-reject-arg
1919 (list 'math-reject-arg var qqual
)))))
1920 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name
)
1921 (fboundp (setq chk
(intern
1923 (math-match-substring
1933 (list 'math-reject-arg
1938 (list 'math-reject-arg var qqual
)))))
1939 (error "Unknown qualifier `%s'" qual-name
))))))))
1941 (defun math-do-arg-list-check (args is-opt is-rest
)
1942 (cond ((null args
) nil
)
1944 (append (math-do-arg-check (car args
)
1945 (math-clean-arg (car args
))
1947 (math-do-arg-list-check (cdr args
) is-opt is-rest
)))
1948 ((eq (car args
) '&optional
)
1949 (math-do-arg-list-check (cdr args
) t nil
))
1950 ((eq (car args
) '&rest
)
1951 (math-do-arg-list-check (cdr args
) nil t
))
1952 (t (math-do-arg-list-check (cdr args
) is-opt is-rest
))))
1954 (defconst math-prim-funcs
1955 '( (~
= . math-nearly-equal
)
1957 (lsh . calcFunc-lsh
)
1958 (ash . calcFunc-ash
)
1959 (logand . calcFunc-and
)
1960 (logandc2 . calcFunc-diff
)
1961 (logior . calcFunc-or
)
1962 (logxor . calcFunc-xor
)
1963 (lognot . calcFunc-not
)
1964 (equal . equal
) ; need to leave these ones alone!
1973 (defconst math-prim-vars
1976 (&optional .
&optional
)
1980 (defun math-define-function-body (body env
)
1981 (let ((body (math-define-body body env
)))
1982 (if (math-body-refers-to body
'math-return
)
1983 (list (cons 'catch
(cons '(quote math-return
) body
)))
1986 ;; The variable math-exp-env is local to math-define-body, but is
1987 ;; used by math-define-exp, which is called (indirectly) by
1988 ;; by math-define-body.
1989 (defvar math-exp-env
)
1991 (defun math-define-body (body math-exp-env
)
1992 (math-define-list body
))
1994 (defun math-define-list (body &optional quote
)
1997 ((and (eq (car body
) ':)
1998 (stringp (nth 1 body
)))
1999 (cons (let* ((math-read-expr-quotes t
)
2000 (exp (math-read-plain-expr (nth 1 body
) t
)))
2001 (math-define-exp exp
))
2002 (math-define-list (cdr (cdr body
)))))
2004 (cons (cond ((consp (car body
))
2005 (math-define-list (cdr body
) t
))
2008 (math-define-list (cdr body
))))
2010 (cons (math-define-exp (car body
))
2011 (math-define-list (cdr body
))))))
2013 (defun math-define-exp (exp)
2015 (let ((func (car exp
)))
2016 (cond ((memq func
'(quote function
))
2017 (if (and (consp (nth 1 exp
))
2018 (eq (car (nth 1 exp
)) 'lambda
))
2020 (math-define-lambda (nth 1 exp
) math-exp-env
))
2022 ((memq func
'(let let
* for foreach
))
2023 (let ((head (nth 1 exp
))
2024 (body (cdr (cdr exp
))))
2025 (if (memq func
'(let let
*))
2027 (setq func
(cdr (assq func
'((for . math-for
)
2028 (foreach . math-foreach
)))))
2029 (if (not (listp (car head
)))
2030 (setq head
(list head
))))
2033 (cons (math-define-let head
)
2034 (math-define-body body
2036 (math-define-let-env head
)
2038 ((and (memq func
'(setq setf
))
2039 (math-complicated-lhs (cdr exp
)))
2040 (if (> (length exp
) 3)
2041 (cons 'progn
(math-define-setf-list (cdr exp
)))
2042 (math-define-setf (nth 1 exp
) (nth 2 exp
))))
2043 ((eq func
'condition-case
)
2046 (math-define-body (cdr (cdr exp
))
2051 (math-define-cond (cdr exp
))))
2052 ((and (consp func
) ; ('spam a b) == force use of plain spam
2053 (eq (car func
) 'quote
))
2054 (cons func
(math-define-list (cdr exp
))))
2056 (let ((args (math-define-list (cdr exp
)))
2057 (prim (assq func math-prim-funcs
)))
2059 (cons (cdr prim
) args
))
2061 (list 'eq
(car args
) '(quote float
)))
2063 (math-define-binop 'math-add
0
2064 (car args
) (cdr args
)))
2066 (if (= (length args
) 1)
2067 (cons 'math-neg args
)
2068 (math-define-binop 'math-sub
0
2069 (car args
) (cdr args
))))
2071 (math-define-binop 'math-mul
1
2072 (car args
) (cdr args
)))
2074 (math-define-binop 'math-div
1
2075 (car args
) (cdr args
)))
2077 (math-define-binop 'math-min
0
2078 (car args
) (cdr args
)))
2080 (math-define-binop 'math-max
0
2081 (car args
) (cdr args
)))
2083 (if (and (math-numberp (nth 1 args
))
2084 (math-zerop (nth 1 args
)))
2085 (list 'math-negp
(car args
))
2086 (cons 'math-lessp args
)))
2088 (if (and (math-numberp (nth 1 args
))
2089 (math-zerop (nth 1 args
)))
2090 (list 'math-posp
(car args
))
2091 (list 'math-lessp
(nth 1 args
) (nth 0 args
))))
2094 (if (and (math-numberp (nth 1 args
))
2095 (math-zerop (nth 1 args
)))
2096 (list 'math-posp
(car args
))
2098 (nth 1 args
) (nth 0 args
)))))
2101 (if (and (math-numberp (nth 1 args
))
2102 (math-zerop (nth 1 args
)))
2103 (list 'math-negp
(car args
))
2104 (cons 'math-lessp args
))))
2106 (if (and (math-numberp (nth 1 args
))
2107 (math-zerop (nth 1 args
)))
2108 (list 'math-zerop
(nth 0 args
))
2109 (if (and (integerp (nth 1 args
))
2110 (/= (%
(nth 1 args
) 10) 0))
2111 (cons 'math-equal-int args
)
2112 (cons 'math-equal args
))))
2115 (if (and (math-numberp (nth 1 args
))
2116 (math-zerop (nth 1 args
)))
2117 (list 'math-zerop
(nth 0 args
))
2118 (if (and (integerp (nth 1 args
))
2119 (/= (%
(nth 1 args
) 10) 0))
2120 (cons 'math-equal-int args
)
2121 (cons 'math-equal args
)))))
2123 (list 'math-add
(car args
) 1))
2125 (list 'math-add
(car args
) -
1))
2126 ((eq func
'not
) ; optimize (not (not x)) => x
2127 (if (eq (car-safe args
) func
)
2130 ((and (eq func
'elt
) (cdr (cdr args
)))
2131 (math-define-elt (car args
) (cdr args
)))
2134 (let* ((name (symbol-name func
))
2135 (cfunc (intern (concat "calcFunc-" name
)))
2136 (mfunc (intern (concat "math-" name
))))
2137 (cond ((fboundp cfunc
)
2142 (string-match "\\`calcFunc-.*" name
))
2145 (cons cfunc args
)))))))))
2146 (t (cons func
(math-define-list (cdr exp
))))))) ;;args
2148 (let ((prim (assq exp math-prim-vars
))
2149 (name (symbol-name exp
)))
2152 ((memq exp math-exp-env
)
2154 ((string-match "-" name
)
2157 (intern (concat "var-" name
))))))
2159 (if (or (<= exp -
1000000) (>= exp
1000000))
2160 (list 'quote
(math-normalize exp
))
2164 (defun math-define-cond (forms)
2166 (cons (math-define-list (car forms
))
2167 (math-define-cond (cdr forms
)))))
2169 (defun math-complicated-lhs (body)
2171 (or (not (symbolp (car body
)))
2172 (math-complicated-lhs (cdr (cdr body
))))))
2174 (defun math-define-setf-list (body)
2176 (cons (math-define-setf (nth 0 body
) (nth 1 body
))
2177 (math-define-setf-list (cdr (cdr body
))))))
2179 (defun math-define-setf (place value
)
2180 (setq place
(math-define-exp place
)
2181 value
(math-define-exp value
))
2182 (cond ((symbolp place
)
2183 (list 'setq place value
))
2184 ((eq (car-safe place
) 'nth
)
2185 (list 'setcar
(list 'nthcdr
(nth 1 place
) (nth 2 place
)) value
))
2186 ((eq (car-safe place
) 'elt
)
2187 (list 'setcar
(list 'nthcdr
(nth 2 place
) (nth 1 place
)) value
))
2188 ((eq (car-safe place
) 'car
)
2189 (list 'setcar
(nth 1 place
) value
))
2190 ((eq (car-safe place
) 'cdr
)
2191 (list 'setcdr
(nth 1 place
) value
))
2193 (error "Bad place form for setf: %s" place
))))
2195 (defun math-define-binop (op ident arg1 rest
)
2197 (math-define-binop op ident
2198 (list op arg1
(car rest
))
2202 (defun math-define-let (vlist)
2204 (cons (if (consp (car vlist
))
2205 (cons (car (car vlist
))
2206 (math-define-list (cdr (car vlist
))))
2208 (math-define-let (cdr vlist
)))))
2210 (defun math-define-let-env (vlist)
2212 (cons (if (consp (car vlist
))
2215 (math-define-let-env (cdr vlist
)))))
2217 (defun math-define-lambda (exp exp-env
)
2218 (nconc (list (nth 0 exp
) ; 'lambda
2219 (nth 1 exp
)) ; arg list
2220 (math-define-function-body (cdr (cdr exp
))
2221 (append (nth 1 exp
) exp-env
))))
2223 (defun math-define-elt (seq idx
)
2225 (math-define-elt (list 'elt seq
(car idx
)) (cdr idx
))
2230 ;;; Useful programming macros.
2232 (defmacro math-while
(head &rest body
)
2233 (let ((body (cons 'while
(cons head body
))))
2234 (if (math-body-refers-to body
'math-break
)
2235 (cons 'catch
(cons '(quote math-break
) (list body
)))
2237 ;; (put 'math-while 'lisp-indent-hook 1)
2239 (defmacro math-for
(head &rest body
)
2240 (let ((body (if head
2241 (math-handle-for head body
)
2242 (cons 'while
(cons t body
)))))
2243 (if (math-body-refers-to body
'math-break
)
2244 (cons 'catch
(cons '(quote math-break
) (list body
)))
2246 ;; (put 'math-for 'lisp-indent-hook 1)
2248 (defun math-handle-for (head body
)
2249 (let* ((var (nth 0 (car head
)))
2250 (init (nth 1 (car head
)))
2251 (limit (nth 2 (car head
)))
2252 (step (or (nth 3 (car head
)) 1))
2253 (body (if (cdr head
)
2254 (list (math-handle-for (cdr head
) body
))
2256 (all-ints (and (integerp init
) (integerp limit
) (integerp step
)))
2257 (const-limit (or (integerp limit
)
2258 (and (eq (car-safe limit
) 'quote
)
2259 (math-realp (nth 1 limit
)))))
2260 (const-step (or (integerp step
)
2261 (and (eq (car-safe step
) 'quote
)
2262 (math-realp (nth 1 step
)))))
2263 (save-limit (if const-limit limit
(make-symbol "<limit>")))
2264 (save-step (if const-step step
(make-symbol "<step>"))))
2266 (cons (append (if const-limit nil
(list (list save-limit limit
)))
2267 (if const-step nil
(list (list save-step step
)))
2268 (list (list var init
)))
2273 (list '<= var save-limit
)
2274 (list '>= var save-limit
))
2277 (if (or (math-posp step
)
2302 save-step
)))))))))))
2304 (defmacro math-foreach
(head &rest body
)
2305 (let ((body (math-handle-foreach head body
)))
2306 (if (math-body-refers-to body
'math-break
)
2307 (cons 'catch
(cons '(quote math-break
) (list body
)))
2309 ;; (put 'math-foreach 'lisp-indent-hook 1)
2311 (defun math-handle-foreach (head body
)
2312 (let ((var (nth 0 (car head
)))
2313 (data (nth 1 (car head
)))
2314 (body (if (cdr head
)
2315 (list (math-handle-foreach (cdr head
) body
))
2318 (cons (list (list var data
))
2325 (list 'cdr var
)))))))))))
2328 (defun math-body-refers-to (body thing
)
2329 (or (equal body thing
)
2331 (or (math-body-refers-to (car body
) thing
)
2332 (math-body-refers-to (cdr body
) thing
)))))
2334 (defun math-break (&optional value
)
2335 (throw 'math-break value
))
2337 (defun math-return (&optional value
)
2338 (throw 'math-return value
))
2344 (defun math-composite-inequalities (x op
)
2345 (if (memq (nth 1 op
) '(calcFunc-eq calcFunc-neq
))
2346 (if (eq (car x
) (nth 1 op
))
2347 (append x
(list (math-read-expr-level (nth 3 op
))))
2348 (throw 'syntax
"Syntax error"))
2351 (if (memq (nth 1 op
) '(calcFunc-lt calcFunc-leq
))
2352 (if (memq (car x
) '(calcFunc-lt calcFunc-leq
))
2354 (+ (if (eq (car x
) 'calcFunc-leq
) 2 0)
2355 (if (eq (nth 1 op
) 'calcFunc-leq
) 1 0))
2356 (nth 1 x
) (math-read-expr-level (nth 3 op
)))
2357 (throw 'syntax
"Syntax error"))
2358 (if (memq (car x
) '(calcFunc-gt calcFunc-geq
))
2360 (+ (if (eq (nth 1 op
) 'calcFunc-geq
) 2 0)
2361 (if (eq (car x
) 'calcFunc-geq
) 1 0))
2362 (math-read-expr-level (nth 3 op
)) (nth 1 x
))
2363 (throw 'syntax
"Syntax error"))))))
2365 (provide 'calc-prog
)
2367 ;; arch-tag: 4c5a183b-c9e5-4632-bb3f-e41a764518b0
2368 ;;; calc-prog.el ends here