EasyPG 1.07 Released
[packages] / xemacs-packages / calc / calc-incom.el
1 ;;; calc-incom.el --- complex data type input functions for Calc
2
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 ;; This file is autoloaded from calc-ext.el.
27
28 (require 'calc-ext)
29 (require 'calc-macs)
30
31 ;;; Incomplete forms.
32
33 (defun calc-begin-complex ()
34   (interactive)
35   (calc-wrapper
36    (if (or calc-algebraic-mode calc-incomplete-algebraic-mode)
37        (calc-alg-entry "(")
38      (calc-push (list 'incomplete calc-complex-mode)))))
39
40 (defun calc-end-complex ()
41   (interactive)
42   (calc-comma t)
43   (calc-wrapper
44    (let ((top (calc-top 1)))
45      (if (and (eq (car-safe top) 'incomplete)
46               (eq (nth 1 top) 'intv))
47          (progn
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))))))
62
63 (defun calc-begin-vector ()
64   (interactive)
65   (calc-wrapper
66    (if (or calc-algebraic-mode calc-incomplete-algebraic-mode)
67        (calc-alg-entry "[")
68      (calc-push '(incomplete vec)))))
69
70 (defun calc-end-vector ()
71   (interactive)
72   (calc-comma t)
73   (calc-wrapper
74    (let ((top (calc-top 1)))
75      (if (and (eq (car-safe top) 'incomplete)
76               (eq (nth 1 top) 'intv))
77          (progn
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))))))
88
89 (defun calc-comma (&optional allow-polar)
90   (interactive)
91   (calc-wrapper
92    (let ((num (calc-find-first-incomplete
93                (nthcdr calc-stack-top calc-stack) 1)))
94      (if (= num 0)
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)))
99        (if (and (null stuff)
100                 (not allow-polar)
101                 (or (eq (nth 1 inc) 'vec)
102                     (< (length new) 4)))
103            (setq new (append new
104                              (if (= (length new) 2)
105                                  '(0)
106                                (nthcdr (1- (length new)) new)))))
107        (or allow-polar
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))
114                 (> (length new) 4))
115            (error "Too many components in complex number"))
116        (if (and (eq (nth 1 new) 'intv)
117                 (> (length new) 5))
118            (error "Too many components in interval form"))
119        (calc-pop-push num new)))))
120
121 (defun calc-semi ()
122   (interactive)
123   (calc-wrapper
124    (let ((num (calc-find-first-incomplete
125                (nthcdr calc-stack-top calc-stack) 1)))
126      (if (= num 0)
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)))
141              ((null stuff)
142               (if (> (length inc) 2)
143                   (if (math-vectorp (nth 2 inc))
144                       (calc-comma)
145                     (calc-pop-push 1
146                                    (list 'incomplete 'vec (cdr (cdr inc)))
147                                    (list 'incomplete 'vec)))))
148              ((math-vectorp (car stuff))
149               (calc-comma))
150              ((eq (car-safe (car-safe (nth (+ num calc-stack-top)
151                                            calc-stack))) 'incomplete)
152               (calc-end-vector)
153               (calc-comma)
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)))
159               (calc-pop-push num
160                              (append inc (list (cons 'vec stuff)))
161                              (list 'incomplete 'vec)))
162              (t
163               (calc-pop-push num
164                              (list 'incomplete 'vec
165                                    (cons 'vec (append (cdr (cdr inc)) stuff)))
166                              (list 'incomplete 'vec))))))))
167
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)
173
174 (defun calc-digit-dots ()
175   (if (eq calc-prev-char ?.)
176       (progn
177         (delete-char -1)
178         (if (calc-minibuffer-contains ".*\\.\\'")
179             (delete-char -1))
180         (setq calc-prev-char 'dots
181               last-command-char ? )
182         (if calc-prev-prev-char
183             (calcDigit-nondigit)
184           (setq calc-digit-value nil)
185           (let ((inhibit-read-only t))
186             (erase-buffer))
187           (exit-minibuffer)))
188     ;; just ignore extra decimal point, anticipating ".."
189     (delete-char -1)))
190
191 (defun calc-dots ()
192   (interactive)
193   (calc-wrapper
194    (let ((num (calc-find-first-incomplete
195                (nthcdr calc-stack-top calc-stack) 1)))
196      (if (= num 0)
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))
204                              (cdr (cdr new)))))
205        (if (and (null stuff)
206                 (= (length new) 3))
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)))))
211
212 (defun calc-find-first-incomplete (stack n)
213   (cond ((null stack)
214          0)
215         ((eq (car-safe (car-safe (car stack))) 'incomplete)
216          n)
217         (t
218          (calc-find-first-incomplete (cdr stack) (1+ n)))))
219
220 (defun calc-incomplete-error (a)
221   (cond ((memq (nth 1 a) '(cplx polar))
222          (error "Complex number is incomplete"))
223         ((eq (nth 1 a) 'vec)
224          (error "Vector is incomplete"))
225         ((eq (nth 1 a) 'intv)
226          (error "Interval form is incomplete"))
227         (t (error "Object is incomplete"))))
228
229 (provide 'calc-incom)
230
231 ;;; calc-incom.el ends here