1 ;;; calc-prog.el --- user programmability functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;; Declare functions which are defined elsewhere.
32 (declare-function edmacro-format-keys
"edmacro" (macro &optional verbose
))
33 (declare-function edmacro-parse-keys
"edmacro" (string &optional need-vector
))
34 (declare-function math-read-expr-level
"calc-aent" (exp-prec &optional exp-term
))
37 (defun calc-equal-to (arg)
40 (if (and (integerp arg
) (> arg
2))
41 (calc-enter-result arg
"eq" (cons 'calcFunc-eq
(calc-top-list-n arg
)))
42 (calc-binary-op "eq" 'calcFunc-eq arg
))))
44 (defun calc-remove-equal (arg)
47 (calc-unary-op "rmeq" 'calcFunc-rmeq arg
)))
49 (defun calc-not-equal-to (arg)
52 (if (and (integerp arg
) (> arg
2))
53 (calc-enter-result arg
"neq" (cons 'calcFunc-neq
(calc-top-list-n arg
)))
54 (calc-binary-op "neq" 'calcFunc-neq arg
))))
56 (defun calc-less-than (arg)
59 (calc-binary-op "lt" 'calcFunc-lt arg
)))
61 (defun calc-greater-than (arg)
64 (calc-binary-op "gt" 'calcFunc-gt arg
)))
66 (defun calc-less-equal (arg)
69 (calc-binary-op "leq" 'calcFunc-leq arg
)))
71 (defun calc-greater-equal (arg)
74 (calc-binary-op "geq" 'calcFunc-geq arg
)))
76 (defun calc-in-set (arg)
79 (calc-binary-op "in" 'calcFunc-in arg
)))
81 (defun calc-logical-and (arg)
84 (calc-binary-op "land" 'calcFunc-land arg
1)))
86 (defun calc-logical-or (arg)
89 (calc-binary-op "lor" 'calcFunc-lor arg
0)))
91 (defun calc-logical-not (arg)
94 (calc-unary-op "lnot" 'calcFunc-lnot arg
)))
96 (defun calc-logical-if ()
99 (calc-enter-result 3 "if" (cons 'calcFunc-if
(calc-top-list-n 3)))))
105 (defun calc-timing (n)
108 (calc-change-mode 'calc-timing n nil t
)
109 (message (if calc-timing
110 "Reporting timing of slow commands in Trail"
111 "Not reporting timing of commands"))))
113 (defun calc-pass-errors ()
115 ;; The following two cases are for the new, optimizing byte compiler
116 ;; or the standard 18.57 byte compiler, respectively.
118 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do
))) 15)))
119 (or (memq (car-safe (car-safe place
)) '(error xxxerror
))
120 (setq place
(aref (nth 2 (nth 2 (symbol-function 'calc-do
))) 27)))
121 (or (memq (car (car place
)) '(error xxxerror
))
123 (setcar (car place
) 'xxxerror
))
124 (error (error "The calc-do function has been modified; unable to patch"))))
126 (defun calc-user-define ()
128 (message "Define user key: z-")
129 (let ((key (read-char)))
130 (if (= (calc-user-function-classify key
) 0)
131 (error "Can't redefine \"?\" key"))
132 (let ((func (intern (completing-read (concat "Set key z "
139 (let* ((kmap (calc-user-key-map))
140 (old (assq key kmap
)))
141 ;; FIXME: Why not (define-key kmap (vector key) func)?
144 (setcdr kmap
(cons (cons key func
) (cdr kmap
))))))))
146 (defun calc-user-undefine ()
148 (message "Undefine user key: z-")
149 (let ((key (read-char)))
150 (if (= (calc-user-function-classify key
) 0)
151 (error "Can't undefine \"?\" key"))
152 (let* ((kmap (calc-user-key-map)))
153 (delq (or (assq key kmap
)
154 (assq (upcase key
) kmap
)
155 (assq (downcase key
) kmap
)
156 (error "No such user key is defined"))
160 ;; math-integral-cache-state is originally declared in calcalg2.el,
161 ;; it is used in calc-user-define-variable.
162 (defvar math-integral-cache-state
)
164 ;; calc-user-formula-alist is local to calc-user-define-formula,
165 ;; calc-user-define-composition and calc-finish-formula-edit,
166 ;; but is used by calc-fix-user-formula.
167 (defvar calc-user-formula-alist
)
169 (defun calc-user-define-formula ()
172 (let* ((form (calc-top 1))
174 (is-lambda (and (eq (car-safe form
) 'calcFunc-lambda
)
175 (>= (length form
) 2)))
176 odef key keyname cmd cmd-base cmd-base-default
177 func calc-user-formula-alist is-symb
)
179 (setq math-arglist
(mapcar (function (lambda (x) (nth 1 x
)))
180 (nreverse (cdr (reverse (cdr form
)))))
181 form
(nth (1- (length form
)) form
))
182 (calc-default-formula-arglist form
)
183 (setq math-arglist
(sort math-arglist
'string-lessp
)))
184 (message "Define user key: z-")
185 (setq key
(read-char))
186 (if (= (calc-user-function-classify key
) 0)
187 (error "Can't redefine \"?\" key"))
188 (setq key
(and (not (memq key
'(13 32))) key
)
190 (if (or (and (<= ?
0 key
) (<= key ?
9))
191 (and (<= ?a key
) (<= key ?z
))
192 (and (<= ?A key
) (<= key ?Z
)))
194 (format "%03d" key
)))
195 odef
(assq key
(calc-user-key-map)))
197 (setq keyname
(format "%05d" (abs (%
(random) 10000)))))
200 (setq cmd-base-default
(concat "User-" keyname
))
201 (setq cmd
(completing-read
202 (concat "Define M-x command name (default calc-"
205 obarray
'commandp nil
206 (if (and odef
(symbolp (cdr odef
)))
207 (symbol-name (cdr odef
))
209 (if (or (string-equal cmd
"")
210 (string-equal cmd
"calc-"))
211 (setq cmd
(concat "calc-User-" keyname
)))
212 (setq cmd-base
(and (string-match "\\`calc-\\(.+\\)\\'" cmd
)
213 (math-match-substring cmd
1)))
214 (setq cmd
(intern cmd
))
220 (if (get cmd
'calc-user-defn
)
221 (concat "Replace previous definition for "
222 (symbol-name cmd
) "? ")
223 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
226 (setq cmd-base-default
229 "\\`User-.+" cmd-base
)
232 (substring cmd-base
5))
234 (concat "User" keyname
)))
238 (concat "Define algebraic function name (default "
239 cmd-base-default
"): ")
240 (mapcar (lambda (x) (substring x
9))
241 (all-completions "calcFunc-"
245 (intern (concat "calcFunc-" x
))))
248 (if (string-equal func
"calcFunc-")
249 (intern (concat "calcFunc-" cmd-base-default
))
257 (if (get func
'calc-user-defn
)
258 (concat "Replace previous definition for "
259 (symbol-name func
) "? ")
260 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
263 (setq func
(intern (concat "calcFunc-User"
265 (and cmd
(symbol-name cmd
))
266 (format "%05d" (%
(random) 10000)))))))
269 (setq calc-user-formula-alist math-arglist
)
272 (setq calc-user-formula-alist
273 (read-from-minibuffer "Function argument list: "
275 (prin1-to-string math-arglist
)
279 (and (not (calc-subsetp calc-user-formula-alist math-arglist
))
281 "Okay for arguments that don't appear in formula to be ignored? "))))))
282 (setq is-symb
(and calc-user-formula-alist
285 "Leave it symbolic for non-constant arguments? ")))
286 (setq calc-user-formula-alist
287 (mapcar (function (lambda (x)
288 (or (cdr (assq x
'((nil . arg-nil
)
290 x
))) calc-user-formula-alist
))
299 (list 'calc-enter-result
300 (length calc-user-formula-alist
)
301 (let ((name (symbol-name (or func cmd
))))
303 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
305 (math-match-substring name
1)))
308 (list 'calc-top-list-n
309 (length calc-user-formula-alist
)))))))
310 (put cmd
'calc-user-defn t
)))
311 (let ((body (list 'math-normalize
(calc-fix-user-formula form
))))
314 (list 'lambda calc-user-formula-alist
)
316 (mapcar (function (lambda (v)
317 (list 'math-check-const v t
)))
318 calc-user-formula-alist
))
320 (put func
'calc-user-defn form
)
321 (setq math-integral-cache-state nil
)
323 (let* ((kmap (calc-user-key-map))
324 (old (assq key kmap
)))
325 ;; FIXME: Why not (define-key kmap (vector key) cmd)?
328 (setcdr kmap
(cons (cons key cmd
) (cdr kmap
)))))))
331 (defvar math-arglist
) ; dynamically bound in all callers
332 (defun calc-default-formula-arglist (form)
334 (if (eq (car form
) 'var
)
335 (if (or (memq (nth 1 form
) math-arglist
)
336 (math-const-var form
))
338 (setq math-arglist
(cons (nth 1 form
) math-arglist
)))
339 (calc-default-formula-arglist-step (cdr form
)))))
341 (defun calc-default-formula-arglist-step (l)
344 (calc-default-formula-arglist (car l
))
345 (calc-default-formula-arglist-step (cdr l
)))))
347 (defun calc-subsetp (a b
)
349 (and (memq (car a
) b
)
350 (calc-subsetp (cdr a
) b
))))
352 (defun calc-fix-user-formula (f)
355 (cond ((and (eq (car f
) 'var
)
356 (memq (setq temp
(or (cdr (assq (nth 1 f
) '((nil . arg-nil
)
359 calc-user-formula-alist
))
361 ((or (math-constp f
) (eq (car f
) 'var
))
363 ((and (eq (car f
) 'calcFunc-eval
)
365 (list 'let
'((calc-simplify-mode nil
))
366 (list 'math-normalize
(calc-fix-user-formula (nth 1 f
)))))
367 ((and (eq (car f
) 'calcFunc-evalsimp
)
369 (list 'math-simplify
(calc-fix-user-formula (nth 1 f
))))
370 ((and (eq (car f
) 'calcFunc-evalextsimp
)
372 (list 'math-simplify-extended
373 (calc-fix-user-formula (nth 1 f
))))
376 (cons (list 'quote
(car f
))
377 (mapcar 'calc-fix-user-formula
(cdr f
)))))))
380 (defun calc-user-define-composition ()
383 (if (eq calc-language
'unform
)
384 (error "Can't define formats for unformatted mode"))
385 (let* ((comp (calc-top 1))
388 (completing-read "Define format for which function: "
389 (mapcar (lambda (x) (substring x
9))
390 (all-completions "calcFunc-"
394 (intern (concat "calcFunc-" x
))))))))
395 (comps (get func
'math-compose-forms
))
398 (calc-user-formula-alist nil
))
399 (if (math-zerop comp
)
400 (if (setq entry
(assq calc-language comps
))
401 (put func
'math-compose-forms
(delq entry comps
)))
402 (calc-default-formula-arglist comp
)
403 (setq math-arglist
(sort math-arglist
'string-lessp
))
406 (setq calc-user-formula-alist
407 (read-from-minibuffer "Composition argument list: "
409 (prin1-to-string math-arglist
)
413 (and (not (calc-subsetp calc-user-formula-alist math-arglist
))
415 "Okay for arguments that don't appear in formula to be invisible? "))))
416 (or (setq entry
(assq calc-language comps
))
417 (put func
'math-compose-forms
418 (cons (setq entry
(list calc-language
)) comps
)))
419 (or (setq entry2
(assq (length calc-user-formula-alist
) (cdr entry
)))
422 (list (length calc-user-formula-alist
))) (cdr entry
))))
424 (list 'lambda calc-user-formula-alist
(calc-fix-user-formula comp
))))
429 (defun calc-user-define-kbd-macro (arg)
432 (error "No keyboard macro defined"))
433 (message "Define last kbd macro on user key: z-")
434 (let ((key (read-char)))
435 (if (= (calc-user-function-classify key
) 0)
436 (error "Can't redefine \"?\" key"))
437 (let ((cmd (intern (completing-read "Full name for new command: "
442 (if (or (and (>= key ?a
)
449 (format "%03d" key
)))))))
451 (not (let ((f (symbol-function cmd
)))
454 (eq (car-safe (nth 3 f
))
455 'calc-execute-kbd-macro
)))))
456 (error "Function %s is already defined and not a keyboard macro"
458 (put cmd
'calc-user-defn t
)
459 (fset cmd
(if (< (prefix-numeric-value arg
) 0)
464 (list 'calc-execute-kbd-macro
465 (vector (key-description last-kbd-macro
)
468 (format "z%c" key
)))))
469 (let* ((kmap (calc-user-key-map))
470 (old (assq key kmap
)))
471 ;; FIXME: Why not (define-key kmap (vector key) func)?
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)))
572 (exp (with-current-buffer calc-buf
573 (let ((calc-user-parse-tables nil
)
575 (math-expr-opers (math-standard-ops))
576 (calc-hashes-used 0))
578 (if (string-match ",[ \t]*\\'" str
)
579 (substring str
0 (match-beginning 0))
581 (if (eq (car-safe exp
) 'error
)
583 (goto-char (+ pos
(nth 1 exp
)))
584 (error (nth 2 exp
))))
585 (setq tab
(nconc tab
(list (cons p exp
)))))))))
588 (defun calc-fix-token-name (name &optional unquoted
)
589 (cond ((string-match "\\`\\.\\." name
)
590 (concat "\\dots" (substring name
2)))
591 ((and (equal name
"{") (memq calc-lang
'(tex latex eqn
)))
593 ((and (equal name
"}") (memq calc-lang
'(tex latex eqn
)))
595 ((and (equal name
"&") (memq calc-lang
'(tex latex
)))
598 (search-backward "#")
599 (error "Token `#' is reserved"))
600 ((and unquoted
(string-match "#" name
))
601 (error "Tokens containing `#' must be quoted"))
602 ((not (string-match "[^ ]" name
))
603 (search-backward "\"" nil t
)
604 (error "Blank tokens are not allowed"))
607 (defun calc-read-parse-table-part (term eterm
)
611 (skip-chars-forward "\n\t ")
612 (if (eobp) (error "Expected `%s'" eterm
))
613 (not (looking-at term
)))
614 (cond ((looking-at "%%")
616 ((looking-at "{[\n\t ]")
618 (let ((p (calc-read-parse-table-part "}" "}")))
619 (or (looking-at "[+*?]")
620 (error "Expected `+', `*', or `?'"))
621 (let ((sym (intern (buffer-substring (point) (1+ (point))))))
623 (looking-at "[^\n\t ]*")
624 (let ((sep (buffer-substring (point) (match-end 0))))
625 (goto-char (match-end 0))
626 (and (eq sym
'\?) (> (length sep
) 0)
627 (not (equal sep
"$")) (not (equal sep
"."))
628 (error "Separator not allowed with { ... }?"))
629 (if (string-match "\\`\"" sep
)
630 (setq sep
(read-from-string sep
)))
631 (if (> (length sep
) 0)
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-event
)
1293 (or executing-kbd-macro
1294 (message "Reading loop body..."))
1296 (setq ch
(read-event))
1298 (error "Unterminated Z%c in keyboard macro" open
))
1301 (setq ch
(read-event)
1302 body
(vconcat body
(vector ?Z ch
)))
1303 (cond ((memq ch
'(?\
< ?\
( ?\
{))
1304 (setq count
(1+ count
)))
1305 ((memq ch
'(?\
> ?\
) ?\
}))
1306 (setq count
(1- count
)))
1309 (setq parts
(nconc parts
(list (vconcat (substring body
0 -
2)
1314 (setq body
(vconcat body
(vector 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
(vconcat (substring body
0 -
2) (vector ?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 "%s" "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 "%s" "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
))
1798 (prog1 (list (car body
))
1799 (setq body
(cdr body
)))))
1800 (clargs (mapcar 'math-clean-arg args
))
1801 (inter (if (and (consp (car body
))
1802 (eq (car (car body
)) 'interactive
))
1804 (setq body
(cdr body
))))))
1805 (setq body
(math-define-function-body body clargs
))
1808 (if (or (> (length inter
) 2)
1809 (integerp (nth 1 inter
)))
1810 (let ((hasprefix nil
) (hasmulti nil
))
1811 (when (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 (unless (integerp (nth 1 inter
))
1821 (error "Expected an integer in interactive specification"))
1822 `(defun ,(intern (concat "calc-" (symbol-name func
)))
1823 ,(if (or hasprefix hasmulti
) '(&optional n
) ())
1825 (interactive ,@(if (or hasprefix hasmulti
) '("P")))
1829 (prefix-numeric-value n
)
1832 ,(if hasmulti
'n
(nth 1 inter
))
1836 (calc-top-list-n ,(nth 1 inter
))
1840 (prefix-numeric-value n
)))))
1845 (nth 1 inter
)))))))))
1846 `(defun ,(intern (concat "calc-" (symbol-name func
))) ,clargs
1849 (calc-wrapper ,@body
))))
1850 (defun ,fname
,clargs
1852 ,@(math-do-arg-list-check args nil nil
)
1855 (defun math-clean-arg (arg)
1857 (math-clean-arg (nth 1 arg
))
1860 (defun math-do-arg-check (arg var is-opt is-rest
)
1862 (let ((chk (math-do-arg-check arg var nil nil
)))
1869 (let* ((rest (math-do-arg-check (nth 1 arg
) var is-opt is-rest
))
1871 (qual-name (symbol-name qual
))
1872 (chk (intern (concat "math-check-" qual-name
))))
1876 `((setq ,var
(mapcar ',chk
,var
)))
1877 `((setq ,var
(,chk
,var
)))))
1878 (if (fboundp (setq chk
(intern (concat "math-" qual-name
))))
1881 `((mapcar #'(lambda (x)
1883 (math-reject-arg x
',qual
)))
1886 (math-reject-arg ,var
',qual
)))))
1887 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name
)
1888 (fboundp (setq chk
(intern
1890 (math-match-substring
1894 `((mapcar #'(lambda (x)
1896 (math-reject-arg x
',qual
)))
1900 (math-reject-arg ,var
',qual
)))))
1901 (error "Unknown qualifier `%s'" qual-name
))))))))
1903 (defun math-do-arg-list-check (args is-opt is-rest
)
1904 (cond ((null args
) nil
)
1906 (append (math-do-arg-check (car args
)
1907 (math-clean-arg (car args
))
1909 (math-do-arg-list-check (cdr args
) is-opt is-rest
)))
1910 ((eq (car args
) '&optional
)
1911 (math-do-arg-list-check (cdr args
) t nil
))
1912 ((eq (car args
) '&rest
)
1913 (math-do-arg-list-check (cdr args
) nil t
))
1914 (t (math-do-arg-list-check (cdr args
) is-opt is-rest
))))
1916 (defconst math-prim-funcs
1917 '( (~
= . math-nearly-equal
)
1919 (lsh . calcFunc-lsh
)
1920 (ash . calcFunc-ash
)
1921 (logand . calcFunc-and
)
1922 (logandc2 . calcFunc-diff
)
1923 (logior . calcFunc-or
)
1924 (logxor . calcFunc-xor
)
1925 (lognot . calcFunc-not
)
1926 (equal . equal
) ; need to leave these ones alone!
1935 (defconst math-prim-vars
1938 (&optional .
&optional
)
1942 (defun math-define-function-body (body env
)
1943 (let ((body (math-define-body body env
)))
1944 (if (math-body-refers-to body
'math-return
)
1945 `((catch 'math-return
,@body
))
1948 ;; The variable math-exp-env is local to math-define-body, but is
1949 ;; used by math-define-exp, which is called (indirectly) by
1950 ;; by math-define-body.
1951 (defvar math-exp-env
)
1953 (defun math-define-body (body math-exp-env
)
1954 (math-define-list body
))
1956 (defun math-define-list (body &optional quote
)
1959 ((and (eq (car body
) ':)
1960 (stringp (nth 1 body
)))
1961 (cons (let* ((math-read-expr-quotes t
)
1962 (exp (math-read-plain-expr (nth 1 body
) t
)))
1963 (math-define-exp exp
))
1964 (math-define-list (cdr (cdr body
)))))
1966 (cons (cond ((consp (car body
))
1967 (math-define-list (cdr body
) t
))
1970 (math-define-list (cdr body
))))
1972 (cons (math-define-exp (car body
))
1973 (math-define-list (cdr body
))))))
1975 (defun math-define-exp (exp)
1977 (let ((func (car exp
)))
1978 (cond ((memq func
'(quote function
))
1979 (if (and (consp (nth 1 exp
))
1980 (eq (car (nth 1 exp
)) 'lambda
))
1982 (math-define-lambda (nth 1 exp
) math-exp-env
))
1984 ((memq func
'(let let
* for foreach
))
1985 (let ((head (nth 1 exp
))
1986 (body (cdr (cdr exp
))))
1987 (if (memq func
'(let let
*))
1989 (setq func
(cdr (assq func
'((for . math-for
)
1990 (foreach . math-foreach
)))))
1991 (if (not (listp (car head
)))
1992 (setq head
(list head
))))
1995 (cons (math-define-let head
)
1996 (math-define-body body
1998 (math-define-let-env head
)
2000 ((and (memq func
'(setq setf
))
2001 (math-complicated-lhs (cdr exp
)))
2002 (if (> (length exp
) 3)
2003 (cons 'progn
(math-define-setf-list (cdr exp
)))
2004 (math-define-setf (nth 1 exp
) (nth 2 exp
))))
2005 ((eq func
'condition-case
)
2008 (math-define-body (cdr (cdr exp
))
2013 (math-define-cond (cdr exp
))))
2014 ((and (consp func
) ; ('spam a b) == force use of plain spam
2015 (eq (car func
) 'quote
))
2016 (cons func
(math-define-list (cdr exp
))))
2018 (let ((args (math-define-list (cdr exp
)))
2019 (prim (assq func math-prim-funcs
)))
2021 (cons (cdr prim
) args
))
2023 (list 'eq
(car args
) '(quote float
)))
2025 (math-define-binop 'math-add
0
2026 (car args
) (cdr args
)))
2028 (if (= (length args
) 1)
2029 (cons 'math-neg args
)
2030 (math-define-binop 'math-sub
0
2031 (car args
) (cdr args
))))
2033 (math-define-binop 'math-mul
1
2034 (car args
) (cdr args
)))
2036 (math-define-binop 'math-div
1
2037 (car args
) (cdr args
)))
2039 (math-define-binop 'math-min
0
2040 (car args
) (cdr args
)))
2042 (math-define-binop 'math-max
0
2043 (car args
) (cdr args
)))
2045 (if (and (math-numberp (nth 1 args
))
2046 (math-zerop (nth 1 args
)))
2047 (list 'math-negp
(car args
))
2048 (cons 'math-lessp args
)))
2050 (if (and (math-numberp (nth 1 args
))
2051 (math-zerop (nth 1 args
)))
2052 (list 'math-posp
(car args
))
2053 (list 'math-lessp
(nth 1 args
) (nth 0 args
))))
2056 (if (and (math-numberp (nth 1 args
))
2057 (math-zerop (nth 1 args
)))
2058 (list 'math-posp
(car args
))
2060 (nth 1 args
) (nth 0 args
)))))
2063 (if (and (math-numberp (nth 1 args
))
2064 (math-zerop (nth 1 args
)))
2065 (list 'math-negp
(car args
))
2066 (cons 'math-lessp args
))))
2068 (if (and (math-numberp (nth 1 args
))
2069 (math-zerop (nth 1 args
)))
2070 (list 'math-zerop
(nth 0 args
))
2071 (if (and (integerp (nth 1 args
))
2072 (/= (%
(nth 1 args
) 10) 0))
2073 (cons 'math-equal-int args
)
2074 (cons 'math-equal args
))))
2077 (if (and (math-numberp (nth 1 args
))
2078 (math-zerop (nth 1 args
)))
2079 (list 'math-zerop
(nth 0 args
))
2080 (if (and (integerp (nth 1 args
))
2081 (/= (%
(nth 1 args
) 10) 0))
2082 (cons 'math-equal-int args
)
2083 (cons 'math-equal args
)))))
2085 (list 'math-add
(car args
) 1))
2087 (list 'math-add
(car args
) -
1))
2088 ((eq func
'not
) ; optimize (not (not x)) => x
2089 (if (eq (car-safe args
) func
)
2092 ((and (eq func
'elt
) (cdr (cdr args
)))
2093 (math-define-elt (car args
) (cdr args
)))
2096 (let* ((name (symbol-name func
))
2097 (cfunc (intern (concat "calcFunc-" name
)))
2098 (mfunc (intern (concat "math-" name
))))
2099 (cond ((fboundp cfunc
)
2104 (string-match "\\`calcFunc-.*" name
))
2107 (cons cfunc args
)))))))))
2108 (t (cons func
(math-define-list (cdr exp
))))))) ;;args
2110 (let ((prim (assq exp math-prim-vars
))
2111 (name (symbol-name exp
)))
2114 ((memq exp math-exp-env
)
2116 ((string-match "-" name
)
2119 (intern (concat "var-" name
))))))
2121 (if (or (<= exp -
1000000) (>= exp
1000000))
2122 (list 'quote
(math-normalize exp
))
2126 (defun math-define-cond (forms)
2128 (cons (math-define-list (car forms
))
2129 (math-define-cond (cdr forms
)))))
2131 (defun math-complicated-lhs (body)
2133 (or (not (symbolp (car body
)))
2134 (math-complicated-lhs (cdr (cdr body
))))))
2136 (defun math-define-setf-list (body)
2138 (cons (math-define-setf (nth 0 body
) (nth 1 body
))
2139 (math-define-setf-list (cdr (cdr body
))))))
2141 (defun math-define-setf (place value
)
2142 (setq place
(math-define-exp place
)
2143 value
(math-define-exp value
))
2144 (cond ((symbolp place
)
2145 (list 'setq place value
))
2146 ((eq (car-safe place
) 'nth
)
2147 (list 'setcar
(list 'nthcdr
(nth 1 place
) (nth 2 place
)) value
))
2148 ((eq (car-safe place
) 'elt
)
2149 (list 'setcar
(list 'nthcdr
(nth 2 place
) (nth 1 place
)) value
))
2150 ((eq (car-safe place
) 'car
)
2151 (list 'setcar
(nth 1 place
) value
))
2152 ((eq (car-safe place
) 'cdr
)
2153 (list 'setcdr
(nth 1 place
) value
))
2155 (error "Bad place form for setf: %s" place
))))
2157 (defun math-define-binop (op ident arg1 rest
)
2159 (math-define-binop op ident
2160 (list op arg1
(car rest
))
2164 (defun math-define-let (vlist)
2166 (cons (if (consp (car vlist
))
2167 (cons (car (car vlist
))
2168 (math-define-list (cdr (car vlist
))))
2170 (math-define-let (cdr vlist
)))))
2172 (defun math-define-let-env (vlist)
2174 (cons (if (consp (car vlist
))
2177 (math-define-let-env (cdr vlist
)))))
2179 (defun math-define-lambda (exp exp-env
)
2180 (nconc (list (nth 0 exp
) ; 'lambda
2181 (nth 1 exp
)) ; arg list
2182 (math-define-function-body (cdr (cdr exp
))
2183 (append (nth 1 exp
) exp-env
))))
2185 (defun math-define-elt (seq idx
)
2187 (math-define-elt (list 'elt seq
(car idx
)) (cdr idx
))
2192 ;;; Useful programming macros.
2194 (defmacro math-while
(head &rest body
)
2195 (let ((body (cons 'while
(cons head body
))))
2196 (if (math-body-refers-to body
'math-break
)
2197 (cons 'catch
(cons '(quote math-break
) (list body
)))
2199 ;; (put 'math-while 'lisp-indent-hook 1)
2201 (defmacro math-for
(head &rest body
)
2202 (let ((body (if head
2203 (math-handle-for head body
)
2204 (cons 'while
(cons t body
)))))
2205 (if (math-body-refers-to body
'math-break
)
2206 (cons 'catch
(cons '(quote math-break
) (list body
)))
2208 ;; (put 'math-for 'lisp-indent-hook 1)
2210 (defun math-handle-for (head body
)
2211 (let* ((var (nth 0 (car head
)))
2212 (init (nth 1 (car head
)))
2213 (limit (nth 2 (car head
)))
2214 (step (or (nth 3 (car head
)) 1))
2215 (body (if (cdr head
)
2216 (list (math-handle-for (cdr head
) body
))
2218 (all-ints (and (integerp init
) (integerp limit
) (integerp step
)))
2219 (const-limit (or (integerp limit
)
2220 (and (eq (car-safe limit
) 'quote
)
2221 (math-realp (nth 1 limit
)))))
2222 (const-step (or (integerp step
)
2223 (and (eq (car-safe step
) 'quote
)
2224 (math-realp (nth 1 step
)))))
2225 (save-limit (if const-limit limit
(make-symbol "<limit>")))
2226 (save-step (if const-step step
(make-symbol "<step>"))))
2228 (cons (append (if const-limit nil
(list (list save-limit limit
)))
2229 (if const-step nil
(list (list save-step step
)))
2230 (list (list var init
)))
2235 (list '<= var save-limit
)
2236 (list '>= var save-limit
))
2239 (if (or (math-posp step
)
2264 save-step
)))))))))))
2266 (defmacro math-foreach
(head &rest body
)
2267 (let ((body (math-handle-foreach head body
)))
2268 (if (math-body-refers-to body
'math-break
)
2269 (cons 'catch
(cons '(quote math-break
) (list body
)))
2271 ;; (put 'math-foreach 'lisp-indent-hook 1)
2273 (defun math-handle-foreach (head body
)
2274 (let ((var (nth 0 (car head
)))
2275 (data (nth 1 (car head
)))
2276 (body (if (cdr head
)
2277 (list (math-handle-foreach (cdr head
) body
))
2280 (cons (list (list var data
))
2287 (list 'cdr var
)))))))))))
2290 (defun math-body-refers-to (body thing
)
2291 (or (equal body thing
)
2293 (or (math-body-refers-to (car body
) thing
)
2294 (math-body-refers-to (cdr body
) thing
)))))
2296 (defun math-break (&optional value
)
2297 (throw 'math-break value
))
2299 (defun math-return (&optional value
)
2300 (throw 'math-return value
))
2306 (defun math-composite-inequalities (x op
)
2307 (if (memq (nth 1 op
) '(calcFunc-eq calcFunc-neq
))
2308 (if (eq (car x
) (nth 1 op
))
2309 (append x
(list (math-read-expr-level (nth 3 op
))))
2310 (throw 'syntax
"Syntax error"))
2313 (if (memq (nth 1 op
) '(calcFunc-lt calcFunc-leq
))
2314 (if (memq (car x
) '(calcFunc-lt calcFunc-leq
))
2316 (+ (if (eq (car x
) 'calcFunc-leq
) 2 0)
2317 (if (eq (nth 1 op
) 'calcFunc-leq
) 1 0))
2318 (nth 1 x
) (math-read-expr-level (nth 3 op
)))
2319 (throw 'syntax
"Syntax error"))
2320 (if (memq (car x
) '(calcFunc-gt calcFunc-geq
))
2322 (+ (if (eq (nth 1 op
) 'calcFunc-geq
) 2 0)
2323 (if (eq (car x
) 'calcFunc-geq
) 1 0))
2324 (math-read-expr-level (nth 3 op
)) (nth 1 x
))
2325 (throw 'syntax
"Syntax error"))))))
2327 (provide 'calc-prog
)
2329 ;;; calc-prog.el ends here