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