1 ;;; calc-incom.el --- complex data type input functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 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 <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
33 (defun calc-begin-complex ()
36 (if (or calc-algebraic-mode calc-incomplete-algebraic-mode)
38 (calc-push (list 'incomplete calc-complex-mode)))))
40 (defun calc-end-complex ()
44 (let ((top (calc-top 1)))
45 (if (and (eq (car-safe top) 'incomplete)
46 (eq (nth 1 top) 'intv))
48 (if (< (length top) 4)
49 (setq top (append top '((neg (var inf var-inf))))))
50 (if (< (length top) 5)
51 (setq top (append top '((var inf var-inf)))))
52 (calc-enter-result 1 "..)" (cdr top)))
53 (if (not (and (eq (car-safe top) 'incomplete)
54 (memq (nth 1 top) '(cplx polar))))
55 (error "Not entering a complex number"))
56 (while (< (length top) 4)
57 (setq top (append top '(0))))
58 (if (not (and (math-realp (nth 2 top))
59 (math-anglep (nth 3 top))))
60 (error "Components must be real"))
61 (calc-enter-result 1 "()" (cdr top))))))
63 (defun calc-begin-vector ()
66 (if (or calc-algebraic-mode calc-incomplete-algebraic-mode)
68 (calc-push '(incomplete vec)))))
70 (defun calc-end-vector ()
74 (let ((top (calc-top 1)))
75 (if (and (eq (car-safe top) 'incomplete)
76 (eq (nth 1 top) 'intv))
78 (if (< (length top) 4)
79 (setq top (append top '((neg (var inf var-inf))))))
80 (if (< (length top) 5)
81 (setq top (append top '((var inf var-inf)))))
82 (setcar (cdr (cdr top)) (1+ (nth 2 top)))
83 (calc-enter-result 1 "..]" (cdr top)))
84 (if (not (and (eq (car-safe top) 'incomplete)
85 (eq (nth 1 top) 'vec)))
86 (error "Not entering a vector"))
87 (calc-pop-push-record 1 "[]" (cdr top))))))
89 (defun calc-comma (&optional allow-polar)
92 (let ((num (calc-find-first-incomplete
93 (nthcdr calc-stack-top calc-stack) 1)))
95 (error "Not entering a vector or complex number"))
96 (let* ((inc (calc-top num))
97 (stuff (calc-top-list (1- num)))
98 (new (append inc stuff)))
101 (or (eq (nth 1 inc) 'vec)
103 (setq new (append new
104 (if (= (length new) 2)
106 (nthcdr (1- (length new)) new)))))
108 (if (eq (nth 1 new) 'polar)
109 (setq new (append '(incomplete cplx) (cdr (cdr new))))
110 (if (eq (nth 1 new) 'intv)
111 (setq new (append '(incomplete cplx)
112 (cdr (cdr (cdr new))))))))
113 (if (and (memq (nth 1 new) '(cplx polar))
115 (error "Too many components in complex number"))
116 (if (and (eq (nth 1 new) 'intv)
118 (error "Too many components in interval form"))
119 (calc-pop-push num new)))))
124 (let ((num (calc-find-first-incomplete
125 (nthcdr calc-stack-top calc-stack) 1)))
127 (error "Not entering a vector or complex number"))
128 (let ((inc (calc-top num))
129 (stuff (calc-top-list (1- num))))
130 (if (eq (nth 1 inc) 'cplx)
131 (setq inc (append '(incomplete polar) (cdr (cdr inc))))
132 (if (eq (nth 1 inc) 'intv)
133 (setq inc (append '(incomplete polar) (cdr (cdr (cdr inc)))))))
134 (cond ((eq (nth 1 inc) 'polar)
135 (let ((new (append inc stuff)))
136 (if (> (length new) 4)
137 (error "Too many components in complex number")
138 (if (= (length new) 2)
139 (setq new (append new '(1)))))
140 (calc-pop-push num new)))
142 (if (> (length inc) 2)
143 (if (math-vectorp (nth 2 inc))
146 (list 'incomplete 'vec (cdr (cdr inc)))
147 (list 'incomplete 'vec)))))
148 ((math-vectorp (car stuff))
150 ((eq (car-safe (car-safe (nth (+ num calc-stack-top)
151 calc-stack))) 'incomplete)
154 (let ((calc-algebraic-mode nil)
155 (calc-incomplete-algebraic-mode nil))
156 (calc-begin-vector)))
157 ((or (= (length inc) 2)
158 (math-vectorp (nth 2 inc)))
160 (append inc (list (cons 'vec stuff)))
161 (list 'incomplete 'vec)))
164 (list 'incomplete 'vec
165 (cons 'vec (append (cdr (cdr inc)) stuff)))
166 (list 'incomplete 'vec))))))))
168 ;; The following variables are initially declared in calc.el,
169 ;; but are used by calc-digit-dots.
170 (defvar calc-prev-char)
171 (defvar calc-prev-prev-char)
172 (defvar calc-digit-value)
174 (defun calc-digit-dots ()
175 (if (eq calc-prev-char ?.)
178 (if (calc-minibuffer-contains ".*\\.\\'")
180 (setq calc-prev-char 'dots
181 last-command-event 32)
182 (if calc-prev-prev-char
184 (setq calc-digit-value nil)
185 (let ((inhibit-read-only t))
188 ;; just ignore extra decimal point, anticipating ".."
194 (let ((num (calc-find-first-incomplete
195 (nthcdr calc-stack-top calc-stack) 1)))
197 (error "Not entering an interval form"))
198 (let* ((inc (calc-top num))
199 (stuff (calc-top-list (1- num)))
200 (new (append inc stuff)))
201 (if (not (eq (nth 1 new) 'intv))
202 (setq new (append '(incomplete intv)
203 (if (eq (nth 1 new) 'vec) '(2) '(0))
205 (if (and (null stuff)
207 (setq new (append new '((neg (var inf var-inf))))))
208 (if (> (length new) 5)
209 (error "Too many components in interval form"))
210 (calc-pop-push num new)))))
212 (defun calc-find-first-incomplete (stack n)
215 ((eq (car-safe (car-safe (car stack))) 'incomplete)
218 (calc-find-first-incomplete (cdr stack) (1+ n)))))
220 (defun calc-incomplete-error (a)
221 (cond ((memq (nth 1 a) '(cplx polar))
222 (error "Complex number is incomplete"))
224 (error "Vector is incomplete"))
225 ((eq (nth 1 a) 'intv)
226 (error "Interval form is incomplete"))
227 (t (error "Object is incomplete"))))
229 (provide 'calc-incom)
231 ;;; calc-incom.el ends here