calc-incom.el 7.1 KB

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