comparison lisp/calc/calc-aent.el @ 90054:f2ebccfa87d4

Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-74 Merge from emacs--cvs-trunk--0 Patches applied: * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-709 Update from CVS: src/indent.c (Fvertical_motion): Fix last change. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-710 - miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-715 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-716 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-74 Update from CVS
author Miles Bader <miles@gnu.org>
date Wed, 08 Dec 2004 05:02:30 +0000
parents cb7f41387eb3 157fd661769f
children fb79180b618d
comparison
equal deleted inserted replaced
90053:fff5f1a61d92 90054:f2ebccfa87d4
1 ;;; calc-aent.el --- algebraic entry functions for Calc 1 ;;; calc-aent.el --- algebraic entry functions for Calc
2 2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4 4
5 ;; Author: Dave Gillespie <daveg@synaptics.com> 5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org> 6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
7 ;; Colin Walters <walters@debian.org>
8 7
9 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
10 9
11 ;; GNU Emacs is distributed in the hope that it will be useful, 10 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor 11 ;; but WITHOUT ANY WARRANTY. No author or distributor
26 ;;; Commentary: 25 ;;; Commentary:
27 26
28 ;;; Code: 27 ;;; Code:
29 28
30 ;; This file is autoloaded from calc.el. 29 ;; This file is autoloaded from calc.el.
30
31 (require 'calc) 31 (require 'calc)
32
33 (require 'calc-macs) 32 (require 'calc-macs)
34 (eval-when-compile '(require calc-macs))
35
36 (defun calc-Need-calc-aent () nil)
37
38 33
39 (defun calc-do-quick-calc () 34 (defun calc-do-quick-calc ()
40 (calc-check-defines) 35 (calc-check-defines)
41 (if (eq major-mode 'calc-mode) 36 (if (eq major-mode 'calc-mode)
42 (calc-algebraic-entry t) 37 (calc-algebraic-entry t)
50 (calc-language (if (memq calc-language '(nil big)) 45 (calc-language (if (memq calc-language '(nil big))
51 'flat calc-language)) 46 'flat calc-language))
52 (entry (calc-do-alg-entry "" "Quick calc: " t)) 47 (entry (calc-do-alg-entry "" "Quick calc: " t))
53 (alg-exp (mapcar (function 48 (alg-exp (mapcar (function
54 (lambda (x) 49 (lambda (x)
55 (if (and (not calc-extensions-loaded) 50 (if (and (not (featurep 'calc-ext))
56 calc-previous-alg-entry 51 calc-previous-alg-entry
57 (string-match 52 (string-match
58 "\\`[-0-9._+*/^() ]+\\'" 53 "\\`[-0-9._+*/^() ]+\\'"
59 calc-previous-alg-entry)) 54 calc-previous-alg-entry))
60 (calc-normalize x) 55 (calc-normalize x)
61 (calc-extensions) 56 (require 'calc-ext)
62 (math-evaluate-expr x)))) 57 (math-evaluate-expr x))))
63 entry))) 58 entry)))
64 (when (and (= (length alg-exp) 1) 59 (when (and (= (length alg-exp) 1)
65 (eq (car-safe (car alg-exp)) 'calcFunc-assign) 60 (eq (car-safe (car alg-exp)) 'calcFunc-assign)
66 (= (length (car alg-exp)) 3) 61 (= (length (car alg-exp)) 3)
67 (eq (car-safe (nth 1 (car alg-exp))) 'var)) 62 (eq (car-safe (nth 1 (car alg-exp))) 'var))
68 (calc-extensions) 63 (require 'calc-ext)
69 (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp))) 64 (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
70 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp)))) 65 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
71 (setq alg-exp (list (nth 2 (car alg-exp))))) 66 (setq alg-exp (list (nth 2 (car alg-exp)))))
72 (setq calc-quick-prev-results alg-exp 67 (setq calc-quick-prev-results alg-exp
73 buf (mapconcat (function (lambda (x) 68 buf (mapconcat (function (lambda (x)
90 (< (car alg-exp) 127)) 85 (< (car alg-exp) 127))
91 (format ", \"%c\"" (car alg-exp)) 86 (format ", \"%c\"" (car alg-exp))
92 "") 87 "")
93 ")"))) 88 ")")))
94 (if (and (< (length buf) (frame-width)) (= (length entry) 1) 89 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
95 calc-extensions-loaded) 90 (featurep 'calc-ext))
96 (let ((long (concat (math-format-value (car entry) 1000) 91 (let ((long (concat (math-format-value (car entry) 1000)
97 " => " buf))) 92 " => " buf)))
98 (if (<= (length long) (- (frame-width) 8)) 93 (if (<= (length long) (- (frame-width) 8))
99 (setq buf long)))) 94 (setq buf long))))
100 (calc-handle-whys) 95 (calc-handle-whys)
146 (setq strp (cdr (cdr strp)))) 141 (setq strp (cdr (cdr strp))))
147 (calc-do-calc-eval (car str) separator args))) 142 (calc-do-calc-eval (car str) separator args)))
148 ((eq separator 'eval) 143 ((eq separator 'eval)
149 (eval str)) 144 (eval str))
150 ((eq separator 'macro) 145 ((eq separator 'macro)
151 (calc-extensions) 146 (require 'calc-ext)
152 (let* ((calc-buffer (current-buffer)) 147 (let* ((calc-buffer (current-buffer))
153 (calc-window (get-buffer-window calc-buffer)) 148 (calc-window (get-buffer-window calc-buffer))
154 (save-window (selected-window))) 149 (save-window (selected-window)))
155 (if calc-window 150 (if calc-window
156 (unwind-protect 151 (unwind-protect
207 (calc-eval-error (cdr res)) 202 (calc-eval-error (cdr res))
208 (setq res (mapcar 'calc-normalize res)) 203 (setq res (mapcar 'calc-normalize res))
209 (and (memq 'clear-message calc-command-flags) 204 (and (memq 'clear-message calc-command-flags)
210 (message "")) 205 (message ""))
211 (cond ((eq separator 'pred) 206 (cond ((eq separator 'pred)
212 (calc-extensions) 207 (require 'calc-ext)
213 (if (= (length res) 1) 208 (if (= (length res) 1)
214 (math-is-true (car res)) 209 (math-is-true (car res))
215 (calc-eval-error '(0 "Single value expected")))) 210 (calc-eval-error '(0 "Single value expected"))))
216 ((eq separator 'raw) 211 ((eq separator 'raw)
217 (if (= (length res) 1) 212 (if (= (length res) 1)
239 (and buf (or separator ", ")) 234 (and buf (or separator ", "))
240 (math-format-value (car res) 1000)) 235 (math-format-value (car res) 1000))
241 res (cdr res))) 236 res (cdr res)))
242 buf))))))))) 237 buf)))))))))
243 238
239 (defvar calc-eval-error nil
240 "Determines how calc handles errors.
241 NIL means return a list containing the character position of error.
242 STRING means return error message as string rather than list.
243 T means abort and give an error message.")
244
244 (defun calc-eval-error (msg) 245 (defun calc-eval-error (msg)
245 (if (and (boundp 'calc-eval-error) 246 (if calc-eval-error
246 calc-eval-error)
247 (if (eq calc-eval-error 'string) 247 (if (eq calc-eval-error 'string)
248 (nth 1 msg) 248 (nth 1 msg)
249 (error "%s" (nth 1 msg))) 249 (error "%s" (nth 1 msg)))
250 msg)) 250 msg))
251 251
270 (calc-dollar-used 0) 270 (calc-dollar-used 0)
271 (calc-plain-entry t) 271 (calc-plain-entry t)
272 (alg-exp (calc-do-alg-entry initial prompt t))) 272 (alg-exp (calc-do-alg-entry initial prompt t)))
273 (if (stringp alg-exp) 273 (if (stringp alg-exp)
274 (progn 274 (progn
275 (calc-extensions) 275 (require 'calc-ext)
276 (calc-alg-edit alg-exp)) 276 (calc-alg-edit alg-exp))
277 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j) 277 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j)
278 'none 278 'none
279 calc-simplify-mode)) 279 calc-simplify-mode))
280 (nvals (mapcar 'calc-normalize alg-exp))) 280 (nvals (mapcar 'calc-normalize alg-exp)))
281 (while alg-exp 281 (while alg-exp
282 (calc-record (if calc-extensions-loaded (car alg-exp) (car nvals)) 282 (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
283 "alg'") 283 "alg'")
284 (calc-pop-push-record-list calc-dollar-used 284 (calc-pop-push-record-list calc-dollar-used
285 (and (not (equal (car alg-exp) 285 (and (not (equal (car alg-exp)
286 (car nvals))) 286 (car nvals)))
287 calc-extensions-loaded 287 (featurep 'calc-ext)
288 "") 288 "")
289 (list (car nvals))) 289 (list (car nvals)))
290 (setq alg-exp (cdr alg-exp) 290 (setq alg-exp (cdr alg-exp)
291 nvals (cdr nvals) 291 nvals (cdr nvals)
292 calc-dollar-used 0))) 292 calc-dollar-used 0)))
384 (insert "`") 384 (insert "`")
385 (setq calc-alg-exp (minibuffer-contents)) 385 (setq calc-alg-exp (minibuffer-contents))
386 (and (> (length calc-alg-exp) 0) (setq calc-previous-alg-entry calc-alg-exp)) 386 (and (> (length calc-alg-exp) 0) (setq calc-previous-alg-entry calc-alg-exp))
387 (exit-minibuffer))) 387 (exit-minibuffer)))
388 388
389 (defvar calc-buffer)
390
389 (defun calcAlg-enter () 391 (defun calcAlg-enter ()
390 (interactive) 392 (interactive)
391 (let* ((str (minibuffer-contents)) 393 (let* ((str (minibuffer-contents))
392 (exp (and (> (length str) 0) 394 (exp (and (> (length str) 0)
393 (save-excursion 395 (save-excursion
441 ((eq last-command-char ?#) (format "%d#" calc-number-radix)) 443 ((eq last-command-char ?#) (format "%d#" calc-number-radix))
442 ((eq last-command-char ?_) "-") 444 ((eq last-command-char ?_) "-")
443 ((eq last-command-char ?@) "0@ ") 445 ((eq last-command-char ?@) "0@ ")
444 (t (char-to-string last-command-char))))) 446 (t (char-to-string last-command-char)))))
445 447
448 ;; The variable calc-digit-value is initially declared in calc.el,
449 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
450 (defvar calc-digit-value)
451
446 (defun calcDigit-algebraic () 452 (defun calcDigit-algebraic ()
447 (interactive) 453 (interactive)
448 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'") 454 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
449 (calcDigit-key) 455 (calcDigit-key)
450 (setq calc-digit-value (minibuffer-contents)) 456 (setq calc-digit-value (minibuffer-contents))
457 (exit-minibuffer)) 463 (exit-minibuffer))
458 464
459 465
460 ;;; Algebraic expression parsing. [Public] 466 ;;; Algebraic expression parsing. [Public]
461 467
462 ;;; The next few variables are local to math-read-exprs (and math-read-expr) 468 ;; The next few variables are local to math-read-exprs (and math-read-expr
463 ;;; but are set in functions they call. 469 ;; in calc-ext.el), but are set in functions they call.
464 470
465 (defvar math-exp-pos) 471 (defvar math-exp-pos)
466 (defvar math-exp-str) 472 (defvar math-exp-str)
467 (defvar math-exp-old-pos) 473 (defvar math-exp-old-pos)
468 (defvar math-exp-token) 474 (defvar math-exp-token)
469 (defvar math-exp-keep-spaces) 475 (defvar math-exp-keep-spaces)
476 (defvar math-expr-data)
470 477
471 (defun math-read-exprs (math-exp-str) 478 (defun math-read-exprs (math-exp-str)
472 (let ((math-exp-pos 0) 479 (let ((math-exp-pos 0)
473 (math-exp-old-pos 0) 480 (math-exp-old-pos 0)
474 (math-exp-keep-spaces nil) 481 (math-exp-keep-spaces nil)
475 math-exp-token math-expr-data) 482 math-exp-token math-expr-data)
483 (setq math-exp-str (math-read-preprocess-string math-exp-str))
476 (if calc-language-input-filter 484 (if calc-language-input-filter
477 (setq math-exp-str (funcall calc-language-input-filter math-exp-str))) 485 (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
478 (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str)) 486 (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
479 (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots" 487 (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
480 (substring math-exp-str (+ math-exp-token 2))))) 488 (substring math-exp-str (+ math-exp-token 2)))))
725 (setq ch ?\,)) 733 (setq ch ?\,))
726 (setq math-exp-token 'punc 734 (setq math-exp-token 'punc
727 math-expr-data (char-to-string ch) 735 math-expr-data (char-to-string ch)
728 math-exp-pos (1+ math-exp-pos))))))) 736 math-exp-pos (1+ math-exp-pos)))))))
729 737
738 (defconst math-alg-inequalities
739 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
740 calcFunc-eq calcFunc-neq))
730 741
731 (defun math-read-expr-level (exp-prec &optional exp-term) 742 (defun math-read-expr-level (exp-prec &optional exp-term)
732 (let* ((x (math-read-factor)) (first t) op op2) 743 (let* ((x (math-read-factor)) (first t) op op2)
733 (while (and (or (and calc-user-parse-table 744 (while (and (or (and calc-user-parse-table
734 (setq op (calc-check-user-syntax x exp-prec)) 745 (setq op (calc-check-user-syntax x exp-prec))
769 (not (and exp-term (equal math-expr-data exp-term))) 780 (not (and exp-term (equal math-expr-data exp-term)))
770 (>= (nth 2 op) exp-prec)) 781 (>= (nth 2 op) exp-prec))
771 (if (not (equal (car op) "2x")) 782 (if (not (equal (car op) "2x"))
772 (math-read-token)) 783 (math-read-token))
773 (and (memq (nth 1 op) '(sdev mod)) 784 (and (memq (nth 1 op) '(sdev mod))
774 (calc-extensions)) 785 (require 'calc-ext))
775 (setq x (cond ((consp (nth 1 op)) 786 (setq x (cond ((consp (nth 1 op))
776 (funcall (car (nth 1 op)) x op)) 787 (funcall (car (nth 1 op)) x op))
777 ((eq (nth 3 op) -1) 788 ((eq (nth 3 op) -1)
778 (if (eq (nth 1 op) 'ident) 789 (if (eq (nth 1 op) 'ident)
779 x 790 x
785 (throw 'syntax "Mismatched delimiters")) 796 (throw 'syntax "Mismatched delimiters"))
786 (list (nth 1 op) x)))) 797 (list (nth 1 op) x))))
787 ((and (not first) 798 ((and (not first)
788 (memq (nth 1 op) math-alg-inequalities) 799 (memq (nth 1 op) math-alg-inequalities)
789 (memq (car-safe x) math-alg-inequalities)) 800 (memq (car-safe x) math-alg-inequalities))
790 (calc-extensions) 801 (require 'calc-ext)
791 (math-composite-inequalities x op)) 802 (math-composite-inequalities x op))
792 (t (list (nth 1 op) 803 (t (list (nth 1 op)
793 x 804 x
794 (math-read-expr-level (nth 3 op) exp-term)))) 805 (math-read-expr-level (nth 3 op) exp-term))))
795 first nil)) 806 first nil))
813 (save-exp-token math-exp-token) 824 (save-exp-token math-exp-token)
814 (save-exp-data math-expr-data)) 825 (save-exp-data math-expr-data))
815 (or (not (listp 826 (or (not (listp
816 (setq matches (calc-match-user-syntax rule)))) 827 (setq matches (calc-match-user-syntax rule))))
817 (let ((args (progn 828 (let ((args (progn
818 (calc-extensions) 829 (require 'calc-ext)
819 calc-arg-values)) 830 calc-arg-values))
820 (conds nil) 831 (conds nil)
821 temp) 832 temp)
822 (if x 833 (if x
823 (setq matches (cons x matches))) 834 (setq matches (cons x matches)))
828 (setq conds (append (math-flatten-lands 839 (setq conds (append (math-flatten-lands
829 (nth 2 match)) 840 (nth 2 match))
830 conds) 841 conds)
831 match (nth 1 match))) 842 match (nth 1 match)))
832 (while (and conds match) 843 (while (and conds match)
833 (calc-extensions) 844 (require 'calc-ext)
834 (cond ((eq (car-safe (car conds)) 845 (cond ((eq (car-safe (car conds))
835 'calcFunc-let) 846 'calcFunc-let)
836 (setq temp (car conds)) 847 (setq temp (car conds))
837 (or (= (length temp) 3) 848 (or (= (length temp) 3)
838 (and (= (length temp) 2) 849 (and (= (length temp) 2)
939 math-exp-token save-exp-token 950 math-exp-token save-exp-token
940 math-expr-data save-exp-data 951 math-expr-data save-exp-data
941 matches "Failed")) 952 matches "Failed"))
942 matches)) 953 matches))
943 954
944 (defconst math-alg-inequalities
945 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
946 calcFunc-eq calcFunc-neq))
947
948 (defun math-remove-dashes (x) 955 (defun math-remove-dashes (x)
949 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x) 956 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
950 (math-remove-dashes 957 (math-remove-dashes
951 (concat (math-match-substring x 1) "#" (math-match-substring x 2))) 958 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
952 x)) 959 x))
1024 (if (not (or (equal math-expr-data calc-function-close) 1031 (if (not (or (equal math-expr-data calc-function-close)
1025 (eq math-exp-token 'end))) 1032 (eq math-exp-token 'end)))
1026 (throw 'syntax "Expected `)'")) 1033 (throw 'syntax "Expected `)'"))
1027 (math-read-token) 1034 (math-read-token)
1028 (if (and (eq calc-language 'fortran) args 1035 (if (and (eq calc-language 'fortran) args
1029 (calc-extensions) 1036 (require 'calc-ext)
1030 (let ((calc-matrix-mode 'scalar)) 1037 (let ((calc-matrix-mode 'scalar))
1031 (math-known-matrixp 1038 (math-known-matrixp
1032 (list 'var sym 1039 (list 'var sym
1033 (intern 1040 (intern
1034 (concat "var-" 1041 (concat "var-"
1083 "Too many $'s" 1090 "Too many $'s"
1084 "$'s not allowed in this context"))))) 1091 "$'s not allowed in this context")))))
1085 ((eq math-exp-token 'hash) 1092 ((eq math-exp-token 'hash)
1086 (or calc-hashes-used 1093 (or calc-hashes-used
1087 (throw 'syntax "#'s not allowed in this context")) 1094 (throw 'syntax "#'s not allowed in this context"))
1088 (calc-extensions) 1095 (require 'calc-ext)
1089 (if (<= math-expr-data (length calc-arg-values)) 1096 (if (<= math-expr-data (length calc-arg-values))
1090 (let ((num math-expr-data)) 1097 (let ((num math-expr-data))
1091 (math-read-token) 1098 (math-read-token)
1092 (setq calc-hashes-used (max calc-hashes-used num)) 1099 (setq calc-hashes-used (max calc-hashes-used num))
1093 (nth (1- num) calc-arg-values)) 1100 (nth (1- num) calc-arg-values))
1114 (math-read-token) 1121 (math-read-token)
1115 (let ((exp2 (math-read-expr-level 0))) 1122 (let ((exp2 (math-read-expr-level 0)))
1116 (setq exp (if (and exp2 (Math-realp exp) 1123 (setq exp (if (and exp2 (Math-realp exp)
1117 (Math-anglep exp2)) 1124 (Math-anglep exp2))
1118 (math-normalize (list 'polar exp exp2)) 1125 (math-normalize (list 'polar exp exp2))
1119 (calc-extensions) 1126 (require 'calc-ext)
1120 (list '* exp 1127 (list '* exp
1121 (list 'calcFunc-exp 1128 (list 'calcFunc-exp
1122 (list '* 1129 (list '*
1123 (math-to-radians-2 exp2) 1130 (math-to-radians-2 exp2)
1124 '(var i var-i))))))))) 1131 '(var i var-i)))))))))
1141 (eq math-exp-token 'end))) 1148 (eq math-exp-token 'end)))
1142 (throw 'syntax "Expected `)'")) 1149 (throw 'syntax "Expected `)'"))
1143 (math-read-token) 1150 (math-read-token)
1144 exp)) 1151 exp))
1145 ((eq math-exp-token 'string) 1152 ((eq math-exp-token 'string)
1146 (calc-extensions) 1153 (require 'calc-ext)
1147 (math-read-string)) 1154 (math-read-string))
1148 ((equal math-expr-data "[") 1155 ((equal math-expr-data "[")
1149 (calc-extensions) 1156 (require 'calc-ext)
1150 (math-read-brackets t "]")) 1157 (math-read-brackets t "]"))
1151 ((equal math-expr-data "{") 1158 ((equal math-expr-data "{")
1152 (calc-extensions) 1159 (require 'calc-ext)
1153 (math-read-brackets nil "}")) 1160 (math-read-brackets nil "}"))
1154 ((equal math-expr-data "<") 1161 ((equal math-expr-data "<")
1155 (calc-extensions) 1162 (require 'calc-ext)
1156 (math-read-angle-brackets)) 1163 (math-read-angle-brackets))
1157 (t (throw 'syntax "Expected a number"))))) 1164 (t (throw 'syntax "Expected a number")))))
1158 1165
1166 (provide 'calc-aent)
1167
1159 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32 1168 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1160 ;;; calc-aent.el ends here 1169 ;;; calc-aent.el ends here