]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-prog.el
Update copyright date.
[gnu-emacs] / lisp / calc / calc-prog.el
1 ;;; calc-prog.el --- user programmability functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
16
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;; This file is autoloaded from calc-ext.el.
30
31 (require 'calc-ext)
32 (require 'calc-macs)
33
34
35 (defun calc-equal-to (arg)
36 (interactive "P")
37 (calc-wrapper
38 (if (and (integerp arg) (> arg 2))
39 (calc-enter-result arg "eq" (cons 'calcFunc-eq (calc-top-list-n arg)))
40 (calc-binary-op "eq" 'calcFunc-eq arg))))
41
42 (defun calc-remove-equal (arg)
43 (interactive "P")
44 (calc-wrapper
45 (calc-unary-op "rmeq" 'calcFunc-rmeq arg)))
46
47 (defun calc-not-equal-to (arg)
48 (interactive "P")
49 (calc-wrapper
50 (if (and (integerp arg) (> arg 2))
51 (calc-enter-result arg "neq" (cons 'calcFunc-neq (calc-top-list-n arg)))
52 (calc-binary-op "neq" 'calcFunc-neq arg))))
53
54 (defun calc-less-than (arg)
55 (interactive "P")
56 (calc-wrapper
57 (calc-binary-op "lt" 'calcFunc-lt arg)))
58
59 (defun calc-greater-than (arg)
60 (interactive "P")
61 (calc-wrapper
62 (calc-binary-op "gt" 'calcFunc-gt arg)))
63
64 (defun calc-less-equal (arg)
65 (interactive "P")
66 (calc-wrapper
67 (calc-binary-op "leq" 'calcFunc-leq arg)))
68
69 (defun calc-greater-equal (arg)
70 (interactive "P")
71 (calc-wrapper
72 (calc-binary-op "geq" 'calcFunc-geq arg)))
73
74 (defun calc-in-set (arg)
75 (interactive "P")
76 (calc-wrapper
77 (calc-binary-op "in" 'calcFunc-in arg)))
78
79 (defun calc-logical-and (arg)
80 (interactive "P")
81 (calc-wrapper
82 (calc-binary-op "land" 'calcFunc-land arg 1)))
83
84 (defun calc-logical-or (arg)
85 (interactive "P")
86 (calc-wrapper
87 (calc-binary-op "lor" 'calcFunc-lor arg 0)))
88
89 (defun calc-logical-not (arg)
90 (interactive "P")
91 (calc-wrapper
92 (calc-unary-op "lnot" 'calcFunc-lnot arg)))
93
94 (defun calc-logical-if ()
95 (interactive)
96 (calc-wrapper
97 (calc-enter-result 3 "if" (cons 'calcFunc-if (calc-top-list-n 3)))))
98
99
100
101
102
103 (defun calc-timing (n)
104 (interactive "P")
105 (calc-wrapper
106 (calc-change-mode 'calc-timing n nil t)
107 (message (if calc-timing
108 "Reporting timing of slow commands in Trail"
109 "Not reporting timing of commands"))))
110
111 (defun calc-pass-errors ()
112 (interactive)
113 ;; The following two cases are for the new, optimizing byte compiler
114 ;; or the standard 18.57 byte compiler, respectively.
115 (condition-case err
116 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 15)))
117 (or (memq (car-safe (car-safe place)) '(error xxxerror))
118 (setq place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 27)))
119 (or (memq (car (car place)) '(error xxxerror))
120 (error "foo"))
121 (setcar (car place) 'xxxerror))
122 (error (error "The calc-do function has been modified; unable to patch"))))
123
124 (defun calc-user-define ()
125 (interactive)
126 (message "Define user key: z-")
127 (let ((key (read-char)))
128 (if (= (calc-user-function-classify key) 0)
129 (error "Can't redefine \"?\" key"))
130 (let ((func (intern (completing-read (concat "Set key z "
131 (char-to-string key)
132 " to command: ")
133 obarray
134 'commandp
135 t
136 "calc-"))))
137 (let* ((kmap (calc-user-key-map))
138 (old (assq key kmap)))
139 (if old
140 (setcdr old func)
141 (setcdr kmap (cons (cons key func) (cdr kmap))))))))
142
143 (defun calc-user-undefine ()
144 (interactive)
145 (message "Undefine user key: z-")
146 (let ((key (read-char)))
147 (if (= (calc-user-function-classify key) 0)
148 (error "Can't undefine \"?\" key"))
149 (let* ((kmap (calc-user-key-map)))
150 (delq (or (assq key kmap)
151 (assq (upcase key) kmap)
152 (assq (downcase key) kmap)
153 (error "No such user key is defined"))
154 kmap))))
155
156
157 ;; math-integral-cache-state is originally declared in calcalg2.el,
158 ;; it is used in calc-user-define-variable.
159 (defvar math-integral-cache-state)
160
161 ;; calc-user-formula-alist is local to calc-user-define-formula,
162 ;; calc-user-define-compostion and calc-finish-formula-edit,
163 ;; but is used by calc-fix-user-formula.
164 (defvar calc-user-formula-alist)
165
166 (defun calc-user-define-formula ()
167 (interactive)
168 (calc-wrapper
169 (let* ((form (calc-top 1))
170 (arglist nil)
171 (is-lambda (and (eq (car-safe form) 'calcFunc-lambda)
172 (>= (length form) 2)))
173 odef key keyname cmd cmd-base cmd-base-default
174 func calc-user-formula-alist is-symb)
175 (if is-lambda
176 (setq arglist (mapcar (function (lambda (x) (nth 1 x)))
177 (nreverse (cdr (reverse (cdr form)))))
178 form (nth (1- (length form)) form))
179 (calc-default-formula-arglist form)
180 (setq arglist (sort arglist 'string-lessp)))
181 (message "Define user key: z-")
182 (setq key (read-char))
183 (if (= (calc-user-function-classify key) 0)
184 (error "Can't redefine \"?\" key"))
185 (setq key (and (not (memq key '(13 32))) key)
186 keyname (and key
187 (if (or (and (<= ?0 key) (<= key ?9))
188 (and (<= ?a key) (<= key ?z))
189 (and (<= ?A key) (<= key ?Z)))
190 (char-to-string key)
191 (format "%03d" key)))
192 odef (assq key (calc-user-key-map)))
193 (unless keyname
194 (setq keyname (format "%05d" (abs (% (random) 10000)))))
195 (while
196 (progn
197 (setq cmd-base-default (concat "User-" keyname))
198 (setq cmd (completing-read
199 (concat "Define M-x command name (default: calc-"
200 cmd-base-default
201 "): ")
202 obarray 'commandp nil
203 (if (and odef (symbolp (cdr odef)))
204 (symbol-name (cdr odef))
205 "calc-")))
206 (if (or (string-equal cmd "")
207 (string-equal cmd "calc-"))
208 (setq cmd (concat "calc-User-" keyname)))
209 (setq cmd-base (and (string-match "\\`calc-\\(.+\\)\\'" cmd)
210 (math-match-substring cmd 1)))
211 (setq cmd (intern cmd))
212 (and cmd
213 (fboundp cmd)
214 odef
215 (not
216 (y-or-n-p
217 (if (get cmd 'calc-user-defn)
218 (concat "Replace previous definition for "
219 (symbol-name cmd) "? ")
220 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
221 (while
222 (progn
223 (setq cmd-base-default
224 (if cmd-base
225 (if (string-match
226 "\\`User-.+" cmd-base)
227 (concat
228 "User"
229 (substring cmd-base 5))
230 cmd-base)
231 (concat "User" keyname)))
232 (setq func
233 (concat "calcFunc-"
234 (completing-read
235 (concat "Define algebraic function name (default: "
236 cmd-base-default "): ")
237 (mapcar (lambda (x) (substring x 9))
238 (all-completions "calcFunc-"
239 obarray))
240 (lambda (x)
241 (fboundp
242 (intern (concat "calcFunc-" x))))
243 nil)))
244 (setq func
245 (if (string-equal func "calcFunc-")
246 (intern (concat "calcFunc-" cmd-base-default))
247 (intern func)))
248 (and func
249 (fboundp func)
250 (not (fboundp cmd))
251 odef
252 (not
253 (y-or-n-p
254 (if (get func 'calc-user-defn)
255 (concat "Replace previous definition for "
256 (symbol-name func) "? ")
257 "That name conflicts with a built-in Emacs function. Replace this function? "))))))
258
259 (if (not func)
260 (setq func (intern (concat "calcFunc-User"
261 (or keyname
262 (and cmd (symbol-name cmd))
263 (format "%05d" (% (random) 10000)))))))
264
265 (if is-lambda
266 (setq calc-user-formula-alist arglist)
267 (while
268 (progn
269 (setq calc-user-formula-alist
270 (read-from-minibuffer "Function argument list: "
271 (if arglist
272 (prin1-to-string arglist)
273 "()")
274 minibuffer-local-map
275 t))
276 (and (not (calc-subsetp calc-user-formula-alist arglist))
277 (not (y-or-n-p
278 "Okay for arguments that don't appear in formula to be ignored? "))))))
279 (setq is-symb (and calc-user-formula-alist
280 func
281 (y-or-n-p
282 "Leave it symbolic for non-constant arguments? ")))
283 (setq calc-user-formula-alist
284 (mapcar (function (lambda (x)
285 (or (cdr (assq x '((nil . arg-nil)
286 (t . arg-t))))
287 x))) calc-user-formula-alist))
288 (if cmd
289 (progn
290 (require 'calc-macs)
291 (fset cmd
292 (list 'lambda
293 '()
294 '(interactive)
295 (list 'calc-wrapper
296 (list 'calc-enter-result
297 (length calc-user-formula-alist)
298 (let ((name (symbol-name (or func cmd))))
299 (and (string-match
300 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
301 name)
302 (math-match-substring name 1)))
303 (list 'cons
304 (list 'quote func)
305 (list 'calc-top-list-n
306 (length calc-user-formula-alist)))))))
307 (put cmd 'calc-user-defn t)))
308 (let ((body (list 'math-normalize (calc-fix-user-formula form))))
309 (fset func
310 (append
311 (list 'lambda calc-user-formula-alist)
312 (and is-symb
313 (mapcar (function (lambda (v)
314 (list 'math-check-const v t)))
315 calc-user-formula-alist))
316 (list body))))
317 (put func 'calc-user-defn form)
318 (setq math-integral-cache-state nil)
319 (if key
320 (let* ((kmap (calc-user-key-map))
321 (old (assq key kmap)))
322 (if old
323 (setcdr old cmd)
324 (setcdr kmap (cons (cons key cmd) (cdr kmap)))))))
325 (message "")))
326
327 (defun calc-default-formula-arglist (form)
328 (if (consp form)
329 (if (eq (car form) 'var)
330 (if (or (memq (nth 1 form) arglist)
331 (math-const-var form))
332 ()
333 (setq arglist (cons (nth 1 form) arglist)))
334 (calc-default-formula-arglist-step (cdr form)))))
335
336 (defun calc-default-formula-arglist-step (l)
337 (and l
338 (progn
339 (calc-default-formula-arglist (car l))
340 (calc-default-formula-arglist-step (cdr l)))))
341
342 (defun calc-subsetp (a b)
343 (or (null a)
344 (and (memq (car a) b)
345 (calc-subsetp (cdr a) b))))
346
347 (defun calc-fix-user-formula (f)
348 (if (consp f)
349 (let (temp)
350 (cond ((and (eq (car f) 'var)
351 (memq (setq temp (or (cdr (assq (nth 1 f) '((nil . arg-nil)
352 (t . arg-t))))
353 (nth 1 f)))
354 calc-user-formula-alist))
355 temp)
356 ((or (math-constp f) (eq (car f) 'var))
357 (list 'quote f))
358 ((and (eq (car f) 'calcFunc-eval)
359 (= (length f) 2))
360 (list 'let '((calc-simplify-mode nil))
361 (list 'math-normalize (calc-fix-user-formula (nth 1 f)))))
362 ((and (eq (car f) 'calcFunc-evalsimp)
363 (= (length f) 2))
364 (list 'math-simplify (calc-fix-user-formula (nth 1 f))))
365 ((and (eq (car f) 'calcFunc-evalextsimp)
366 (= (length f) 2))
367 (list 'math-simplify-extended
368 (calc-fix-user-formula (nth 1 f))))
369 (t
370 (cons 'list
371 (cons (list 'quote (car f))
372 (mapcar 'calc-fix-user-formula (cdr f)))))))
373 f))
374
375 (defun calc-user-define-composition ()
376 (interactive)
377 (calc-wrapper
378 (if (eq calc-language 'unform)
379 (error "Can't define formats for unformatted mode"))
380 (let* ((comp (calc-top 1))
381 (func (intern
382 (concat "calcFunc-"
383 (completing-read "Define format for which function: "
384 (mapcar (lambda (x) (substring x 9))
385 (all-completions "calcFunc-"
386 obarray))
387 (lambda (x)
388 (fboundp
389 (intern (concat "calcFunc-" x))))))))
390 (comps (get func 'math-compose-forms))
391 entry entry2
392 (arglist nil)
393 (calc-user-formula-alist nil))
394 (if (math-zerop comp)
395 (if (setq entry (assq calc-language comps))
396 (put func 'math-compose-forms (delq entry comps)))
397 (calc-default-formula-arglist comp)
398 (setq arglist (sort arglist 'string-lessp))
399 (while
400 (progn
401 (setq calc-user-formula-alist
402 (read-from-minibuffer "Composition argument list: "
403 (if arglist
404 (prin1-to-string arglist)
405 "()")
406 minibuffer-local-map
407 t))
408 (and (not (calc-subsetp calc-user-formula-alist arglist))
409 (y-or-n-p
410 "Okay for arguments that don't appear in formula to be invisible? "))))
411 (or (setq entry (assq calc-language comps))
412 (put func 'math-compose-forms
413 (cons (setq entry (list calc-language)) comps)))
414 (or (setq entry2 (assq (length calc-user-formula-alist) (cdr entry)))
415 (setcdr entry
416 (cons (setq entry2
417 (list (length calc-user-formula-alist))) (cdr entry))))
418 (setcdr entry2
419 (list 'lambda calc-user-formula-alist (calc-fix-user-formula comp))))
420 (calc-pop-stack 1)
421 (calc-do-refresh))))
422
423
424 (defun calc-user-define-kbd-macro (arg)
425 (interactive "P")
426 (or last-kbd-macro
427 (error "No keyboard macro defined"))
428 (message "Define last kbd macro on user key: z-")
429 (let ((key (read-char)))
430 (if (= (calc-user-function-classify key) 0)
431 (error "Can't redefine \"?\" key"))
432 (let ((cmd (intern (completing-read "Full name for new command: "
433 obarray
434 'commandp
435 nil
436 (concat "calc-User-"
437 (if (or (and (>= key ?a)
438 (<= key ?z))
439 (and (>= key ?A)
440 (<= key ?Z))
441 (and (>= key ?0)
442 (<= key ?9)))
443 (char-to-string key)
444 (format "%03d" key)))))))
445 (and (fboundp cmd)
446 (not (let ((f (symbol-function cmd)))
447 (or (stringp f)
448 (and (consp f)
449 (eq (car-safe (nth 3 f))
450 'calc-execute-kbd-macro)))))
451 (error "Function %s is already defined and not a keyboard macro"
452 cmd))
453 (put cmd 'calc-user-defn t)
454 (fset cmd (if (< (prefix-numeric-value arg) 0)
455 last-kbd-macro
456 (list 'lambda
457 '(arg)
458 '(interactive "P")
459 (list 'calc-execute-kbd-macro
460 (vector (key-description last-kbd-macro)
461 last-kbd-macro)
462 'arg
463 (format "z%c" key)))))
464 (let* ((kmap (calc-user-key-map))
465 (old (assq key kmap)))
466 (if old
467 (setcdr old cmd)
468 (setcdr kmap (cons (cons key cmd) (cdr kmap))))))))
469
470
471 (defun calc-edit-user-syntax ()
472 (interactive)
473 (calc-wrapper
474 (let ((lang calc-language))
475 (calc-edit-mode (list 'calc-finish-user-syntax-edit (list 'quote lang))
476 t
477 (format "Editing %s-Mode Syntax Table. "
478 (cond ((null lang) "Normal")
479 ((eq lang 'tex) "TeX")
480 ((eq lang 'latex) "LaTeX")
481 (t (capitalize (symbol-name lang))))))
482 (calc-write-parse-table (cdr (assq lang calc-user-parse-tables))
483 lang)))
484 (calc-show-edit-buffer))
485
486 (defvar calc-original-buffer)
487
488 (defun calc-finish-user-syntax-edit (lang)
489 (let ((tab (calc-read-parse-table calc-original-buffer lang))
490 (entry (assq lang calc-user-parse-tables)))
491 (if tab
492 (setcdr (or entry
493 (car (setq calc-user-parse-tables
494 (cons (list lang) calc-user-parse-tables))))
495 tab)
496 (if entry
497 (setq calc-user-parse-tables
498 (delq entry calc-user-parse-tables)))))
499 (switch-to-buffer calc-original-buffer))
500
501 ;; The variable calc-lang is local to calc-write-parse-table, but is
502 ;; used by calc-write-parse-table-part which is called by
503 ;; calc-write-parse-table. The variable is also local to
504 ;; calc-read-parse-table, but is used by calc-fix-token-name which
505 ;; is called (indirectly) by calc-read-parse-table.
506 (defvar calc-lang)
507
508 (defun calc-write-parse-table (tab calc-lang)
509 (let ((p tab))
510 (while p
511 (calc-write-parse-table-part (car (car p)))
512 (insert ":= "
513 (let ((math-format-hash-args t))
514 (math-format-flat-expr (cdr (car p)) 0))
515 "\n")
516 (setq p (cdr p)))))
517
518 (defun calc-write-parse-table-part (p)
519 (while p
520 (cond ((stringp (car p))
521 (let ((s (car p)))
522 (if (and (string-match "\\`\\\\dots\\>" s)
523 (not (memq calc-lang '(tex latex))))
524 (setq s (concat ".." (substring s 5))))
525 (if (or (and (string-match
526 "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s)
527 (string-match "[^a-zA-Z0-9\\]" s))
528 (and (assoc s '((")") ("]") (">")))
529 (not (cdr p))))
530 (insert (prin1-to-string s) " ")
531 (insert s " "))))
532 ((integerp (car p))
533 (insert "#")
534 (or (= (car p) 0)
535 (insert "/" (int-to-string (car p))))
536 (insert " "))
537 ((and (eq (car (car p)) '\?) (equal (car (nth 2 (car p))) "$$"))
538 (insert (car (nth 1 (car p))) " "))
539 (t
540 (insert "{ ")
541 (calc-write-parse-table-part (nth 1 (car p)))
542 (insert "}" (symbol-name (car (car p))))
543 (if (nth 2 (car p))
544 (calc-write-parse-table-part (list (car (nth 2 (car p)))))
545 (insert " "))))
546 (setq p (cdr p))))
547
548 (defun calc-read-parse-table (calc-buf calc-lang)
549 (let ((tab nil))
550 (while (progn
551 (skip-chars-forward "\n\t ")
552 (not (eobp)))
553 (if (looking-at "%%")
554 (end-of-line)
555 (let ((pt (point))
556 (p (calc-read-parse-table-part ":=[\n\t ]+" ":=")))
557 (or (stringp (car p))
558 (and (integerp (car p))
559 (stringp (nth 1 p)))
560 (progn
561 (goto-char pt)
562 (error "Malformed syntax rule")))
563 (let ((pos (point)))
564 (end-of-line)
565 (let* ((str (buffer-substring pos (point)))
566 (exp (save-excursion
567 (set-buffer calc-buf)
568 (let ((calc-user-parse-tables nil)
569 (calc-language nil)
570 (math-expr-opers math-standard-opers)
571 (calc-hashes-used 0))
572 (math-read-expr
573 (if (string-match ",[ \t]*\\'" str)
574 (substring str 0 (match-beginning 0))
575 str))))))
576 (if (eq (car-safe exp) 'error)
577 (progn
578 (goto-char (+ pos (nth 1 exp)))
579 (error (nth 2 exp))))
580 (setq tab (nconc tab (list (cons p exp)))))))))
581 tab))
582
583 (defun calc-fix-token-name (name &optional unquoted)
584 (cond ((string-match "\\`\\.\\." name)
585 (concat "\\dots" (substring name 2)))
586 ((and (equal name "{") (memq calc-lang '(tex latex eqn)))
587 "(")
588 ((and (equal name "}") (memq calc-lang '(tex latex eqn)))
589 ")")
590 ((and (equal name "&") (memq calc-lang '(tex latex)))
591 ",")
592 ((equal name "#")
593 (search-backward "#")
594 (error "Token '#' is reserved"))
595 ((and unquoted (string-match "#" name))
596 (error "Tokens containing '#' must be quoted"))
597 ((not (string-match "[^ ]" name))
598 (search-backward "\"" nil t)
599 (error "Blank tokens are not allowed"))
600 (t name)))
601
602 (defun calc-read-parse-table-part (term eterm)
603 (let ((part nil)
604 (quoted nil))
605 (while (progn
606 (skip-chars-forward "\n\t ")
607 (if (eobp) (error "Expected '%s'" eterm))
608 (not (looking-at term)))
609 (cond ((looking-at "%%")
610 (end-of-line))
611 ((looking-at "{[\n\t ]")
612 (forward-char 2)
613 (let ((p (calc-read-parse-table-part "}" "}")))
614 (or (looking-at "[+*?]")
615 (error "Expected '+', '*', or '?'"))
616 (let ((sym (intern (buffer-substring (point) (1+ (point))))))
617 (forward-char 1)
618 (looking-at "[^\n\t ]*")
619 (let ((sep (buffer-substring (point) (match-end 0))))
620 (goto-char (match-end 0))
621 (and (eq sym '\?) (> (length sep) 0)
622 (not (equal sep "$")) (not (equal sep "."))
623 (error "Separator not allowed with { ... }?"))
624 (if (string-match "\\`\"" sep)
625 (setq sep (read-from-string sep)))
626 (setq sep (calc-fix-token-name sep))
627 (setq part (nconc part
628 (list (list sym p
629 (and (> (length sep) 0)
630 (cons sep p))))))))))
631 ((looking-at "}")
632 (error "Too many }'s"))
633 ((looking-at "\"")
634 (setq quoted (calc-fix-token-name (read (current-buffer)))
635 part (nconc part (list quoted))))
636 ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]")
637 (setq part (nconc part (list (if (= (match-beginning 1)
638 (match-end 1))
639 0
640 (string-to-int
641 (buffer-substring
642 (1+ (match-beginning 1))
643 (match-end 1)))))))
644 (goto-char (match-end 0)))
645 ((looking-at ":=[\n\t ]")
646 (error "Misplaced ':='"))
647 (t
648 (looking-at "[^\n\t ]*")
649 (let ((end (match-end 0)))
650 (setq part (nconc part (list (calc-fix-token-name
651 (buffer-substring
652 (point) end) t))))
653 (goto-char end)))))
654 (goto-char (match-end 0))
655 (let ((len (length part)))
656 (while (and (> len 1)
657 (let ((last (nthcdr (setq len (1- len)) part)))
658 (and (assoc (car last) '((")") ("]") (">")))
659 (not (eq (car last) quoted))
660 (setcar last
661 (list '\? (list (car last)) '("$$"))))))))
662 part))
663
664 (defun calc-user-define-invocation ()
665 (interactive)
666 (or last-kbd-macro
667 (error "No keyboard macro defined"))
668 (setq calc-invocation-macro last-kbd-macro)
669 (message "Use `M-# Z' to invoke this macro"))
670
671 (defun calc-user-define-edit ()
672 (interactive) ; but no calc-wrapper!
673 (message "Edit definition of command: z-")
674 (let* (cmdname
675 (key (read-char))
676 (def (or (assq key (calc-user-key-map))
677 (assq (upcase key) (calc-user-key-map))
678 (assq (downcase key) (calc-user-key-map))
679 (error "No command defined for that key")))
680 (cmd (cdr def)))
681 (when (symbolp cmd)
682 (setq cmdname (symbol-name cmd))
683 (setq cmd (symbol-function cmd)))
684 (cond ((or (stringp cmd)
685 (and (consp cmd)
686 (eq (car-safe (nth 3 cmd)) 'calc-execute-kbd-macro)))
687 (let* ((mac (elt (nth 1 (nth 3 cmd)) 1))
688 (str (edmacro-format-keys mac t))
689 (kys (nth 3 (nth 3 cmd))))
690 (calc-edit-mode
691 (list 'calc-edit-macro-finish-edit cmdname kys)
692 t (format (concat
693 "Editing keyboard macro (%s, bound to %s).\n"
694 "Original keys: %s \n")
695 cmdname kys (elt (nth 1 (nth 3 cmd)) 0)))
696 (insert str "\n")
697 (calc-edit-format-macro-buffer)
698 (calc-show-edit-buffer)))
699 (t (let* ((func (calc-stack-command-p cmd))
700 (defn (and func
701 (symbolp func)
702 (get func 'calc-user-defn)))
703 (kys (concat "z" (char-to-string (car def))))
704 (intcmd (symbol-name (cdr def)))
705 (algcmd (substring (symbol-name func) 9)))
706 (if (and defn (calc-valid-formula-func func))
707 (let ((niceexpr (math-format-nice-expr defn (frame-width))))
708 (calc-wrapper
709 (calc-edit-mode
710 (list 'calc-finish-formula-edit (list 'quote func))
711 nil
712 (format (concat
713 "Editing formula (%s, %s, bound to %s).\n"
714 "Original formula: %s\n")
715 intcmd algcmd kys niceexpr))
716 (insert (math-showing-full-precision
717 niceexpr)
718 "\n"))
719 (calc-show-edit-buffer))
720 (error "That command's definition cannot be edited")))))))
721
722 ;; Formatting the macro buffer
723
724 (defvar calc-edit-top)
725
726 (defun calc-edit-macro-repeats ()
727 (goto-char calc-edit-top)
728 (while
729 (re-search-forward "^\\([0-9]+\\)\\*" nil t)
730 (let ((num (string-to-int (match-string 1)))
731 (line (buffer-substring (point) (line-end-position))))
732 (goto-char (line-beginning-position))
733 (kill-line 1)
734 (while (> num 0)
735 (insert line "\n")
736 (setq num (1- num))))))
737
738 (defun calc-edit-macro-adjust-buffer ()
739 (calc-edit-macro-repeats)
740 (goto-char calc-edit-top)
741 (while (re-search-forward "^RET$" nil t)
742 (delete-char 1))
743 (goto-char calc-edit-top)
744 (while (and (re-search-forward "^$" nil t)
745 (not (= (point) (point-max))))
746 (delete-char 1)))
747
748 (defun calc-edit-macro-command ()
749 "Return the command on the current line in a Calc macro editing buffer."
750 (let ((beg (line-beginning-position))
751 (end (save-excursion
752 (if (search-forward ";;" (line-end-position) 1)
753 (forward-char -2))
754 (skip-chars-backward " \t")
755 (point))))
756 (buffer-substring beg end)))
757
758 (defun calc-edit-macro-command-type ()
759 "Return the type of command on the current line in a Calc macro editing buffer."
760 (let ((beg (save-excursion
761 (if (search-forward ";;" (line-end-position) t)
762 (progn
763 (skip-chars-forward " \t")
764 (point)))))
765 (end (save-excursion
766 (goto-char (line-end-position))
767 (skip-chars-backward " \t")
768 (point))))
769 (if beg
770 (buffer-substring beg end)
771 "")))
772
773 (defun calc-edit-macro-combine-alg-ent ()
774 "Put an entire algebraic entry on a single line."
775 (let ((line (calc-edit-macro-command))
776 (type (calc-edit-macro-command-type))
777 curline
778 match)
779 (goto-char (line-beginning-position))
780 (kill-line 1)
781 (setq curline (calc-edit-macro-command))
782 (while (and curline
783 (not (string-equal "RET" curline))
784 (not (setq match (string-match "<return>" curline))))
785 (setq line (concat line curline))
786 (kill-line 1)
787 (setq curline (calc-edit-macro-command)))
788 (when match
789 (kill-line 1)
790 (setq line (concat line (substring curline 0 match))))
791 (setq line (replace-regexp-in-string "SPC" " SPC "
792 (replace-regexp-in-string " " "" line)))
793 (insert line "\t\t\t")
794 (if (> (current-column) 24)
795 (delete-char -1))
796 (insert ";; " type "\n")
797 (if match
798 (insert "RET\t\t\t;; calc-enter\n"))))
799
800 (defun calc-edit-macro-combine-ext-command ()
801 "Put an entire extended command on a single line."
802 (let ((cmdbeg (calc-edit-macro-command))
803 (line "")
804 (type (calc-edit-macro-command-type))
805 curline
806 match)
807 (goto-char (line-beginning-position))
808 (kill-line 1)
809 (setq curline (calc-edit-macro-command))
810 (while (and curline
811 (not (string-equal "RET" curline))
812 (not (setq match (string-match "<return>" curline))))
813 (setq line (concat line curline))
814 (kill-line 1)
815 (setq curline (calc-edit-macro-command)))
816 (when match
817 (kill-line 1)
818 (setq line (concat line (substring curline 0 match))))
819 (setq line (replace-regexp-in-string " " "" line))
820 (insert cmdbeg " " line "\t\t\t")
821 (if (> (current-column) 24)
822 (delete-char -1))
823 (insert ";; " type "\n")
824 (if match
825 (insert "RET\t\t\t;; calc-enter\n"))))
826
827 (defun calc-edit-macro-combine-var-name ()
828 "Put an entire variable name on a single line."
829 (let ((line (calc-edit-macro-command))
830 curline
831 match)
832 (goto-char (line-beginning-position))
833 (kill-line 1)
834 (if (member line '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
835 (insert line "\t\t\t;; calc quick variable\n")
836 (setq curline (calc-edit-macro-command))
837 (while (and curline
838 (not (string-equal "RET" curline))
839 (not (setq match (string-match "<return>" curline))))
840 (setq line (concat line curline))
841 (kill-line 1)
842 (setq curline (calc-edit-macro-command)))
843 (when match
844 (kill-line 1)
845 (setq line (concat line (substring curline 0 match))))
846 (setq line (replace-regexp-in-string " " "" line))
847 (insert line "\t\t\t")
848 (if (> (current-column) 24)
849 (delete-char -1))
850 (insert ";; calc variable\n")
851 (if match
852 (insert "RET\t\t\t;; calc-enter\n")))))
853
854 (defun calc-edit-macro-combine-digits ()
855 "Put an entire sequence of digits on a single line."
856 (let ((line (calc-edit-macro-command))
857 curline)
858 (goto-char (line-beginning-position))
859 (kill-line 1)
860 (while (string-equal (calc-edit-macro-command-type) "calcDigit-start")
861 (setq line (concat line (calc-edit-macro-command)))
862 (kill-line 1))
863 (insert line "\t\t\t")
864 (if (> (current-column) 24)
865 (delete-char -1))
866 (insert ";; calc digits\n")))
867
868 (defun calc-edit-format-macro-buffer ()
869 "Rewrite the Calc macro editing buffer."
870 (calc-edit-macro-adjust-buffer)
871 (goto-char calc-edit-top)
872 (let ((type (calc-edit-macro-command-type)))
873 (while (not (string-equal type ""))
874 (cond
875 ((or
876 (string-equal type "calc-algebraic-entry")
877 (string-equal type "calc-auto-algebraic-entry"))
878 (calc-edit-macro-combine-alg-ent))
879 ((string-equal type "calc-execute-extended-command")
880 (calc-edit-macro-combine-ext-command))
881 ((string-equal type "calcDigit-start")
882 (calc-edit-macro-combine-digits))
883 ((or
884 (string-equal type "calc-store")
885 (string-equal type "calc-store-into")
886 (string-equal type "calc-store-neg")
887 (string-equal type "calc-store-plus")
888 (string-equal type "calc-store-minus")
889 (string-equal type "calc-store-div")
890 (string-equal type "calc-store-times")
891 (string-equal type "calc-store-power")
892 (string-equal type "calc-store-concat")
893 (string-equal type "calc-store-inv")
894 (string-equal type "calc-store-dec")
895 (string-equal type "calc-store-incr")
896 (string-equal type "calc-store-exchange")
897 (string-equal type "calc-unstore")
898 (string-equal type "calc-recall")
899 (string-equal type "calc-let")
900 (string-equal type "calc-permanent-variable"))
901 (forward-line 1)
902 (calc-edit-macro-combine-var-name))
903 ((or
904 (string-equal type "calc-copy-variable")
905 (string-equal type "calc-declare-variable"))
906 (forward-line 1)
907 (calc-edit-macro-combine-var-name)
908 (calc-edit-macro-combine-var-name))
909 (t (forward-line 1)))
910 (setq type (calc-edit-macro-command-type))))
911 (goto-char calc-edit-top))
912
913 ;; Finish editing the macro
914
915 (defun calc-edit-macro-pre-finish-edit ()
916 (goto-char calc-edit-top)
917 (while (re-search-forward "\\(^\\| \\)RET\\($\\|\t\\| \\)" nil t)
918 (search-backward "RET")
919 (delete-char 3)
920 (insert "<return>")))
921
922 (defun calc-edit-macro-finish-edit (cmdname key)
923 "Finish editing a Calc macro.
924 Redefine the corresponding command."
925 (interactive)
926 (let ((cmd (intern cmdname)))
927 (calc-edit-macro-pre-finish-edit)
928 (let* ((str (buffer-substring calc-edit-top (point-max)))
929 (mac (edmacro-parse-keys str t)))
930 (if (= (length mac) 0)
931 (fmakunbound cmd)
932 (fset cmd
933 (list 'lambda '(arg)
934 '(interactive "P")
935 (list 'calc-execute-kbd-macro
936 (vector (key-description mac)
937 mac)
938 'arg key)))))))
939
940 (defun calc-finish-formula-edit (func)
941 (let ((buf (current-buffer))
942 (str (buffer-substring calc-edit-top (point-max)))
943 (start (point))
944 (body (calc-valid-formula-func func)))
945 (set-buffer calc-original-buffer)
946 (let ((val (math-read-expr str)))
947 (if (eq (car-safe val) 'error)
948 (progn
949 (set-buffer buf)
950 (goto-char (+ start (nth 1 val)))
951 (error (nth 2 val))))
952 (setcar (cdr body)
953 (let ((calc-user-formula-alist (nth 1 (symbol-function func))))
954 (calc-fix-user-formula val)))
955 (put func 'calc-user-defn val))))
956
957 (defun calc-valid-formula-func (func)
958 (let ((def (symbol-function func)))
959 (and (consp def)
960 (eq (car def) 'lambda)
961 (progn
962 (setq def (cdr (cdr def)))
963 (while (and def
964 (not (eq (car (car def)) 'math-normalize)))
965 (setq def (cdr def)))
966 (car def)))))
967
968
969 (defun calc-get-user-defn ()
970 (interactive)
971 (calc-wrapper
972 (message "Get definition of command: z-")
973 (let* ((key (read-char))
974 (def (or (assq key (calc-user-key-map))
975 (assq (upcase key) (calc-user-key-map))
976 (assq (downcase key) (calc-user-key-map))
977 (error "No command defined for that key")))
978 (cmd (cdr def)))
979 (if (symbolp cmd)
980 (setq cmd (symbol-function cmd)))
981 (cond ((stringp cmd)
982 (message "Keyboard macro: %s" cmd))
983 (t (let* ((func (calc-stack-command-p cmd))
984 (defn (and func
985 (symbolp func)
986 (get func 'calc-user-defn))))
987 (if defn
988 (progn
989 (and (calc-valid-formula-func func)
990 (setq defn (append '(calcFunc-lambda)
991 (mapcar 'math-build-var-name
992 (nth 1 (symbol-function
993 func)))
994 (list defn))))
995 (calc-enter-result 0 "gdef" defn))
996 (error "That command is not defined by a formula"))))))))
997
998
999 (defun calc-user-define-permanent ()
1000 (interactive)
1001 (calc-wrapper
1002 (message "Record in %s the command: z-" calc-settings-file)
1003 (let* ((key (read-char))
1004 (def (or (assq key (calc-user-key-map))
1005 (assq (upcase key) (calc-user-key-map))
1006 (assq (downcase key) (calc-user-key-map))
1007 (and (eq key ?\')
1008 (cons nil
1009 (intern
1010 (concat "calcFunc-"
1011 (completing-read
1012 (format "Record in %s the algebraic function: "
1013 calc-settings-file)
1014 (mapcar (lambda (x) (substring x 9))
1015 (all-completions "calcFunc-"
1016 obarray))
1017 (lambda (x)
1018 (fboundp
1019 (intern (concat "calcFunc-" x))))
1020 t)))))
1021 (and (eq key ?\M-x)
1022 (cons nil
1023 (intern (completing-read
1024 (format "Record in %s the command: "
1025 calc-settings-file)
1026 obarray 'fboundp nil "calc-"))))
1027 (error "No command defined for that key"))))
1028 (set-buffer (find-file-noselect (substitute-in-file-name
1029 calc-settings-file)))
1030 (goto-char (point-max))
1031 (let* ((cmd (cdr def))
1032 (fcmd (and cmd (symbolp cmd) (symbol-function cmd)))
1033 (func nil)
1034 (pt (point))
1035 (fill-column 70)
1036 (fill-prefix nil)
1037 str q-ok)
1038 (insert "\n;;; Definition stored by Calc on " (current-time-string)
1039 "\n(put 'calc-define '"
1040 (if (symbolp cmd) (symbol-name cmd) (format "key%d" key))
1041 " '(progn\n")
1042 (if (and fcmd
1043 (eq (car-safe fcmd) 'lambda)
1044 (get cmd 'calc-user-defn))
1045 (let ((pt (point)))
1046 (and (eq (car-safe (nth 3 fcmd)) 'calc-execute-kbd-macro)
1047 (vectorp (nth 1 (nth 3 fcmd)))
1048 (progn (and (fboundp 'edit-kbd-macro)
1049 (edit-kbd-macro nil))
1050 (fboundp 'edmacro-parse-keys))
1051 (setq q-ok t)
1052 (aset (nth 1 (nth 3 fcmd)) 1 nil))
1053 (insert (setq str (prin1-to-string
1054 (cons 'defun (cons cmd (cdr fcmd)))))
1055 "\n")
1056 (or (and (string-match "\"" str) (not q-ok))
1057 (fill-region pt (point)))
1058 (indent-rigidly pt (point) 2)
1059 (delete-region pt (1+ pt))
1060 (insert " (put '" (symbol-name cmd)
1061 " 'calc-user-defn '"
1062 (prin1-to-string (get cmd 'calc-user-defn))
1063 ")\n")
1064 (setq func (calc-stack-command-p cmd))
1065 (let ((ffunc (and func (symbolp func) (symbol-function func)))
1066 (pt (point)))
1067 (and ffunc
1068 (eq (car-safe ffunc) 'lambda)
1069 (get func 'calc-user-defn)
1070 (progn
1071 (insert (setq str (prin1-to-string
1072 (cons 'defun (cons func
1073 (cdr ffunc)))))
1074 "\n")
1075 (or (and (string-match "\"" str) (not q-ok))
1076 (fill-region pt (point)))
1077 (indent-rigidly pt (point) 2)
1078 (delete-region pt (1+ pt))
1079 (setq pt (point))
1080 (insert "(put '" (symbol-name func)
1081 " 'calc-user-defn '"
1082 (prin1-to-string (get func 'calc-user-defn))
1083 ")\n")
1084 (fill-region pt (point))
1085 (indent-rigidly pt (point) 2)
1086 (delete-region pt (1+ pt))))))
1087 (and (stringp fcmd)
1088 (insert " (fset '" (prin1-to-string cmd)
1089 " " (prin1-to-string fcmd) ")\n")))
1090 (or func (setq func (and cmd (symbolp cmd) (fboundp cmd) cmd)))
1091 (if (get func 'math-compose-forms)
1092 (let ((pt (point)))
1093 (insert "(put '" (symbol-name cmd)
1094 " 'math-compose-forms '"
1095 (prin1-to-string (get func 'math-compose-forms))
1096 ")\n")
1097 (fill-region pt (point))
1098 (indent-rigidly pt (point) 2)
1099 (delete-region pt (1+ pt))))
1100 (if (car def)
1101 (insert " (define-key calc-mode-map "
1102 (prin1-to-string (concat "z" (char-to-string key)))
1103 " '"
1104 (prin1-to-string cmd)
1105 ")\n")))
1106 (insert "))\n")
1107 (save-buffer))))
1108
1109 (defun calc-stack-command-p (cmd)
1110 (if (and cmd (symbolp cmd))
1111 (and (fboundp cmd)
1112 (calc-stack-command-p (symbol-function cmd)))
1113 (and (consp cmd)
1114 (eq (car cmd) 'lambda)
1115 (setq cmd (or (assq 'calc-wrapper cmd)
1116 (assq 'calc-slow-wrapper cmd)))
1117 (setq cmd (assq 'calc-enter-result cmd))
1118 (memq (car (nth 3 cmd)) '(cons list))
1119 (eq (car (nth 1 (nth 3 cmd))) 'quote)
1120 (nth 1 (nth 1 (nth 3 cmd))))))
1121
1122
1123 (defun calc-call-last-kbd-macro (arg)
1124 (interactive "P")
1125 (and defining-kbd-macro
1126 (error "Can't execute anonymous macro while defining one"))
1127 (or last-kbd-macro
1128 (error "No kbd macro has been defined"))
1129 (calc-execute-kbd-macro last-kbd-macro arg))
1130
1131 (defun calc-execute-kbd-macro (mac arg &rest prefix)
1132 (if calc-keep-args-flag
1133 (calc-keep-args))
1134 (if (and (vectorp mac) (> (length mac) 0) (stringp (aref mac 0)))
1135 (setq mac (or (aref mac 1)
1136 (aset mac 1 (progn (and (fboundp 'edit-kbd-macro)
1137 (edit-kbd-macro nil))
1138 (edmacro-parse-keys (aref mac 0)))))))
1139 (if (< (prefix-numeric-value arg) 0)
1140 (execute-kbd-macro mac (- (prefix-numeric-value arg)))
1141 (if calc-executing-macro
1142 (execute-kbd-macro mac arg)
1143 (calc-slow-wrapper
1144 (let ((old-stack-whole (copy-sequence calc-stack))
1145 (old-stack-top calc-stack-top)
1146 (old-buffer-size (buffer-size))
1147 (old-refresh-count calc-refresh-count))
1148 (unwind-protect
1149 (let ((calc-executing-macro mac))
1150 (execute-kbd-macro mac arg))
1151 (calc-select-buffer)
1152 (let ((new-stack (reverse calc-stack))
1153 (old-stack (reverse old-stack-whole)))
1154 (while (and new-stack old-stack
1155 (equal (car new-stack) (car old-stack)))
1156 (setq new-stack (cdr new-stack)
1157 old-stack (cdr old-stack)))
1158 (or (equal prefix '(nil))
1159 (calc-record-list (if (> (length new-stack) 1)
1160 (mapcar 'car new-stack)
1161 '(""))
1162 (or (car prefix) "kmac")))
1163 (calc-record-undo (list 'set 'saved-stack-top old-stack-top))
1164 (and old-stack
1165 (calc-record-undo (list 'pop 1 (mapcar 'car old-stack))))
1166 (let ((calc-stack old-stack-whole)
1167 (calc-stack-top 0))
1168 (calc-cursor-stack-index (length old-stack)))
1169 (if (and (= old-buffer-size (buffer-size))
1170 (= old-refresh-count calc-refresh-count))
1171 (let ((buffer-read-only nil))
1172 (delete-region (point) (point-max))
1173 (while new-stack
1174 (calc-record-undo (list 'push 1))
1175 (insert (math-format-stack-value (car new-stack)) "\n")
1176 (setq new-stack (cdr new-stack)))
1177 (calc-renumber-stack))
1178 (while new-stack
1179 (calc-record-undo (list 'push 1))
1180 (setq new-stack (cdr new-stack)))
1181 (calc-refresh))
1182 (calc-record-undo (list 'set 'saved-stack-top 0)))))))))
1183
1184 (defun calc-push-list-in-macro (vals m sels)
1185 (let ((entry (list (car vals) 1 (car sels)))
1186 (mm (+ (or m 1) calc-stack-top)))
1187 (if (> mm 1)
1188 (setcdr (nthcdr (- mm 2) calc-stack)
1189 (cons entry (nthcdr (1- mm) calc-stack)))
1190 (setq calc-stack (cons entry calc-stack)))))
1191
1192 (defun calc-pop-stack-in-macro (n mm)
1193 (if (> mm 1)
1194 (setcdr (nthcdr (- mm 2) calc-stack)
1195 (nthcdr (+ n mm -1) calc-stack))
1196 (setq calc-stack (nthcdr n calc-stack))))
1197
1198
1199 (defun calc-kbd-if ()
1200 (interactive)
1201 (calc-wrapper
1202 (let ((cond (calc-top-n 1)))
1203 (calc-pop-stack 1)
1204 (if (math-is-true cond)
1205 (if defining-kbd-macro
1206 (message "If true.."))
1207 (if defining-kbd-macro
1208 (message "Condition is false; skipping to Z: or Z] ..."))
1209 (calc-kbd-skip-to-else-if t)))))
1210
1211 (defun calc-kbd-else-if ()
1212 (interactive)
1213 (calc-kbd-if))
1214
1215 (defun calc-kbd-skip-to-else-if (else-okay)
1216 (let ((count 0)
1217 ch)
1218 (while (>= count 0)
1219 (setq ch (read-char))
1220 (if (= ch -1)
1221 (error "Unterminated Z[ in keyboard macro"))
1222 (if (= ch ?Z)
1223 (progn
1224 (setq ch (read-char))
1225 (cond ((= ch ?\[)
1226 (setq count (1+ count)))
1227 ((= ch ?\])
1228 (setq count (1- count)))
1229 ((= ch ?\:)
1230 (and (= count 0)
1231 else-okay
1232 (setq count -1)))
1233 ((eq ch 7)
1234 (keyboard-quit))))))
1235 (and defining-kbd-macro
1236 (if (= ch ?\:)
1237 (message "Else...")
1238 (message "End-if...")))))
1239
1240 (defun calc-kbd-end-if ()
1241 (interactive)
1242 (if defining-kbd-macro
1243 (message "End-if...")))
1244
1245 (defun calc-kbd-else ()
1246 (interactive)
1247 (if defining-kbd-macro
1248 (message "Else; skipping to Z] ..."))
1249 (calc-kbd-skip-to-else-if nil))
1250
1251
1252 (defun calc-kbd-repeat ()
1253 (interactive)
1254 (let (count)
1255 (calc-wrapper
1256 (setq count (math-trunc (calc-top-n 1)))
1257 (or (Math-integerp count)
1258 (error "Count must be an integer"))
1259 (if (Math-integer-negp count)
1260 (setq count 0))
1261 (or (integerp count)
1262 (setq count 1000000))
1263 (calc-pop-stack 1))
1264 (calc-kbd-loop count)))
1265
1266 (defun calc-kbd-for (dir)
1267 (interactive "P")
1268 (let (init final)
1269 (calc-wrapper
1270 (setq init (calc-top-n 2)
1271 final (calc-top-n 1))
1272 (or (and (math-anglep init) (math-anglep final))
1273 (error "Initial and final values must be real numbers"))
1274 (calc-pop-stack 2))
1275 (calc-kbd-loop nil init final (and dir (prefix-numeric-value dir)))))
1276
1277 (defun calc-kbd-loop (rpt-count &optional initial final dir)
1278 (interactive "P")
1279 (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000))
1280 (let* ((count 0)
1281 (parts nil)
1282 (body "")
1283 (open last-command-char)
1284 (counter initial)
1285 ch)
1286 (or executing-kbd-macro
1287 (message "Reading loop body..."))
1288 (while (>= count 0)
1289 (setq ch (read-char))
1290 (if (= ch -1)
1291 (error "Unterminated Z%c in keyboard macro" open))
1292 (if (= ch ?Z)
1293 (progn
1294 (setq ch (read-char)
1295 body (concat body "Z" (char-to-string ch)))
1296 (cond ((memq ch '(?\< ?\( ?\{))
1297 (setq count (1+ count)))
1298 ((memq ch '(?\> ?\) ?\}))
1299 (setq count (1- count)))
1300 ((and (= ch ?/)
1301 (= count 0))
1302 (setq parts (nconc parts (list (concat (substring body 0 -2)
1303 "Z]")))
1304 body ""))
1305 ((eq ch 7)
1306 (keyboard-quit))))
1307 (setq body (concat body (char-to-string ch)))))
1308 (if (/= ch (cdr (assq open '( (?\< . ?\>) (?\( . ?\)) (?\{ . ?\}) ))))
1309 (error "Mismatched Z%c and Z%c in keyboard macro" open ch))
1310 (or executing-kbd-macro
1311 (message "Looping..."))
1312 (setq body (concat (substring body 0 -2) "Z]"))
1313 (and (not executing-kbd-macro)
1314 (= rpt-count 1000000)
1315 (null parts)
1316 (null counter)
1317 (progn
1318 (message "Warning: Infinite loop! Not executing")
1319 (setq rpt-count 0)))
1320 (or (not initial) dir
1321 (setq dir (math-compare final initial)))
1322 (calc-wrapper
1323 (while (> rpt-count 0)
1324 (let ((part parts))
1325 (if counter
1326 (if (cond ((eq dir 0) (Math-equal final counter))
1327 ((eq dir 1) (Math-lessp final counter))
1328 ((eq dir -1) (Math-lessp counter final)))
1329 (setq rpt-count 0)
1330 (calc-push counter)))
1331 (while (and part (> rpt-count 0))
1332 (execute-kbd-macro (car part))
1333 (if (math-is-true (calc-top-n 1))
1334 (setq rpt-count 0)
1335 (setq part (cdr part)))
1336 (calc-pop-stack 1))
1337 (if (> rpt-count 0)
1338 (progn
1339 (execute-kbd-macro body)
1340 (if counter
1341 (let ((step (calc-top-n 1)))
1342 (calc-pop-stack 1)
1343 (setq counter (calcFunc-add counter step)))
1344 (setq rpt-count (1- rpt-count))))))))
1345 (or executing-kbd-macro
1346 (message "Looping...done"))))
1347
1348 (defun calc-kbd-end-repeat ()
1349 (interactive)
1350 (error "Unbalanced Z> in keyboard macro"))
1351
1352 (defun calc-kbd-end-for ()
1353 (interactive)
1354 (error "Unbalanced Z) in keyboard macro"))
1355
1356 (defun calc-kbd-end-loop ()
1357 (interactive)
1358 (error "Unbalanced Z} in keyboard macro"))
1359
1360 (defun calc-kbd-break ()
1361 (interactive)
1362 (calc-wrapper
1363 (let ((cond (calc-top-n 1)))
1364 (calc-pop-stack 1)
1365 (if (math-is-true cond)
1366 (error "Keyboard macro aborted")))))
1367
1368
1369 (defvar calc-kbd-push-level 0)
1370
1371 ;; The variables var-q0 through var-q9 are the "quick" variables.
1372 (defvar var-q0 nil)
1373 (defvar var-q1 nil)
1374 (defvar var-q2 nil)
1375 (defvar var-q3 nil)
1376 (defvar var-q4 nil)
1377 (defvar var-q5 nil)
1378 (defvar var-q6 nil)
1379 (defvar var-q7 nil)
1380 (defvar var-q8 nil)
1381 (defvar var-q9 nil)
1382
1383 (defun calc-kbd-push (arg)
1384 (interactive "P")
1385 (calc-wrapper
1386 (let* ((defs (and arg (> (prefix-numeric-value arg) 0)))
1387 (var-q0 var-q0)
1388 (var-q1 var-q1)
1389 (var-q2 var-q2)
1390 (var-q3 var-q3)
1391 (var-q4 var-q4)
1392 (var-q5 var-q5)
1393 (var-q6 var-q6)
1394 (var-q7 var-q7)
1395 (var-q8 var-q8)
1396 (var-q9 var-q9)
1397 (calc-internal-prec (if defs 12 calc-internal-prec))
1398 (calc-word-size (if defs 32 calc-word-size))
1399 (calc-angle-mode (if defs 'deg calc-angle-mode))
1400 (calc-simplify-mode (if defs nil calc-simplify-mode))
1401 (calc-algebraic-mode (if arg nil calc-algebraic-mode))
1402 (calc-incomplete-algebraic-mode (if arg nil
1403 calc-incomplete-algebraic-mode))
1404 (calc-symbolic-mode (if defs nil calc-symbolic-mode))
1405 (calc-matrix-mode (if defs nil calc-matrix-mode))
1406 (calc-prefer-frac (if defs nil calc-prefer-frac))
1407 (calc-complex-mode (if defs nil calc-complex-mode))
1408 (calc-infinite-mode (if defs nil calc-infinite-mode))
1409 (count 0)
1410 (body "")
1411 ch)
1412 (if (or executing-kbd-macro defining-kbd-macro)
1413 (progn
1414 (if defining-kbd-macro
1415 (message "Reading body..."))
1416 (while (>= count 0)
1417 (setq ch (read-char))
1418 (if (= ch -1)
1419 (error "Unterminated Z` in keyboard macro"))
1420 (if (= ch ?Z)
1421 (progn
1422 (setq ch (read-char)
1423 body (concat body "Z" (char-to-string ch)))
1424 (cond ((eq ch ?\`)
1425 (setq count (1+ count)))
1426 ((eq ch ?\')
1427 (setq count (1- count)))
1428 ((eq ch 7)
1429 (keyboard-quit))))
1430 (setq body (concat body (char-to-string ch)))))
1431 (if defining-kbd-macro
1432 (message "Reading body...done"))
1433 (let ((calc-kbd-push-level 0))
1434 (execute-kbd-macro (substring body 0 -2))))
1435 (let ((calc-kbd-push-level (1+ calc-kbd-push-level)))
1436 (message "Saving modes; type Z' to restore")
1437 (recursive-edit))))))
1438
1439 (defun calc-kbd-pop ()
1440 (interactive)
1441 (if (> calc-kbd-push-level 0)
1442 (progn
1443 (message "Mode settings restored")
1444 (exit-recursive-edit))
1445 (error "Unbalanced Z' in keyboard macro")))
1446
1447
1448 (defun calc-kbd-report (msg)
1449 (interactive "sMessage: ")
1450 (calc-wrapper
1451 (math-working msg (calc-top-n 1))))
1452
1453 (defun calc-kbd-query (msg)
1454 (interactive "sPrompt: ")
1455 (calc-wrapper
1456 (calc-alg-entry nil (and (not (equal msg "")) msg))))
1457
1458 ;;;; Logical operations.
1459
1460 (defun calcFunc-eq (a b &rest more)
1461 (if more
1462 (let* ((args (cons a (cons b (copy-sequence more))))
1463 (res 1)
1464 (p args)
1465 p2)
1466 (while (and (cdr p) (not (eq res 0)))
1467 (setq p2 p)
1468 (while (and (setq p2 (cdr p2)) (not (eq res 0)))
1469 (setq res (math-two-eq (car p) (car p2)))
1470 (if (eq res 1)
1471 (setcdr p (delq (car p2) (cdr p)))))
1472 (setq p (cdr p)))
1473 (if (eq res 0)
1474 0
1475 (if (cdr args)
1476 (cons 'calcFunc-eq args)
1477 1)))
1478 (or (math-two-eq a b)
1479 (if (and (or (math-looks-negp a) (math-zerop a))
1480 (or (math-looks-negp b) (math-zerop b)))
1481 (list 'calcFunc-eq (math-neg a) (math-neg b))
1482 (list 'calcFunc-eq a b)))))
1483
1484 (defun calcFunc-neq (a b &rest more)
1485 (if more
1486 (let* ((args (cons a (cons b more)))
1487 (res 0)
1488 (all t)
1489 (p args)
1490 p2)
1491 (while (and (cdr p) (not (eq res 1)))
1492 (setq p2 p)
1493 (while (and (setq p2 (cdr p2)) (not (eq res 1)))
1494 (setq res (math-two-eq (car p) (car p2)))
1495 (or res (setq all nil)))
1496 (setq p (cdr p)))
1497 (if (eq res 1)
1498 0
1499 (if all
1500 1
1501 (cons 'calcFunc-neq args))))
1502 (or (cdr (assq (math-two-eq a b) '((0 . 1) (1 . 0))))
1503 (if (and (or (math-looks-negp a) (math-zerop a))
1504 (or (math-looks-negp b) (math-zerop b)))
1505 (list 'calcFunc-neq (math-neg a) (math-neg b))
1506 (list 'calcFunc-neq a b)))))
1507
1508 (defun math-two-eq (a b)
1509 (if (eq (car-safe a) 'vec)
1510 (if (eq (car-safe b) 'vec)
1511 (if (= (length a) (length b))
1512 (let ((res 1))
1513 (while (and (setq a (cdr a) b (cdr b)) (not (eq res 0)))
1514 (if res
1515 (setq res (math-two-eq (car a) (car b)))
1516 (if (eq (math-two-eq (car a) (car b)) 0)
1517 (setq res 0))))
1518 res)
1519 0)
1520 (if (Math-objectp b)
1521 0
1522 nil))
1523 (if (eq (car-safe b) 'vec)
1524 (if (Math-objectp a)
1525 0
1526 nil)
1527 (let ((res (math-compare a b)))
1528 (if (= res 0)
1529 1
1530 (if (and (= res 2) (not (and (Math-scalarp a) (Math-scalarp b))))
1531 nil
1532 0))))))
1533
1534 (defun calcFunc-lt (a b)
1535 (let ((res (math-compare a b)))
1536 (if (= res -1)
1537 1
1538 (if (= res 2)
1539 (if (and (or (math-looks-negp a) (math-zerop a))
1540 (or (math-looks-negp b) (math-zerop b)))
1541 (list 'calcFunc-gt (math-neg a) (math-neg b))
1542 (list 'calcFunc-lt a b))
1543 0))))
1544
1545 (defun calcFunc-gt (a b)
1546 (let ((res (math-compare a b)))
1547 (if (= res 1)
1548 1
1549 (if (= res 2)
1550 (if (and (or (math-looks-negp a) (math-zerop a))
1551 (or (math-looks-negp b) (math-zerop b)))
1552 (list 'calcFunc-lt (math-neg a) (math-neg b))
1553 (list 'calcFunc-gt a b))
1554 0))))
1555
1556 (defun calcFunc-leq (a b)
1557 (let ((res (math-compare a b)))
1558 (if (= res 1)
1559 0
1560 (if (= res 2)
1561 (if (and (or (math-looks-negp a) (math-zerop a))
1562 (or (math-looks-negp b) (math-zerop b)))
1563 (list 'calcFunc-geq (math-neg a) (math-neg b))
1564 (list 'calcFunc-leq a b))
1565 1))))
1566
1567 (defun calcFunc-geq (a b)
1568 (let ((res (math-compare a b)))
1569 (if (= res -1)
1570 0
1571 (if (= res 2)
1572 (if (and (or (math-looks-negp a) (math-zerop a))
1573 (or (math-looks-negp b) (math-zerop b)))
1574 (list 'calcFunc-leq (math-neg a) (math-neg b))
1575 (list 'calcFunc-geq a b))
1576 1))))
1577
1578 (defun calcFunc-rmeq (a)
1579 (if (math-vectorp a)
1580 (math-map-vec 'calcFunc-rmeq a)
1581 (if (assq (car-safe a) calc-tweak-eqn-table)
1582 (if (and (eq (car-safe (nth 2 a)) 'var)
1583 (math-objectp (nth 1 a)))
1584 (nth 1 a)
1585 (nth 2 a))
1586 (if (eq (car-safe a) 'calcFunc-assign)
1587 (nth 2 a)
1588 (if (eq (car-safe a) 'calcFunc-evalto)
1589 (nth 1 a)
1590 (list 'calcFunc-rmeq a))))))
1591
1592 (defun calcFunc-land (a b)
1593 (cond ((Math-zerop a)
1594 a)
1595 ((Math-zerop b)
1596 b)
1597 ((math-is-true a)
1598 b)
1599 ((math-is-true b)
1600 a)
1601 (t (list 'calcFunc-land a b))))
1602
1603 (defun calcFunc-lor (a b)
1604 (cond ((Math-zerop a)
1605 b)
1606 ((Math-zerop b)
1607 a)
1608 ((math-is-true a)
1609 a)
1610 ((math-is-true b)
1611 b)
1612 (t (list 'calcFunc-lor a b))))
1613
1614 (defun calcFunc-lnot (a)
1615 (if (Math-zerop a)
1616 1
1617 (if (math-is-true a)
1618 0
1619 (let ((op (and (= (length a) 3)
1620 (assq (car a) calc-tweak-eqn-table))))
1621 (if op
1622 (cons (nth 2 op) (cdr a))
1623 (list 'calcFunc-lnot a))))))
1624
1625 (defun calcFunc-if (c e1 e2)
1626 (if (Math-zerop c)
1627 e2
1628 (if (and (math-is-true c) (not (Math-vectorp c)))
1629 e1
1630 (or (and (Math-vectorp c)
1631 (math-constp c)
1632 (let ((ee1 (if (Math-vectorp e1)
1633 (if (= (length c) (length e1))
1634 (cdr e1)
1635 (calc-record-why "*Dimension error" e1))
1636 (list e1)))
1637 (ee2 (if (Math-vectorp e2)
1638 (if (= (length c) (length e2))
1639 (cdr e2)
1640 (calc-record-why "*Dimension error" e2))
1641 (list e2))))
1642 (and ee1 ee2
1643 (cons 'vec (math-if-vector (cdr c) ee1 ee2)))))
1644 (list 'calcFunc-if c e1 e2)))))
1645
1646 (defun math-if-vector (c e1 e2)
1647 (and c
1648 (cons (if (Math-zerop (car c)) (car e2) (car e1))
1649 (math-if-vector (cdr c)
1650 (or (cdr e1) e1)
1651 (or (cdr e2) e2)))))
1652
1653 (defun math-normalize-logical-op (a)
1654 (or (and (eq (car a) 'calcFunc-if)
1655 (= (length a) 4)
1656 (let ((a1 (math-normalize (nth 1 a))))
1657 (if (Math-zerop a1)
1658 (math-normalize (nth 3 a))
1659 (if (Math-numberp a1)
1660 (math-normalize (nth 2 a))
1661 (if (and (Math-vectorp (nth 1 a))
1662 (math-constp (nth 1 a)))
1663 (calcFunc-if (nth 1 a)
1664 (math-normalize (nth 2 a))
1665 (math-normalize (nth 3 a)))
1666 (let ((calc-simplify-mode 'none))
1667 (list 'calcFunc-if a1
1668 (math-normalize (nth 2 a))
1669 (math-normalize (nth 3 a)))))))))
1670 a))
1671
1672 (defun calcFunc-in (a b)
1673 (or (and (eq (car-safe b) 'vec)
1674 (let ((bb b))
1675 (while (and (setq bb (cdr bb))
1676 (not (if (memq (car-safe (car bb)) '(vec intv))
1677 (eq (calcFunc-in a (car bb)) 1)
1678 (Math-equal a (car bb))))))
1679 (if bb 1 (and (math-constp a) (math-constp bb) 0))))
1680 (and (eq (car-safe b) 'intv)
1681 (let ((res (math-compare a (nth 2 b))) res2)
1682 (cond ((= res -1)
1683 0)
1684 ((and (= res 0)
1685 (or (/= (nth 1 b) 2)
1686 (Math-lessp (nth 2 b) (nth 3 b))))
1687 (if (memq (nth 1 b) '(2 3)) 1 0))
1688 ((= (setq res2 (math-compare a (nth 3 b))) 1)
1689 0)
1690 ((and (= res2 0)
1691 (or (/= (nth 1 b) 1)
1692 (Math-lessp (nth 2 b) (nth 3 b))))
1693 (if (memq (nth 1 b) '(1 3)) 1 0))
1694 ((/= res 1)
1695 nil)
1696 ((/= res2 -1)
1697 nil)
1698 (t 1))))
1699 (and (Math-equal a b)
1700 1)
1701 (and (math-constp a) (math-constp b)
1702 0)
1703 (list 'calcFunc-in a b)))
1704
1705 (defun calcFunc-typeof (a)
1706 (cond ((Math-integerp a) 1)
1707 ((eq (car a) 'frac) 2)
1708 ((eq (car a) 'float) 3)
1709 ((eq (car a) 'hms) 4)
1710 ((eq (car a) 'cplx) 5)
1711 ((eq (car a) 'polar) 6)
1712 ((eq (car a) 'sdev) 7)
1713 ((eq (car a) 'intv) 8)
1714 ((eq (car a) 'mod) 9)
1715 ((eq (car a) 'date) (if (Math-integerp (nth 1 a)) 10 11))
1716 ((eq (car a) 'var)
1717 (if (memq (nth 2 a) '(var-inf var-uinf var-nan)) 12 100))
1718 ((eq (car a) 'vec) (if (math-matrixp a) 102 101))
1719 (t (math-calcFunc-to-var (car a)))))
1720
1721 (defun calcFunc-integer (a)
1722 (if (Math-integerp a)
1723 1
1724 (if (Math-objvecp a)
1725 0
1726 (list 'calcFunc-integer a))))
1727
1728 (defun calcFunc-real (a)
1729 (if (Math-realp a)
1730 1
1731 (if (Math-objvecp a)
1732 0
1733 (list 'calcFunc-real a))))
1734
1735 (defun calcFunc-constant (a)
1736 (if (math-constp a)
1737 1
1738 (if (Math-objvecp a)
1739 0
1740 (list 'calcFunc-constant a))))
1741
1742 (defun calcFunc-refers (a b)
1743 (if (math-expr-contains a b)
1744 1
1745 (if (eq (car-safe a) 'var)
1746 (list 'calcFunc-refers a b)
1747 0)))
1748
1749 (defun calcFunc-negative (a)
1750 (if (math-looks-negp a)
1751 1
1752 (if (or (math-zerop a)
1753 (math-posp a))
1754 0
1755 (list 'calcFunc-negative a))))
1756
1757 (defun calcFunc-variable (a)
1758 (if (eq (car-safe a) 'var)
1759 1
1760 (if (Math-objvecp a)
1761 0
1762 (list 'calcFunc-variable a))))
1763
1764 (defun calcFunc-nonvar (a)
1765 (if (eq (car-safe a) 'var)
1766 (list 'calcFunc-nonvar a)
1767 1))
1768
1769 (defun calcFunc-istrue (a)
1770 (if (math-is-true a)
1771 1
1772 0))
1773
1774
1775
1776 ;;;; User-programmability.
1777
1778 ;;; Compiling Lisp-like forms to use the math library.
1779
1780 (defun math-do-defmath (func args body)
1781 (require 'calc-macs)
1782 (let* ((fname (intern (concat "calcFunc-" (symbol-name func))))
1783 (doc (if (stringp (car body)) (list (car body))))
1784 (clargs (mapcar 'math-clean-arg args))
1785 (body (math-define-function-body
1786 (if (stringp (car body)) (cdr body) body)
1787 clargs)))
1788 (list 'progn
1789 (if (and (consp (car body))
1790 (eq (car (car body)) 'interactive))
1791 (let ((inter (car body)))
1792 (setq body (cdr body))
1793 (if (or (> (length inter) 2)
1794 (integerp (nth 1 inter)))
1795 (let ((hasprefix nil) (hasmulti nil))
1796 (if (stringp (nth 1 inter))
1797 (progn
1798 (cond ((equal (nth 1 inter) "p")
1799 (setq hasprefix t))
1800 ((equal (nth 1 inter) "m")
1801 (setq hasmulti t))
1802 (t (error
1803 "Can't handle interactive code string \"%s\""
1804 (nth 1 inter))))
1805 (setq inter (cdr inter))))
1806 (if (not (integerp (nth 1 inter)))
1807 (error
1808 "Expected an integer in interactive specification"))
1809 (append (list 'defun
1810 (intern (concat "calc-"
1811 (symbol-name func)))
1812 (if (or hasprefix hasmulti)
1813 '(&optional n)
1814 ()))
1815 doc
1816 (if (or hasprefix hasmulti)
1817 '((interactive "P"))
1818 '((interactive)))
1819 (list
1820 (append
1821 '(calc-slow-wrapper)
1822 (and hasmulti
1823 (list
1824 (list 'setq
1825 'n
1826 (list 'if
1827 'n
1828 (list 'prefix-numeric-value
1829 'n)
1830 (nth 1 inter)))))
1831 (list
1832 (list 'calc-enter-result
1833 (if hasmulti 'n (nth 1 inter))
1834 (nth 2 inter)
1835 (if hasprefix
1836 (list 'append
1837 (list 'quote (list fname))
1838 (list 'calc-top-list-n
1839 (nth 1 inter))
1840 (list 'and
1841 'n
1842 (list
1843 'list
1844 (list
1845 'math-normalize
1846 (list
1847 'prefix-numeric-value
1848 'n)))))
1849 (list 'cons
1850 (list 'quote fname)
1851 (list 'calc-top-list-n
1852 (if hasmulti
1853 'n
1854 (nth 1 inter)))))))))))
1855 (append (list 'defun
1856 (intern (concat "calc-" (symbol-name func)))
1857 args)
1858 doc
1859 (list
1860 inter
1861 (cons 'calc-wrapper body))))))
1862 (append (list 'defun fname clargs)
1863 doc
1864 (math-do-arg-list-check args nil nil)
1865 body))))
1866
1867 (defun math-clean-arg (arg)
1868 (if (consp arg)
1869 (math-clean-arg (nth 1 arg))
1870 arg))
1871
1872 (defun math-do-arg-check (arg var is-opt is-rest)
1873 (if is-opt
1874 (let ((chk (math-do-arg-check arg var nil nil)))
1875 (list (cons 'and
1876 (cons var
1877 (if (cdr chk)
1878 (setq chk (list (cons 'progn chk)))
1879 chk)))))
1880 (and (consp arg)
1881 (let* ((rest (math-do-arg-check (nth 1 arg) var is-opt is-rest))
1882 (qual (car arg))
1883 (qqual (list 'quote qual))
1884 (qual-name (symbol-name qual))
1885 (chk (intern (concat "math-check-" qual-name))))
1886 (if (fboundp chk)
1887 (append rest
1888 (list
1889 (if is-rest
1890 (list 'setq var
1891 (list 'mapcar (list 'quote chk) var))
1892 (list 'setq var (list chk var)))))
1893 (if (fboundp (setq chk (intern (concat "math-" qual-name))))
1894 (append rest
1895 (list
1896 (if is-rest
1897 (list 'mapcar
1898 (list 'function
1899 (list 'lambda '(x)
1900 (list 'or
1901 (list chk 'x)
1902 (list 'math-reject-arg
1903 'x qqual))))
1904 var)
1905 (list 'or
1906 (list chk var)
1907 (list 'math-reject-arg var qqual)))))
1908 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name)
1909 (fboundp (setq chk (intern
1910 (concat "math-"
1911 (math-match-substring
1912 qual-name 1))))))
1913 (append rest
1914 (list
1915 (if is-rest
1916 (list 'mapcar
1917 (list 'function
1918 (list 'lambda '(x)
1919 (list 'and
1920 (list chk 'x)
1921 (list 'math-reject-arg
1922 'x qqual))))
1923 var)
1924 (list 'and
1925 (list chk var)
1926 (list 'math-reject-arg var qqual)))))
1927 (error "Unknown qualifier `%s'" qual-name))))))))
1928
1929 (defun math-do-arg-list-check (args is-opt is-rest)
1930 (cond ((null args) nil)
1931 ((consp (car args))
1932 (append (math-do-arg-check (car args)
1933 (math-clean-arg (car args))
1934 is-opt is-rest)
1935 (math-do-arg-list-check (cdr args) is-opt is-rest)))
1936 ((eq (car args) '&optional)
1937 (math-do-arg-list-check (cdr args) t nil))
1938 ((eq (car args) '&rest)
1939 (math-do-arg-list-check (cdr args) nil t))
1940 (t (math-do-arg-list-check (cdr args) is-opt is-rest))))
1941
1942 (defconst math-prim-funcs
1943 '( (~= . math-nearly-equal)
1944 (% . math-mod)
1945 (lsh . calcFunc-lsh)
1946 (ash . calcFunc-ash)
1947 (logand . calcFunc-and)
1948 (logandc2 . calcFunc-diff)
1949 (logior . calcFunc-or)
1950 (logxor . calcFunc-xor)
1951 (lognot . calcFunc-not)
1952 (equal . equal) ; need to leave these ones alone!
1953 (eq . eq)
1954 (and . and)
1955 (or . or)
1956 (if . if)
1957 (^ . math-pow)
1958 (expt . math-pow)
1959 ))
1960
1961 (defconst math-prim-vars
1962 '( (nil . nil)
1963 (t . t)
1964 (&optional . &optional)
1965 (&rest . &rest)
1966 ))
1967
1968 (defun math-define-function-body (body env)
1969 (let ((body (math-define-body body env)))
1970 (if (math-body-refers-to body 'math-return)
1971 (list (cons 'catch (cons '(quote math-return) body)))
1972 body)))
1973
1974 ;; The variable math-exp-env is local to math-define-body, but is
1975 ;; used by math-define-exp, which is called (indirectly) by
1976 ;; by math-define-body.
1977 (defvar math-exp-env)
1978
1979 (defun math-define-body (body math-exp-env)
1980 (math-define-list body))
1981
1982 (defun math-define-list (body &optional quote)
1983 (cond ((null body)
1984 nil)
1985 ((and (eq (car body) ':)
1986 (stringp (nth 1 body)))
1987 (cons (let* ((math-read-expr-quotes t)
1988 (exp (math-read-plain-expr (nth 1 body) t)))
1989 (math-define-exp exp))
1990 (math-define-list (cdr (cdr body)))))
1991 (quote
1992 (cons (cond ((consp (car body))
1993 (math-define-list (cdr body) t))
1994 (t
1995 (car body)))
1996 (math-define-list (cdr body))))
1997 (t
1998 (cons (math-define-exp (car body))
1999 (math-define-list (cdr body))))))
2000
2001 (defun math-define-exp (exp)
2002 (cond ((consp exp)
2003 (let ((func (car exp)))
2004 (cond ((memq func '(quote function))
2005 (if (and (consp (nth 1 exp))
2006 (eq (car (nth 1 exp)) 'lambda))
2007 (cons 'quote
2008 (math-define-lambda (nth 1 exp) math-exp-env))
2009 exp))
2010 ((memq func '(let let* for foreach))
2011 (let ((head (nth 1 exp))
2012 (body (cdr (cdr exp))))
2013 (if (memq func '(let let*))
2014 ()
2015 (setq func (cdr (assq func '((for . math-for)
2016 (foreach . math-foreach)))))
2017 (if (not (listp (car head)))
2018 (setq head (list head))))
2019 (macroexpand
2020 (cons func
2021 (cons (math-define-let head)
2022 (math-define-body body
2023 (nconc
2024 (math-define-let-env head)
2025 math-exp-env)))))))
2026 ((and (memq func '(setq setf))
2027 (math-complicated-lhs (cdr exp)))
2028 (if (> (length exp) 3)
2029 (cons 'progn (math-define-setf-list (cdr exp)))
2030 (math-define-setf (nth 1 exp) (nth 2 exp))))
2031 ((eq func 'condition-case)
2032 (cons func
2033 (cons (nth 1 exp)
2034 (math-define-body (cdr (cdr exp))
2035 (cons (nth 1 exp)
2036 math-exp-env)))))
2037 ((eq func 'cond)
2038 (cons func
2039 (math-define-cond (cdr exp))))
2040 ((and (consp func) ; ('spam a b) == force use of plain spam
2041 (eq (car func) 'quote))
2042 (cons func (math-define-list (cdr exp))))
2043 ((symbolp func)
2044 (let ((args (math-define-list (cdr exp)))
2045 (prim (assq func math-prim-funcs)))
2046 (cond (prim
2047 (cons (cdr prim) args))
2048 ((eq func 'floatp)
2049 (list 'eq (car args) '(quote float)))
2050 ((eq func '+)
2051 (math-define-binop 'math-add 0
2052 (car args) (cdr args)))
2053 ((eq func '-)
2054 (if (= (length args) 1)
2055 (cons 'math-neg args)
2056 (math-define-binop 'math-sub 0
2057 (car args) (cdr args))))
2058 ((eq func '*)
2059 (math-define-binop 'math-mul 1
2060 (car args) (cdr args)))
2061 ((eq func '/)
2062 (math-define-binop 'math-div 1
2063 (car args) (cdr args)))
2064 ((eq func 'min)
2065 (math-define-binop 'math-min 0
2066 (car args) (cdr args)))
2067 ((eq func 'max)
2068 (math-define-binop 'math-max 0
2069 (car args) (cdr args)))
2070 ((eq func '<)
2071 (if (and (math-numberp (nth 1 args))
2072 (math-zerop (nth 1 args)))
2073 (list 'math-negp (car args))
2074 (cons 'math-lessp args)))
2075 ((eq func '>)
2076 (if (and (math-numberp (nth 1 args))
2077 (math-zerop (nth 1 args)))
2078 (list 'math-posp (car args))
2079 (list 'math-lessp (nth 1 args) (nth 0 args))))
2080 ((eq func '<=)
2081 (list 'not
2082 (if (and (math-numberp (nth 1 args))
2083 (math-zerop (nth 1 args)))
2084 (list 'math-posp (car args))
2085 (list 'math-lessp
2086 (nth 1 args) (nth 0 args)))))
2087 ((eq func '>=)
2088 (list 'not
2089 (if (and (math-numberp (nth 1 args))
2090 (math-zerop (nth 1 args)))
2091 (list 'math-negp (car args))
2092 (cons 'math-lessp args))))
2093 ((eq func '=)
2094 (if (and (math-numberp (nth 1 args))
2095 (math-zerop (nth 1 args)))
2096 (list 'math-zerop (nth 0 args))
2097 (if (and (integerp (nth 1 args))
2098 (/= (% (nth 1 args) 10) 0))
2099 (cons 'math-equal-int args)
2100 (cons 'math-equal args))))
2101 ((eq func '/=)
2102 (list 'not
2103 (if (and (math-numberp (nth 1 args))
2104 (math-zerop (nth 1 args)))
2105 (list 'math-zerop (nth 0 args))
2106 (if (and (integerp (nth 1 args))
2107 (/= (% (nth 1 args) 10) 0))
2108 (cons 'math-equal-int args)
2109 (cons 'math-equal args)))))
2110 ((eq func '1+)
2111 (list 'math-add (car args) 1))
2112 ((eq func '1-)
2113 (list 'math-add (car args) -1))
2114 ((eq func 'not) ; optimize (not (not x)) => x
2115 (if (eq (car-safe args) func)
2116 (car (nth 1 args))
2117 (cons func args)))
2118 ((and (eq func 'elt) (cdr (cdr args)))
2119 (math-define-elt (car args) (cdr args)))
2120 (t
2121 (macroexpand
2122 (let* ((name (symbol-name func))
2123 (cfunc (intern (concat "calcFunc-" name)))
2124 (mfunc (intern (concat "math-" name))))
2125 (cond ((fboundp cfunc)
2126 (cons cfunc args))
2127 ((fboundp mfunc)
2128 (cons mfunc args))
2129 ((or (fboundp func)
2130 (string-match "\\`calcFunc-.*" name))
2131 (cons func args))
2132 (t
2133 (cons cfunc args)))))))))
2134 (t (cons func (math-define-list (cdr exp))))))) ;;args
2135 ((symbolp exp)
2136 (let ((prim (assq exp math-prim-vars))
2137 (name (symbol-name exp)))
2138 (cond (prim
2139 (cdr prim))
2140 ((memq exp math-exp-env)
2141 exp)
2142 ((string-match "-" name)
2143 exp)
2144 (t
2145 (intern (concat "var-" name))))))
2146 ((integerp exp)
2147 (if (or (<= exp -1000000) (>= exp 1000000))
2148 (list 'quote (math-normalize exp))
2149 exp))
2150 (t exp)))
2151
2152 (defun math-define-cond (forms)
2153 (and forms
2154 (cons (math-define-list (car forms))
2155 (math-define-cond (cdr forms)))))
2156
2157 (defun math-complicated-lhs (body)
2158 (and body
2159 (or (not (symbolp (car body)))
2160 (math-complicated-lhs (cdr (cdr body))))))
2161
2162 (defun math-define-setf-list (body)
2163 (and body
2164 (cons (math-define-setf (nth 0 body) (nth 1 body))
2165 (math-define-setf-list (cdr (cdr body))))))
2166
2167 (defun math-define-setf (place value)
2168 (setq place (math-define-exp place)
2169 value (math-define-exp value))
2170 (cond ((symbolp place)
2171 (list 'setq place value))
2172 ((eq (car-safe place) 'nth)
2173 (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value))
2174 ((eq (car-safe place) 'elt)
2175 (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value))
2176 ((eq (car-safe place) 'car)
2177 (list 'setcar (nth 1 place) value))
2178 ((eq (car-safe place) 'cdr)
2179 (list 'setcdr (nth 1 place) value))
2180 (t
2181 (error "Bad place form for setf: %s" place))))
2182
2183 (defun math-define-binop (op ident arg1 rest)
2184 (if rest
2185 (math-define-binop op ident
2186 (list op arg1 (car rest))
2187 (cdr rest))
2188 (or arg1 ident)))
2189
2190 (defun math-define-let (vlist)
2191 (and vlist
2192 (cons (if (consp (car vlist))
2193 (cons (car (car vlist))
2194 (math-define-list (cdr (car vlist))))
2195 (car vlist))
2196 (math-define-let (cdr vlist)))))
2197
2198 (defun math-define-let-env (vlist)
2199 (and vlist
2200 (cons (if (consp (car vlist))
2201 (car (car vlist))
2202 (car vlist))
2203 (math-define-let-env (cdr vlist)))))
2204
2205 (defun math-define-lambda (exp exp-env)
2206 (nconc (list (nth 0 exp) ; 'lambda
2207 (nth 1 exp)) ; arg list
2208 (math-define-function-body (cdr (cdr exp))
2209 (append (nth 1 exp) exp-env))))
2210
2211 (defun math-define-elt (seq idx)
2212 (if idx
2213 (math-define-elt (list 'elt seq (car idx)) (cdr idx))
2214 seq))
2215
2216
2217
2218 ;;; Useful programming macros.
2219
2220 (defmacro math-while (head &rest body)
2221 (let ((body (cons 'while (cons head body))))
2222 (if (math-body-refers-to body 'math-break)
2223 (cons 'catch (cons '(quote math-break) (list body)))
2224 body)))
2225 ;; (put 'math-while 'lisp-indent-hook 1)
2226
2227 (defmacro math-for (head &rest body)
2228 (let ((body (if head
2229 (math-handle-for head body)
2230 (cons 'while (cons t body)))))
2231 (if (math-body-refers-to body 'math-break)
2232 (cons 'catch (cons '(quote math-break) (list body)))
2233 body)))
2234 ;; (put 'math-for 'lisp-indent-hook 1)
2235
2236 (defun math-handle-for (head body)
2237 (let* ((var (nth 0 (car head)))
2238 (init (nth 1 (car head)))
2239 (limit (nth 2 (car head)))
2240 (step (or (nth 3 (car head)) 1))
2241 (body (if (cdr head)
2242 (list (math-handle-for (cdr head) body))
2243 body))
2244 (all-ints (and (integerp init) (integerp limit) (integerp step)))
2245 (const-limit (or (integerp limit)
2246 (and (eq (car-safe limit) 'quote)
2247 (math-realp (nth 1 limit)))))
2248 (const-step (or (integerp step)
2249 (and (eq (car-safe step) 'quote)
2250 (math-realp (nth 1 step)))))
2251 (save-limit (if const-limit limit (make-symbol "<limit>")))
2252 (save-step (if const-step step (make-symbol "<step>"))))
2253 (cons 'let
2254 (cons (append (if const-limit nil (list (list save-limit limit)))
2255 (if const-step nil (list (list save-step step)))
2256 (list (list var init)))
2257 (list
2258 (cons 'while
2259 (cons (if all-ints
2260 (if (> step 0)
2261 (list '<= var save-limit)
2262 (list '>= var save-limit))
2263 (list 'not
2264 (if const-step
2265 (if (or (math-posp step)
2266 (math-posp
2267 (cdr-safe step)))
2268 (list 'math-lessp
2269 save-limit
2270 var)
2271 (list 'math-lessp
2272 var
2273 save-limit))
2274 (list 'if
2275 (list 'math-posp
2276 save-step)
2277 (list 'math-lessp
2278 save-limit
2279 var)
2280 (list 'math-lessp
2281 var
2282 save-limit)))))
2283 (append body
2284 (list (list 'setq
2285 var
2286 (list (if all-ints
2287 '+
2288 'math-add)
2289 var
2290 save-step)))))))))))
2291
2292 (defmacro math-foreach (head &rest body)
2293 (let ((body (math-handle-foreach head body)))
2294 (if (math-body-refers-to body 'math-break)
2295 (cons 'catch (cons '(quote math-break) (list body)))
2296 body)))
2297 ;; (put 'math-foreach 'lisp-indent-hook 1)
2298
2299 (defun math-handle-foreach (head body)
2300 (let ((var (nth 0 (car head)))
2301 (data (nth 1 (car head)))
2302 (body (if (cdr head)
2303 (list (math-handle-foreach (cdr head) body))
2304 body)))
2305 (cons 'let
2306 (cons (list (list var data))
2307 (list
2308 (cons 'while
2309 (cons var
2310 (append body
2311 (list (list 'setq
2312 var
2313 (list 'cdr var)))))))))))
2314
2315
2316 (defun math-body-refers-to (body thing)
2317 (or (equal body thing)
2318 (and (consp body)
2319 (or (math-body-refers-to (car body) thing)
2320 (math-body-refers-to (cdr body) thing)))))
2321
2322 (defun math-break (&optional value)
2323 (throw 'math-break value))
2324
2325 (defun math-return (&optional value)
2326 (throw 'math-return value))
2327
2328
2329
2330
2331
2332 (defun math-composite-inequalities (x op)
2333 (if (memq (nth 1 op) '(calcFunc-eq calcFunc-neq))
2334 (if (eq (car x) (nth 1 op))
2335 (append x (list (math-read-expr-level (nth 3 op))))
2336 (throw 'syntax "Syntax error"))
2337 (list 'calcFunc-in
2338 (nth 2 x)
2339 (if (memq (nth 1 op) '(calcFunc-lt calcFunc-leq))
2340 (if (memq (car x) '(calcFunc-lt calcFunc-leq))
2341 (math-make-intv
2342 (+ (if (eq (car x) 'calcFunc-leq) 2 0)
2343 (if (eq (nth 1 op) 'calcFunc-leq) 1 0))
2344 (nth 1 x) (math-read-expr-level (nth 3 op)))
2345 (throw 'syntax "Syntax error"))
2346 (if (memq (car x) '(calcFunc-gt calcFunc-geq))
2347 (math-make-intv
2348 (+ (if (eq (nth 1 op) 'calcFunc-geq) 2 0)
2349 (if (eq (car x) 'calcFunc-geq) 1 0))
2350 (math-read-expr-level (nth 3 op)) (nth 1 x))
2351 (throw 'syntax "Syntax error"))))))
2352
2353 (provide 'calc-prog)
2354
2355 ;;; arch-tag: 4c5a183b-c9e5-4632-bb3f-e41a764518b0
2356 ;;; calc-prog.el ends here