calc-aent.el 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. ;;; calc-aent.el --- algebraic entry functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Dave 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.el.
  18. (require 'calc)
  19. (require 'calc-macs)
  20. ;; Declare functions which are defined elsewhere.
  21. (declare-function calc-digit-start-entry "calc" ())
  22. (declare-function calc-refresh-evaltos "calc-ext" (&optional which-var))
  23. (declare-function calc-execute-kbd-macro "calc-prog" (mac arg &rest prefix))
  24. (declare-function math-is-true "calc-ext" (expr))
  25. (declare-function calc-explain-why "calc-stuff" (why &optional more))
  26. (declare-function calc-alg-edit "calc-yank" (str))
  27. (declare-function math-composite-inequalities "calc-prog" (x op))
  28. (declare-function math-flatten-lands "calc-rewr" (expr))
  29. (declare-function math-multi-subst "calc-map" (expr olds news))
  30. (declare-function calcFunc-vmatches "calc-rewr" (expr pat))
  31. (declare-function math-simplify "calc-alg" (top-expr))
  32. (declare-function math-known-matrixp "calc-arith" (a))
  33. (declare-function math-parse-fortran-subscr "calc-lang" (sym args))
  34. (declare-function math-to-radians-2 "calc-math" (a &optional force-symbolic))
  35. (declare-function math-read-string "calc-ext" ())
  36. (declare-function math-read-brackets "calc-vec" (space-sep math-rb-close))
  37. (declare-function math-read-angle-brackets "calc-forms" ())
  38. (declare-function math-to-percentsigns "calccomp" (x))
  39. (defvar calc-quick-calc-history nil
  40. "The history list for quick-calc.")
  41. ;;;###autoload
  42. (defun calc-do-quick-calc (&optional insert)
  43. (require 'calc-ext)
  44. (calc-check-defines)
  45. (if (eq major-mode 'calc-mode)
  46. (calc-algebraic-entry t)
  47. (let (buf shortbuf)
  48. (save-excursion
  49. (calc-create-buffer)
  50. (let* ((calc-command-flags nil)
  51. (calc-dollar-values calc-quick-prev-results)
  52. (calc-dollar-used 0)
  53. (enable-recursive-minibuffers t)
  54. (calc-language (if (memq calc-language '(nil big))
  55. 'flat calc-language))
  56. (entry (calc-do-alg-entry "" "Quick calc: " t 'calc-quick-calc-history))
  57. (alg-exp (mapcar 'math-evaluate-expr entry)))
  58. (when (and (= (length alg-exp) 1)
  59. (eq (car-safe (car alg-exp)) 'calcFunc-assign)
  60. (= (length (car alg-exp)) 3)
  61. (eq (car-safe (nth 1 (car alg-exp))) 'var))
  62. (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp)))
  63. (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp))))
  64. (setq alg-exp (list (nth 2 (car alg-exp)))))
  65. (setq calc-quick-prev-results alg-exp
  66. buf (mapconcat (function (lambda (x)
  67. (math-format-value x 1000)))
  68. alg-exp
  69. " ")
  70. shortbuf buf)
  71. (if (and (= (length alg-exp) 1)
  72. (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
  73. (< (length buf) 20)
  74. (= calc-number-radix 10))
  75. (setq buf (concat buf " ("
  76. (let ((calc-number-radix 16))
  77. (math-format-value (car alg-exp) 1000))
  78. ", "
  79. (let ((calc-number-radix 8))
  80. (math-format-value (car alg-exp) 1000))
  81. ", "
  82. (let ((calc-number-radix 2))
  83. (math-format-value (car alg-exp) 1000))
  84. (if (and (integerp (car alg-exp))
  85. (> (car alg-exp) 0)
  86. (< (car alg-exp) 127))
  87. (format ", \"%c\"" (car alg-exp))
  88. "")
  89. ")")))
  90. (if (and (< (length buf) (frame-width)) (= (length entry) 1)
  91. (featurep 'calc-ext))
  92. (let ((long (concat (math-format-value (car entry) 1000)
  93. " => " buf)))
  94. (if (<= (length long) (- (frame-width) 8))
  95. (setq buf long))))
  96. (calc-handle-whys)
  97. (message "Result: %s" buf)))
  98. (if (or insert
  99. (eq last-command-event 10))
  100. (insert shortbuf)
  101. (kill-new shortbuf)))))
  102. ;;;###autoload
  103. (defun calc-do-calc-eval (str separator args)
  104. (calc-check-defines)
  105. (catch 'calc-error
  106. (save-excursion
  107. (calc-create-buffer)
  108. (cond
  109. ((and (consp str) (not (symbolp (car str))))
  110. (let ((calc-language nil)
  111. (math-expr-opers (math-standard-ops))
  112. (calc-internal-prec 12)
  113. (calc-word-size 32)
  114. (calc-symbolic-mode nil)
  115. (calc-matrix-mode nil)
  116. (calc-angle-mode 'deg)
  117. (calc-number-radix 10)
  118. (calc-twos-complement-mode nil)
  119. (calc-leading-zeros nil)
  120. (calc-group-digits nil)
  121. (calc-point-char ".")
  122. (calc-frac-format '(":" nil))
  123. (calc-prefer-frac nil)
  124. (calc-hms-format "%s@ %s' %s\"")
  125. (calc-date-format '((H ":" mm C SS pp " ")
  126. Www " " Mmm " " D ", " YYYY))
  127. (calc-float-format '(float 0))
  128. (calc-full-float-format '(float 0))
  129. (calc-complex-format nil)
  130. (calc-matrix-just nil)
  131. (calc-full-vectors t)
  132. (calc-break-vectors nil)
  133. (calc-vector-commas ",")
  134. (calc-vector-brackets "[]")
  135. (calc-matrix-brackets '(R O))
  136. (calc-complex-mode 'cplx)
  137. (calc-infinite-mode nil)
  138. (calc-display-strings nil)
  139. (calc-simplify-mode nil)
  140. (calc-display-working-message 'lots)
  141. (strp (cdr str)))
  142. (while strp
  143. (set (car strp) (nth 1 strp))
  144. (setq strp (cdr (cdr strp))))
  145. (calc-do-calc-eval (car str) separator args)))
  146. ((eq separator 'eval)
  147. (eval str))
  148. ((eq separator 'macro)
  149. (require 'calc-ext)
  150. (let* ((calc-buffer (current-buffer))
  151. (calc-window (get-buffer-window calc-buffer))
  152. (save-window (selected-window)))
  153. (if calc-window
  154. (unwind-protect
  155. (progn
  156. (select-window calc-window)
  157. (calc-execute-kbd-macro str nil (car args)))
  158. (and (window-point save-window)
  159. (select-window save-window)))
  160. (save-window-excursion
  161. (select-window (get-largest-window))
  162. (switch-to-buffer calc-buffer)
  163. (calc-execute-kbd-macro str nil (car args)))))
  164. nil)
  165. ((eq separator 'pop)
  166. (or (not (integerp str))
  167. (= str 0)
  168. (calc-pop (min str (calc-stack-size))))
  169. (calc-stack-size))
  170. ((eq separator 'top)
  171. (and (integerp str)
  172. (> str 0)
  173. (<= str (calc-stack-size))
  174. (math-format-value (calc-top-n str (car args)) 1000)))
  175. ((eq separator 'rawtop)
  176. (and (integerp str)
  177. (> str 0)
  178. (<= str (calc-stack-size))
  179. (calc-top-n str (car args))))
  180. (t
  181. (let* ((calc-command-flags nil)
  182. (calc-next-why nil)
  183. (calc-language (if (memq calc-language '(nil big))
  184. 'flat calc-language))
  185. (calc-dollar-values (mapcar
  186. (function
  187. (lambda (x)
  188. (if (stringp x)
  189. (progn
  190. (setq x (math-read-exprs x))
  191. (if (eq (car-safe x)
  192. 'error)
  193. (throw 'calc-error
  194. (calc-eval-error
  195. (cdr x)))
  196. (car x)))
  197. x)))
  198. args))
  199. (calc-dollar-used 0)
  200. (res (if (stringp str)
  201. (math-read-exprs str)
  202. (list str)))
  203. buf)
  204. (if (eq (car res) 'error)
  205. (calc-eval-error (cdr res))
  206. (setq res (mapcar 'calc-normalize res))
  207. (and (memq 'clear-message calc-command-flags)
  208. (message ""))
  209. (cond ((eq separator 'pred)
  210. (require 'calc-ext)
  211. (if (= (length res) 1)
  212. (math-is-true (car res))
  213. (calc-eval-error '(0 "Single value expected"))))
  214. ((eq separator 'raw)
  215. (if (= (length res) 1)
  216. (car res)
  217. (calc-eval-error '(0 "Single value expected"))))
  218. ((eq separator 'list)
  219. res)
  220. ((memq separator '(num rawnum))
  221. (if (= (length res) 1)
  222. (if (math-constp (car res))
  223. (if (eq separator 'num)
  224. (math-format-value (car res) 1000)
  225. (car res))
  226. (calc-eval-error
  227. (list 0
  228. (if calc-next-why
  229. (calc-explain-why (car calc-next-why))
  230. "Number expected"))))
  231. (calc-eval-error '(0 "Single value expected"))))
  232. ((eq separator 'push)
  233. (calc-push-list res)
  234. nil)
  235. (t (while res
  236. (setq buf (concat buf
  237. (and buf (or separator ", "))
  238. (math-format-value (car res) 1000))
  239. res (cdr res)))
  240. buf)))))))))
  241. (defvar calc-eval-error nil
  242. "Determines how calc handles errors.
  243. If nil, return a list containing the character position of error.
  244. STRING means return error message as string rather than list.
  245. The value t means abort and give an error message.")
  246. (defun calc-eval-error (msg)
  247. (if calc-eval-error
  248. (if (eq calc-eval-error 'string)
  249. (nth 1 msg)
  250. (error "%s" (nth 1 msg)))
  251. msg))
  252. ;;;; Reading an expression in algebraic form.
  253. ;;;###autoload
  254. (defun calc-auto-algebraic-entry (&optional prefix)
  255. (interactive "P")
  256. (calc-algebraic-entry prefix t))
  257. ;;;###autoload
  258. (defun calc-algebraic-entry (&optional prefix auto)
  259. (interactive "P")
  260. (calc-wrapper
  261. (let ((calc-language (if prefix nil calc-language))
  262. (math-expr-opers (if prefix (math-standard-ops) (math-expr-ops))))
  263. (calc-alg-entry (and auto (char-to-string last-command-event))))))
  264. (defvar calc-alg-entry-history nil
  265. "History for algebraic entry.")
  266. ;;;###autoload
  267. (defun calc-alg-entry (&optional initial prompt)
  268. (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
  269. (nthcdr calc-stack-top calc-stack)))
  270. (calc-dollar-used 0)
  271. (calc-plain-entry t)
  272. (alg-exp (calc-do-alg-entry initial prompt t 'calc-alg-entry-history)))
  273. (if (stringp alg-exp)
  274. (progn
  275. (require 'calc-ext)
  276. (calc-alg-edit alg-exp))
  277. (let* ((calc-simplify-mode (if (eq last-command-event ?\C-j)
  278. 'none
  279. calc-simplify-mode))
  280. (nvals (mapcar 'calc-normalize alg-exp)))
  281. (while alg-exp
  282. (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
  283. "alg'")
  284. (calc-pop-push-record-list calc-dollar-used
  285. (and (not (equal (car alg-exp)
  286. (car nvals)))
  287. (featurep 'calc-ext)
  288. "")
  289. (list (car nvals)))
  290. (setq alg-exp (cdr alg-exp)
  291. nvals (cdr nvals)
  292. calc-dollar-used 0)))
  293. (calc-handle-whys))))
  294. (defvar calc-alg-ent-map
  295. (let ((map (make-sparse-keymap)))
  296. (set-keymap-parent map minibuffer-local-map)
  297. (define-key map "'" 'calcAlg-previous)
  298. (define-key map "`" 'calcAlg-edit)
  299. (define-key map "\C-m" 'calcAlg-enter)
  300. (define-key map "\C-j" 'calcAlg-enter)
  301. map)
  302. "The keymap used for algebraic entry.")
  303. (defvar calc-alg-ent-esc-map
  304. (let ((map (make-keymap))
  305. (i 33))
  306. (set-keymap-parent map esc-map)
  307. (while (< i 127)
  308. (define-key map (vector i) 'calcAlg-escape)
  309. (setq i (1+ i)))
  310. map)
  311. "The keymap used for escapes in algebraic entry.")
  312. (defvar calc-alg-exp)
  313. ;;;###autoload
  314. (defun calc-do-alg-entry (&optional initial prompt no-normalize history)
  315. (let* ((calc-buffer (current-buffer))
  316. (blink-matching-check-function 'calcAlg-blink-matching-check)
  317. (calc-alg-exp 'error))
  318. (define-key calc-alg-ent-map "\e" nil)
  319. (if (eq calc-algebraic-mode 'total)
  320. (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
  321. (define-key calc-alg-ent-map "\e+" 'calcAlg-plus-minus)
  322. (define-key calc-alg-ent-map "\em" 'calcAlg-mod)
  323. (define-key calc-alg-ent-map "\e=" 'calcAlg-equals)
  324. (define-key calc-alg-ent-map "\e\r" 'calcAlg-equals)
  325. (define-key calc-alg-ent-map "\ep" 'previous-history-element)
  326. (define-key calc-alg-ent-map "\en" 'next-history-element)
  327. (define-key calc-alg-ent-map "\e%" 'self-insert-command))
  328. (setq calc-aborted-prefix nil)
  329. (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
  330. (or initial "")
  331. calc-alg-ent-map nil history)))
  332. (when (eq calc-alg-exp 'error)
  333. (when (eq (car-safe (setq calc-alg-exp (math-read-exprs buf))) 'error)
  334. (setq calc-alg-exp nil)))
  335. (setq calc-aborted-prefix "alg'")
  336. (or no-normalize
  337. (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
  338. calc-alg-exp)))
  339. (defun calcAlg-plus-minus ()
  340. (interactive)
  341. (if (calc-minibuffer-contains ".* \\'")
  342. (insert "+/- ")
  343. (insert " +/- ")))
  344. (defun calcAlg-mod ()
  345. (interactive)
  346. (if (not (calc-minibuffer-contains ".* \\'"))
  347. (insert " "))
  348. (if (calc-minibuffer-contains ".* mod +\\'")
  349. (if calc-previous-modulo
  350. (insert (math-format-flat-expr calc-previous-modulo 0))
  351. (beep))
  352. (insert "mod ")))
  353. (defun calcAlg-previous ()
  354. (interactive)
  355. (if (calc-minibuffer-contains "\\'")
  356. (previous-history-element 1)
  357. (insert "'")))
  358. (defun calcAlg-equals ()
  359. (interactive)
  360. (unwind-protect
  361. (calcAlg-enter)
  362. (if (consp calc-alg-exp)
  363. (progn (setq prefix-arg (length calc-alg-exp))
  364. (calc-unread-command ?=)))))
  365. (defun calcAlg-escape ()
  366. (interactive)
  367. (calc-unread-command)
  368. (save-excursion
  369. (calc-select-buffer)
  370. (use-local-map calc-mode-map))
  371. (calcAlg-enter))
  372. (defvar calc-plain-entry nil)
  373. (defun calcAlg-edit ()
  374. (interactive)
  375. (if (or (not calc-plain-entry)
  376. (calc-minibuffer-contains
  377. "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
  378. (insert "`")
  379. (setq calc-alg-exp (minibuffer-contents))
  380. (exit-minibuffer)))
  381. (defvar calc-buffer)
  382. (defun calcAlg-enter ()
  383. (interactive)
  384. (let* ((str (minibuffer-contents))
  385. (exp (and (> (length str) 0)
  386. (with-current-buffer calc-buffer
  387. (math-read-exprs str)))))
  388. (if (eq (car-safe exp) 'error)
  389. (progn
  390. (goto-char (minibuffer-prompt-end))
  391. (forward-char (nth 1 exp))
  392. (beep)
  393. (calc-temp-minibuffer-message
  394. (concat " [" (or (nth 2 exp) "Error") "]"))
  395. (calc-clear-unread-commands))
  396. (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
  397. '((incomplete vec))
  398. exp))
  399. (exit-minibuffer))))
  400. (defun calcAlg-blink-matching-check (leftpt rightpt)
  401. (let ((rightchar (char-before rightpt))
  402. (leftchar (if leftpt (char-after leftpt))))
  403. (if (and leftpt
  404. (or (and (= rightchar ?\))
  405. (= leftchar ?\[))
  406. (and (= rightchar ?\])
  407. (= leftchar ?\()))
  408. (save-excursion
  409. (goto-char leftpt)
  410. (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
  411. ;; [2..5) perfectly valid!
  412. nil
  413. (blink-matching-check-mismatch leftpt rightpt))))
  414. ;;;###autoload
  415. (defun calc-alg-digit-entry ()
  416. (calc-alg-entry
  417. (calc-digit-start-entry)))
  418. ;; The variable calc-digit-value is initially declared in calc.el,
  419. ;; but can be set by calcDigit-algebraic and calcDigit-edit.
  420. (defvar calc-digit-value)
  421. ;;;###autoload
  422. (defun calcDigit-algebraic ()
  423. (interactive)
  424. (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
  425. (calcDigit-key)
  426. (setq calc-digit-value (minibuffer-contents))
  427. (exit-minibuffer)))
  428. ;;;###autoload
  429. (defun calcDigit-edit ()
  430. (interactive)
  431. (calc-unread-command)
  432. (setq calc-digit-value (minibuffer-contents))
  433. (exit-minibuffer))
  434. ;;; Algebraic expression parsing. [Public]
  435. (defvar math-read-replacement-list
  436. '(;; Misc symbols
  437. ("±" "+/-") ; plus or minus
  438. ("×" "*") ; multiplication sign
  439. ("÷" ":") ; division sign
  440. ("−" "-") ; subtraction sign
  441. ("∕" "/") ; division sign
  442. ("∗" "*") ; asterisk multiplication
  443. ("∞" "inf") ; infinity symbol
  444. ("≤" "<=")
  445. ("≥" ">=")
  446. ("≦" "<=")
  447. ("≧" ">=")
  448. ("µ" "μ")
  449. ;; fractions
  450. ("¼" "(1:4)") ; 1/4
  451. ("½" "(1:2)") ; 1/2
  452. ("¾" "(3:4)") ; 3/4
  453. ("⅓" "(1:3)") ; 1/3
  454. ("⅔" "(2:3)") ; 2/3
  455. ("⅕" "(1:5)") ; 1/5
  456. ("⅖" "(2:5)") ; 2/5
  457. ("⅗" "(3:5)") ; 3/5
  458. ("⅘" "(4:5)") ; 4/5
  459. ("⅙" "(1:6)") ; 1/6
  460. ("⅚" "(5:6)") ; 5/6
  461. ("⅛" "(1:8)") ; 1/8
  462. ("⅜" "(3:8)") ; 3/8
  463. ("⅝" "(5:8)") ; 5/8
  464. ("⅞" "(7:8)") ; 7/8
  465. ("⅟" "1:") ; 1/...
  466. ;; superscripts
  467. ("⁰" "0") ; 0
  468. ("¹" "1") ; 1
  469. ("²" "2") ; 2
  470. ("³" "3") ; 3
  471. ("⁴" "4") ; 4
  472. ("⁵" "5") ; 5
  473. ("⁶" "6") ; 6
  474. ("⁷" "7") ; 7
  475. ("⁸" "8") ; 8
  476. ("⁹" "9") ; 9
  477. ("⁺" "+") ; +
  478. ("⁻" "-") ; -
  479. ("⁽" "(") ; (
  480. ("⁾" ")") ; )
  481. ("ⁿ" "n") ; n
  482. ("ⁱ" "i") ; i
  483. ;; subscripts
  484. ("₀" "0") ; 0
  485. ("₁" "1") ; 1
  486. ("₂" "2") ; 2
  487. ("₃" "3") ; 3
  488. ("₄" "4") ; 4
  489. ("₅" "5") ; 5
  490. ("₆" "6") ; 6
  491. ("₇" "7") ; 7
  492. ("₈" "8") ; 8
  493. ("₉" "9") ; 9
  494. ("₊" "+") ; +
  495. ("₋" "-") ; -
  496. ("₍" "(") ; (
  497. ("₎" ")")) ; )
  498. "A list whose elements (old new) indicate replacements to make
  499. in Calc algebraic input.")
  500. (defvar math-read-superscripts
  501. "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
  502. "A string consisting of the superscripts allowed by Calc.")
  503. (defvar math-read-subscripts
  504. "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
  505. "A string consisting of the subscripts allowed by Calc.")
  506. ;;;###autoload
  507. (defun math-read-preprocess-string (str)
  508. "Replace some substrings of STR by Calc equivalents."
  509. (setq str
  510. (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
  511. "^(\\&)" str))
  512. (setq str
  513. (replace-regexp-in-string (concat "[" math-read-subscripts "]+")
  514. "_(\\&)" str))
  515. (let ((rep-list math-read-replacement-list))
  516. (while rep-list
  517. (setq str
  518. (replace-regexp-in-string (nth 0 (car rep-list))
  519. (nth 1 (car rep-list)) str))
  520. (setq rep-list (cdr rep-list))))
  521. str)
  522. ;; The next few variables are local to math-read-exprs (and math-read-expr
  523. ;; in calc-ext.el), but are set in functions they call.
  524. (defvar math-exp-pos)
  525. (defvar math-exp-str)
  526. (defvar math-exp-old-pos)
  527. (defvar math-exp-token)
  528. (defvar math-exp-keep-spaces)
  529. (defvar math-expr-data)
  530. ;;;###autoload
  531. (defun math-read-exprs (math-exp-str)
  532. (let ((math-exp-pos 0)
  533. (math-exp-old-pos 0)
  534. (math-exp-keep-spaces nil)
  535. math-exp-token math-expr-data)
  536. (setq math-exp-str (math-read-preprocess-string math-exp-str))
  537. (if (memq calc-language calc-lang-allow-percentsigns)
  538. (setq math-exp-str (math-remove-percentsigns math-exp-str)))
  539. (if calc-language-input-filter
  540. (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
  541. (while (setq math-exp-token
  542. (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
  543. (setq math-exp-str
  544. (concat (substring math-exp-str 0 math-exp-token) "\\dots"
  545. (substring math-exp-str (+ math-exp-token 2)))))
  546. (math-build-parse-table)
  547. (math-read-token)
  548. (let ((val (catch 'syntax (math-read-expr-list))))
  549. (if (stringp val)
  550. (list 'error math-exp-old-pos val)
  551. (if (equal math-exp-token 'end)
  552. val
  553. (list 'error math-exp-old-pos "Syntax error"))))))
  554. ;;;###autoload
  555. (defun math-read-expr-list ()
  556. (let* ((math-exp-keep-spaces nil)
  557. (val (list (math-read-expr-level 0)))
  558. (last val))
  559. (while (equal math-expr-data ",")
  560. (math-read-token)
  561. (let ((rest (list (math-read-expr-level 0))))
  562. (setcdr last rest)
  563. (setq last rest)))
  564. val))
  565. (defvar calc-user-parse-table nil)
  566. (defvar calc-last-main-parse-table nil)
  567. (defvar calc-last-user-lang-parse-table nil)
  568. (defvar calc-last-lang-parse-table nil)
  569. (defvar calc-user-tokens nil)
  570. (defvar calc-user-token-chars nil)
  571. (defvar math-toks nil
  572. "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
  573. ;;;###autoload
  574. (defun math-build-parse-table ()
  575. (let ((mtab (cdr (assq nil calc-user-parse-tables)))
  576. (ltab (cdr (assq calc-language calc-user-parse-tables)))
  577. (lltab (get calc-language 'math-parse-table)))
  578. (or (and (eq mtab calc-last-main-parse-table)
  579. (eq ltab calc-last-user-lang-parse-table)
  580. (eq lltab calc-last-lang-parse-table))
  581. (let ((p (append mtab ltab lltab))
  582. (math-toks nil))
  583. (setq calc-user-parse-table p)
  584. (setq calc-user-token-chars nil)
  585. (while p
  586. (math-find-user-tokens (car (car p)))
  587. (setq p (cdr p)))
  588. (setq calc-user-tokens (mapconcat 'identity
  589. (sort (mapcar 'car math-toks)
  590. (function (lambda (x y)
  591. (> (length x)
  592. (length y)))))
  593. "\\|")
  594. calc-last-main-parse-table mtab
  595. calc-last-user-lang-parse-table ltab
  596. calc-last-lang-parse-table lltab)))))
  597. ;;;###autoload
  598. (defun math-find-user-tokens (p)
  599. (while p
  600. (cond ((and (stringp (car p))
  601. (or (> (length (car p)) 1) (equal (car p) "$")
  602. (equal (car p) "\""))
  603. (string-match "[^a-zA-Zα-ωΑ-Ω0-9]" (car p)))
  604. (let ((s (regexp-quote (car p))))
  605. (if (string-match "\\`[a-zA-Zα-ωΑ-Ω0-9]" s)
  606. (setq s (concat "\\<" s)))
  607. (if (string-match "[a-zA-Zα-ωΑ-Ω0-9]\\'" s)
  608. (setq s (concat s "\\>")))
  609. (or (assoc s math-toks)
  610. (progn
  611. (setq math-toks (cons (list s) math-toks))
  612. (or (memq (aref (car p) 0) calc-user-token-chars)
  613. (setq calc-user-token-chars
  614. (cons (aref (car p) 0)
  615. calc-user-token-chars)))))))
  616. ((consp (car p))
  617. (math-find-user-tokens (nth 1 (car p)))
  618. (or (eq (car (car p)) '\?)
  619. (math-find-user-tokens (nth 2 (car p))))))
  620. (setq p (cdr p))))
  621. ;;;###autoload
  622. (defun math-read-token ()
  623. (if (>= math-exp-pos (length math-exp-str))
  624. (setq math-exp-old-pos math-exp-pos
  625. math-exp-token 'end
  626. math-expr-data "\000")
  627. (let (adfn
  628. (ch (aref math-exp-str math-exp-pos)))
  629. (setq math-exp-old-pos math-exp-pos)
  630. (cond ((memq ch '(32 10 9))
  631. (setq math-exp-pos (1+ math-exp-pos))
  632. (if math-exp-keep-spaces
  633. (setq math-exp-token 'space
  634. math-expr-data " ")
  635. (math-read-token)))
  636. ((and (memq ch calc-user-token-chars)
  637. (let ((case-fold-search nil))
  638. (eq (string-match
  639. calc-user-tokens math-exp-str math-exp-pos)
  640. math-exp-pos)))
  641. (setq math-exp-token 'punc
  642. math-expr-data (math-match-substring math-exp-str 0)
  643. math-exp-pos (match-end 0)))
  644. ((or (and (>= ch ?a) (<= ch ?z))
  645. (and (>= ch ?A) (<= ch ?Z))
  646. (and (>= ch ?α) (<= ch ?ω))
  647. (and (>= ch ?Α) (<= ch ?Ω)))
  648. (string-match
  649. (cond
  650. ((and (memq calc-language calc-lang-allow-underscores)
  651. (memq calc-language calc-lang-allow-percentsigns))
  652. "[a-zA-Zα-ωΑ-Ω0-9_'#]*")
  653. ((memq calc-language calc-lang-allow-underscores)
  654. "[a-zA-Zα-ωΑ-Ω0-9_#]*")
  655. (t "[a-zA-Zα-ωΑ-Ω0-9'#]*"))
  656. math-exp-str math-exp-pos)
  657. (setq math-exp-token 'symbol
  658. math-exp-pos (match-end 0)
  659. math-expr-data (math-restore-dashes
  660. (math-match-substring math-exp-str 0)))
  661. (if (setq adfn (get calc-language 'math-lang-adjust-words))
  662. (funcall adfn)))
  663. ((or (and (>= ch ?0) (<= ch ?9))
  664. (and (eq ch '?\.)
  665. (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
  666. math-exp-pos))
  667. (and (eq ch '?_)
  668. (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
  669. math-exp-pos)
  670. (or (eq math-exp-pos 0)
  671. (and (not (memq calc-language
  672. calc-lang-allow-underscores))
  673. (eq (string-match "[^])}\"a-zA-Zα-ωΑ-Ω0-9'$]_"
  674. math-exp-str (1- math-exp-pos))
  675. (1- math-exp-pos))))))
  676. (or (and (memq calc-language calc-lang-c-type-hex)
  677. (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
  678. (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\(0*\\([2-9]\\|1[0-4]\\)\\(#[#]?\\|\\^\\^\\)[0-9a-dA-D.]+[eE][-+_]?[0-9]+\\|0*\\([2-9]\\|[0-2][0-9]\\|3[0-6]\\)\\(#[#]?\\|\\^\\^\\)[0-9a-zA-Zα-ωΑ-Ω:.]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?"
  679. math-exp-str math-exp-pos))
  680. (setq math-exp-token 'number
  681. math-expr-data (math-match-substring math-exp-str 0)
  682. math-exp-pos (match-end 0)))
  683. ((and (setq adfn
  684. (assq ch (get calc-language 'math-lang-read-symbol)))
  685. (eval (nth 1 adfn)))
  686. (eval (nth 2 adfn)))
  687. ((eq ch ?\$)
  688. (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
  689. math-exp-pos)
  690. (setq math-expr-data (- (string-to-number (math-match-substring
  691. math-exp-str 1))))
  692. (string-match "\\$+" math-exp-str math-exp-pos)
  693. (setq math-expr-data (- (match-end 0) (match-beginning 0))))
  694. (setq math-exp-token 'dollar
  695. math-exp-pos (match-end 0)))
  696. ((eq ch ?\#)
  697. (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
  698. math-exp-pos)
  699. (setq math-expr-data (string-to-number
  700. (math-match-substring math-exp-str 1))
  701. math-exp-pos (match-end 0))
  702. (setq math-expr-data 1
  703. math-exp-pos (1+ math-exp-pos)))
  704. (setq math-exp-token 'hash))
  705. ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
  706. math-exp-str math-exp-pos)
  707. math-exp-pos)
  708. (setq math-exp-token 'punc
  709. math-expr-data (math-match-substring math-exp-str 0)
  710. math-exp-pos (match-end 0)))
  711. ((and (eq ch ?\")
  712. (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
  713. math-exp-str math-exp-pos))
  714. (setq math-exp-token 'string
  715. math-expr-data (math-match-substring math-exp-str 1)
  716. math-exp-pos (match-end 0)))
  717. ((and (setq adfn (get calc-language 'math-lang-read))
  718. (eval (nth 0 adfn))
  719. (eval (nth 1 adfn))))
  720. ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
  721. (setq math-exp-pos (match-end 0))
  722. (math-read-token))
  723. (t
  724. (if (setq adfn (assq ch (get calc-language 'math-punc-table)))
  725. (setq ch (cdr adfn)))
  726. (setq math-exp-token 'punc
  727. math-expr-data (char-to-string ch)
  728. math-exp-pos (1+ math-exp-pos)))))))
  729. (defconst math-alg-inequalities
  730. '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
  731. calcFunc-eq calcFunc-neq))
  732. (defun math-read-expr-level (exp-prec &optional exp-term)
  733. (let* ((math-expr-opers (math-expr-ops))
  734. (x (math-read-factor))
  735. (first t)
  736. op op2)
  737. (while (and (or (and calc-user-parse-table
  738. (setq op (calc-check-user-syntax x exp-prec))
  739. (setq x op
  740. op '("2x" ident 999999 -1)))
  741. (and (setq op (assoc math-expr-data math-expr-opers))
  742. (/= (nth 2 op) -1)
  743. (or (and (setq op2 (assoc
  744. math-expr-data
  745. (cdr (memq op math-expr-opers))))
  746. (eq (= (nth 3 op) -1)
  747. (/= (nth 3 op2) -1))
  748. (eq (= (nth 3 op2) -1)
  749. (not (math-factor-after)))
  750. (setq op op2))
  751. t))
  752. (and (or (eq (nth 2 op) -1)
  753. (memq math-exp-token '(symbol number dollar hash))
  754. (equal math-expr-data "(")
  755. (and (equal math-expr-data "[")
  756. (not (equal
  757. (get calc-language
  758. 'math-function-open) "["))
  759. (not (and math-exp-keep-spaces
  760. (eq (car-safe x) 'vec)))))
  761. (or (not (setq op (assoc math-expr-data math-expr-opers)))
  762. (/= (nth 2 op) -1))
  763. (or (not calc-user-parse-table)
  764. (not (eq math-exp-token 'symbol))
  765. (let ((p calc-user-parse-table))
  766. (while (and p
  767. (or (not (integerp
  768. (car (car (car p)))))
  769. (not (equal
  770. (nth 1 (car (car p)))
  771. math-expr-data))))
  772. (setq p (cdr p)))
  773. (not p)))
  774. (setq op (assoc "2x" math-expr-opers))))
  775. (not (and exp-term (equal math-expr-data exp-term)))
  776. (>= (nth 2 op) exp-prec))
  777. (if (not (equal (car op) "2x"))
  778. (math-read-token))
  779. (and (memq (nth 1 op) '(sdev mod))
  780. (require 'calc-ext))
  781. (setq x (cond ((consp (nth 1 op))
  782. (funcall (car (nth 1 op)) x op))
  783. ((eq (nth 3 op) -1)
  784. (if (eq (nth 1 op) 'ident)
  785. x
  786. (if (eq (nth 1 op) 'closing)
  787. (if (eq (nth 2 op) exp-prec)
  788. (progn
  789. (setq exp-prec 1000)
  790. x)
  791. (throw 'syntax "Mismatched delimiters"))
  792. (list (nth 1 op) x))))
  793. ((and (not first)
  794. (memq (nth 1 op) math-alg-inequalities)
  795. (memq (car-safe x) math-alg-inequalities))
  796. (require 'calc-ext)
  797. (math-composite-inequalities x op))
  798. (t (list (nth 1 op)
  799. x
  800. (math-read-expr-level (nth 3 op) exp-term))))
  801. first nil))
  802. x))
  803. ;; calc-arg-values is defined in calc-ext.el, but is used here.
  804. (defvar calc-arg-values)
  805. ;;;###autoload
  806. (defun calc-check-user-syntax (&optional x prec)
  807. (let ((p calc-user-parse-table)
  808. (matches nil)
  809. match rule)
  810. (while (and p
  811. (or (not (progn
  812. (setq rule (car (car p)))
  813. (if x
  814. (and (integerp (car rule))
  815. (>= (car rule) prec)
  816. (equal math-expr-data
  817. (car (setq rule (cdr rule)))))
  818. (equal math-expr-data (car rule)))))
  819. (let ((save-exp-pos math-exp-pos)
  820. (save-exp-old-pos math-exp-old-pos)
  821. (save-exp-token math-exp-token)
  822. (save-exp-data math-expr-data))
  823. (or (not (listp
  824. (setq matches (calc-match-user-syntax rule))))
  825. (let ((args (progn
  826. (require 'calc-ext)
  827. calc-arg-values))
  828. (conds nil)
  829. temp)
  830. (if x
  831. (setq matches (cons x matches)))
  832. (setq match (cdr (car p)))
  833. (while (and (eq (car-safe match)
  834. 'calcFunc-condition)
  835. (= (length match) 3))
  836. (setq conds (append (math-flatten-lands
  837. (nth 2 match))
  838. conds)
  839. match (nth 1 match)))
  840. (while (and conds match)
  841. (require 'calc-ext)
  842. (cond ((eq (car-safe (car conds))
  843. 'calcFunc-let)
  844. (setq temp (car conds))
  845. (or (= (length temp) 3)
  846. (and (= (length temp) 2)
  847. (eq (car-safe (nth 1 temp))
  848. 'calcFunc-assign)
  849. (= (length (nth 1 temp)) 3)
  850. (setq temp (nth 1 temp)))
  851. (setq match nil))
  852. (setq matches (cons
  853. (math-normalize
  854. (math-multi-subst
  855. (nth 2 temp)
  856. args matches))
  857. matches)
  858. args (cons (nth 1 temp)
  859. args)))
  860. ((and (eq (car-safe (car conds))
  861. 'calcFunc-matches)
  862. (= (length (car conds)) 3))
  863. (setq temp (calcFunc-vmatches
  864. (math-multi-subst
  865. (nth 1 (car conds))
  866. args matches)
  867. (nth 2 (car conds))))
  868. (if (eq temp 0)
  869. (setq match nil)
  870. (while (setq temp (cdr temp))
  871. (setq matches (cons (nth 2 (car temp))
  872. matches)
  873. args (cons (nth 1 (car temp))
  874. args)))))
  875. (t
  876. (or (math-is-true (math-simplify
  877. (math-multi-subst
  878. (car conds)
  879. args matches)))
  880. (setq match nil))))
  881. (setq conds (cdr conds)))
  882. (if match
  883. (not (setq match (math-multi-subst
  884. match args matches)))
  885. (setq math-exp-old-pos save-exp-old-pos
  886. math-exp-token save-exp-token
  887. math-expr-data save-exp-data
  888. math-exp-pos save-exp-pos)))))))
  889. (setq p (cdr p)))
  890. (and p match)))
  891. ;;;###autoload
  892. (defun calc-match-user-syntax (p &optional term)
  893. (let ((matches nil)
  894. (save-exp-pos math-exp-pos)
  895. (save-exp-old-pos math-exp-old-pos)
  896. (save-exp-token math-exp-token)
  897. (save-exp-data math-expr-data)
  898. m)
  899. (while (and p
  900. (cond ((stringp (car p))
  901. (and (equal math-expr-data (car p))
  902. (progn
  903. (math-read-token)
  904. t)))
  905. ((integerp (car p))
  906. (and (setq m (catch 'syntax
  907. (math-read-expr-level
  908. (car p)
  909. (if (cdr p)
  910. (if (consp (nth 1 p))
  911. (car (nth 1 (nth 1 p)))
  912. (nth 1 p))
  913. term))))
  914. (not (stringp m))
  915. (setq matches (nconc matches (list m)))))
  916. ((eq (car (car p)) '\?)
  917. (setq m (calc-match-user-syntax (nth 1 (car p))))
  918. (or (nth 2 (car p))
  919. (setq matches
  920. (nconc matches
  921. (list
  922. (cons 'vec (and (listp m) m))))))
  923. (or (listp m) (not (nth 2 (car p)))
  924. (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
  925. (eq math-exp-token 'end)))
  926. (t
  927. (setq m (calc-match-user-syntax (nth 1 (car p))
  928. (car (nth 2 (car p)))))
  929. (if (listp m)
  930. (let ((vec (cons 'vec m))
  931. opos mm)
  932. (while (and (listp
  933. (setq opos math-exp-pos
  934. mm (calc-match-user-syntax
  935. (or (nth 2 (car p))
  936. (nth 1 (car p)))
  937. (car (nth 2 (car p))))))
  938. (> math-exp-pos opos))
  939. (setq vec (nconc vec mm)))
  940. (setq matches (nconc matches (list vec))))
  941. (and (eq (car (car p)) '*)
  942. (setq matches (nconc matches (list '(vec)))))))))
  943. (setq p (cdr p)))
  944. (if p
  945. (setq math-exp-pos save-exp-pos
  946. math-exp-old-pos save-exp-old-pos
  947. math-exp-token save-exp-token
  948. math-expr-data save-exp-data
  949. matches "Failed"))
  950. matches))
  951. ;;;###autoload
  952. (defun math-remove-dashes (x)
  953. (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
  954. (math-remove-dashes
  955. (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
  956. x))
  957. (defun math-remove-percentsigns (x)
  958. (if (string-match "\\`\\(.*\\)%\\(.*\\)\\'" x)
  959. (math-remove-percentsigns
  960. (concat (math-match-substring x 1) "o'o" (math-match-substring x 2)))
  961. x))
  962. (defun math-restore-dashes (x)
  963. (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
  964. (math-restore-dashes
  965. (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
  966. x))
  967. (defun math-restore-placeholders (x)
  968. "Replace placeholders by the proper characters in the symbol x.
  969. This includes `#' for `_' and `'' for `%'.
  970. If the current Calc language does not use placeholders, return nil."
  971. (if (or (memq calc-language calc-lang-allow-underscores)
  972. (memq calc-language calc-lang-allow-percentsigns))
  973. (let ((sx (symbol-name x)))
  974. (when (memq calc-language calc-lang-allow-percentsigns)
  975. (require 'calccomp)
  976. (setq sx (math-to-percentsigns sx)))
  977. (if (memq calc-language calc-lang-allow-underscores)
  978. (setq sx (math-string-restore-underscores sx)))
  979. (intern-soft sx))))
  980. (defun math-string-restore-underscores (x)
  981. "Replace pound signs by underscores in the string x."
  982. (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x)
  983. (math-string-restore-underscores
  984. (concat (math-match-substring x 1) "_" (math-match-substring x 2)))
  985. x))
  986. ;;;###autoload
  987. (defun math-read-if (cond op)
  988. (let ((then (math-read-expr-level 0)))
  989. (or (equal math-expr-data ":")
  990. (throw 'syntax "Expected `:'"))
  991. (math-read-token)
  992. (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
  993. (defun math-factor-after ()
  994. (let ((math-exp-pos math-exp-pos)
  995. math-exp-old-pos math-exp-token math-expr-data)
  996. (math-read-token)
  997. (or (memq math-exp-token '(number symbol dollar hash string))
  998. (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
  999. (assoc (concat "u" math-expr-data) math-expr-opers))
  1000. (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
  1001. (assoc math-expr-data '(("(") ("[") ("{"))))))
  1002. (defun math-read-factor ()
  1003. (let ((math-expr-opers (math-expr-ops))
  1004. op)
  1005. (cond ((eq math-exp-token 'number)
  1006. (let ((num (math-read-number math-expr-data)))
  1007. (if (not num)
  1008. (progn
  1009. (setq math-exp-old-pos math-exp-pos)
  1010. (throw 'syntax "Bad format")))
  1011. (math-read-token)
  1012. (if (and math-read-expr-quotes
  1013. (consp num))
  1014. (list 'quote num)
  1015. num)))
  1016. ((and calc-user-parse-table
  1017. (setq op (calc-check-user-syntax)))
  1018. op)
  1019. ((or (equal math-expr-data "-")
  1020. (equal math-expr-data "+")
  1021. (equal math-expr-data "!")
  1022. (equal math-expr-data "|")
  1023. (equal math-expr-data "/"))
  1024. (setq math-expr-data (concat "u" math-expr-data))
  1025. (math-read-factor))
  1026. ((and (setq op (assoc math-expr-data math-expr-opers))
  1027. (eq (nth 2 op) -1))
  1028. (if (consp (nth 1 op))
  1029. (funcall (car (nth 1 op)) op)
  1030. (math-read-token)
  1031. (let ((val (math-read-expr-level (nth 3 op))))
  1032. (cond ((eq (nth 1 op) 'ident)
  1033. val)
  1034. ((and (Math-numberp val)
  1035. (equal (car op) "u-"))
  1036. (math-neg val))
  1037. (t (list (nth 1 op) val))))))
  1038. ((eq math-exp-token 'symbol)
  1039. (let ((sym (intern math-expr-data)))
  1040. (math-read-token)
  1041. (if (equal math-expr-data calc-function-open)
  1042. (let ((f (assq sym math-expr-function-mapping)))
  1043. (math-read-token)
  1044. (if (consp (cdr f))
  1045. (funcall (car (cdr f)) f sym)
  1046. (let ((args (if (or (equal math-expr-data calc-function-close)
  1047. (eq math-exp-token 'end))
  1048. nil
  1049. (math-read-expr-list))))
  1050. (if (not (or (equal math-expr-data calc-function-close)
  1051. (eq math-exp-token 'end)))
  1052. (throw 'syntax "Expected `)'"))
  1053. (math-read-token)
  1054. (if (and (memq calc-language
  1055. calc-lang-parens-are-subscripts)
  1056. args
  1057. (require 'calc-ext)
  1058. (let ((calc-matrix-mode 'scalar))
  1059. (math-known-matrixp
  1060. (list 'var sym
  1061. (intern
  1062. (concat "var-"
  1063. (symbol-name sym)))))))
  1064. (math-parse-fortran-subscr sym args)
  1065. (if f
  1066. (setq sym (cdr f))
  1067. (and (= (aref (symbol-name sym) 0) ?\\)
  1068. (< (prefix-numeric-value calc-language-option)
  1069. 0)
  1070. (setq sym (intern (substring (symbol-name sym)
  1071. 1))))
  1072. (or (string-match "-" (symbol-name sym))
  1073. (setq sym (intern
  1074. (concat "calcFunc-"
  1075. (symbol-name sym))))))
  1076. (cons sym args)))))
  1077. (if math-read-expr-quotes
  1078. sym
  1079. (let ((val (list 'var
  1080. (intern (math-remove-dashes
  1081. (symbol-name sym)))
  1082. (if (string-match "-" (symbol-name sym))
  1083. sym
  1084. (intern (concat "var-"
  1085. (symbol-name sym)))))))
  1086. (let ((v (or
  1087. (assq (nth 1 val) math-expr-variable-mapping)
  1088. (assq (math-restore-placeholders (nth 1 val))
  1089. math-expr-variable-mapping))))
  1090. (and v (setq val (if (consp (cdr v))
  1091. (funcall (car (cdr v)) v val)
  1092. (list 'var
  1093. (intern
  1094. (substring (symbol-name (cdr v))
  1095. 4))
  1096. (cdr v))))))
  1097. (while (and (memq calc-language
  1098. calc-lang-brackets-are-subscripts)
  1099. (equal math-expr-data "["))
  1100. (math-read-token)
  1101. (let ((el (math-read-expr-list)))
  1102. (while el
  1103. (setq val (append (list 'calcFunc-subscr val)
  1104. (list (car el))))
  1105. (setq el (cdr el))))
  1106. (if (equal math-expr-data "]")
  1107. (math-read-token)
  1108. (throw 'syntax "Expected `]'")))
  1109. val)))))
  1110. ((eq math-exp-token 'dollar)
  1111. (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
  1112. (if (>= (length calc-dollar-values) abs)
  1113. (let ((num math-expr-data))
  1114. (math-read-token)
  1115. (setq calc-dollar-used (max calc-dollar-used num))
  1116. (math-check-complete (nth (1- abs) calc-dollar-values)))
  1117. (throw 'syntax (if calc-dollar-values
  1118. "Too many $'s"
  1119. "$'s not allowed in this context")))))
  1120. ((eq math-exp-token 'hash)
  1121. (or calc-hashes-used
  1122. (throw 'syntax "#'s not allowed in this context"))
  1123. (require 'calc-ext)
  1124. (if (<= math-expr-data (length calc-arg-values))
  1125. (let ((num math-expr-data))
  1126. (math-read-token)
  1127. (setq calc-hashes-used (max calc-hashes-used num))
  1128. (nth (1- num) calc-arg-values))
  1129. (throw 'syntax "Too many # arguments")))
  1130. ((equal math-expr-data "(")
  1131. (let* ((exp (let ((math-exp-keep-spaces nil))
  1132. (math-read-token)
  1133. (if (or (equal math-expr-data "\\dots")
  1134. (equal math-expr-data "\\ldots"))
  1135. '(neg (var inf var-inf))
  1136. (math-read-expr-level 0)))))
  1137. (let ((math-exp-keep-spaces nil))
  1138. (cond
  1139. ((equal math-expr-data ",")
  1140. (progn
  1141. (math-read-token)
  1142. (let ((exp2 (math-read-expr-level 0)))
  1143. (setq exp
  1144. (if (and exp2 (Math-realp exp) (Math-realp exp2))
  1145. (math-normalize (list 'cplx exp exp2))
  1146. (list '+ exp (list '* exp2 '(var i var-i))))))))
  1147. ((equal math-expr-data ";")
  1148. (progn
  1149. (math-read-token)
  1150. (let ((exp2 (math-read-expr-level 0)))
  1151. (setq exp (if (and exp2 (Math-realp exp)
  1152. (Math-anglep exp2))
  1153. (math-normalize (list 'polar exp exp2))
  1154. (require 'calc-ext)
  1155. (list '* exp
  1156. (list 'calcFunc-exp
  1157. (list '*
  1158. (math-to-radians-2 exp2)
  1159. '(var i var-i)))))))))
  1160. ((or (equal math-expr-data "\\dots")
  1161. (equal math-expr-data "\\ldots"))
  1162. (progn
  1163. (math-read-token)
  1164. (let ((exp2 (if (or (equal math-expr-data ")")
  1165. (equal math-expr-data "]")
  1166. (eq math-exp-token 'end))
  1167. '(var inf var-inf)
  1168. (math-read-expr-level 0))))
  1169. (setq exp
  1170. (list 'intv
  1171. (if (equal math-expr-data ")") 0 1)
  1172. exp
  1173. exp2)))))))
  1174. (if (not (or (equal math-expr-data ")")
  1175. (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
  1176. (eq math-exp-token 'end)))
  1177. (throw 'syntax "Expected `)'"))
  1178. (math-read-token)
  1179. exp))
  1180. ((eq math-exp-token 'string)
  1181. (require 'calc-ext)
  1182. (math-read-string))
  1183. ((equal math-expr-data "[")
  1184. (require 'calc-ext)
  1185. (math-read-brackets t "]"))
  1186. ((equal math-expr-data "{")
  1187. (require 'calc-ext)
  1188. (math-read-brackets nil "}"))
  1189. ((equal math-expr-data "<")
  1190. (require 'calc-ext)
  1191. (math-read-angle-brackets))
  1192. (t (throw 'syntax "Expected a number")))))
  1193. (provide 'calc-aent)
  1194. ;; Local variables:
  1195. ;; generated-autoload-file: "calc-loaddefs.el"
  1196. ;; End:
  1197. ;;; calc-aent.el ends here