]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-incom.el
Merge from emacs-23
[gnu-emacs] / lisp / calc / calc-incom.el
1 ;;; calc-incom.el --- complex data type input functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; This file is autoloaded from calc-ext.el.
29
30 (require 'calc-ext)
31 (require 'calc-macs)
32
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 "(")
40 (calc-push (list 'incomplete calc-complex-mode)))))
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"))
63 (calc-enter-result 1 "()" (cdr top))))))
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 "[")
70 (calc-push '(incomplete vec)))))
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"))
89 (calc-pop-push-record 1 "[]" (cdr top))))))
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"))
121 (calc-pop-push num new)))))
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)))
168 (list 'incomplete 'vec))))))))
169
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
176 (defun calc-digit-dots ()
177 (if (eq calc-prev-char ?.)
178 (progn
179 (delete-char -1)
180 (if (calc-minibuffer-contains ".*\\.\\'")
181 (delete-char -1))
182 (setq calc-prev-char 'dots
183 last-command-event 32)
184 (if calc-prev-prev-char
185 (calcDigit-nondigit)
186 (setq calc-digit-value nil)
187 (let ((inhibit-read-only t))
188 (erase-buffer))
189 (exit-minibuffer)))
190 ;; just ignore extra decimal point, anticipating ".."
191 (delete-char -1)))
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"))
212 (calc-pop-push num new)))))
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
220 (calc-find-first-incomplete (cdr stack) (1+ n)))))
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"))
229 (t (error "Object is incomplete"))))
230
231 (provide 'calc-incom)
232
233 ;; arch-tag: b8001270-4dc7-481b-a3e3-a952e19b390d
234 ;;; calc-incom.el ends here