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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 ;; Declare functions which are defined elsewhere.
36 (declare-function edmacro-format-keys
"edmacro" (macro &optional verbose
))
37 (declare-function edmacro-parse-keys
"edmacro" (string &optional need-vector
))
38 (declare-function math-read-expr-level
"calc-aent" (exp-prec &optional exp-term
))
41 (defun calc-equal-to (arg)
44 (if (and (integerp arg
) (> arg
2))
45 (calc-enter-result arg
"eq" (cons 'calcFunc-eq
(calc-top-list-n arg
)))
46 (calc-binary-op "eq" 'calcFunc-eq arg
))))
48 (defun calc-remove-equal (arg)
51 (calc-unary-op "rmeq" 'calcFunc-rmeq arg
)))
53 (defun calc-not-equal-to (arg)
56 (if (and (integerp arg
) (> arg
2))
57 (calc-enter-result arg
"neq" (cons 'calcFunc-neq
(calc-top-list-n arg
)))
58 (calc-binary-op "neq" 'calcFunc-neq arg
))))
60 (defun calc-less-than (arg)
63 (calc-binary-op "lt" 'calcFunc-lt arg
)))
65 (defun calc-greater-than (arg)
68 (calc-binary-op "gt" 'calcFunc-gt arg
)))
70 (defun calc-less-equal (arg)
73 (calc-binary-op "leq" 'calcFunc-leq arg
)))
75 (defun calc-greater-equal (arg)
78 (calc-binary-op "geq" 'calcFunc-geq arg
)))
80 (defun calc-in-set (arg)
83 (calc-binary-op "in" 'calcFunc-in arg
)))
85 (defun calc-logical-and (arg)
88 (calc-binary-op "land" 'calcFunc-land arg
1)))
90 (defun calc-logical-or (arg)
93 (calc-binary-op "lor" 'calcFunc-lor arg
0)))
95 (defun calc-logical-not (arg)
98 (calc-unary-op "lnot" 'calcFunc-lnot arg
)))
100 (defun calc-logical-if ()
103 (calc-enter-result 3 "if" (cons 'calcFunc-if
(calc-top-list-n 3)))))
109 (defun calc-timing (n)
112 (calc-change-mode 'calc-timing n nil t
)
113 (message (if calc-timing
114 "Reporting timing of slow commands in Trail"
115 "Not reporting timing of commands"))))
117 (defun calc-pass-errors ()
119 ;; The following two cases are for the new, optimizing byte compiler
120 ;; or the standard 18.57 byte compiler, respectively.
122 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do
))) 15)))
123 (or (memq (car-safe (car-safe place
)) '(error xxxerror
))
124 (setq place
(aref (nth 2 (nth 2 (symbol-function 'calc-do
))) 27)))
125 (or (memq (car (car place
)) '(error xxxerror
))
127 (setcar (car place
) 'xxxerror
))
128 (error (error "The calc-do function has been modified; unable to patch"))))
130 (defun calc-user-define ()
132 (message "Define user key: z-")
133 (let ((key (read-char)))
134 (if (= (calc-user-function-classify key
) 0)
135 (error "Can't redefine \"?\" key"))
136 (let ((func (intern (completing-read (concat "Set key z "
143 (let* ((kmap (calc-user-key-map))
144 (old (assq key kmap
)))
147 (setcdr kmap
(cons (cons key func
) (cdr kmap
))))))))
149 (defun calc-user-undefine ()
151 (message "Undefine user key: z-")
152 (let ((key (read-char)))
153 (if (= (calc-user-function-classify key
) 0)
154 (error "Can't undefine \"?\" key"))
155 (let* ((kmap (calc-user-key-map)))
156 (delq (or (assq key kmap
)
157 (assq (upcase key
) kmap
)
158 (assq (downcase key
) kmap
)
159 (error "No such user key is defined"))
163 ;; math-integral-cache-state is originally declared in calcalg2.el,
164 ;; it is used in calc-user-define-variable.
165 (defvar math-integral-cache-state
)
167 ;; calc-user-formula-alist is local to calc-user-define-formula,
168 ;; calc-user-define-compostion and calc-finish-formula-edit,
169 ;; but is used by calc-fix-user-formula.
170 (defvar calc-user-formula-alist
)
172 (defun calc-user-define-formula ()
175 (let* ((form (calc-top 1))
177 (is-lambda (and (eq (car-safe form
) 'calcFunc-lambda
)
178 (>= (length form
) 2)))
179 odef key keyname cmd cmd-base cmd-base-default
180 func calc-user-formula-alist is-symb
)
182 (setq arglist
(mapcar (function (lambda (x) (nth 1 x
)))
183 (nreverse (cdr (reverse (cdr form
)))))
184 form
(nth (1- (length form
)) form
))
185 (calc-default-formula-arglist form
)
186 (setq arglist
(sort arglist
'string-lessp
)))
187 (message "Define user key: z-")
188 (setq key
(read-char))
189 (if (= (calc-user-function-classify key
) 0)
190 (error "Can't redefine \"?\" key"))
191 (setq key
(and (not (memq key
'(13 32))) key
)
193 (if (or (and (<= ?
0 key
) (<= key ?
9))
194 (and (<= ?a key
) (<= key ?z
))
195 (and (<= ?A key
) (<= key ?Z
)))
197 (format "%03d" key
)))
198 odef
(assq key
(calc-user-key-map)))
200 (setq keyname
(format "%05d" (abs (%
(random) 10000)))))
203 (setq cmd-base-default
(concat "User-" keyname
))
204 (setq cmd
(completing-read
205 (concat "Define M-x command name (default calc-"
208 obarray
'commandp nil
209 (if (and odef
(symbolp (cdr odef
)))
210 (symbol-name (cdr odef
))
212 (if (or (string-equal cmd
"")
213 (string-equal cmd
"calc-"))
214 (setq cmd
(concat "calc-User-" keyname
)))
215 (setq cmd-base
(and (string-match "\\`calc-\\(.+\\)\\'" cmd
)
216 (math-match-substring cmd
1)))
217 (setq cmd
(intern cmd
))
223 (if (get cmd
'calc-user-defn
)
224 (concat "Replace previous definition for "
225 (symbol-name cmd
) "? ")
226 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
229 (setq cmd-base-default
232 "\\`User-.+" cmd-base
)
235 (substring cmd-base
5))
237 (concat "User" keyname
)))
241 (concat "Define algebraic function name (default "
242 cmd-base-default
"): ")
243 (mapcar (lambda (x) (substring x
9))
244 (all-completions "calcFunc-"
248 (intern (concat "calcFunc-" x
))))
251 (if (string-equal func
"calcFunc-")
252 (intern (concat "calcFunc-" cmd-base-default
))
260 (if (get func
'calc-user-defn
)
261 (concat "Replace previous definition for "
262 (symbol-name func
) "? ")
263 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
266 (setq func
(intern (concat "calcFunc-User"
268 (and cmd
(symbol-name cmd
))
269 (format "%05d" (%
(random) 10000)))))))
272 (setq calc-user-formula-alist arglist
)
275 (setq calc-user-formula-alist
276 (read-from-minibuffer "Function argument list: "
278 (prin1-to-string arglist
)
282 (and (not (calc-subsetp calc-user-formula-alist arglist
))
284 "Okay for arguments that don't appear in formula to be ignored? "))))))
285 (setq is-symb
(and calc-user-formula-alist
288 "Leave it symbolic for non-constant arguments? ")))
289 (setq calc-user-formula-alist
290 (mapcar (function (lambda (x)
291 (or (cdr (assq x
'((nil . arg-nil
)
293 x
))) calc-user-formula-alist
))
302 (list 'calc-enter-result
303 (length calc-user-formula-alist
)
304 (let ((name (symbol-name (or func cmd
))))
306 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
308 (math-match-substring name
1)))
311 (list 'calc-top-list-n
312 (length calc-user-formula-alist
)))))))
313 (put cmd
'calc-user-defn t
)))
314 (let ((body (list 'math-normalize
(calc-fix-user-formula form
))))
317 (list 'lambda calc-user-formula-alist
)
319 (mapcar (function (lambda (v)
320 (list 'math-check-const v t
)))
321 calc-user-formula-alist
))
323 (put func
'calc-user-defn form
)
324 (setq math-integral-cache-state nil
)
326 (let* ((kmap (calc-user-key-map))
327 (old (assq key kmap
)))
330 (setcdr kmap
(cons (cons key cmd
) (cdr kmap
)))))))
333 (defun calc-default-formula-arglist (form)
335 (if (eq (car form
) 'var
)
336 (if (or (memq (nth 1 form
) arglist
)
337 (math-const-var form
))
339 (setq arglist
(cons (nth 1 form
) arglist
)))
340 (calc-default-formula-arglist-step (cdr form
)))))
342 (defun calc-default-formula-arglist-step (l)
345 (calc-default-formula-arglist (car l
))
346 (calc-default-formula-arglist-step (cdr l
)))))
348 (defun calc-subsetp (a b
)
350 (and (memq (car a
) b
)
351 (calc-subsetp (cdr a
) b
))))
353 (defun calc-fix-user-formula (f)
356 (cond ((and (eq (car f
) 'var
)
357 (memq (setq temp
(or (cdr (assq (nth 1 f
) '((nil . arg-nil
)
360 calc-user-formula-alist
))
362 ((or (math-constp f
) (eq (car f
) 'var
))
364 ((and (eq (car f
) 'calcFunc-eval
)
366 (list 'let
'((calc-simplify-mode nil
))
367 (list 'math-normalize
(calc-fix-user-formula (nth 1 f
)))))
368 ((and (eq (car f
) 'calcFunc-evalsimp
)
370 (list 'math-simplify
(calc-fix-user-formula (nth 1 f
))))
371 ((and (eq (car f
) 'calcFunc-evalextsimp
)
373 (list 'math-simplify-extended
374 (calc-fix-user-formula (nth 1 f
))))
377 (cons (list 'quote
(car f
))
378 (mapcar 'calc-fix-user-formula
(cdr f
)))))))
381 (defun calc-user-define-composition ()
384 (if (eq calc-language
'unform
)
385 (error "Can't define formats for unformatted mode"))
386 (let* ((comp (calc-top 1))
389 (completing-read "Define format for which function: "
390 (mapcar (lambda (x) (substring x
9))
391 (all-completions "calcFunc-"
395 (intern (concat "calcFunc-" x
))))))))
396 (comps (get func
'math-compose-forms
))
399 (calc-user-formula-alist nil
))
400 (if (math-zerop comp
)
401 (if (setq entry
(assq calc-language comps
))
402 (put func
'math-compose-forms
(delq entry comps
)))
403 (calc-default-formula-arglist comp
)
404 (setq arglist
(sort arglist
'string-lessp
))
407 (setq calc-user-formula-alist
408 (read-from-minibuffer "Composition argument list: "
410 (prin1-to-string arglist
)
414 (and (not (calc-subsetp calc-user-formula-alist arglist
))
416 "Okay for arguments that don't appear in formula to be invisible? "))))
417 (or (setq entry
(assq calc-language comps
))
418 (put func
'math-compose-forms
419 (cons (setq entry
(list calc-language
)) comps
)))
420 (or (setq entry2
(assq (length calc-user-formula-alist
) (cdr entry
)))
423 (list (length calc-user-formula-alist
))) (cdr entry
))))
425 (list 'lambda calc-user-formula-alist
(calc-fix-user-formula comp
))))
430 (defun calc-user-define-kbd-macro (arg)
433 (error "No keyboard macro defined"))
434 (message "Define last kbd macro on user key: z-")
435 (let ((key (read-char)))
436 (if (= (calc-user-function-classify key
) 0)
437 (error "Can't redefine \"?\" key"))
438 (let ((cmd (intern (completing-read "Full name for new command: "
443 (if (or (and (>= key ?a
)
450 (format "%03d" key
)))))))
452 (not (let ((f (symbol-function cmd
)))
455 (eq (car-safe (nth 3 f
))
456 'calc-execute-kbd-macro
)))))
457 (error "Function %s is already defined and not a keyboard macro"
459 (put cmd
'calc-user-defn t
)
460 (fset cmd
(if (< (prefix-numeric-value arg
) 0)
465 (list 'calc-execute-kbd-macro
466 (vector (key-description last-kbd-macro
)
469 (format "z%c" key
)))))
470 (let* ((kmap (calc-user-key-map))
471 (old (assq key kmap
)))
474 (setcdr kmap
(cons (cons key cmd
) (cdr kmap
))))))))
477 (defun calc-edit-user-syntax ()
480 (let ((lang calc-language
))
481 (calc-edit-mode (list 'calc-finish-user-syntax-edit
(list 'quote lang
))
483 (format "Editing %s-Mode Syntax Table. "
484 (cond ((null lang
) "Normal")
485 ((eq lang
'tex
) "TeX")
486 ((eq lang
'latex
) "LaTeX")
487 (t (capitalize (symbol-name lang
))))))
488 (calc-write-parse-table (cdr (assq lang calc-user-parse-tables
))
490 (calc-show-edit-buffer))
492 (defvar calc-original-buffer
)
494 (defun calc-finish-user-syntax-edit (lang)
495 (let ((tab (calc-read-parse-table calc-original-buffer lang
))
496 (entry (assq lang calc-user-parse-tables
)))
499 (car (setq calc-user-parse-tables
500 (cons (list lang
) calc-user-parse-tables
))))
503 (setq calc-user-parse-tables
504 (delq entry calc-user-parse-tables
)))))
505 (switch-to-buffer calc-original-buffer
))
507 ;; The variable calc-lang is local to calc-write-parse-table, but is
508 ;; used by calc-write-parse-table-part which is called by
509 ;; calc-write-parse-table. The variable is also local to
510 ;; calc-read-parse-table, but is used by calc-fix-token-name which
511 ;; is called (indirectly) by calc-read-parse-table.
514 (defun calc-write-parse-table (tab calc-lang
)
517 (calc-write-parse-table-part (car (car p
)))
519 (let ((math-format-hash-args t
))
520 (math-format-flat-expr (cdr (car p
)) 0))
524 (defun calc-write-parse-table-part (p)
526 (cond ((stringp (car p
))
528 (if (and (string-match "\\`\\\\dots\\>" s
)
529 (not (memq calc-lang
'(tex latex
))))
530 (setq s
(concat ".." (substring s
5))))
531 (if (or (and (string-match
532 "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s
)
533 (string-match "[^a-zA-Z0-9\\]" s
))
534 (and (assoc s
'((")") ("]") (">")))
536 (insert (prin1-to-string s
) " ")
541 (insert "/" (int-to-string (car p
))))
543 ((and (eq (car (car p
)) '\?) (equal (car (nth 2 (car p
))) "$$"))
544 (insert (car (nth 1 (car p
))) " "))
547 (calc-write-parse-table-part (nth 1 (car p
)))
548 (insert "}" (symbol-name (car (car p
))))
550 (calc-write-parse-table-part (list (car (nth 2 (car p
)))))
554 (defun calc-read-parse-table (calc-buf calc-lang
)
557 (skip-chars-forward "\n\t ")
559 (if (looking-at "%%")
562 (p (calc-read-parse-table-part ":=[\n\t ]+" ":=")))
563 (or (stringp (car p
))
564 (and (integerp (car p
))
568 (error "Malformed syntax rule")))
571 (let* ((str (buffer-substring pos
(point)))
573 (set-buffer calc-buf
)
574 (let ((calc-user-parse-tables nil
)
576 (math-expr-opers (math-standard-ops))
577 (calc-hashes-used 0))
579 (if (string-match ",[ \t]*\\'" str
)
580 (substring str
0 (match-beginning 0))
582 (if (eq (car-safe exp
) 'error
)
584 (goto-char (+ pos
(nth 1 exp
)))
585 (error (nth 2 exp
))))
586 (setq tab
(nconc tab
(list (cons p exp
)))))))))
589 (defun calc-fix-token-name (name &optional unquoted
)
590 (cond ((string-match "\\`\\.\\." name
)
591 (concat "\\dots" (substring name
2)))
592 ((and (equal name
"{") (memq calc-lang
'(tex latex eqn
)))
594 ((and (equal name
"}") (memq calc-lang
'(tex latex eqn
)))
596 ((and (equal name
"&") (memq calc-lang
'(tex latex
)))
599 (search-backward "#")
600 (error "Token '#' is reserved"))
601 ((and unquoted
(string-match "#" name
))
602 (error "Tokens containing '#' must be quoted"))
603 ((not (string-match "[^ ]" name
))
604 (search-backward "\"" nil t
)
605 (error "Blank tokens are not allowed"))
608 (defun calc-read-parse-table-part (term eterm
)
612 (skip-chars-forward "\n\t ")
613 (if (eobp) (error "Expected '%s'" eterm
))
614 (not (looking-at term
)))
615 (cond ((looking-at "%%")
617 ((looking-at "{[\n\t ]")
619 (let ((p (calc-read-parse-table-part "}" "}")))
620 (or (looking-at "[+*?]")
621 (error "Expected '+', '*', or '?'"))
622 (let ((sym (intern (buffer-substring (point) (1+ (point))))))
624 (looking-at "[^\n\t ]*")
625 (let ((sep (buffer-substring (point) (match-end 0))))
626 (goto-char (match-end 0))
627 (and (eq sym
'\?) (> (length sep
) 0)
628 (not (equal sep
"$")) (not (equal sep
"."))
629 (error "Separator not allowed with { ... }?"))
630 (if (string-match "\\`\"" sep
)
631 (setq sep
(read-from-string sep
)))
632 (setq sep
(calc-fix-token-name sep
))
633 (setq part
(nconc part
635 (and (> (length sep
) 0)
636 (cons sep p
))))))))))
638 (error "Too many }'s"))
640 (setq quoted
(calc-fix-token-name (read (current-buffer)))
641 part
(nconc part
(list quoted
))))
642 ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]")
643 (setq part
(nconc part
(list (if (= (match-beginning 1)
648 (1+ (match-beginning 1))
650 (goto-char (match-end 0)))
651 ((looking-at ":=[\n\t ]")
652 (error "Misplaced ':='"))
654 (looking-at "[^\n\t ]*")
655 (let ((end (match-end 0)))
656 (setq part
(nconc part
(list (calc-fix-token-name
660 (goto-char (match-end 0))
661 (let ((len (length part
)))
662 (while (and (> len
1)
663 (let ((last (nthcdr (setq len
(1- len
)) part
)))
664 (and (assoc (car last
) '((")") ("]") (">")))
665 (not (eq (car last
) quoted
))
667 (list '\? (list (car last
)) '("$$"))))))))
670 (defun calc-user-define-invocation ()
673 (error "No keyboard macro defined"))
674 (setq calc-invocation-macro last-kbd-macro
)
675 (message "Use `C-x * Z' to invoke this macro"))
677 (defun calc-user-define-edit ()
678 (interactive) ; but no calc-wrapper!
679 (message "Edit definition of command: z-")
682 (def (or (assq key
(calc-user-key-map))
683 (assq (upcase key
) (calc-user-key-map))
684 (assq (downcase key
) (calc-user-key-map))
685 (error "No command defined for that key")))
688 (setq cmdname
(symbol-name cmd
))
689 (setq cmd
(symbol-function cmd
)))
690 (cond ((or (stringp cmd
)
692 (eq (car-safe (nth 3 cmd
)) 'calc-execute-kbd-macro
)))
693 (let* ((mac (elt (nth 1 (nth 3 cmd
)) 1))
694 (str (edmacro-format-keys mac t
))
695 (kys (nth 3 (nth 3 cmd
))))
697 (list 'calc-edit-macro-finish-edit cmdname kys
)
699 "Editing keyboard macro (%s, bound to %s).\n"
700 "Original keys: %s \n")
701 cmdname kys
(elt (nth 1 (nth 3 cmd
)) 0)))
703 (calc-edit-format-macro-buffer)
704 (calc-show-edit-buffer)))
705 (t (let* ((func (calc-stack-command-p cmd
))
708 (get func
'calc-user-defn
)))
709 (kys (concat "z" (char-to-string (car def
))))
710 (intcmd (symbol-name (cdr def
)))
711 (algcmd (if func
(substring (symbol-name func
) 9) "")))
712 (if (and defn
(calc-valid-formula-func func
))
713 (let ((niceexpr (math-format-nice-expr defn
(frame-width))))
716 (list 'calc-finish-formula-edit
(list 'quote func
))
719 "Editing formula (%s, %s, bound to %s).\n"
720 "Original formula: %s\n")
721 intcmd algcmd kys niceexpr
))
722 (insert (math-showing-full-precision
725 (calc-show-edit-buffer))
726 (error "That command's definition cannot be edited")))))))
728 ;; Formatting the macro buffer
730 (defvar calc-edit-top
)
732 (defun calc-edit-macro-repeats ()
733 (goto-char calc-edit-top
)
735 (re-search-forward "^\\([0-9]+\\)\\*" nil t
)
736 (let ((num (string-to-number (match-string 1)))
737 (line (buffer-substring (point) (line-end-position))))
738 (goto-char (line-beginning-position))
742 (setq num
(1- num
))))))
744 (defun calc-edit-macro-adjust-buffer ()
745 (calc-edit-macro-repeats)
746 (goto-char calc-edit-top
)
747 (while (re-search-forward "^RET$" nil t
)
749 (goto-char calc-edit-top
)
750 (while (and (re-search-forward "^$" nil t
)
751 (not (= (point) (point-max))))
754 (defun calc-edit-macro-command ()
755 "Return the command on the current line in a Calc macro editing buffer."
756 (let ((beg (line-beginning-position))
758 (if (search-forward ";;" (line-end-position) 1)
760 (skip-chars-backward " \t")
762 (buffer-substring beg end
)))
764 (defun calc-edit-macro-command-type ()
765 "Return the type of command on the current line in a Calc macro editing buffer."
766 (let ((beg (save-excursion
767 (if (search-forward ";;" (line-end-position) t
)
769 (skip-chars-forward " \t")
772 (goto-char (line-end-position))
773 (skip-chars-backward " \t")
776 (buffer-substring beg end
)
779 (defun calc-edit-macro-combine-alg-ent ()
780 "Put an entire algebraic entry on a single line."
781 (let ((line (calc-edit-macro-command))
782 (type (calc-edit-macro-command-type))
785 (goto-char (line-beginning-position))
787 (setq curline
(calc-edit-macro-command))
789 (not (string-equal "RET" curline
))
790 (not (setq match
(string-match "<return>" curline
))))
791 (setq line
(concat line curline
))
793 (setq curline
(calc-edit-macro-command)))
796 (setq line
(concat line
(substring curline
0 match
))))
797 (setq line
(replace-regexp-in-string "SPC" " SPC "
798 (replace-regexp-in-string " " "" line
)))
799 (insert line
"\t\t\t")
800 (if (> (current-column) 24)
802 (insert ";; " type
"\n")
804 (insert "RET\t\t\t;; calc-enter\n"))))
806 (defun calc-edit-macro-combine-ext-command ()
807 "Put an entire extended command on a single line."
808 (let ((cmdbeg (calc-edit-macro-command))
810 (type (calc-edit-macro-command-type))
813 (goto-char (line-beginning-position))
815 (setq curline
(calc-edit-macro-command))
817 (not (string-equal "RET" curline
))
818 (not (setq match
(string-match "<return>" curline
))))
819 (setq line
(concat line curline
))
821 (setq curline
(calc-edit-macro-command)))
824 (setq line
(concat line
(substring curline
0 match
))))
825 (setq line
(replace-regexp-in-string " " "" line
))
826 (insert cmdbeg
" " line
"\t\t\t")
827 (if (> (current-column) 24)
829 (insert ";; " type
"\n")
831 (insert "RET\t\t\t;; calc-enter\n"))))
833 (defun calc-edit-macro-combine-var-name ()
834 "Put an entire variable name on a single line."
835 (let ((line (calc-edit-macro-command))
838 (goto-char (line-beginning-position))
840 (if (member line
'("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
841 (insert line
"\t\t\t;; calc quick variable\n")
842 (setq curline
(calc-edit-macro-command))
844 (not (string-equal "RET" curline
))
845 (not (setq match
(string-match "<return>" curline
))))
846 (setq line
(concat line curline
))
848 (setq curline
(calc-edit-macro-command)))
851 (setq line
(concat line
(substring curline
0 match
))))
852 (setq line
(replace-regexp-in-string " " "" line
))
853 (insert line
"\t\t\t")
854 (if (> (current-column) 24)
856 (insert ";; calc variable\n")
858 (insert "RET\t\t\t;; calc-enter\n")))))
860 (defun calc-edit-macro-combine-digits ()
861 "Put an entire sequence of digits on a single line."
862 (let ((line (calc-edit-macro-command))
864 (goto-char (line-beginning-position))
866 (while (string-equal (calc-edit-macro-command-type) "calcDigit-start")
867 (setq line
(concat line
(calc-edit-macro-command)))
869 (insert line
"\t\t\t")
870 (if (> (current-column) 24)
872 (insert ";; calc digits\n")))
874 (defun calc-edit-format-macro-buffer ()
875 "Rewrite the Calc macro editing buffer."
876 (calc-edit-macro-adjust-buffer)
877 (goto-char calc-edit-top
)
878 (let ((type (calc-edit-macro-command-type)))
879 (while (not (string-equal type
""))
882 (string-equal type
"calc-algebraic-entry")
883 (string-equal type
"calc-auto-algebraic-entry"))
884 (calc-edit-macro-combine-alg-ent))
885 ((string-equal type
"calc-execute-extended-command")
886 (calc-edit-macro-combine-ext-command))
887 ((string-equal type
"calcDigit-start")
888 (calc-edit-macro-combine-digits))
890 (string-equal type
"calc-store")
891 (string-equal type
"calc-store-into")
892 (string-equal type
"calc-store-neg")
893 (string-equal type
"calc-store-plus")
894 (string-equal type
"calc-store-minus")
895 (string-equal type
"calc-store-div")
896 (string-equal type
"calc-store-times")
897 (string-equal type
"calc-store-power")
898 (string-equal type
"calc-store-concat")
899 (string-equal type
"calc-store-inv")
900 (string-equal type
"calc-store-dec")
901 (string-equal type
"calc-store-incr")
902 (string-equal type
"calc-store-exchange")
903 (string-equal type
"calc-unstore")
904 (string-equal type
"calc-recall")
905 (string-equal type
"calc-let")
906 (string-equal type
"calc-permanent-variable"))
908 (calc-edit-macro-combine-var-name))
910 (string-equal type
"calc-copy-variable")
911 (string-equal type
"calc-copy-special-constant")
912 (string-equal type
"calc-declare-variable"))
914 (calc-edit-macro-combine-var-name)
915 (calc-edit-macro-combine-var-name))
916 (t (forward-line 1)))
917 (setq type
(calc-edit-macro-command-type))))
918 (goto-char calc-edit-top
))
920 ;; Finish editing the macro
922 (defun calc-edit-macro-pre-finish-edit ()
923 (goto-char calc-edit-top
)
924 (while (re-search-forward "\\(^\\| \\)RET\\($\\|\t\\| \\)" nil t
)
925 (search-backward "RET")
927 (insert "<return>")))
929 (defun calc-edit-macro-finish-edit (cmdname key
)
930 "Finish editing a Calc macro.
931 Redefine the corresponding command."
933 (let ((cmd (intern cmdname
)))
934 (calc-edit-macro-pre-finish-edit)
935 (let* ((str (buffer-substring calc-edit-top
(point-max)))
936 (mac (edmacro-parse-keys str t
)))
937 (if (= (length mac
) 0)
942 (list 'calc-execute-kbd-macro
943 (vector (key-description mac
)
947 (defun calc-finish-formula-edit (func)
948 (let ((buf (current-buffer))
949 (str (buffer-substring calc-edit-top
(point-max)))
951 (body (calc-valid-formula-func func
)))
952 (set-buffer calc-original-buffer
)
953 (let ((val (math-read-expr str
)))
954 (if (eq (car-safe val
) 'error
)
957 (goto-char (+ start
(nth 1 val
)))
958 (error (nth 2 val
))))
960 (let ((calc-user-formula-alist (nth 1 (symbol-function func
))))
961 (calc-fix-user-formula val
)))
962 (put func
'calc-user-defn val
))))
964 (defun calc-valid-formula-func (func)
965 (let ((def (symbol-function func
)))
967 (eq (car def
) 'lambda
)
969 (setq def
(cdr (cdr def
)))
971 (not (eq (car (car def
)) 'math-normalize
)))
972 (setq def
(cdr def
)))
976 (defun calc-get-user-defn ()
979 (message "Get definition of command: z-")
980 (let* ((key (read-char))
981 (def (or (assq key
(calc-user-key-map))
982 (assq (upcase key
) (calc-user-key-map))
983 (assq (downcase key
) (calc-user-key-map))
984 (error "No command defined for that key")))
987 (setq cmd
(symbol-function cmd
)))
989 (message "Keyboard macro: %s" cmd
))
990 (t (let* ((func (calc-stack-command-p cmd
))
993 (get func
'calc-user-defn
))))
996 (and (calc-valid-formula-func func
)
997 (setq defn
(append '(calcFunc-lambda)
998 (mapcar 'math-build-var-name
999 (nth 1 (symbol-function
1002 (calc-enter-result 0 "gdef" defn
))
1003 (error "That command is not defined by a formula"))))))))
1006 (defun calc-user-define-permanent ()
1009 (message "Record in %s the command: z-" calc-settings-file
)
1010 (let* ((key (read-char))
1011 (def (or (assq key
(calc-user-key-map))
1012 (assq (upcase key
) (calc-user-key-map))
1013 (assq (downcase key
) (calc-user-key-map))
1019 (format "Record in %s the algebraic function: "
1021 (mapcar (lambda (x) (substring x
9))
1022 (all-completions "calcFunc-"
1026 (intern (concat "calcFunc-" x
))))
1030 (intern (completing-read
1031 (format "Record in %s the command: "
1033 obarray
'fboundp nil
"calc-"))))
1034 (error "No command defined for that key"))))
1035 (set-buffer (find-file-noselect (substitute-in-file-name
1036 calc-settings-file
)))
1037 (goto-char (point-max))
1038 (let* ((cmd (cdr def
))
1039 (fcmd (and cmd
(symbolp cmd
) (symbol-function cmd
)))
1045 (insert "\n;;; Definition stored by Calc on " (current-time-string)
1046 "\n(put 'calc-define '"
1047 (if (symbolp cmd
) (symbol-name cmd
) (format "key%d" key
))
1050 (eq (car-safe fcmd
) 'lambda
)
1051 (get cmd
'calc-user-defn
))
1053 (and (eq (car-safe (nth 3 fcmd
)) 'calc-execute-kbd-macro
)
1054 (vectorp (nth 1 (nth 3 fcmd
)))
1055 (progn (and (fboundp 'edit-kbd-macro
)
1056 (edit-kbd-macro nil
))
1057 (fboundp 'edmacro-parse-keys
))
1059 (aset (nth 1 (nth 3 fcmd
)) 1 nil
))
1060 (insert (setq str
(prin1-to-string
1061 (cons 'defun
(cons cmd
(cdr fcmd
)))))
1063 (or (and (string-match "\"" str
) (not q-ok
))
1064 (fill-region pt
(point)))
1065 (indent-rigidly pt
(point) 2)
1066 (delete-region pt
(1+ pt
))
1067 (insert " (put '" (symbol-name cmd
)
1068 " 'calc-user-defn '"
1069 (prin1-to-string (get cmd
'calc-user-defn
))
1071 (setq func
(calc-stack-command-p cmd
))
1072 (let ((ffunc (and func
(symbolp func
) (symbol-function func
)))
1075 (eq (car-safe ffunc
) 'lambda
)
1076 (get func
'calc-user-defn
)
1078 (insert (setq str
(prin1-to-string
1079 (cons 'defun
(cons func
1082 (or (and (string-match "\"" str
) (not q-ok
))
1083 (fill-region pt
(point)))
1084 (indent-rigidly pt
(point) 2)
1085 (delete-region pt
(1+ pt
))
1087 (insert "(put '" (symbol-name func
)
1088 " 'calc-user-defn '"
1089 (prin1-to-string (get func
'calc-user-defn
))
1091 (fill-region pt
(point))
1092 (indent-rigidly pt
(point) 2)
1093 (delete-region pt
(1+ pt
))))))
1095 (insert " (fset '" (prin1-to-string cmd
)
1096 " " (prin1-to-string fcmd
) ")\n")))
1097 (or func
(setq func
(and cmd
(symbolp cmd
) (fboundp cmd
) cmd
)))
1098 (if (get func
'math-compose-forms
)
1100 (insert "(put '" (symbol-name cmd
)
1101 " 'math-compose-forms '"
1102 (prin1-to-string (get func
'math-compose-forms
))
1104 (fill-region pt
(point))
1105 (indent-rigidly pt
(point) 2)
1106 (delete-region pt
(1+ pt
))))
1108 (insert " (define-key calc-mode-map "
1109 (prin1-to-string (concat "z" (char-to-string key
)))
1111 (prin1-to-string cmd
)
1116 (defun calc-stack-command-p (cmd)
1117 (if (and cmd
(symbolp cmd
))
1119 (calc-stack-command-p (symbol-function cmd
)))
1121 (eq (car cmd
) 'lambda
)
1122 (setq cmd
(or (assq 'calc-wrapper cmd
)
1123 (assq 'calc-slow-wrapper cmd
)))
1124 (setq cmd
(assq 'calc-enter-result cmd
))
1125 (memq (car (nth 3 cmd
)) '(cons list
))
1126 (eq (car (nth 1 (nth 3 cmd
))) 'quote
)
1127 (nth 1 (nth 1 (nth 3 cmd
))))))
1130 (defun calc-call-last-kbd-macro (arg)
1132 (and defining-kbd-macro
1133 (error "Can't execute anonymous macro while defining one"))
1135 (error "No kbd macro has been defined"))
1136 (calc-execute-kbd-macro last-kbd-macro arg
))
1138 (defun calc-execute-kbd-macro (mac arg
&rest prefix
)
1139 (if calc-keep-args-flag
1141 (if (and (vectorp mac
) (> (length mac
) 0) (stringp (aref mac
0)))
1142 (setq mac
(or (aref mac
1)
1143 (aset mac
1 (progn (and (fboundp 'edit-kbd-macro
)
1144 (edit-kbd-macro nil
))
1145 (edmacro-parse-keys (aref mac
0)))))))
1146 (if (< (prefix-numeric-value arg
) 0)
1147 (execute-kbd-macro mac
(- (prefix-numeric-value arg
)))
1148 (if calc-executing-macro
1149 (execute-kbd-macro mac arg
)
1151 (let ((old-stack-whole (copy-sequence calc-stack
))
1152 (old-stack-top calc-stack-top
)
1153 (old-buffer-size (buffer-size))
1154 (old-refresh-count calc-refresh-count
))
1156 (let ((calc-executing-macro mac
))
1157 (execute-kbd-macro mac arg
))
1158 (calc-select-buffer)
1159 (let ((new-stack (reverse calc-stack
))
1160 (old-stack (reverse old-stack-whole
)))
1161 (while (and new-stack old-stack
1162 (equal (car new-stack
) (car old-stack
)))
1163 (setq new-stack
(cdr new-stack
)
1164 old-stack
(cdr old-stack
)))
1165 (or (equal prefix
'(nil))
1166 (calc-record-list (if (> (length new-stack
) 1)
1167 (mapcar 'car new-stack
)
1169 (or (car prefix
) "kmac")))
1170 (calc-record-undo (list 'set
'saved-stack-top old-stack-top
))
1172 (calc-record-undo (list 'pop
1 (mapcar 'car old-stack
))))
1173 (let ((calc-stack old-stack-whole
)
1175 (calc-cursor-stack-index (length old-stack
)))
1176 (if (and (= old-buffer-size
(buffer-size))
1177 (= old-refresh-count calc-refresh-count
))
1178 (let ((buffer-read-only nil
))
1179 (delete-region (point) (point-max))
1181 (calc-record-undo (list 'push
1))
1182 (insert (math-format-stack-value (car new-stack
)) "\n")
1183 (setq new-stack
(cdr new-stack
)))
1184 (calc-renumber-stack))
1186 (calc-record-undo (list 'push
1))
1187 (setq new-stack
(cdr new-stack
)))
1189 (calc-record-undo (list 'set
'saved-stack-top
0)))))))))
1191 (defun calc-push-list-in-macro (vals m sels
)
1192 (let ((entry (list (car vals
) 1 (car sels
)))
1193 (mm (+ (or m
1) calc-stack-top
)))
1195 (setcdr (nthcdr (- mm
2) calc-stack
)
1196 (cons entry
(nthcdr (1- mm
) calc-stack
)))
1197 (setq calc-stack
(cons entry calc-stack
)))))
1199 (defun calc-pop-stack-in-macro (n mm
)
1201 (setcdr (nthcdr (- mm
2) calc-stack
)
1202 (nthcdr (+ n mm -
1) calc-stack
))
1203 (setq calc-stack
(nthcdr n calc-stack
))))
1206 (defun calc-kbd-if ()
1209 (let ((cond (calc-top-n 1)))
1211 (if (math-is-true cond
)
1212 (if defining-kbd-macro
1213 (message "If true.."))
1214 (if defining-kbd-macro
1215 (message "Condition is false; skipping to Z: or Z] ..."))
1216 (calc-kbd-skip-to-else-if t
)))))
1218 (defun calc-kbd-else-if ()
1222 (defun calc-kbd-skip-to-else-if (else-okay)
1226 (setq ch
(read-char))
1228 (error "Unterminated Z[ in keyboard macro"))
1231 (setq ch
(read-char))
1233 (setq count
(1+ count
)))
1235 (setq count
(1- count
)))
1241 (keyboard-quit))))))
1242 (and defining-kbd-macro
1245 (message "End-if...")))))
1247 (defun calc-kbd-end-if ()
1249 (if defining-kbd-macro
1250 (message "End-if...")))
1252 (defun calc-kbd-else ()
1254 (if defining-kbd-macro
1255 (message "Else; skipping to Z] ..."))
1256 (calc-kbd-skip-to-else-if nil
))
1259 (defun calc-kbd-repeat ()
1263 (setq count
(math-trunc (calc-top-n 1)))
1264 (or (Math-integerp count
)
1265 (error "Count must be an integer"))
1266 (if (Math-integer-negp count
)
1268 (or (integerp count
)
1269 (setq count
1000000))
1271 (calc-kbd-loop count
)))
1273 (defun calc-kbd-for (dir)
1277 (setq init
(calc-top-n 2)
1278 final
(calc-top-n 1))
1279 (or (and (math-anglep init
) (math-anglep final
))
1280 (error "Initial and final values must be real numbers"))
1282 (calc-kbd-loop nil init final
(and dir
(prefix-numeric-value dir
)))))
1284 (defun calc-kbd-loop (rpt-count &optional initial final dir
)
1286 (setq rpt-count
(if rpt-count
(prefix-numeric-value rpt-count
) 1000000))
1290 (open last-command-char
)
1293 (or executing-kbd-macro
1294 (message "Reading loop body..."))
1296 (setq ch
(read-char))
1298 (error "Unterminated Z%c in keyboard macro" open
))
1301 (setq ch
(read-char)
1302 body
(concat body
"Z" (char-to-string ch
)))
1303 (cond ((memq ch
'(?\
< ?\
( ?\
{))
1304 (setq count
(1+ count
)))
1305 ((memq ch
'(?\
> ?\
) ?\
}))
1306 (setq count
(1- count
)))
1309 (setq parts
(nconc parts
(list (concat (substring body
0 -
2)
1314 (setq body
(concat body
(char-to-string ch
)))))
1315 (if (/= ch
(cdr (assq open
'( (?\
< . ?\
>) (?\
( . ?\
)) (?\
{ . ?\
}) ))))
1316 (error "Mismatched Z%c and Z%c in keyboard macro" open ch
))
1317 (or executing-kbd-macro
1318 (message "Looping..."))
1319 (setq body
(concat (substring body
0 -
2) "Z]"))
1320 (and (not executing-kbd-macro
)
1321 (= rpt-count
1000000)
1325 (message "Warning: Infinite loop! Not executing")
1326 (setq rpt-count
0)))
1327 (or (not initial
) dir
1328 (setq dir
(math-compare final initial
)))
1330 (while (> rpt-count
0)
1333 (if (cond ((eq dir
0) (Math-equal final counter
))
1334 ((eq dir
1) (Math-lessp final counter
))
1335 ((eq dir -
1) (Math-lessp counter final
)))
1337 (calc-push counter
)))
1338 (while (and part
(> rpt-count
0))
1339 (execute-kbd-macro (car part
))
1340 (if (math-is-true (calc-top-n 1))
1342 (setq part
(cdr part
)))
1346 (execute-kbd-macro body
)
1348 (let ((step (calc-top-n 1)))
1350 (setq counter
(calcFunc-add counter step
)))
1351 (setq rpt-count
(1- rpt-count
))))))))
1352 (or executing-kbd-macro
1353 (message "Looping...done"))))
1355 (defun calc-kbd-end-repeat ()
1357 (error "Unbalanced Z> in keyboard macro"))
1359 (defun calc-kbd-end-for ()
1361 (error "Unbalanced Z) in keyboard macro"))
1363 (defun calc-kbd-end-loop ()
1365 (error "Unbalanced Z} in keyboard macro"))
1367 (defun calc-kbd-break ()
1370 (let ((cond (calc-top-n 1)))
1372 (if (math-is-true cond
)
1373 (error "Keyboard macro aborted")))))
1376 (defvar calc-kbd-push-level
0)
1378 ;; The variables var-q0 through var-q9 are the "quick" variables.
1390 (defun calc-kbd-push (arg)
1393 (let* ((defs (and arg
(> (prefix-numeric-value arg
) 0)))
1404 (calc-internal-prec (if defs
12 calc-internal-prec
))
1405 (calc-word-size (if defs
32 calc-word-size
))
1406 (calc-angle-mode (if defs
'deg calc-angle-mode
))
1407 (calc-simplify-mode (if defs nil calc-simplify-mode
))
1408 (calc-algebraic-mode (if arg nil calc-algebraic-mode
))
1409 (calc-incomplete-algebraic-mode (if arg nil
1410 calc-incomplete-algebraic-mode
))
1411 (calc-symbolic-mode (if defs nil calc-symbolic-mode
))
1412 (calc-matrix-mode (if defs nil calc-matrix-mode
))
1413 (calc-prefer-frac (if defs nil calc-prefer-frac
))
1414 (calc-complex-mode (if defs nil calc-complex-mode
))
1415 (calc-infinite-mode (if defs nil calc-infinite-mode
))
1419 (if (or executing-kbd-macro defining-kbd-macro
)
1421 (if defining-kbd-macro
1422 (message "Reading body..."))
1424 (setq ch
(read-char))
1426 (error "Unterminated Z` in keyboard macro"))
1429 (setq ch
(read-char)
1430 body
(concat body
"Z" (char-to-string ch
)))
1432 (setq count
(1+ count
)))
1434 (setq count
(1- count
)))
1437 (setq body
(concat body
(char-to-string ch
)))))
1438 (if defining-kbd-macro
1439 (message "Reading body...done"))
1440 (let ((calc-kbd-push-level 0))
1441 (execute-kbd-macro (substring body
0 -
2))))
1442 (let ((calc-kbd-push-level (1+ calc-kbd-push-level
)))
1443 (message "Saving modes; type Z' to restore")
1444 (recursive-edit))))))
1446 (defun calc-kbd-pop ()
1448 (if (> calc-kbd-push-level
0)
1450 (message "Mode settings restored")
1451 (exit-recursive-edit))
1452 (error "Unbalanced Z' in keyboard macro")))
1455 ;; (defun calc-kbd-report (msg)
1456 ;; (interactive "sMessage: ")
1458 ;; (math-working msg (calc-top-n 1))))
1460 (defun calc-kbd-query ()
1462 (let ((defining-kbd-macro nil
)
1463 (executing-kbd-macro nil
)
1465 (if (not (eq (car-safe msg
) 'vec
))
1466 (error "No prompt string provided")
1467 (setq msg
(math-vector-to-string msg
))
1470 (calc-alg-entry nil
(and (not (equal msg
"")) msg
))))))
1472 ;;;; Logical operations.
1474 (defun calcFunc-eq (a b
&rest more
)
1476 (let* ((args (cons a
(cons b
(copy-sequence more
))))
1480 (while (and (cdr p
) (not (eq res
0)))
1482 (while (and (setq p2
(cdr p2
)) (not (eq res
0)))
1483 (setq res
(math-two-eq (car p
) (car p2
)))
1485 (setcdr p
(delq (car p2
) (cdr p
)))))
1490 (cons 'calcFunc-eq args
)
1492 (or (math-two-eq a b
)
1493 (if (and (or (math-looks-negp a
) (math-zerop a
))
1494 (or (math-looks-negp b
) (math-zerop b
)))
1495 (list 'calcFunc-eq
(math-neg a
) (math-neg b
))
1496 (list 'calcFunc-eq a b
)))))
1498 (defun calcFunc-neq (a b
&rest more
)
1500 (let* ((args (cons a
(cons b more
)))
1505 (while (and (cdr p
) (not (eq res
1)))
1507 (while (and (setq p2
(cdr p2
)) (not (eq res
1)))
1508 (setq res
(math-two-eq (car p
) (car p2
)))
1509 (or res
(setq all nil
)))
1515 (cons 'calcFunc-neq args
))))
1516 (or (cdr (assq (math-two-eq a b
) '((0 .
1) (1 .
0))))
1517 (if (and (or (math-looks-negp a
) (math-zerop a
))
1518 (or (math-looks-negp b
) (math-zerop b
)))
1519 (list 'calcFunc-neq
(math-neg a
) (math-neg b
))
1520 (list 'calcFunc-neq a b
)))))
1522 (defun math-two-eq (a b
)
1523 (if (eq (car-safe a
) 'vec
)
1524 (if (eq (car-safe b
) 'vec
)
1525 (if (= (length a
) (length b
))
1527 (while (and (setq a
(cdr a
) b
(cdr b
)) (not (eq res
0)))
1529 (setq res
(math-two-eq (car a
) (car b
)))
1530 (if (eq (math-two-eq (car a
) (car b
)) 0)
1534 (if (Math-objectp b
)
1537 (if (eq (car-safe b
) 'vec
)
1538 (if (Math-objectp a
)
1541 (let ((res (math-compare a b
)))
1544 (if (and (= res
2) (not (and (Math-scalarp a
) (Math-scalarp b
))))
1548 (defun calcFunc-lt (a b
)
1549 (let ((res (math-compare a b
)))
1553 (if (and (or (math-looks-negp a
) (math-zerop a
))
1554 (or (math-looks-negp b
) (math-zerop b
)))
1555 (list 'calcFunc-gt
(math-neg a
) (math-neg b
))
1556 (list 'calcFunc-lt a b
))
1559 (defun calcFunc-gt (a b
)
1560 (let ((res (math-compare a b
)))
1564 (if (and (or (math-looks-negp a
) (math-zerop a
))
1565 (or (math-looks-negp b
) (math-zerop b
)))
1566 (list 'calcFunc-lt
(math-neg a
) (math-neg b
))
1567 (list 'calcFunc-gt a b
))
1570 (defun calcFunc-leq (a b
)
1571 (let ((res (math-compare a b
)))
1575 (if (and (or (math-looks-negp a
) (math-zerop a
))
1576 (or (math-looks-negp b
) (math-zerop b
)))
1577 (list 'calcFunc-geq
(math-neg a
) (math-neg b
))
1578 (list 'calcFunc-leq a b
))
1581 (defun calcFunc-geq (a b
)
1582 (let ((res (math-compare a b
)))
1586 (if (and (or (math-looks-negp a
) (math-zerop a
))
1587 (or (math-looks-negp b
) (math-zerop b
)))
1588 (list 'calcFunc-leq
(math-neg a
) (math-neg b
))
1589 (list 'calcFunc-geq a b
))
1592 (defun calcFunc-rmeq (a)
1593 (if (math-vectorp a
)
1594 (math-map-vec 'calcFunc-rmeq a
)
1595 (if (assq (car-safe a
) calc-tweak-eqn-table
)
1596 (if (and (eq (car-safe (nth 2 a
)) 'var
)
1597 (math-objectp (nth 1 a
)))
1600 (if (eq (car-safe a
) 'calcFunc-assign
)
1602 (if (eq (car-safe a
) 'calcFunc-evalto
)
1604 (list 'calcFunc-rmeq a
))))))
1606 (defun calcFunc-land (a b
)
1607 (cond ((Math-zerop a
)
1615 (t (list 'calcFunc-land a b
))))
1617 (defun calcFunc-lor (a b
)
1618 (cond ((Math-zerop a
)
1626 (t (list 'calcFunc-lor a b
))))
1628 (defun calcFunc-lnot (a)
1631 (if (math-is-true a
)
1633 (let ((op (and (= (length a
) 3)
1634 (assq (car a
) calc-tweak-eqn-table
))))
1636 (cons (nth 2 op
) (cdr a
))
1637 (list 'calcFunc-lnot a
))))))
1639 (defun calcFunc-if (c e1 e2
)
1642 (if (and (math-is-true c
) (not (Math-vectorp c
)))
1644 (or (and (Math-vectorp c
)
1646 (let ((ee1 (if (Math-vectorp e1
)
1647 (if (= (length c
) (length e1
))
1649 (calc-record-why "*Dimension error" e1
))
1651 (ee2 (if (Math-vectorp e2
)
1652 (if (= (length c
) (length e2
))
1654 (calc-record-why "*Dimension error" e2
))
1657 (cons 'vec
(math-if-vector (cdr c
) ee1 ee2
)))))
1658 (list 'calcFunc-if c e1 e2
)))))
1660 (defun math-if-vector (c e1 e2
)
1662 (cons (if (Math-zerop (car c
)) (car e2
) (car e1
))
1663 (math-if-vector (cdr c
)
1665 (or (cdr e2
) e2
)))))
1667 (defun math-normalize-logical-op (a)
1668 (or (and (eq (car a
) 'calcFunc-if
)
1670 (let ((a1 (math-normalize (nth 1 a
))))
1672 (math-normalize (nth 3 a
))
1673 (if (Math-numberp a1
)
1674 (math-normalize (nth 2 a
))
1675 (if (and (Math-vectorp (nth 1 a
))
1676 (math-constp (nth 1 a
)))
1677 (calcFunc-if (nth 1 a
)
1678 (math-normalize (nth 2 a
))
1679 (math-normalize (nth 3 a
)))
1680 (let ((calc-simplify-mode 'none
))
1681 (list 'calcFunc-if a1
1682 (math-normalize (nth 2 a
))
1683 (math-normalize (nth 3 a
)))))))))
1686 (defun calcFunc-in (a b
)
1687 (or (and (eq (car-safe b
) 'vec
)
1689 (while (and (setq bb
(cdr bb
))
1690 (not (if (memq (car-safe (car bb
)) '(vec intv
))
1691 (eq (calcFunc-in a
(car bb
)) 1)
1692 (Math-equal a
(car bb
))))))
1693 (if bb
1 (and (math-constp a
) (math-constp bb
) 0))))
1694 (and (eq (car-safe b
) 'intv
)
1695 (let ((res (math-compare a
(nth 2 b
))) res2
)
1699 (or (/= (nth 1 b
) 2)
1700 (Math-lessp (nth 2 b
) (nth 3 b
))))
1701 (if (memq (nth 1 b
) '(2 3)) 1 0))
1702 ((= (setq res2
(math-compare a
(nth 3 b
))) 1)
1705 (or (/= (nth 1 b
) 1)
1706 (Math-lessp (nth 2 b
) (nth 3 b
))))
1707 (if (memq (nth 1 b
) '(1 3)) 1 0))
1713 (and (Math-equal a b
)
1715 (and (math-constp a
) (math-constp b
)
1717 (list 'calcFunc-in a b
)))
1719 (defun calcFunc-typeof (a)
1720 (cond ((Math-integerp a
) 1)
1721 ((eq (car a
) 'frac
) 2)
1722 ((eq (car a
) 'float
) 3)
1723 ((eq (car a
) 'hms
) 4)
1724 ((eq (car a
) 'cplx
) 5)
1725 ((eq (car a
) 'polar
) 6)
1726 ((eq (car a
) 'sdev
) 7)
1727 ((eq (car a
) 'intv
) 8)
1728 ((eq (car a
) 'mod
) 9)
1729 ((eq (car a
) 'date
) (if (Math-integerp (nth 1 a
)) 10 11))
1731 (if (memq (nth 2 a
) '(var-inf var-uinf var-nan
)) 12 100))
1732 ((eq (car a
) 'vec
) (if (math-matrixp a
) 102 101))
1733 (t (math-calcFunc-to-var (car a
)))))
1735 (defun calcFunc-integer (a)
1736 (if (Math-integerp a
)
1738 (if (Math-objvecp a
)
1740 (list 'calcFunc-integer a
))))
1742 (defun calcFunc-real (a)
1745 (if (Math-objvecp a
)
1747 (list 'calcFunc-real a
))))
1749 (defun calcFunc-constant (a)
1752 (if (Math-objvecp a
)
1754 (list 'calcFunc-constant a
))))
1756 (defun calcFunc-refers (a b
)
1757 (if (math-expr-contains a b
)
1759 (if (eq (car-safe a
) 'var
)
1760 (list 'calcFunc-refers a b
)
1763 (defun calcFunc-negative (a)
1764 (if (math-looks-negp a
)
1766 (if (or (math-zerop a
)
1769 (list 'calcFunc-negative a
))))
1771 (defun calcFunc-variable (a)
1772 (if (eq (car-safe a
) 'var
)
1774 (if (Math-objvecp a
)
1776 (list 'calcFunc-variable a
))))
1778 (defun calcFunc-nonvar (a)
1779 (if (eq (car-safe a
) 'var
)
1780 (list 'calcFunc-nonvar a
)
1783 (defun calcFunc-istrue (a)
1784 (if (math-is-true a
)
1790 ;;;; User-programmability.
1792 ;;; Compiling Lisp-like forms to use the math library.
1794 (defun math-do-defmath (func args body
)
1795 (require 'calc-macs
)
1796 (let* ((fname (intern (concat "calcFunc-" (symbol-name func
))))
1797 (doc (if (stringp (car body
)) (list (car body
))))
1798 (clargs (mapcar 'math-clean-arg args
))
1799 (body (math-define-function-body
1800 (if (stringp (car body
)) (cdr body
) body
)
1803 (if (and (consp (car body
))
1804 (eq (car (car body
)) 'interactive
))
1805 (let ((inter (car body
)))
1806 (setq body
(cdr body
))
1807 (if (or (> (length inter
) 2)
1808 (integerp (nth 1 inter
)))
1809 (let ((hasprefix nil
) (hasmulti nil
))
1810 (if (stringp (nth 1 inter
))
1812 (cond ((equal (nth 1 inter
) "p")
1814 ((equal (nth 1 inter
) "m")
1817 "Can't handle interactive code string \"%s\""
1819 (setq inter
(cdr inter
))))
1820 (if (not (integerp (nth 1 inter
)))
1822 "Expected an integer in interactive specification"))
1823 (append (list 'defun
1824 (intern (concat "calc-"
1825 (symbol-name func
)))
1826 (if (or hasprefix hasmulti
)
1830 (if (or hasprefix hasmulti
)
1831 '((interactive "P"))
1835 '(calc-slow-wrapper)
1842 (list 'prefix-numeric-value
1846 (list 'calc-enter-result
1847 (if hasmulti
'n
(nth 1 inter
))
1851 (list 'quote
(list fname
))
1852 (list 'calc-top-list-n
1861 'prefix-numeric-value
1865 (list 'calc-top-list-n
1868 (nth 1 inter
)))))))))))
1869 (append (list 'defun
1870 (intern (concat "calc-" (symbol-name func
)))
1875 (cons 'calc-wrapper body
))))))
1876 (append (list 'defun
fname clargs
)
1878 (math-do-arg-list-check args nil nil
)
1881 (defun math-clean-arg (arg)
1883 (math-clean-arg (nth 1 arg
))
1886 (defun math-do-arg-check (arg var is-opt is-rest
)
1888 (let ((chk (math-do-arg-check arg var nil nil
)))
1892 (setq chk
(list (cons 'progn chk
)))
1895 (let* ((rest (math-do-arg-check (nth 1 arg
) var is-opt is-rest
))
1897 (qqual (list 'quote qual
))
1898 (qual-name (symbol-name qual
))
1899 (chk (intern (concat "math-check-" qual-name
))))
1905 (list 'mapcar
(list 'quote chk
) var
))
1906 (list 'setq var
(list chk var
)))))
1907 (if (fboundp (setq chk
(intern (concat "math-" qual-name
))))
1916 (list 'math-reject-arg
1921 (list 'math-reject-arg var qqual
)))))
1922 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name
)
1923 (fboundp (setq chk
(intern
1925 (math-match-substring
1935 (list 'math-reject-arg
1940 (list 'math-reject-arg var qqual
)))))
1941 (error "Unknown qualifier `%s'" qual-name
))))))))
1943 (defun math-do-arg-list-check (args is-opt is-rest
)
1944 (cond ((null args
) nil
)
1946 (append (math-do-arg-check (car args
)
1947 (math-clean-arg (car args
))
1949 (math-do-arg-list-check (cdr args
) is-opt is-rest
)))
1950 ((eq (car args
) '&optional
)
1951 (math-do-arg-list-check (cdr args
) t nil
))
1952 ((eq (car args
) '&rest
)
1953 (math-do-arg-list-check (cdr args
) nil t
))
1954 (t (math-do-arg-list-check (cdr args
) is-opt is-rest
))))
1956 (defconst math-prim-funcs
1957 '( (~
= . math-nearly-equal
)
1959 (lsh . calcFunc-lsh
)
1960 (ash . calcFunc-ash
)
1961 (logand . calcFunc-and
)
1962 (logandc2 . calcFunc-diff
)
1963 (logior . calcFunc-or
)
1964 (logxor . calcFunc-xor
)
1965 (lognot . calcFunc-not
)
1966 (equal . equal
) ; need to leave these ones alone!
1975 (defconst math-prim-vars
1978 (&optional .
&optional
)
1982 (defun math-define-function-body (body env
)
1983 (let ((body (math-define-body body env
)))
1984 (if (math-body-refers-to body
'math-return
)
1985 (list (cons 'catch
(cons '(quote math-return
) body
)))
1988 ;; The variable math-exp-env is local to math-define-body, but is
1989 ;; used by math-define-exp, which is called (indirectly) by
1990 ;; by math-define-body.
1991 (defvar math-exp-env
)
1993 (defun math-define-body (body math-exp-env
)
1994 (math-define-list body
))
1996 (defun math-define-list (body &optional quote
)
1999 ((and (eq (car body
) ':)
2000 (stringp (nth 1 body
)))
2001 (cons (let* ((math-read-expr-quotes t
)
2002 (exp (math-read-plain-expr (nth 1 body
) t
)))
2003 (math-define-exp exp
))
2004 (math-define-list (cdr (cdr body
)))))
2006 (cons (cond ((consp (car body
))
2007 (math-define-list (cdr body
) t
))
2010 (math-define-list (cdr body
))))
2012 (cons (math-define-exp (car body
))
2013 (math-define-list (cdr body
))))))
2015 (defun math-define-exp (exp)
2017 (let ((func (car exp
)))
2018 (cond ((memq func
'(quote function
))
2019 (if (and (consp (nth 1 exp
))
2020 (eq (car (nth 1 exp
)) 'lambda
))
2022 (math-define-lambda (nth 1 exp
) math-exp-env
))
2024 ((memq func
'(let let
* for foreach
))
2025 (let ((head (nth 1 exp
))
2026 (body (cdr (cdr exp
))))
2027 (if (memq func
'(let let
*))
2029 (setq func
(cdr (assq func
'((for . math-for
)
2030 (foreach . math-foreach
)))))
2031 (if (not (listp (car head
)))
2032 (setq head
(list head
))))
2035 (cons (math-define-let head
)
2036 (math-define-body body
2038 (math-define-let-env head
)
2040 ((and (memq func
'(setq setf
))
2041 (math-complicated-lhs (cdr exp
)))
2042 (if (> (length exp
) 3)
2043 (cons 'progn
(math-define-setf-list (cdr exp
)))
2044 (math-define-setf (nth 1 exp
) (nth 2 exp
))))
2045 ((eq func
'condition-case
)
2048 (math-define-body (cdr (cdr exp
))
2053 (math-define-cond (cdr exp
))))
2054 ((and (consp func
) ; ('spam a b) == force use of plain spam
2055 (eq (car func
) 'quote
))
2056 (cons func
(math-define-list (cdr exp
))))
2058 (let ((args (math-define-list (cdr exp
)))
2059 (prim (assq func math-prim-funcs
)))
2061 (cons (cdr prim
) args
))
2063 (list 'eq
(car args
) '(quote float
)))
2065 (math-define-binop 'math-add
0
2066 (car args
) (cdr args
)))
2068 (if (= (length args
) 1)
2069 (cons 'math-neg args
)
2070 (math-define-binop 'math-sub
0
2071 (car args
) (cdr args
))))
2073 (math-define-binop 'math-mul
1
2074 (car args
) (cdr args
)))
2076 (math-define-binop 'math-div
1
2077 (car args
) (cdr args
)))
2079 (math-define-binop 'math-min
0
2080 (car args
) (cdr args
)))
2082 (math-define-binop 'math-max
0
2083 (car args
) (cdr args
)))
2085 (if (and (math-numberp (nth 1 args
))
2086 (math-zerop (nth 1 args
)))
2087 (list 'math-negp
(car args
))
2088 (cons 'math-lessp args
)))
2090 (if (and (math-numberp (nth 1 args
))
2091 (math-zerop (nth 1 args
)))
2092 (list 'math-posp
(car args
))
2093 (list 'math-lessp
(nth 1 args
) (nth 0 args
))))
2096 (if (and (math-numberp (nth 1 args
))
2097 (math-zerop (nth 1 args
)))
2098 (list 'math-posp
(car args
))
2100 (nth 1 args
) (nth 0 args
)))))
2103 (if (and (math-numberp (nth 1 args
))
2104 (math-zerop (nth 1 args
)))
2105 (list 'math-negp
(car args
))
2106 (cons 'math-lessp args
))))
2108 (if (and (math-numberp (nth 1 args
))
2109 (math-zerop (nth 1 args
)))
2110 (list 'math-zerop
(nth 0 args
))
2111 (if (and (integerp (nth 1 args
))
2112 (/= (%
(nth 1 args
) 10) 0))
2113 (cons 'math-equal-int args
)
2114 (cons 'math-equal args
))))
2117 (if (and (math-numberp (nth 1 args
))
2118 (math-zerop (nth 1 args
)))
2119 (list 'math-zerop
(nth 0 args
))
2120 (if (and (integerp (nth 1 args
))
2121 (/= (%
(nth 1 args
) 10) 0))
2122 (cons 'math-equal-int args
)
2123 (cons 'math-equal args
)))))
2125 (list 'math-add
(car args
) 1))
2127 (list 'math-add
(car args
) -
1))
2128 ((eq func
'not
) ; optimize (not (not x)) => x
2129 (if (eq (car-safe args
) func
)
2132 ((and (eq func
'elt
) (cdr (cdr args
)))
2133 (math-define-elt (car args
) (cdr args
)))
2136 (let* ((name (symbol-name func
))
2137 (cfunc (intern (concat "calcFunc-" name
)))
2138 (mfunc (intern (concat "math-" name
))))
2139 (cond ((fboundp cfunc
)
2144 (string-match "\\`calcFunc-.*" name
))
2147 (cons cfunc args
)))))))))
2148 (t (cons func
(math-define-list (cdr exp
))))))) ;;args
2150 (let ((prim (assq exp math-prim-vars
))
2151 (name (symbol-name exp
)))
2154 ((memq exp math-exp-env
)
2156 ((string-match "-" name
)
2159 (intern (concat "var-" name
))))))
2161 (if (or (<= exp -
1000000) (>= exp
1000000))
2162 (list 'quote
(math-normalize exp
))
2166 (defun math-define-cond (forms)
2168 (cons (math-define-list (car forms
))
2169 (math-define-cond (cdr forms
)))))
2171 (defun math-complicated-lhs (body)
2173 (or (not (symbolp (car body
)))
2174 (math-complicated-lhs (cdr (cdr body
))))))
2176 (defun math-define-setf-list (body)
2178 (cons (math-define-setf (nth 0 body
) (nth 1 body
))
2179 (math-define-setf-list (cdr (cdr body
))))))
2181 (defun math-define-setf (place value
)
2182 (setq place
(math-define-exp place
)
2183 value
(math-define-exp value
))
2184 (cond ((symbolp place
)
2185 (list 'setq place value
))
2186 ((eq (car-safe place
) 'nth
)
2187 (list 'setcar
(list 'nthcdr
(nth 1 place
) (nth 2 place
)) value
))
2188 ((eq (car-safe place
) 'elt
)
2189 (list 'setcar
(list 'nthcdr
(nth 2 place
) (nth 1 place
)) value
))
2190 ((eq (car-safe place
) 'car
)
2191 (list 'setcar
(nth 1 place
) value
))
2192 ((eq (car-safe place
) 'cdr
)
2193 (list 'setcdr
(nth 1 place
) value
))
2195 (error "Bad place form for setf: %s" place
))))
2197 (defun math-define-binop (op ident arg1 rest
)
2199 (math-define-binop op ident
2200 (list op arg1
(car rest
))
2204 (defun math-define-let (vlist)
2206 (cons (if (consp (car vlist
))
2207 (cons (car (car vlist
))
2208 (math-define-list (cdr (car vlist
))))
2210 (math-define-let (cdr vlist
)))))
2212 (defun math-define-let-env (vlist)
2214 (cons (if (consp (car vlist
))
2217 (math-define-let-env (cdr vlist
)))))
2219 (defun math-define-lambda (exp exp-env
)
2220 (nconc (list (nth 0 exp
) ; 'lambda
2221 (nth 1 exp
)) ; arg list
2222 (math-define-function-body (cdr (cdr exp
))
2223 (append (nth 1 exp
) exp-env
))))
2225 (defun math-define-elt (seq idx
)
2227 (math-define-elt (list 'elt seq
(car idx
)) (cdr idx
))
2232 ;;; Useful programming macros.
2234 (defmacro math-while
(head &rest body
)
2235 (let ((body (cons 'while
(cons head body
))))
2236 (if (math-body-refers-to body
'math-break
)
2237 (cons 'catch
(cons '(quote math-break
) (list body
)))
2239 ;; (put 'math-while 'lisp-indent-hook 1)
2241 (defmacro math-for
(head &rest body
)
2242 (let ((body (if head
2243 (math-handle-for head body
)
2244 (cons 'while
(cons t body
)))))
2245 (if (math-body-refers-to body
'math-break
)
2246 (cons 'catch
(cons '(quote math-break
) (list body
)))
2248 ;; (put 'math-for 'lisp-indent-hook 1)
2250 (defun math-handle-for (head body
)
2251 (let* ((var (nth 0 (car head
)))
2252 (init (nth 1 (car head
)))
2253 (limit (nth 2 (car head
)))
2254 (step (or (nth 3 (car head
)) 1))
2255 (body (if (cdr head
)
2256 (list (math-handle-for (cdr head
) body
))
2258 (all-ints (and (integerp init
) (integerp limit
) (integerp step
)))
2259 (const-limit (or (integerp limit
)
2260 (and (eq (car-safe limit
) 'quote
)
2261 (math-realp (nth 1 limit
)))))
2262 (const-step (or (integerp step
)
2263 (and (eq (car-safe step
) 'quote
)
2264 (math-realp (nth 1 step
)))))
2265 (save-limit (if const-limit limit
(make-symbol "<limit>")))
2266 (save-step (if const-step step
(make-symbol "<step>"))))
2268 (cons (append (if const-limit nil
(list (list save-limit limit
)))
2269 (if const-step nil
(list (list save-step step
)))
2270 (list (list var init
)))
2275 (list '<= var save-limit
)
2276 (list '>= var save-limit
))
2279 (if (or (math-posp step
)
2304 save-step
)))))))))))
2306 (defmacro math-foreach
(head &rest body
)
2307 (let ((body (math-handle-foreach head body
)))
2308 (if (math-body-refers-to body
'math-break
)
2309 (cons 'catch
(cons '(quote math-break
) (list body
)))
2311 ;; (put 'math-foreach 'lisp-indent-hook 1)
2313 (defun math-handle-foreach (head body
)
2314 (let ((var (nth 0 (car head
)))
2315 (data (nth 1 (car head
)))
2316 (body (if (cdr head
)
2317 (list (math-handle-foreach (cdr head
) body
))
2320 (cons (list (list var data
))
2327 (list 'cdr var
)))))))))))
2330 (defun math-body-refers-to (body thing
)
2331 (or (equal body thing
)
2333 (or (math-body-refers-to (car body
) thing
)
2334 (math-body-refers-to (cdr body
) thing
)))))
2336 (defun math-break (&optional value
)
2337 (throw 'math-break value
))
2339 (defun math-return (&optional value
)
2340 (throw 'math-return value
))
2346 (defun math-composite-inequalities (x op
)
2347 (if (memq (nth 1 op
) '(calcFunc-eq calcFunc-neq
))
2348 (if (eq (car x
) (nth 1 op
))
2349 (append x
(list (math-read-expr-level (nth 3 op
))))
2350 (throw 'syntax
"Syntax error"))
2353 (if (memq (nth 1 op
) '(calcFunc-lt calcFunc-leq
))
2354 (if (memq (car x
) '(calcFunc-lt calcFunc-leq
))
2356 (+ (if (eq (car x
) 'calcFunc-leq
) 2 0)
2357 (if (eq (nth 1 op
) 'calcFunc-leq
) 1 0))
2358 (nth 1 x
) (math-read-expr-level (nth 3 op
)))
2359 (throw 'syntax
"Syntax error"))
2360 (if (memq (car x
) '(calcFunc-gt calcFunc-geq
))
2362 (+ (if (eq (nth 1 op
) 'calcFunc-geq
) 2 0)
2363 (if (eq (car x
) 'calcFunc-geq
) 1 0))
2364 (math-read-expr-level (nth 3 op
)) (nth 1 x
))
2365 (throw 'syntax
"Syntax error"))))))
2367 (provide 'calc-prog
)
2369 ;;; arch-tag: 4c5a183b-c9e5-4632-bb3f-e41a764518b0
2370 ;;; calc-prog.el ends here