calc-incom.el 7.1 KB

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