Add 2010 to copyright years.
[bpt/emacs.git] / lisp / calc / calc-incom.el
CommitLineData
3132f345
CW
1;;; calc-incom.el --- complex data type input functions for Calc
2
58ba2f8f 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3132f345
CW
5
6;; Author: David Gillespie <daveg@synaptics.com>
e8fff8ed 7;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
136211a9
EZ
8
9;; This file is part of GNU Emacs.
10
662c9c64 11;; GNU Emacs is free software: you can redistribute it and/or modify
7c671b23 12;; it under the terms of the GNU General Public License as published by
662c9c64
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
7c671b23 15
136211a9 16;; GNU Emacs is distributed in the hope that it will be useful,
7c671b23
GM
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
662c9c64 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
136211a9 23
3132f345 24;;; Commentary:
136211a9 25
3132f345 26;;; Code:
136211a9
EZ
27
28;; This file is autoloaded from calc-ext.el.
136211a9 29
c66b0f18 30(require 'calc-ext)
136211a9
EZ
31(require 'calc-macs)
32
136211a9
EZ
33;;; Incomplete forms.
34
35(defun calc-begin-complex ()
36 (interactive)
37 (calc-wrapper
38 (if (or calc-algebraic-mode calc-incomplete-algebraic-mode)
39 (calc-alg-entry "(")
bf77c646 40 (calc-push (list 'incomplete calc-complex-mode)))))
136211a9
EZ
41
42(defun calc-end-complex ()
43 (interactive)
44 (calc-comma t)
45 (calc-wrapper
46 (let ((top (calc-top 1)))
47 (if (and (eq (car-safe top) 'incomplete)
48 (eq (nth 1 top) 'intv))
49 (progn
50 (if (< (length top) 4)
51 (setq top (append top '((neg (var inf var-inf))))))
52 (if (< (length top) 5)
53 (setq top (append top '((var inf var-inf)))))
54 (calc-enter-result 1 "..)" (cdr top)))
55 (if (not (and (eq (car-safe top) 'incomplete)
56 (memq (nth 1 top) '(cplx polar))))
57 (error "Not entering a complex number"))
58 (while (< (length top) 4)
59 (setq top (append top '(0))))
60 (if (not (and (math-realp (nth 2 top))
61 (math-anglep (nth 3 top))))
62 (error "Components must be real"))
bf77c646 63 (calc-enter-result 1 "()" (cdr top))))))
136211a9
EZ
64
65(defun calc-begin-vector ()
66 (interactive)
67 (calc-wrapper
68 (if (or calc-algebraic-mode calc-incomplete-algebraic-mode)
69 (calc-alg-entry "[")
bf77c646 70 (calc-push '(incomplete vec)))))
136211a9
EZ
71
72(defun calc-end-vector ()
73 (interactive)
74 (calc-comma t)
75 (calc-wrapper
76 (let ((top (calc-top 1)))
77 (if (and (eq (car-safe top) 'incomplete)
78 (eq (nth 1 top) 'intv))
79 (progn
80 (if (< (length top) 4)
81 (setq top (append top '((neg (var inf var-inf))))))
82 (if (< (length top) 5)
83 (setq top (append top '((var inf var-inf)))))
84 (setcar (cdr (cdr top)) (1+ (nth 2 top)))
85 (calc-enter-result 1 "..]" (cdr top)))
86 (if (not (and (eq (car-safe top) 'incomplete)
87 (eq (nth 1 top) 'vec)))
88 (error "Not entering a vector"))
bf77c646 89 (calc-pop-push-record 1 "[]" (cdr top))))))
136211a9
EZ
90
91(defun calc-comma (&optional allow-polar)
92 (interactive)
93 (calc-wrapper
94 (let ((num (calc-find-first-incomplete
95 (nthcdr calc-stack-top calc-stack) 1)))
96 (if (= num 0)
97 (error "Not entering a vector or complex number"))
98 (let* ((inc (calc-top num))
99 (stuff (calc-top-list (1- num)))
100 (new (append inc stuff)))
101 (if (and (null stuff)
102 (not allow-polar)
103 (or (eq (nth 1 inc) 'vec)
104 (< (length new) 4)))
105 (setq new (append new
106 (if (= (length new) 2)
107 '(0)
108 (nthcdr (1- (length new)) new)))))
109 (or allow-polar
110 (if (eq (nth 1 new) 'polar)
111 (setq new (append '(incomplete cplx) (cdr (cdr new))))
112 (if (eq (nth 1 new) 'intv)
113 (setq new (append '(incomplete cplx)
114 (cdr (cdr (cdr new))))))))
115 (if (and (memq (nth 1 new) '(cplx polar))
116 (> (length new) 4))
117 (error "Too many components in complex number"))
118 (if (and (eq (nth 1 new) 'intv)
119 (> (length new) 5))
120 (error "Too many components in interval form"))
bf77c646 121 (calc-pop-push num new)))))
136211a9
EZ
122
123(defun calc-semi ()
124 (interactive)
125 (calc-wrapper
126 (let ((num (calc-find-first-incomplete
127 (nthcdr calc-stack-top calc-stack) 1)))
128 (if (= num 0)
129 (error "Not entering a vector or complex number"))
130 (let ((inc (calc-top num))
131 (stuff (calc-top-list (1- num))))
132 (if (eq (nth 1 inc) 'cplx)
133 (setq inc (append '(incomplete polar) (cdr (cdr inc))))
134 (if (eq (nth 1 inc) 'intv)
135 (setq inc (append '(incomplete polar) (cdr (cdr (cdr inc)))))))
136 (cond ((eq (nth 1 inc) 'polar)
137 (let ((new (append inc stuff)))
138 (if (> (length new) 4)
139 (error "Too many components in complex number")
140 (if (= (length new) 2)
141 (setq new (append new '(1)))))
142 (calc-pop-push num new)))
143 ((null stuff)
144 (if (> (length inc) 2)
145 (if (math-vectorp (nth 2 inc))
146 (calc-comma)
147 (calc-pop-push 1
148 (list 'incomplete 'vec (cdr (cdr inc)))
149 (list 'incomplete 'vec)))))
150 ((math-vectorp (car stuff))
151 (calc-comma))
152 ((eq (car-safe (car-safe (nth (+ num calc-stack-top)
153 calc-stack))) 'incomplete)
154 (calc-end-vector)
155 (calc-comma)
156 (let ((calc-algebraic-mode nil)
157 (calc-incomplete-algebraic-mode nil))
158 (calc-begin-vector)))
159 ((or (= (length inc) 2)
160 (math-vectorp (nth 2 inc)))
161 (calc-pop-push num
162 (append inc (list (cons 'vec stuff)))
163 (list 'incomplete 'vec)))
164 (t
165 (calc-pop-push num
166 (list 'incomplete 'vec
167 (cons 'vec (append (cdr (cdr inc)) stuff)))
bf77c646 168 (list 'incomplete 'vec))))))))
136211a9 169
53cbeb3d
JB
170;; The following variables are initially declared in calc.el,
171;; but are used by calc-digit-dots.
172(defvar calc-prev-char)
173(defvar calc-prev-prev-char)
174(defvar calc-digit-value)
175
136211a9
EZ
176(defun calc-digit-dots ()
177 (if (eq calc-prev-char ?.)
178 (progn
179 (delete-backward-char 1)
180 (if (calc-minibuffer-contains ".*\\.\\'")
181 (delete-backward-char 1))
182 (setq calc-prev-char 'dots
e93c003e 183 last-command-event 32)
136211a9
EZ
184 (if calc-prev-prev-char
185 (calcDigit-nondigit)
186 (setq calc-digit-value nil)
b89c5b71
JB
187 (let ((inhibit-read-only t))
188 (erase-buffer))
136211a9
EZ
189 (exit-minibuffer)))
190 ;; just ignore extra decimal point, anticipating ".."
bf77c646 191 (delete-backward-char 1)))
136211a9
EZ
192
193(defun calc-dots ()
194 (interactive)
195 (calc-wrapper
196 (let ((num (calc-find-first-incomplete
197 (nthcdr calc-stack-top calc-stack) 1)))
198 (if (= num 0)
199 (error "Not entering an interval form"))
200 (let* ((inc (calc-top num))
201 (stuff (calc-top-list (1- num)))
202 (new (append inc stuff)))
203 (if (not (eq (nth 1 new) 'intv))
204 (setq new (append '(incomplete intv)
205 (if (eq (nth 1 new) 'vec) '(2) '(0))
206 (cdr (cdr new)))))
207 (if (and (null stuff)
208 (= (length new) 3))
209 (setq new (append new '((neg (var inf var-inf))))))
210 (if (> (length new) 5)
211 (error "Too many components in interval form"))
bf77c646 212 (calc-pop-push num new)))))
136211a9
EZ
213
214(defun calc-find-first-incomplete (stack n)
215 (cond ((null stack)
216 0)
217 ((eq (car-safe (car-safe (car stack))) 'incomplete)
218 n)
219 (t
bf77c646 220 (calc-find-first-incomplete (cdr stack) (1+ n)))))
136211a9
EZ
221
222(defun calc-incomplete-error (a)
223 (cond ((memq (nth 1 a) '(cplx polar))
224 (error "Complex number is incomplete"))
225 ((eq (nth 1 a) 'vec)
226 (error "Vector is incomplete"))
227 ((eq (nth 1 a) 'intv)
228 (error "Interval form is incomplete"))
bf77c646 229 (t (error "Object is incomplete"))))
136211a9 230
c66b0f18
JB
231(provide 'calc-incom)
232
cbee283d 233;; arch-tag: b8001270-4dc7-481b-a3e3-a952e19b390d
bf77c646 234;;; calc-incom.el ends here