calc-prog.el 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. ;;; calc-prog.el --- user programmability functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2017 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. ;; Declare functions which are defined elsewhere.
  21. (declare-function edmacro-format-keys "edmacro" (macro &optional verbose))
  22. (declare-function edmacro-parse-keys "edmacro" (string &optional need-vector))
  23. (declare-function math-read-expr-level "calc-aent" (exp-prec &optional exp-term))
  24. (defun calc-equal-to (arg)
  25. (interactive "P")
  26. (calc-wrapper
  27. (if (and (integerp arg) (> arg 2))
  28. (calc-enter-result arg "eq" (cons 'calcFunc-eq (calc-top-list-n arg)))
  29. (calc-binary-op "eq" 'calcFunc-eq arg))))
  30. (defun calc-remove-equal (arg)
  31. (interactive "P")
  32. (calc-wrapper
  33. (calc-unary-op "rmeq" 'calcFunc-rmeq arg)))
  34. (defun calc-not-equal-to (arg)
  35. (interactive "P")
  36. (calc-wrapper
  37. (if (and (integerp arg) (> arg 2))
  38. (calc-enter-result arg "neq" (cons 'calcFunc-neq (calc-top-list-n arg)))
  39. (calc-binary-op "neq" 'calcFunc-neq arg))))
  40. (defun calc-less-than (arg)
  41. (interactive "P")
  42. (calc-wrapper
  43. (calc-binary-op "lt" 'calcFunc-lt arg)))
  44. (defun calc-greater-than (arg)
  45. (interactive "P")
  46. (calc-wrapper
  47. (calc-binary-op "gt" 'calcFunc-gt arg)))
  48. (defun calc-less-equal (arg)
  49. (interactive "P")
  50. (calc-wrapper
  51. (calc-binary-op "leq" 'calcFunc-leq arg)))
  52. (defun calc-greater-equal (arg)
  53. (interactive "P")
  54. (calc-wrapper
  55. (calc-binary-op "geq" 'calcFunc-geq arg)))
  56. (defun calc-in-set (arg)
  57. (interactive "P")
  58. (calc-wrapper
  59. (calc-binary-op "in" 'calcFunc-in arg)))
  60. (defun calc-logical-and (arg)
  61. (interactive "P")
  62. (calc-wrapper
  63. (calc-binary-op "land" 'calcFunc-land arg 1)))
  64. (defun calc-logical-or (arg)
  65. (interactive "P")
  66. (calc-wrapper
  67. (calc-binary-op "lor" 'calcFunc-lor arg 0)))
  68. (defun calc-logical-not (arg)
  69. (interactive "P")
  70. (calc-wrapper
  71. (calc-unary-op "lnot" 'calcFunc-lnot arg)))
  72. (defun calc-logical-if ()
  73. (interactive)
  74. (calc-wrapper
  75. (calc-enter-result 3 "if" (cons 'calcFunc-if (calc-top-list-n 3)))))
  76. (defun calc-timing (n)
  77. (interactive "P")
  78. (calc-wrapper
  79. (calc-change-mode 'calc-timing n nil t)
  80. (message (if calc-timing
  81. "Reporting timing of slow commands in Trail"
  82. "Not reporting timing of commands"))))
  83. (defun calc-pass-errors ()
  84. (interactive)
  85. ;; The following two cases are for the new, optimizing byte compiler
  86. ;; or the standard 18.57 byte compiler, respectively.
  87. (condition-case err
  88. (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 15)))
  89. (or (memq (car-safe (car-safe place)) '(error xxxerror))
  90. (setq place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 27)))
  91. (or (memq (car (car place)) '(error xxxerror))
  92. (error "foo"))
  93. (setcar (car place) 'xxxerror))
  94. (error (error "The calc-do function has been modified; unable to patch"))))
  95. (defun calc-user-define ()
  96. (interactive)
  97. (message "Define user key: z-")
  98. (let ((key (read-char)))
  99. (if (= (calc-user-function-classify key) 0)
  100. (error "Can't redefine \"?\" key"))
  101. (let ((func (intern (completing-read (concat "Set key z "
  102. (char-to-string key)
  103. " to command: ")
  104. obarray
  105. 'commandp
  106. t
  107. "calc-"))))
  108. (let* ((kmap (calc-user-key-map))
  109. (old (assq key kmap)))
  110. ;; FIXME: Why not (define-key kmap (vector key) func)?
  111. (if old
  112. (setcdr old func)
  113. (setcdr kmap (cons (cons key func) (cdr kmap))))))))
  114. (defun calc-user-undefine ()
  115. (interactive)
  116. (message "Undefine user key: z-")
  117. (let ((key (read-char)))
  118. (if (= (calc-user-function-classify key) 0)
  119. (error "Can't undefine \"?\" key"))
  120. (let* ((kmap (calc-user-key-map)))
  121. (delq (or (assq key kmap)
  122. (assq (upcase key) kmap)
  123. (assq (downcase key) kmap)
  124. (error "No such user key is defined"))
  125. kmap))))
  126. ;; math-integral-cache-state is originally declared in calcalg2.el,
  127. ;; it is used in calc-user-define-variable.
  128. (defvar math-integral-cache-state)
  129. ;; calc-user-formula-alist is local to calc-user-define-formula,
  130. ;; calc-user-define-composition and calc-finish-formula-edit,
  131. ;; but is used by calc-fix-user-formula.
  132. (defvar calc-user-formula-alist)
  133. (defun calc-user-define-formula ()
  134. (interactive)
  135. (calc-wrapper
  136. (let* ((form (calc-top 1))
  137. (math-arglist nil)
  138. (is-lambda (and (eq (car-safe form) 'calcFunc-lambda)
  139. (>= (length form) 2)))
  140. odef key keyname cmd cmd-base cmd-base-default
  141. func calc-user-formula-alist is-symb)
  142. (if is-lambda
  143. (setq math-arglist (mapcar (function (lambda (x) (nth 1 x)))
  144. (nreverse (cdr (reverse (cdr form)))))
  145. form (nth (1- (length form)) form))
  146. (calc-default-formula-arglist form)
  147. (setq math-arglist (sort math-arglist 'string-lessp)))
  148. (message "Define user key: z-")
  149. (setq key (read-char))
  150. (if (= (calc-user-function-classify key) 0)
  151. (error "Can't redefine \"?\" key"))
  152. (setq key (and (not (memq key '(13 32))) key)
  153. keyname (and key
  154. (if (or (and (<= ?0 key) (<= key ?9))
  155. (and (<= ?a key) (<= key ?z))
  156. (and (<= ?A key) (<= key ?Z)))
  157. (char-to-string key)
  158. (format "%03d" key)))
  159. odef (assq key (calc-user-key-map)))
  160. (unless keyname
  161. (setq keyname (format "%05d" (abs (% (random) 10000)))))
  162. (while
  163. (progn
  164. (setq cmd-base-default (concat "User-" keyname))
  165. (setq cmd (completing-read
  166. (concat "Define M-x command name (default calc-"
  167. cmd-base-default
  168. "): ")
  169. obarray 'commandp nil
  170. (if (and odef (symbolp (cdr odef)))
  171. (symbol-name (cdr odef))
  172. "calc-")))
  173. (if (or (string-equal cmd "")
  174. (string-equal cmd "calc-"))
  175. (setq cmd (concat "calc-User-" keyname)))
  176. (setq cmd-base (and (string-match "\\`calc-\\(.+\\)\\'" cmd)
  177. (math-match-substring cmd 1)))
  178. (setq cmd (intern cmd))
  179. (and cmd
  180. (fboundp cmd)
  181. odef
  182. (not
  183. (y-or-n-p
  184. (if (get cmd 'calc-user-defn)
  185. (concat "Replace previous definition for "
  186. (symbol-name cmd) "? ")
  187. "That name conflicts with a built-in Emacs function. Replace this function? "))))))
  188. (while
  189. (progn
  190. (setq cmd-base-default
  191. (if cmd-base
  192. (if (string-match
  193. "\\`User-.+" cmd-base)
  194. (concat
  195. "User"
  196. (substring cmd-base 5))
  197. cmd-base)
  198. (concat "User" keyname)))
  199. (setq func
  200. (concat "calcFunc-"
  201. (completing-read
  202. (concat "Define algebraic function name (default "
  203. cmd-base-default "): ")
  204. (mapcar (lambda (x) (substring x 9))
  205. (all-completions "calcFunc-"
  206. obarray))
  207. (lambda (x)
  208. (fboundp
  209. (intern (concat "calcFunc-" x))))
  210. nil)))
  211. (setq func
  212. (if (string-equal func "calcFunc-")
  213. (intern (concat "calcFunc-" cmd-base-default))
  214. (intern func)))
  215. (and func
  216. (fboundp func)
  217. (not (fboundp cmd))
  218. odef
  219. (not
  220. (y-or-n-p
  221. (if (get func 'calc-user-defn)
  222. (concat "Replace previous definition for "
  223. (symbol-name func) "? ")
  224. "That name conflicts with a built-in Emacs function. Replace this function? "))))))
  225. (if (not func)
  226. (setq func (intern (concat "calcFunc-User"
  227. (or keyname
  228. (and cmd (symbol-name cmd))
  229. (format "%05d" (% (random) 10000)))))))
  230. (if is-lambda
  231. (setq calc-user-formula-alist math-arglist)
  232. (while
  233. (progn
  234. (setq calc-user-formula-alist
  235. (read-from-minibuffer "Function argument list: "
  236. (if math-arglist
  237. (prin1-to-string math-arglist)
  238. "()")
  239. minibuffer-local-map
  240. t))
  241. (and (not (calc-subsetp calc-user-formula-alist math-arglist))
  242. (not (y-or-n-p
  243. "Okay for arguments that don't appear in formula to be ignored? "))))))
  244. (setq is-symb (and calc-user-formula-alist
  245. func
  246. (y-or-n-p
  247. "Leave it symbolic for non-constant arguments? ")))
  248. (setq calc-user-formula-alist
  249. (mapcar (function (lambda (x)
  250. (or (cdr (assq x '((nil . arg-nil)
  251. (t . arg-t))))
  252. x))) calc-user-formula-alist))
  253. (if cmd
  254. (progn
  255. (require 'calc-macs)
  256. (fset cmd
  257. (list 'lambda
  258. '()
  259. '(interactive)
  260. (list 'calc-wrapper
  261. (list 'calc-enter-result
  262. (length calc-user-formula-alist)
  263. (let ((name (symbol-name (or func cmd))))
  264. (and (string-match
  265. "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'"
  266. name)
  267. (math-match-substring name 1)))
  268. (list 'cons
  269. (list 'quote func)
  270. (list 'calc-top-list-n
  271. (length calc-user-formula-alist)))))))
  272. (put cmd 'calc-user-defn t)))
  273. (let ((body (list 'math-normalize (calc-fix-user-formula form))))
  274. (fset func
  275. (append
  276. (list 'lambda calc-user-formula-alist)
  277. (and is-symb
  278. (mapcar (function (lambda (v)
  279. (list 'math-check-const v t)))
  280. calc-user-formula-alist))
  281. (list body))))
  282. (put func 'calc-user-defn form)
  283. (setq math-integral-cache-state nil)
  284. (if key
  285. (let* ((kmap (calc-user-key-map))
  286. (old (assq key kmap)))
  287. ;; FIXME: Why not (define-key kmap (vector key) cmd)?
  288. (if old
  289. (setcdr old cmd)
  290. (setcdr kmap (cons (cons key cmd) (cdr kmap)))))))
  291. (message "")))
  292. (defvar math-arglist) ; dynamically bound in all callers
  293. (defun calc-default-formula-arglist (form)
  294. (if (consp form)
  295. (if (eq (car form) 'var)
  296. (if (or (memq (nth 1 form) math-arglist)
  297. (math-const-var form))
  298. ()
  299. (setq math-arglist (cons (nth 1 form) math-arglist)))
  300. (calc-default-formula-arglist-step (cdr form)))))
  301. (defun calc-default-formula-arglist-step (l)
  302. (and l
  303. (progn
  304. (calc-default-formula-arglist (car l))
  305. (calc-default-formula-arglist-step (cdr l)))))
  306. (defun calc-subsetp (a b)
  307. (or (null a)
  308. (and (memq (car a) b)
  309. (calc-subsetp (cdr a) b))))
  310. (defun calc-fix-user-formula (f)
  311. (if (consp f)
  312. (let (temp)
  313. (cond ((and (eq (car f) 'var)
  314. (memq (setq temp (or (cdr (assq (nth 1 f) '((nil . arg-nil)
  315. (t . arg-t))))
  316. (nth 1 f)))
  317. calc-user-formula-alist))
  318. temp)
  319. ((or (math-constp f) (eq (car f) 'var))
  320. (list 'quote f))
  321. ((and (eq (car f) 'calcFunc-eval)
  322. (= (length f) 2))
  323. (list 'let '((calc-simplify-mode nil))
  324. (list 'math-normalize (calc-fix-user-formula (nth 1 f)))))
  325. ((and (eq (car f) 'calcFunc-evalsimp)
  326. (= (length f) 2))
  327. (list 'math-simplify (calc-fix-user-formula (nth 1 f))))
  328. ((and (eq (car f) 'calcFunc-evalextsimp)
  329. (= (length f) 2))
  330. (list 'math-simplify-extended
  331. (calc-fix-user-formula (nth 1 f))))
  332. (t
  333. (cons 'list
  334. (cons (list 'quote (car f))
  335. (mapcar 'calc-fix-user-formula (cdr f)))))))
  336. f))
  337. (defun calc-user-define-composition ()
  338. (interactive)
  339. (calc-wrapper
  340. (if (eq calc-language 'unform)
  341. (error "Can't define formats for unformatted mode"))
  342. (let* ((comp (calc-top 1))
  343. (func (intern
  344. (concat "calcFunc-"
  345. (completing-read "Define format for which function: "
  346. (mapcar (lambda (x) (substring x 9))
  347. (all-completions "calcFunc-"
  348. obarray))
  349. (lambda (x)
  350. (fboundp
  351. (intern (concat "calcFunc-" x))))))))
  352. (comps (get func 'math-compose-forms))
  353. entry entry2
  354. (math-arglist nil)
  355. (calc-user-formula-alist nil))
  356. (if (math-zerop comp)
  357. (if (setq entry (assq calc-language comps))
  358. (put func 'math-compose-forms (delq entry comps)))
  359. (calc-default-formula-arglist comp)
  360. (setq math-arglist (sort math-arglist 'string-lessp))
  361. (while
  362. (progn
  363. (setq calc-user-formula-alist
  364. (read-from-minibuffer "Composition argument list: "
  365. (if math-arglist
  366. (prin1-to-string math-arglist)
  367. "()")
  368. minibuffer-local-map
  369. t))
  370. (and (not (calc-subsetp calc-user-formula-alist math-arglist))
  371. (y-or-n-p
  372. "Okay for arguments that don't appear in formula to be invisible? "))))
  373. (or (setq entry (assq calc-language comps))
  374. (put func 'math-compose-forms
  375. (cons (setq entry (list calc-language)) comps)))
  376. (or (setq entry2 (assq (length calc-user-formula-alist) (cdr entry)))
  377. (setcdr entry
  378. (cons (setq entry2
  379. (list (length calc-user-formula-alist))) (cdr entry))))
  380. (setcdr entry2
  381. (list 'lambda calc-user-formula-alist (calc-fix-user-formula comp))))
  382. (calc-pop-stack 1)
  383. (calc-do-refresh))))
  384. (defun calc-user-define-kbd-macro (arg)
  385. (interactive "P")
  386. (or last-kbd-macro
  387. (error "No keyboard macro defined"))
  388. (message "Define last kbd macro on user key: z-")
  389. (let ((key (read-char)))
  390. (if (= (calc-user-function-classify key) 0)
  391. (error "Can't redefine \"?\" key"))
  392. (let ((cmd (intern (completing-read "Full name for new command: "
  393. obarray
  394. 'commandp
  395. nil
  396. (concat "calc-User-"
  397. (if (or (and (>= key ?a)
  398. (<= key ?z))
  399. (and (>= key ?A)
  400. (<= key ?Z))
  401. (and (>= key ?0)
  402. (<= key ?9)))
  403. (char-to-string key)
  404. (format "%03d" key)))))))
  405. (and (fboundp cmd)
  406. (not (let ((f (symbol-function cmd)))
  407. (or (stringp f)
  408. (and (consp f)
  409. (eq (car-safe (nth 3 f))
  410. 'calc-execute-kbd-macro)))))
  411. (error "Function %s is already defined and not a keyboard macro"
  412. cmd))
  413. (put cmd 'calc-user-defn t)
  414. (fset cmd (if (< (prefix-numeric-value arg) 0)
  415. last-kbd-macro
  416. (list 'lambda
  417. '(arg)
  418. '(interactive "P")
  419. (list 'calc-execute-kbd-macro
  420. (vector (key-description last-kbd-macro)
  421. last-kbd-macro)
  422. 'arg
  423. (format "z%c" key)))))
  424. (let* ((kmap (calc-user-key-map))
  425. (old (assq key kmap)))
  426. ;; FIXME: Why not (define-key kmap (vector key) func)?
  427. (if old
  428. (setcdr old cmd)
  429. (setcdr kmap (cons (cons key cmd) (cdr kmap))))))))
  430. (defun calc-edit-user-syntax ()
  431. (interactive)
  432. (calc-wrapper
  433. (let ((lang calc-language))
  434. (calc-edit-mode (list 'calc-finish-user-syntax-edit (list 'quote lang))
  435. t
  436. (format "Editing %s-Mode Syntax Table. "
  437. (cond ((null lang) "Normal")
  438. ((eq lang 'tex) "TeX")
  439. ((eq lang 'latex) "LaTeX")
  440. (t (capitalize (symbol-name lang))))))
  441. (calc-write-parse-table (cdr (assq lang calc-user-parse-tables))
  442. lang)))
  443. (calc-show-edit-buffer))
  444. (defvar calc-original-buffer)
  445. (defun calc-finish-user-syntax-edit (lang)
  446. (let ((tab (calc-read-parse-table calc-original-buffer lang))
  447. (entry (assq lang calc-user-parse-tables)))
  448. (if tab
  449. (setcdr (or entry
  450. (car (setq calc-user-parse-tables
  451. (cons (list lang) calc-user-parse-tables))))
  452. tab)
  453. (if entry
  454. (setq calc-user-parse-tables
  455. (delq entry calc-user-parse-tables)))))
  456. (switch-to-buffer calc-original-buffer))
  457. ;; The variable calc-lang is local to calc-write-parse-table, but is
  458. ;; used by calc-write-parse-table-part which is called by
  459. ;; calc-write-parse-table. The variable is also local to
  460. ;; calc-read-parse-table, but is used by calc-fix-token-name which
  461. ;; is called (indirectly) by calc-read-parse-table.
  462. (defvar calc-lang)
  463. (defun calc-write-parse-table (tab calc-lang)
  464. (let ((p tab))
  465. (while p
  466. (calc-write-parse-table-part (car (car p)))
  467. (insert ":= "
  468. (let ((math-format-hash-args t))
  469. (math-format-flat-expr (cdr (car p)) 0))
  470. "\n")
  471. (setq p (cdr p)))))
  472. (defun calc-write-parse-table-part (p)
  473. (while p
  474. (cond ((stringp (car p))
  475. (let ((s (car p)))
  476. (if (and (string-match "\\`\\\\dots\\>" s)
  477. (not (memq calc-lang '(tex latex))))
  478. (setq s (concat ".." (substring s 5))))
  479. (if (or (and (string-match
  480. "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s)
  481. (string-match "[^a-zA-Z0-9\\]" s))
  482. (and (assoc s '((")") ("]") (">")))
  483. (not (cdr p))))
  484. (insert (prin1-to-string s) " ")
  485. (insert s " "))))
  486. ((integerp (car p))
  487. (insert "#")
  488. (or (= (car p) 0)
  489. (insert "/" (int-to-string (car p))))
  490. (insert " "))
  491. ((and (eq (car (car p)) '\?) (equal (car (nth 2 (car p))) "$$"))
  492. (insert (car (nth 1 (car p))) " "))
  493. (t
  494. (insert "{ ")
  495. (calc-write-parse-table-part (nth 1 (car p)))
  496. (insert "}" (symbol-name (car (car p))))
  497. (if (nth 2 (car p))
  498. (calc-write-parse-table-part (list (car (nth 2 (car p)))))
  499. (insert " "))))
  500. (setq p (cdr p))))
  501. (defun calc-read-parse-table (calc-buf calc-lang)
  502. (let ((tab nil))
  503. (while (progn
  504. (skip-chars-forward "\n\t ")
  505. (not (eobp)))
  506. (if (looking-at "%%")
  507. (end-of-line)
  508. (let ((pt (point))
  509. (p (calc-read-parse-table-part ":=[\n\t ]+" ":=")))
  510. (or (stringp (car p))
  511. (and (integerp (car p))
  512. (stringp (nth 1 p)))
  513. (progn
  514. (goto-char pt)
  515. (error "Malformed syntax rule")))
  516. (let ((pos (point)))
  517. (end-of-line)
  518. (let* ((str (buffer-substring pos (point)))
  519. (exp (with-current-buffer calc-buf
  520. (let ((calc-user-parse-tables nil)
  521. (calc-language nil)
  522. (math-expr-opers (math-standard-ops))
  523. (calc-hashes-used 0))
  524. (math-read-expr
  525. (if (string-match ",[ \t]*\\'" str)
  526. (substring str 0 (match-beginning 0))
  527. str))))))
  528. (if (eq (car-safe exp) 'error)
  529. (progn
  530. (goto-char (+ pos (nth 1 exp)))
  531. (error (nth 2 exp))))
  532. (setq tab (nconc tab (list (cons p exp)))))))))
  533. tab))
  534. (defun calc-fix-token-name (name &optional unquoted)
  535. (cond ((string-match "\\`\\.\\." name)
  536. (concat "\\dots" (substring name 2)))
  537. ((and (equal name "{") (memq calc-lang '(tex latex eqn)))
  538. "(")
  539. ((and (equal name "}") (memq calc-lang '(tex latex eqn)))
  540. ")")
  541. ((and (equal name "&") (memq calc-lang '(tex latex)))
  542. ",")
  543. ((equal name "#")
  544. (search-backward "#")
  545. (error "Token `#' is reserved"))
  546. ((and unquoted (string-match "#" name))
  547. (error "Tokens containing `#' must be quoted"))
  548. ((not (string-match "[^ ]" name))
  549. (search-backward "\"" nil t)
  550. (error "Blank tokens are not allowed"))
  551. (t name)))
  552. (defun calc-read-parse-table-part (term eterm)
  553. (let ((part nil)
  554. (quoted nil))
  555. (while (progn
  556. (skip-chars-forward "\n\t ")
  557. (if (eobp) (error "Expected `%s'" eterm))
  558. (not (looking-at term)))
  559. (cond ((looking-at "%%")
  560. (end-of-line))
  561. ((looking-at "{[\n\t ]")
  562. (forward-char 2)
  563. (let ((p (calc-read-parse-table-part "}" "}")))
  564. (or (looking-at "[+*?]")
  565. (error "Expected `+', `*', or `?'"))
  566. (let ((sym (intern (buffer-substring (point) (1+ (point))))))
  567. (forward-char 1)
  568. (looking-at "[^\n\t ]*")
  569. (let ((sep (buffer-substring (point) (match-end 0))))
  570. (goto-char (match-end 0))
  571. (and (eq sym '\?) (> (length sep) 0)
  572. (not (equal sep "$")) (not (equal sep "."))
  573. (error "Separator not allowed with { ... }?"))
  574. (if (string-match "\\`\"" sep)
  575. (setq sep (read-from-string sep)))
  576. (if (> (length sep) 0)
  577. (setq sep (calc-fix-token-name sep)))
  578. (setq part (nconc part
  579. (list (list sym p
  580. (and (> (length sep) 0)
  581. (cons sep p))))))))))
  582. ((looking-at "}")
  583. (error "Too many }'s"))
  584. ((looking-at "\"")
  585. (setq quoted (calc-fix-token-name (read (current-buffer)))
  586. part (nconc part (list quoted))))
  587. ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]")
  588. (setq part (nconc part (list (if (= (match-beginning 1)
  589. (match-end 1))
  590. 0
  591. (string-to-number
  592. (buffer-substring
  593. (1+ (match-beginning 1))
  594. (match-end 1)))))))
  595. (goto-char (match-end 0)))
  596. ((looking-at ":=[\n\t ]")
  597. (error "Misplaced `:='"))
  598. (t
  599. (looking-at "[^\n\t ]*")
  600. (let ((end (match-end 0)))
  601. (setq part (nconc part (list (calc-fix-token-name
  602. (buffer-substring
  603. (point) end) t))))
  604. (goto-char end)))))
  605. (goto-char (match-end 0))
  606. (let ((len (length part)))
  607. (while (and (> len 1)
  608. (let ((last (nthcdr (setq len (1- len)) part)))
  609. (and (assoc (car last) '((")") ("]") (">")))
  610. (not (eq (car last) quoted))
  611. (setcar last
  612. (list '\? (list (car last)) '("$$"))))))))
  613. part))
  614. (defun calc-user-define-invocation ()
  615. (interactive)
  616. (or last-kbd-macro
  617. (error "No keyboard macro defined"))
  618. (setq calc-invocation-macro last-kbd-macro)
  619. (message "Use `C-x * Z' to invoke this macro"))
  620. (defun calc-user-define-edit ()
  621. (interactive) ; but no calc-wrapper!
  622. (message "Edit definition of command: z-")
  623. (let* (cmdname
  624. (key (read-char))
  625. (def (or (assq key (calc-user-key-map))
  626. (assq (upcase key) (calc-user-key-map))
  627. (assq (downcase key) (calc-user-key-map))
  628. (error "No command defined for that key")))
  629. (cmd (cdr def)))
  630. (when (symbolp cmd)
  631. (setq cmdname (symbol-name cmd))
  632. (setq cmd (symbol-function cmd)))
  633. (cond ((or (stringp cmd)
  634. (and (consp cmd)
  635. (eq (car-safe (nth 3 cmd)) 'calc-execute-kbd-macro)))
  636. (let* ((mac (elt (nth 1 (nth 3 cmd)) 1))
  637. (str (edmacro-format-keys mac t))
  638. (kys (nth 3 (nth 3 cmd))))
  639. (calc-edit-mode
  640. (list 'calc-edit-macro-finish-edit cmdname kys)
  641. t (format (concat
  642. "Editing keyboard macro (%s, bound to %s).\n"
  643. "Original keys: %s \n")
  644. cmdname kys (elt (nth 1 (nth 3 cmd)) 0)))
  645. (insert str "\n")
  646. (calc-edit-format-macro-buffer)
  647. (calc-show-edit-buffer)))
  648. (t (let* ((func (calc-stack-command-p cmd))
  649. (defn (and func
  650. (symbolp func)
  651. (get func 'calc-user-defn)))
  652. (kys (concat "z" (char-to-string (car def))))
  653. (intcmd (symbol-name (cdr def)))
  654. (algcmd (if func (substring (symbol-name func) 9) "")))
  655. (if (and defn (calc-valid-formula-func func))
  656. (let ((niceexpr (math-format-nice-expr defn (frame-width))))
  657. (calc-wrapper
  658. (calc-edit-mode
  659. (list 'calc-finish-formula-edit (list 'quote func))
  660. nil
  661. (format (concat
  662. "Editing formula (%s, %s, bound to %s).\n"
  663. "Original formula: %s\n")
  664. intcmd algcmd kys niceexpr))
  665. (insert (math-showing-full-precision
  666. niceexpr)
  667. "\n"))
  668. (calc-show-edit-buffer))
  669. (error "That command's definition cannot be edited")))))))
  670. ;; Formatting the macro buffer
  671. (defvar calc-edit-top)
  672. (defun calc-edit-macro-repeats ()
  673. (goto-char calc-edit-top)
  674. (while
  675. (re-search-forward "^\\([0-9]+\\)\\*" nil t)
  676. (let ((num (string-to-number (match-string 1)))
  677. (line (buffer-substring (point) (line-end-position))))
  678. (goto-char (line-beginning-position))
  679. (kill-line 1)
  680. (while (> num 0)
  681. (insert line "\n")
  682. (setq num (1- num))))))
  683. (defun calc-edit-macro-adjust-buffer ()
  684. (calc-edit-macro-repeats)
  685. (goto-char calc-edit-top)
  686. (while (re-search-forward "^RET$" nil t)
  687. (delete-char 1))
  688. (goto-char calc-edit-top)
  689. (while (and (re-search-forward "^$" nil t)
  690. (not (= (point) (point-max))))
  691. (delete-char 1)))
  692. (defun calc-edit-macro-command ()
  693. "Return the command on the current line in a Calc macro editing buffer."
  694. (let ((beg (line-beginning-position))
  695. (end (save-excursion
  696. (if (search-forward ";;" (line-end-position) 1)
  697. (forward-char -2))
  698. (skip-chars-backward " \t")
  699. (point))))
  700. (buffer-substring beg end)))
  701. (defun calc-edit-macro-command-type ()
  702. "Return the type of command on the current line in a Calc macro editing buffer."
  703. (let ((beg (save-excursion
  704. (if (search-forward ";;" (line-end-position) t)
  705. (progn
  706. (skip-chars-forward " \t")
  707. (point)))))
  708. (end (save-excursion
  709. (goto-char (line-end-position))
  710. (skip-chars-backward " \t")
  711. (point))))
  712. (if beg
  713. (buffer-substring beg end)
  714. "")))
  715. (defun calc-edit-macro-combine-alg-ent ()
  716. "Put an entire algebraic entry on a single line."
  717. (let ((line (calc-edit-macro-command))
  718. (type (calc-edit-macro-command-type))
  719. curline
  720. match)
  721. (goto-char (line-beginning-position))
  722. (kill-line 1)
  723. (setq curline (calc-edit-macro-command))
  724. (while (and curline
  725. (not (string-equal "RET" curline))
  726. (not (setq match (string-match "<return>" curline))))
  727. (setq line (concat line curline))
  728. (kill-line 1)
  729. (setq curline (calc-edit-macro-command)))
  730. (when match
  731. (kill-line 1)
  732. (setq line (concat line (substring curline 0 match))))
  733. (setq line (replace-regexp-in-string "SPC" " SPC "
  734. (replace-regexp-in-string " " "" line)))
  735. (insert line "\t\t\t")
  736. (if (> (current-column) 24)
  737. (delete-char -1))
  738. (insert ";; " type "\n")
  739. (if match
  740. (insert "RET\t\t\t;; calc-enter\n"))))
  741. (defun calc-edit-macro-combine-ext-command ()
  742. "Put an entire extended command on a single line."
  743. (let ((cmdbeg (calc-edit-macro-command))
  744. (line "")
  745. (type (calc-edit-macro-command-type))
  746. curline
  747. match)
  748. (goto-char (line-beginning-position))
  749. (kill-line 1)
  750. (setq curline (calc-edit-macro-command))
  751. (while (and curline
  752. (not (string-equal "RET" curline))
  753. (not (setq match (string-match "<return>" curline))))
  754. (setq line (concat line curline))
  755. (kill-line 1)
  756. (setq curline (calc-edit-macro-command)))
  757. (when match
  758. (kill-line 1)
  759. (setq line (concat line (substring curline 0 match))))
  760. (setq line (replace-regexp-in-string " " "" line))
  761. (insert cmdbeg " " line "\t\t\t")
  762. (if (> (current-column) 24)
  763. (delete-char -1))
  764. (insert ";; " type "\n")
  765. (if match
  766. (insert "RET\t\t\t;; calc-enter\n"))))
  767. (defun calc-edit-macro-combine-var-name ()
  768. "Put an entire variable name on a single line."
  769. (let ((line (calc-edit-macro-command))
  770. curline
  771. match)
  772. (goto-char (line-beginning-position))
  773. (kill-line 1)
  774. (if (member line '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
  775. (insert line "\t\t\t;; calc quick variable\n")
  776. (setq curline (calc-edit-macro-command))
  777. (while (and curline
  778. (not (string-equal "RET" curline))
  779. (not (setq match (string-match "<return>" curline))))
  780. (setq line (concat line curline))
  781. (kill-line 1)
  782. (setq curline (calc-edit-macro-command)))
  783. (when match
  784. (kill-line 1)
  785. (setq line (concat line (substring curline 0 match))))
  786. (setq line (replace-regexp-in-string " " "" line))
  787. (insert line "\t\t\t")
  788. (if (> (current-column) 24)
  789. (delete-char -1))
  790. (insert ";; calc variable\n")
  791. (if match
  792. (insert "RET\t\t\t;; calc-enter\n")))))
  793. (defun calc-edit-macro-combine-digits ()
  794. "Put an entire sequence of digits on a single line."
  795. (let ((line (calc-edit-macro-command))
  796. curline)
  797. (goto-char (line-beginning-position))
  798. (kill-line 1)
  799. (while (string-equal (calc-edit-macro-command-type) "calcDigit-start")
  800. (setq line (concat line (calc-edit-macro-command)))
  801. (kill-line 1))
  802. (insert line "\t\t\t")
  803. (if (> (current-column) 24)
  804. (delete-char -1))
  805. (insert ";; calc digits\n")))
  806. (defun calc-edit-format-macro-buffer ()
  807. "Rewrite the Calc macro editing buffer."
  808. (calc-edit-macro-adjust-buffer)
  809. (goto-char calc-edit-top)
  810. (let ((type (calc-edit-macro-command-type)))
  811. (while (not (string-equal type ""))
  812. (cond
  813. ((or
  814. (string-equal type "calc-algebraic-entry")
  815. (string-equal type "calc-auto-algebraic-entry"))
  816. (calc-edit-macro-combine-alg-ent))
  817. ((string-equal type "calc-execute-extended-command")
  818. (calc-edit-macro-combine-ext-command))
  819. ((string-equal type "calcDigit-start")
  820. (calc-edit-macro-combine-digits))
  821. ((or
  822. (string-equal type "calc-store")
  823. (string-equal type "calc-store-into")
  824. (string-equal type "calc-store-neg")
  825. (string-equal type "calc-store-plus")
  826. (string-equal type "calc-store-minus")
  827. (string-equal type "calc-store-div")
  828. (string-equal type "calc-store-times")
  829. (string-equal type "calc-store-power")
  830. (string-equal type "calc-store-concat")
  831. (string-equal type "calc-store-inv")
  832. (string-equal type "calc-store-dec")
  833. (string-equal type "calc-store-incr")
  834. (string-equal type "calc-store-exchange")
  835. (string-equal type "calc-unstore")
  836. (string-equal type "calc-recall")
  837. (string-equal type "calc-let")
  838. (string-equal type "calc-permanent-variable"))
  839. (forward-line 1)
  840. (calc-edit-macro-combine-var-name))
  841. ((or
  842. (string-equal type "calc-copy-variable")
  843. (string-equal type "calc-copy-special-constant")
  844. (string-equal type "calc-declare-variable"))
  845. (forward-line 1)
  846. (calc-edit-macro-combine-var-name)
  847. (calc-edit-macro-combine-var-name))
  848. (t (forward-line 1)))
  849. (setq type (calc-edit-macro-command-type))))
  850. (goto-char calc-edit-top))
  851. ;; Finish editing the macro
  852. (defun calc-edit-macro-pre-finish-edit ()
  853. (goto-char calc-edit-top)
  854. (while (re-search-forward "\\(^\\| \\)RET\\($\\|\t\\| \\)" nil t)
  855. (search-backward "RET")
  856. (delete-char 3)
  857. (insert "<return>")))
  858. (defun calc-edit-macro-finish-edit (cmdname key)
  859. "Finish editing a Calc macro.
  860. Redefine the corresponding command."
  861. (interactive)
  862. (let ((cmd (intern cmdname)))
  863. (calc-edit-macro-pre-finish-edit)
  864. (let* ((str (buffer-substring calc-edit-top (point-max)))
  865. (mac (edmacro-parse-keys str t)))
  866. (if (= (length mac) 0)
  867. (fmakunbound cmd)
  868. (fset cmd
  869. (list 'lambda '(arg)
  870. '(interactive "P")
  871. (list 'calc-execute-kbd-macro
  872. (vector (key-description mac)
  873. mac)
  874. 'arg key)))))))
  875. (defun calc-finish-formula-edit (func)
  876. (let ((buf (current-buffer))
  877. (str (buffer-substring calc-edit-top (point-max)))
  878. (start (point))
  879. (body (calc-valid-formula-func func)))
  880. (set-buffer calc-original-buffer)
  881. (let ((val (math-read-expr str)))
  882. (if (eq (car-safe val) 'error)
  883. (progn
  884. (set-buffer buf)
  885. (goto-char (+ start (nth 1 val)))
  886. (error (nth 2 val))))
  887. (setcar (cdr body)
  888. (let ((calc-user-formula-alist (nth 1 (symbol-function func))))
  889. (calc-fix-user-formula val)))
  890. (put func 'calc-user-defn val))))
  891. (defun calc-valid-formula-func (func)
  892. (let ((def (symbol-function func)))
  893. (and (consp def)
  894. (eq (car def) 'lambda)
  895. (progn
  896. (setq def (cdr (cdr def)))
  897. (while (and def
  898. (not (eq (car (car def)) 'math-normalize)))
  899. (setq def (cdr def)))
  900. (car def)))))
  901. (defun calc-get-user-defn ()
  902. (interactive)
  903. (calc-wrapper
  904. (message "Get definition of command: z-")
  905. (let* ((key (read-char))
  906. (def (or (assq key (calc-user-key-map))
  907. (assq (upcase key) (calc-user-key-map))
  908. (assq (downcase key) (calc-user-key-map))
  909. (error "No command defined for that key")))
  910. (cmd (cdr def)))
  911. (if (symbolp cmd)
  912. (setq cmd (symbol-function cmd)))
  913. (cond ((stringp cmd)
  914. (message "Keyboard macro: %s" cmd))
  915. (t (let* ((func (calc-stack-command-p cmd))
  916. (defn (and func
  917. (symbolp func)
  918. (get func 'calc-user-defn))))
  919. (if defn
  920. (progn
  921. (and (calc-valid-formula-func func)
  922. (setq defn (append '(calcFunc-lambda)
  923. (mapcar 'math-build-var-name
  924. (nth 1 (symbol-function
  925. func)))
  926. (list defn))))
  927. (calc-enter-result 0 "gdef" defn))
  928. (error "That command is not defined by a formula"))))))))
  929. (defun calc-user-define-permanent ()
  930. (interactive)
  931. (calc-wrapper
  932. (message "Record in %s the command: z-" calc-settings-file)
  933. (let* ((key (read-char))
  934. (def (or (assq key (calc-user-key-map))
  935. (assq (upcase key) (calc-user-key-map))
  936. (assq (downcase key) (calc-user-key-map))
  937. (and (eq key ?\')
  938. (cons nil
  939. (intern
  940. (concat "calcFunc-"
  941. (completing-read
  942. (format "Record in %s the algebraic function: "
  943. calc-settings-file)
  944. (mapcar (lambda (x) (substring x 9))
  945. (all-completions "calcFunc-"
  946. obarray))
  947. (lambda (x)
  948. (fboundp
  949. (intern (concat "calcFunc-" x))))
  950. t)))))
  951. (and (eq key ?\M-x)
  952. (cons nil
  953. (intern (completing-read
  954. (format "Record in %s the command: "
  955. calc-settings-file)
  956. obarray 'fboundp nil "calc-"))))
  957. (error "No command defined for that key"))))
  958. (set-buffer (find-file-noselect (substitute-in-file-name
  959. calc-settings-file)))
  960. (goto-char (point-max))
  961. (let* ((cmd (cdr def))
  962. (fcmd (and cmd (symbolp cmd) (symbol-function cmd)))
  963. (func nil)
  964. (pt (point))
  965. (fill-column 70)
  966. (fill-prefix nil)
  967. str q-ok)
  968. (insert "\n;;; Definition stored by Calc on " (current-time-string)
  969. "\n(put 'calc-define '"
  970. (if (symbolp cmd) (symbol-name cmd) (format "key%d" key))
  971. " '(progn\n")
  972. (if (and fcmd
  973. (eq (car-safe fcmd) 'lambda)
  974. (get cmd 'calc-user-defn))
  975. (let ((pt (point)))
  976. (and (eq (car-safe (nth 3 fcmd)) 'calc-execute-kbd-macro)
  977. (vectorp (nth 1 (nth 3 fcmd)))
  978. (progn (and (fboundp 'edit-kbd-macro)
  979. (edit-kbd-macro nil))
  980. (fboundp 'edmacro-parse-keys))
  981. (setq q-ok t)
  982. (aset (nth 1 (nth 3 fcmd)) 1 nil))
  983. (insert (setq str (prin1-to-string
  984. (cons 'defun (cons cmd (cdr fcmd)))))
  985. "\n")
  986. (or (and (string-match "\"" str) (not q-ok))
  987. (fill-region pt (point)))
  988. (indent-rigidly pt (point) 2)
  989. (delete-region pt (1+ pt))
  990. (insert " (put '" (symbol-name cmd)
  991. " 'calc-user-defn '"
  992. (prin1-to-string (get cmd 'calc-user-defn))
  993. ")\n")
  994. (setq func (calc-stack-command-p cmd))
  995. (let ((ffunc (and func (symbolp func) (symbol-function func)))
  996. (pt (point)))
  997. (and ffunc
  998. (eq (car-safe ffunc) 'lambda)
  999. (get func 'calc-user-defn)
  1000. (progn
  1001. (insert (setq str (prin1-to-string
  1002. (cons 'defun (cons func
  1003. (cdr ffunc)))))
  1004. "\n")
  1005. (or (and (string-match "\"" str) (not q-ok))
  1006. (fill-region pt (point)))
  1007. (indent-rigidly pt (point) 2)
  1008. (delete-region pt (1+ pt))
  1009. (setq pt (point))
  1010. (insert "(put '" (symbol-name func)
  1011. " 'calc-user-defn '"
  1012. (prin1-to-string (get func 'calc-user-defn))
  1013. ")\n")
  1014. (fill-region pt (point))
  1015. (indent-rigidly pt (point) 2)
  1016. (delete-region pt (1+ pt))))))
  1017. (and (stringp fcmd)
  1018. (insert " (fset '" (prin1-to-string cmd)
  1019. " " (prin1-to-string fcmd) ")\n")))
  1020. (or func (setq func (and cmd (symbolp cmd) (fboundp cmd) cmd)))
  1021. (if (get func 'math-compose-forms)
  1022. (let ((pt (point)))
  1023. (insert "(put '" (symbol-name cmd)
  1024. " 'math-compose-forms '"
  1025. (prin1-to-string (get func 'math-compose-forms))
  1026. ")\n")
  1027. (fill-region pt (point))
  1028. (indent-rigidly pt (point) 2)
  1029. (delete-region pt (1+ pt))))
  1030. (if (car def)
  1031. (insert " (define-key calc-mode-map "
  1032. (prin1-to-string (concat "z" (char-to-string key)))
  1033. " '"
  1034. (prin1-to-string cmd)
  1035. ")\n")))
  1036. (insert "))\n")
  1037. (save-buffer))))
  1038. (defun calc-stack-command-p (cmd)
  1039. (if (and cmd (symbolp cmd))
  1040. (and (fboundp cmd)
  1041. (calc-stack-command-p (symbol-function cmd)))
  1042. (and (consp cmd)
  1043. (eq (car cmd) 'lambda)
  1044. (setq cmd (or (assq 'calc-wrapper cmd)
  1045. (assq 'calc-slow-wrapper cmd)))
  1046. (setq cmd (assq 'calc-enter-result cmd))
  1047. (memq (car (nth 3 cmd)) '(cons list))
  1048. (eq (car (nth 1 (nth 3 cmd))) 'quote)
  1049. (nth 1 (nth 1 (nth 3 cmd))))))
  1050. (defun calc-call-last-kbd-macro (arg)
  1051. (interactive "P")
  1052. (and defining-kbd-macro
  1053. (error "Can't execute anonymous macro while defining one"))
  1054. (or last-kbd-macro
  1055. (error "No kbd macro has been defined"))
  1056. (calc-execute-kbd-macro last-kbd-macro arg))
  1057. (defun calc-execute-kbd-macro (mac arg &rest prefix)
  1058. (if calc-keep-args-flag
  1059. (calc-keep-args))
  1060. (if (and (vectorp mac) (> (length mac) 0) (stringp (aref mac 0)))
  1061. (setq mac (or (aref mac 1)
  1062. (aset mac 1 (progn (and (fboundp 'edit-kbd-macro)
  1063. (edit-kbd-macro nil))
  1064. (edmacro-parse-keys (aref mac 0)))))))
  1065. (if (< (prefix-numeric-value arg) 0)
  1066. (execute-kbd-macro mac (- (prefix-numeric-value arg)))
  1067. (if calc-executing-macro
  1068. (execute-kbd-macro mac arg)
  1069. (calc-slow-wrapper
  1070. (let ((old-stack-whole (copy-sequence calc-stack))
  1071. (old-stack-top calc-stack-top)
  1072. (old-buffer-size (buffer-size))
  1073. (old-refresh-count calc-refresh-count))
  1074. (unwind-protect
  1075. (let ((calc-executing-macro mac))
  1076. (execute-kbd-macro mac arg))
  1077. (calc-select-buffer)
  1078. (let ((new-stack (reverse calc-stack))
  1079. (old-stack (reverse old-stack-whole)))
  1080. (while (and new-stack old-stack
  1081. (equal (car new-stack) (car old-stack)))
  1082. (setq new-stack (cdr new-stack)
  1083. old-stack (cdr old-stack)))
  1084. (or (equal prefix '(nil))
  1085. (calc-record-list (if (> (length new-stack) 1)
  1086. (mapcar 'car new-stack)
  1087. '(""))
  1088. (or (car prefix) "kmac")))
  1089. (calc-record-undo (list 'set 'saved-stack-top old-stack-top))
  1090. (and old-stack
  1091. (calc-record-undo (list 'pop 1 (mapcar 'car old-stack))))
  1092. (let ((calc-stack old-stack-whole)
  1093. (calc-stack-top 0))
  1094. (calc-cursor-stack-index (length old-stack)))
  1095. (if (and (= old-buffer-size (buffer-size))
  1096. (= old-refresh-count calc-refresh-count))
  1097. (let ((buffer-read-only nil))
  1098. (delete-region (point) (point-max))
  1099. (while new-stack
  1100. (calc-record-undo (list 'push 1))
  1101. (insert (math-format-stack-value (car new-stack)) "\n")
  1102. (setq new-stack (cdr new-stack)))
  1103. (calc-renumber-stack))
  1104. (while new-stack
  1105. (calc-record-undo (list 'push 1))
  1106. (setq new-stack (cdr new-stack)))
  1107. (calc-refresh))
  1108. (calc-record-undo (list 'set 'saved-stack-top 0)))))))))
  1109. (defun calc-push-list-in-macro (vals m sels)
  1110. (let ((entry (list (car vals) 1 (car sels)))
  1111. (mm (+ (or m 1) calc-stack-top)))
  1112. (if (> mm 1)
  1113. (setcdr (nthcdr (- mm 2) calc-stack)
  1114. (cons entry (nthcdr (1- mm) calc-stack)))
  1115. (setq calc-stack (cons entry calc-stack)))))
  1116. (defun calc-pop-stack-in-macro (n mm)
  1117. (if (> mm 1)
  1118. (setcdr (nthcdr (- mm 2) calc-stack)
  1119. (nthcdr (+ n mm -1) calc-stack))
  1120. (setq calc-stack (nthcdr n calc-stack))))
  1121. (defun calc-kbd-if ()
  1122. (interactive)
  1123. (calc-wrapper
  1124. (let ((cond (calc-top-n 1)))
  1125. (calc-pop-stack 1)
  1126. (if (math-is-true cond)
  1127. (if defining-kbd-macro
  1128. (message "If true..."))
  1129. (if defining-kbd-macro
  1130. (message "Condition is false; skipping to Z: or Z] ..."))
  1131. (calc-kbd-skip-to-else-if t)))))
  1132. (defun calc-kbd-else-if ()
  1133. (interactive)
  1134. (calc-kbd-if))
  1135. (defun calc-kbd-skip-to-else-if (else-okay)
  1136. (let ((count 0)
  1137. ch)
  1138. (while (>= count 0)
  1139. (setq ch (read-char))
  1140. (if (= ch -1)
  1141. (error "Unterminated Z[ in keyboard macro"))
  1142. (if (= ch ?Z)
  1143. (progn
  1144. (setq ch (read-char))
  1145. (cond ((= ch ?\[)
  1146. (setq count (1+ count)))
  1147. ((= ch ?\])
  1148. (setq count (1- count)))
  1149. ((= ch ?\:)
  1150. (and (= count 0)
  1151. else-okay
  1152. (setq count -1)))
  1153. ((eq ch 7)
  1154. (keyboard-quit))))))
  1155. (and defining-kbd-macro
  1156. (if (= ch ?\:)
  1157. (message "Else...")
  1158. (message "End-if...")))))
  1159. (defun calc-kbd-end-if ()
  1160. (interactive)
  1161. (if defining-kbd-macro
  1162. (message "End-if...")))
  1163. (defun calc-kbd-else ()
  1164. (interactive)
  1165. (if defining-kbd-macro
  1166. (message "Else; skipping to Z] ..."))
  1167. (calc-kbd-skip-to-else-if nil))
  1168. (defun calc-kbd-repeat ()
  1169. (interactive)
  1170. (let (count)
  1171. (calc-wrapper
  1172. (setq count (math-trunc (calc-top-n 1)))
  1173. (or (Math-integerp count)
  1174. (error "Count must be an integer"))
  1175. (if (Math-integer-negp count)
  1176. (setq count 0))
  1177. (or (integerp count)
  1178. (setq count 1000000))
  1179. (calc-pop-stack 1))
  1180. (calc-kbd-loop count)))
  1181. (defun calc-kbd-for (dir)
  1182. (interactive "P")
  1183. (let (init final)
  1184. (calc-wrapper
  1185. (setq init (calc-top-n 2)
  1186. final (calc-top-n 1))
  1187. (or (and (math-anglep init) (math-anglep final))
  1188. (error "Initial and final values must be real numbers"))
  1189. (calc-pop-stack 2))
  1190. (calc-kbd-loop nil init final (and dir (prefix-numeric-value dir)))))
  1191. (defun calc-kbd-loop (rpt-count &optional initial final dir)
  1192. (interactive "P")
  1193. (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000))
  1194. (let* ((count 0)
  1195. (parts nil)
  1196. (body (vector))
  1197. (open last-command-event)
  1198. (counter initial)
  1199. ch)
  1200. (or executing-kbd-macro
  1201. (message "Reading loop body..."))
  1202. (while (>= count 0)
  1203. (setq ch (read-event))
  1204. (if (eq ch -1)
  1205. (error "Unterminated Z%c in keyboard macro" open))
  1206. (if (eq ch ?Z)
  1207. (progn
  1208. (setq ch (read-event)
  1209. body (vconcat body (vector ?Z ch)))
  1210. (cond ((memq ch '(?\< ?\( ?\{))
  1211. (setq count (1+ count)))
  1212. ((memq ch '(?\> ?\) ?\}))
  1213. (setq count (1- count)))
  1214. ((and (= ch ?/)
  1215. (= count 0))
  1216. (setq parts (nconc parts (list (vconcat (substring body 0 -2)
  1217. (vector ?Z ?\]) )))
  1218. body ""))
  1219. ((eq ch 7)
  1220. (keyboard-quit))))
  1221. (setq body (vconcat body (vector ch)))))
  1222. (if (/= ch (cdr (assq open '( (?\< . ?\>) (?\( . ?\)) (?\{ . ?\}) ))))
  1223. (error "Mismatched Z%c and Z%c in keyboard macro" open ch))
  1224. (or executing-kbd-macro
  1225. (message "Looping..."))
  1226. (setq body (vconcat (substring body 0 -2) (vector ?Z ?\]) ))
  1227. (and (not executing-kbd-macro)
  1228. (= rpt-count 1000000)
  1229. (null parts)
  1230. (null counter)
  1231. (progn
  1232. (message "Warning: Infinite loop! Not executing")
  1233. (setq rpt-count 0)))
  1234. (or (not initial) dir
  1235. (setq dir (math-compare final initial)))
  1236. (calc-wrapper
  1237. (while (> rpt-count 0)
  1238. (let ((part parts))
  1239. (if counter
  1240. (if (cond ((eq dir 0) (Math-equal final counter))
  1241. ((eq dir 1) (Math-lessp final counter))
  1242. ((eq dir -1) (Math-lessp counter final)))
  1243. (setq rpt-count 0)
  1244. (calc-push counter)))
  1245. (while (and part (> rpt-count 0))
  1246. (execute-kbd-macro (car part))
  1247. (if (math-is-true (calc-top-n 1))
  1248. (setq rpt-count 0)
  1249. (setq part (cdr part)))
  1250. (calc-pop-stack 1))
  1251. (if (> rpt-count 0)
  1252. (progn
  1253. (execute-kbd-macro body)
  1254. (if counter
  1255. (let ((step (calc-top-n 1)))
  1256. (calc-pop-stack 1)
  1257. (setq counter (calcFunc-add counter step)))
  1258. (setq rpt-count (1- rpt-count))))))))
  1259. (or executing-kbd-macro
  1260. (message "Looping...done"))))
  1261. (defun calc-kbd-end-repeat ()
  1262. (interactive)
  1263. (error "Unbalanced Z> in keyboard macro"))
  1264. (defun calc-kbd-end-for ()
  1265. (interactive)
  1266. (error "Unbalanced Z) in keyboard macro"))
  1267. (defun calc-kbd-end-loop ()
  1268. (interactive)
  1269. (error "Unbalanced Z} in keyboard macro"))
  1270. (defun calc-kbd-break ()
  1271. (interactive)
  1272. (calc-wrapper
  1273. (let ((cond (calc-top-n 1)))
  1274. (calc-pop-stack 1)
  1275. (if (math-is-true cond)
  1276. (error "Keyboard macro aborted")))))
  1277. (defvar calc-kbd-push-level 0)
  1278. ;; The variables var-q0 through var-q9 are the "quick" variables.
  1279. (defvar var-q0 nil)
  1280. (defvar var-q1 nil)
  1281. (defvar var-q2 nil)
  1282. (defvar var-q3 nil)
  1283. (defvar var-q4 nil)
  1284. (defvar var-q5 nil)
  1285. (defvar var-q6 nil)
  1286. (defvar var-q7 nil)
  1287. (defvar var-q8 nil)
  1288. (defvar var-q9 nil)
  1289. (defun calc-kbd-push (arg)
  1290. (interactive "P")
  1291. (calc-wrapper
  1292. (let* ((defs (and arg (> (prefix-numeric-value arg) 0)))
  1293. (var-q0 var-q0)
  1294. (var-q1 var-q1)
  1295. (var-q2 var-q2)
  1296. (var-q3 var-q3)
  1297. (var-q4 var-q4)
  1298. (var-q5 var-q5)
  1299. (var-q6 var-q6)
  1300. (var-q7 var-q7)
  1301. (var-q8 var-q8)
  1302. (var-q9 var-q9)
  1303. (calc-internal-prec (if defs 12 calc-internal-prec))
  1304. (calc-word-size (if defs 32 calc-word-size))
  1305. (calc-angle-mode (if defs 'deg calc-angle-mode))
  1306. (calc-simplify-mode (if defs nil calc-simplify-mode))
  1307. (calc-algebraic-mode (if arg nil calc-algebraic-mode))
  1308. (calc-incomplete-algebraic-mode (if arg nil
  1309. calc-incomplete-algebraic-mode))
  1310. (calc-symbolic-mode (if defs nil calc-symbolic-mode))
  1311. (calc-matrix-mode (if defs nil calc-matrix-mode))
  1312. (calc-prefer-frac (if defs nil calc-prefer-frac))
  1313. (calc-complex-mode (if defs nil calc-complex-mode))
  1314. (calc-infinite-mode (if defs nil calc-infinite-mode))
  1315. (count 0)
  1316. (body "")
  1317. ch)
  1318. (if (or executing-kbd-macro defining-kbd-macro)
  1319. (progn
  1320. (if defining-kbd-macro
  1321. (message "Reading body..."))
  1322. (while (>= count 0)
  1323. (setq ch (read-char))
  1324. (if (= ch -1)
  1325. (error "Unterminated Z` in keyboard macro"))
  1326. (if (= ch ?Z)
  1327. (progn
  1328. (setq ch (read-char)
  1329. body (concat body "Z" (char-to-string ch)))
  1330. (cond ((eq ch ?\`)
  1331. (setq count (1+ count)))
  1332. ((eq ch ?\')
  1333. (setq count (1- count)))
  1334. ((eq ch 7)
  1335. (keyboard-quit))))
  1336. (setq body (concat body (char-to-string ch)))))
  1337. (if defining-kbd-macro
  1338. (message "Reading body...done"))
  1339. (let ((calc-kbd-push-level 0))
  1340. (execute-kbd-macro (substring body 0 -2))))
  1341. (let ((calc-kbd-push-level (1+ calc-kbd-push-level)))
  1342. (message "%s" "Saving modes; type Z' to restore")
  1343. (recursive-edit))))))
  1344. (defun calc-kbd-pop ()
  1345. (interactive)
  1346. (if (> calc-kbd-push-level 0)
  1347. (progn
  1348. (message "Mode settings restored")
  1349. (exit-recursive-edit))
  1350. (error "%s" "Unbalanced Z' in keyboard macro")))
  1351. ;; (defun calc-kbd-report (msg)
  1352. ;; (interactive "sMessage: ")
  1353. ;; (calc-wrapper
  1354. ;; (math-working msg (calc-top-n 1))))
  1355. (defun calc-kbd-query ()
  1356. (interactive)
  1357. (let ((defining-kbd-macro nil)
  1358. (executing-kbd-macro nil)
  1359. (msg (calc-top 1)))
  1360. (if (not (eq (car-safe msg) 'vec))
  1361. (error "No prompt string provided")
  1362. (setq msg (math-vector-to-string msg))
  1363. (calc-wrapper
  1364. (calc-pop-stack 1)
  1365. (calc-alg-entry nil (and (not (equal msg "")) msg))))))
  1366. ;;;; Logical operations.
  1367. (defun calcFunc-eq (a b &rest more)
  1368. (if more
  1369. (let* ((args (cons a (cons b (copy-sequence more))))
  1370. (res 1)
  1371. (p args)
  1372. p2)
  1373. (while (and (cdr p) (not (eq res 0)))
  1374. (setq p2 p)
  1375. (while (and (setq p2 (cdr p2)) (not (eq res 0)))
  1376. (setq res (math-two-eq (car p) (car p2)))
  1377. (if (eq res 1)
  1378. (setcdr p (delq (car p2) (cdr p)))))
  1379. (setq p (cdr p)))
  1380. (if (eq res 0)
  1381. 0
  1382. (if (cdr args)
  1383. (cons 'calcFunc-eq args)
  1384. 1)))
  1385. (or (math-two-eq a b)
  1386. (if (and (or (math-looks-negp a) (math-zerop a))
  1387. (or (math-looks-negp b) (math-zerop b)))
  1388. (list 'calcFunc-eq (math-neg a) (math-neg b))
  1389. (list 'calcFunc-eq a b)))))
  1390. (defun calcFunc-neq (a b &rest more)
  1391. (if more
  1392. (let* ((args (cons a (cons b more)))
  1393. (res 0)
  1394. (all t)
  1395. (p args)
  1396. p2)
  1397. (while (and (cdr p) (not (eq res 1)))
  1398. (setq p2 p)
  1399. (while (and (setq p2 (cdr p2)) (not (eq res 1)))
  1400. (setq res (math-two-eq (car p) (car p2)))
  1401. (or res (setq all nil)))
  1402. (setq p (cdr p)))
  1403. (if (eq res 1)
  1404. 0
  1405. (if all
  1406. 1
  1407. (cons 'calcFunc-neq args))))
  1408. (or (cdr (assq (math-two-eq a b) '((0 . 1) (1 . 0))))
  1409. (if (and (or (math-looks-negp a) (math-zerop a))
  1410. (or (math-looks-negp b) (math-zerop b)))
  1411. (list 'calcFunc-neq (math-neg a) (math-neg b))
  1412. (list 'calcFunc-neq a b)))))
  1413. (defun math-two-eq (a b)
  1414. (if (eq (car-safe a) 'vec)
  1415. (if (eq (car-safe b) 'vec)
  1416. (if (= (length a) (length b))
  1417. (let ((res 1))
  1418. (while (and (setq a (cdr a) b (cdr b)) (not (eq res 0)))
  1419. (if res
  1420. (setq res (math-two-eq (car a) (car b)))
  1421. (if (eq (math-two-eq (car a) (car b)) 0)
  1422. (setq res 0))))
  1423. res)
  1424. 0)
  1425. (if (Math-objectp b)
  1426. 0
  1427. nil))
  1428. (if (eq (car-safe b) 'vec)
  1429. (if (Math-objectp a)
  1430. 0
  1431. nil)
  1432. (let ((res (math-compare a b)))
  1433. (if (= res 0)
  1434. 1
  1435. (if (and (= res 2) (not (and (Math-scalarp a) (Math-scalarp b))))
  1436. nil
  1437. 0))))))
  1438. (defun calcFunc-lt (a b)
  1439. (let ((res (math-compare a b)))
  1440. (if (= res -1)
  1441. 1
  1442. (if (= res 2)
  1443. (if (and (or (math-looks-negp a) (math-zerop a))
  1444. (or (math-looks-negp b) (math-zerop b)))
  1445. (list 'calcFunc-gt (math-neg a) (math-neg b))
  1446. (list 'calcFunc-lt a b))
  1447. 0))))
  1448. (defun calcFunc-gt (a b)
  1449. (let ((res (math-compare a b)))
  1450. (if (= res 1)
  1451. 1
  1452. (if (= res 2)
  1453. (if (and (or (math-looks-negp a) (math-zerop a))
  1454. (or (math-looks-negp b) (math-zerop b)))
  1455. (list 'calcFunc-lt (math-neg a) (math-neg b))
  1456. (list 'calcFunc-gt a b))
  1457. 0))))
  1458. (defun calcFunc-leq (a b)
  1459. (let ((res (math-compare a b)))
  1460. (if (= res 1)
  1461. 0
  1462. (if (= res 2)
  1463. (if (and (or (math-looks-negp a) (math-zerop a))
  1464. (or (math-looks-negp b) (math-zerop b)))
  1465. (list 'calcFunc-geq (math-neg a) (math-neg b))
  1466. (list 'calcFunc-leq a b))
  1467. 1))))
  1468. (defun calcFunc-geq (a b)
  1469. (let ((res (math-compare a b)))
  1470. (if (= res -1)
  1471. 0
  1472. (if (= res 2)
  1473. (if (and (or (math-looks-negp a) (math-zerop a))
  1474. (or (math-looks-negp b) (math-zerop b)))
  1475. (list 'calcFunc-leq (math-neg a) (math-neg b))
  1476. (list 'calcFunc-geq a b))
  1477. 1))))
  1478. (defun calcFunc-rmeq (a)
  1479. (if (math-vectorp a)
  1480. (math-map-vec 'calcFunc-rmeq a)
  1481. (if (assq (car-safe a) calc-tweak-eqn-table)
  1482. (if (and (eq (car-safe (nth 2 a)) 'var)
  1483. (math-objectp (nth 1 a)))
  1484. (nth 1 a)
  1485. (nth 2 a))
  1486. (if (eq (car-safe a) 'calcFunc-assign)
  1487. (nth 2 a)
  1488. (if (eq (car-safe a) 'calcFunc-evalto)
  1489. (nth 1 a)
  1490. (list 'calcFunc-rmeq a))))))
  1491. (defun calcFunc-land (a b)
  1492. (cond ((Math-zerop a)
  1493. a)
  1494. ((Math-zerop b)
  1495. b)
  1496. ((math-is-true a)
  1497. b)
  1498. ((math-is-true b)
  1499. a)
  1500. (t (list 'calcFunc-land a b))))
  1501. (defun calcFunc-lor (a b)
  1502. (cond ((Math-zerop a)
  1503. b)
  1504. ((Math-zerop b)
  1505. a)
  1506. ((math-is-true a)
  1507. a)
  1508. ((math-is-true b)
  1509. b)
  1510. (t (list 'calcFunc-lor a b))))
  1511. (defun calcFunc-lnot (a)
  1512. (if (Math-zerop a)
  1513. 1
  1514. (if (math-is-true a)
  1515. 0
  1516. (let ((op (and (= (length a) 3)
  1517. (assq (car a) calc-tweak-eqn-table))))
  1518. (if op
  1519. (cons (nth 2 op) (cdr a))
  1520. (list 'calcFunc-lnot a))))))
  1521. (defun calcFunc-if (c e1 e2)
  1522. (if (Math-zerop c)
  1523. e2
  1524. (if (and (math-is-true c) (not (Math-vectorp c)))
  1525. e1
  1526. (or (and (Math-vectorp c)
  1527. (math-constp c)
  1528. (let ((ee1 (if (Math-vectorp e1)
  1529. (if (= (length c) (length e1))
  1530. (cdr e1)
  1531. (calc-record-why "*Dimension error" e1))
  1532. (list e1)))
  1533. (ee2 (if (Math-vectorp e2)
  1534. (if (= (length c) (length e2))
  1535. (cdr e2)
  1536. (calc-record-why "*Dimension error" e2))
  1537. (list e2))))
  1538. (and ee1 ee2
  1539. (cons 'vec (math-if-vector (cdr c) ee1 ee2)))))
  1540. (list 'calcFunc-if c e1 e2)))))
  1541. (defun math-if-vector (c e1 e2)
  1542. (and c
  1543. (cons (if (Math-zerop (car c)) (car e2) (car e1))
  1544. (math-if-vector (cdr c)
  1545. (or (cdr e1) e1)
  1546. (or (cdr e2) e2)))))
  1547. (defun math-normalize-logical-op (a)
  1548. (or (and (eq (car a) 'calcFunc-if)
  1549. (= (length a) 4)
  1550. (let ((a1 (math-normalize (nth 1 a))))
  1551. (if (Math-zerop a1)
  1552. (math-normalize (nth 3 a))
  1553. (if (Math-numberp a1)
  1554. (math-normalize (nth 2 a))
  1555. (if (and (Math-vectorp (nth 1 a))
  1556. (math-constp (nth 1 a)))
  1557. (calcFunc-if (nth 1 a)
  1558. (math-normalize (nth 2 a))
  1559. (math-normalize (nth 3 a)))
  1560. (let ((calc-simplify-mode 'none))
  1561. (list 'calcFunc-if a1
  1562. (math-normalize (nth 2 a))
  1563. (math-normalize (nth 3 a)))))))))
  1564. a))
  1565. (defun calcFunc-in (a b)
  1566. (or (and (eq (car-safe b) 'vec)
  1567. (let ((bb b))
  1568. (while (and (setq bb (cdr bb))
  1569. (not (if (memq (car-safe (car bb)) '(vec intv))
  1570. (eq (calcFunc-in a (car bb)) 1)
  1571. (Math-equal a (car bb))))))
  1572. (if bb 1 (and (math-constp a) (math-constp bb) 0))))
  1573. (and (eq (car-safe b) 'intv)
  1574. (let ((res (math-compare a (nth 2 b))) res2)
  1575. (cond ((= res -1)
  1576. 0)
  1577. ((and (= res 0)
  1578. (or (/= (nth 1 b) 2)
  1579. (Math-lessp (nth 2 b) (nth 3 b))))
  1580. (if (memq (nth 1 b) '(2 3)) 1 0))
  1581. ((= (setq res2 (math-compare a (nth 3 b))) 1)
  1582. 0)
  1583. ((and (= res2 0)
  1584. (or (/= (nth 1 b) 1)
  1585. (Math-lessp (nth 2 b) (nth 3 b))))
  1586. (if (memq (nth 1 b) '(1 3)) 1 0))
  1587. ((/= res 1)
  1588. nil)
  1589. ((/= res2 -1)
  1590. nil)
  1591. (t 1))))
  1592. (and (Math-equal a b)
  1593. 1)
  1594. (and (math-constp a) (math-constp b)
  1595. 0)
  1596. (list 'calcFunc-in a b)))
  1597. (defun calcFunc-typeof (a)
  1598. (cond ((Math-integerp a) 1)
  1599. ((eq (car a) 'frac) 2)
  1600. ((eq (car a) 'float) 3)
  1601. ((eq (car a) 'hms) 4)
  1602. ((eq (car a) 'cplx) 5)
  1603. ((eq (car a) 'polar) 6)
  1604. ((eq (car a) 'sdev) 7)
  1605. ((eq (car a) 'intv) 8)
  1606. ((eq (car a) 'mod) 9)
  1607. ((eq (car a) 'date) (if (Math-integerp (nth 1 a)) 10 11))
  1608. ((eq (car a) 'var)
  1609. (if (memq (nth 2 a) '(var-inf var-uinf var-nan)) 12 100))
  1610. ((eq (car a) 'vec) (if (math-matrixp a) 102 101))
  1611. (t (math-calcFunc-to-var (car a)))))
  1612. (defun calcFunc-integer (a)
  1613. (if (Math-integerp a)
  1614. 1
  1615. (if (Math-objvecp a)
  1616. 0
  1617. (list 'calcFunc-integer a))))
  1618. (defun calcFunc-real (a)
  1619. (if (Math-realp a)
  1620. 1
  1621. (if (Math-objvecp a)
  1622. 0
  1623. (list 'calcFunc-real a))))
  1624. (defun calcFunc-constant (a)
  1625. (if (math-constp a)
  1626. 1
  1627. (if (Math-objvecp a)
  1628. 0
  1629. (list 'calcFunc-constant a))))
  1630. (defun calcFunc-refers (a b)
  1631. (if (math-expr-contains a b)
  1632. 1
  1633. (if (eq (car-safe a) 'var)
  1634. (list 'calcFunc-refers a b)
  1635. 0)))
  1636. (defun calcFunc-negative (a)
  1637. (if (math-looks-negp a)
  1638. 1
  1639. (if (or (math-zerop a)
  1640. (math-posp a))
  1641. 0
  1642. (list 'calcFunc-negative a))))
  1643. (defun calcFunc-variable (a)
  1644. (if (eq (car-safe a) 'var)
  1645. 1
  1646. (if (Math-objvecp a)
  1647. 0
  1648. (list 'calcFunc-variable a))))
  1649. (defun calcFunc-nonvar (a)
  1650. (if (eq (car-safe a) 'var)
  1651. (list 'calcFunc-nonvar a)
  1652. 1))
  1653. (defun calcFunc-istrue (a)
  1654. (if (math-is-true a)
  1655. 1
  1656. 0))
  1657. ;;;; User-programmability.
  1658. ;;; Compiling Lisp-like forms to use the math library.
  1659. (defun math-do-defmath (func args body)
  1660. (require 'calc-macs)
  1661. (let* ((fname (intern (concat "calcFunc-" (symbol-name func))))
  1662. (doc (if (stringp (car body))
  1663. (prog1 (list (car body))
  1664. (setq body (cdr body)))))
  1665. (clargs (mapcar 'math-clean-arg args))
  1666. (inter (if (and (consp (car body))
  1667. (eq (car (car body)) 'interactive))
  1668. (prog1 (car body)
  1669. (setq body (cdr body))))))
  1670. (setq body (math-define-function-body body clargs))
  1671. `(progn
  1672. ,(if inter
  1673. (if (or (> (length inter) 2)
  1674. (integerp (nth 1 inter)))
  1675. (let ((hasprefix nil) (hasmulti nil))
  1676. (when (stringp (nth 1 inter))
  1677. (cond ((equal (nth 1 inter) "p")
  1678. (setq hasprefix t))
  1679. ((equal (nth 1 inter) "m")
  1680. (setq hasmulti t))
  1681. (t (error
  1682. "Can't handle interactive code string \"%s\""
  1683. (nth 1 inter))))
  1684. (setq inter (cdr inter)))
  1685. (unless (integerp (nth 1 inter))
  1686. (error "Expected an integer in interactive specification"))
  1687. `(defun ,(intern (concat "calc-" (symbol-name func)))
  1688. ,(if (or hasprefix hasmulti) '(&optional n) ())
  1689. ,@doc
  1690. (interactive ,@(if (or hasprefix hasmulti) '("P")))
  1691. (calc-slow-wrapper
  1692. ,@(if hasmulti
  1693. `((setq n (if n
  1694. (prefix-numeric-value n)
  1695. ,(nth 1 inter)))))
  1696. (calc-enter-result
  1697. ,(if hasmulti 'n (nth 1 inter))
  1698. ,(nth 2 inter)
  1699. ,(if hasprefix
  1700. `(append '(,fname)
  1701. (calc-top-list-n ,(nth 1 inter))
  1702. (and n
  1703. (list
  1704. (math-normalize
  1705. (prefix-numeric-value n)))))
  1706. `(cons ',fname
  1707. (calc-top-list-n
  1708. ,(if hasmulti
  1709. 'n
  1710. (nth 1 inter)))))))))
  1711. `(defun ,(intern (concat "calc-" (symbol-name func))) ,clargs
  1712. ,@doc
  1713. ,inter
  1714. (calc-wrapper ,@body))))
  1715. (defun ,fname ,clargs
  1716. ,@doc
  1717. ,@(math-do-arg-list-check args nil nil)
  1718. ,@body))))
  1719. (defun math-clean-arg (arg)
  1720. (if (consp arg)
  1721. (math-clean-arg (nth 1 arg))
  1722. arg))
  1723. (defun math-do-arg-check (arg var is-opt is-rest)
  1724. (if is-opt
  1725. (let ((chk (math-do-arg-check arg var nil nil)))
  1726. (list (cons 'and
  1727. (cons var
  1728. (if (cdr chk)
  1729. `((progn ,@chk))
  1730. chk)))))
  1731. (when (consp arg)
  1732. (let* ((rest (math-do-arg-check (nth 1 arg) var is-opt is-rest))
  1733. (qual (car arg))
  1734. (qual-name (symbol-name qual))
  1735. (chk (intern (concat "math-check-" qual-name))))
  1736. (if (fboundp chk)
  1737. (append rest
  1738. (if is-rest
  1739. `((setq ,var (mapcar ',chk ,var)))
  1740. `((setq ,var (,chk ,var)))))
  1741. (if (fboundp (setq chk (intern (concat "math-" qual-name))))
  1742. (append rest
  1743. (if is-rest
  1744. `((mapcar #'(lambda (x)
  1745. (or (,chk x)
  1746. (math-reject-arg x ',qual)))
  1747. ,var))
  1748. `((or (,chk ,var)
  1749. (math-reject-arg ,var ',qual)))))
  1750. (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name)
  1751. (fboundp (setq chk (intern
  1752. (concat "math-"
  1753. (math-match-substring
  1754. qual-name 1))))))
  1755. (append rest
  1756. (if is-rest
  1757. `((mapcar #'(lambda (x)
  1758. (and (,chk x)
  1759. (math-reject-arg x ',qual)))
  1760. ,var))
  1761. `((and
  1762. (,chk ,var)
  1763. (math-reject-arg ,var ',qual)))))
  1764. (error "Unknown qualifier `%s'" qual-name))))))))
  1765. (defun math-do-arg-list-check (args is-opt is-rest)
  1766. (cond ((null args) nil)
  1767. ((consp (car args))
  1768. (append (math-do-arg-check (car args)
  1769. (math-clean-arg (car args))
  1770. is-opt is-rest)
  1771. (math-do-arg-list-check (cdr args) is-opt is-rest)))
  1772. ((eq (car args) '&optional)
  1773. (math-do-arg-list-check (cdr args) t nil))
  1774. ((eq (car args) '&rest)
  1775. (math-do-arg-list-check (cdr args) nil t))
  1776. (t (math-do-arg-list-check (cdr args) is-opt is-rest))))
  1777. (defconst math-prim-funcs
  1778. '( (~= . math-nearly-equal)
  1779. (% . math-mod)
  1780. (lsh . calcFunc-lsh)
  1781. (ash . calcFunc-ash)
  1782. (logand . calcFunc-and)
  1783. (logandc2 . calcFunc-diff)
  1784. (logior . calcFunc-or)
  1785. (logxor . calcFunc-xor)
  1786. (lognot . calcFunc-not)
  1787. (equal . equal) ; need to leave these ones alone!
  1788. (eq . eq)
  1789. (and . and)
  1790. (or . or)
  1791. (if . if)
  1792. (^ . math-pow)
  1793. (expt . math-pow)
  1794. ))
  1795. (defconst math-prim-vars
  1796. '( (nil . nil)
  1797. (t . t)
  1798. (&optional . &optional)
  1799. (&rest . &rest)
  1800. ))
  1801. (defun math-define-function-body (body env)
  1802. (let ((body (math-define-body body env)))
  1803. (if (math-body-refers-to body 'math-return)
  1804. `((catch 'math-return ,@body))
  1805. body)))
  1806. ;; The variable math-exp-env is local to math-define-body, but is
  1807. ;; used by math-define-exp, which is called (indirectly) by
  1808. ;; by math-define-body.
  1809. (defvar math-exp-env)
  1810. (defun math-define-body (body math-exp-env)
  1811. (math-define-list body))
  1812. (defun math-define-list (body &optional quote)
  1813. (cond ((null body)
  1814. nil)
  1815. ((and (eq (car body) ':)
  1816. (stringp (nth 1 body)))
  1817. (cons (let* ((math-read-expr-quotes t)
  1818. (exp (math-read-plain-expr (nth 1 body) t)))
  1819. (math-define-exp exp))
  1820. (math-define-list (cdr (cdr body)))))
  1821. (quote
  1822. (cons (cond ((consp (car body))
  1823. (math-define-list (cdr body) t))
  1824. (t
  1825. (car body)))
  1826. (math-define-list (cdr body))))
  1827. (t
  1828. (cons (math-define-exp (car body))
  1829. (math-define-list (cdr body))))))
  1830. (defun math-define-exp (exp)
  1831. (cond ((consp exp)
  1832. (let ((func (car exp)))
  1833. (cond ((memq func '(quote function))
  1834. (if (and (consp (nth 1 exp))
  1835. (eq (car (nth 1 exp)) 'lambda))
  1836. (cons 'quote
  1837. (math-define-lambda (nth 1 exp) math-exp-env))
  1838. exp))
  1839. ((memq func '(let let* for foreach))
  1840. (let ((head (nth 1 exp))
  1841. (body (cdr (cdr exp))))
  1842. (if (memq func '(let let*))
  1843. ()
  1844. (setq func (cdr (assq func '((for . math-for)
  1845. (foreach . math-foreach)))))
  1846. (if (not (listp (car head)))
  1847. (setq head (list head))))
  1848. (macroexpand
  1849. (cons func
  1850. (cons (math-define-let head)
  1851. (math-define-body body
  1852. (nconc
  1853. (math-define-let-env head)
  1854. math-exp-env)))))))
  1855. ((and (memq func '(setq setf))
  1856. (math-complicated-lhs (cdr exp)))
  1857. (if (> (length exp) 3)
  1858. (cons 'progn (math-define-setf-list (cdr exp)))
  1859. (math-define-setf (nth 1 exp) (nth 2 exp))))
  1860. ((eq func 'condition-case)
  1861. (cons func
  1862. (cons (nth 1 exp)
  1863. (math-define-body (cdr (cdr exp))
  1864. (cons (nth 1 exp)
  1865. math-exp-env)))))
  1866. ((eq func 'cond)
  1867. (cons func
  1868. (math-define-cond (cdr exp))))
  1869. ((and (consp func) ; ('spam a b) == force use of plain spam
  1870. (eq (car func) 'quote))
  1871. (cons func (math-define-list (cdr exp))))
  1872. ((symbolp func)
  1873. (let ((args (math-define-list (cdr exp)))
  1874. (prim (assq func math-prim-funcs)))
  1875. (cond (prim
  1876. (cons (cdr prim) args))
  1877. ((eq func 'floatp)
  1878. (list 'eq (car args) '(quote float)))
  1879. ((eq func '+)
  1880. (math-define-binop 'math-add 0
  1881. (car args) (cdr args)))
  1882. ((eq func '-)
  1883. (if (= (length args) 1)
  1884. (cons 'math-neg args)
  1885. (math-define-binop 'math-sub 0
  1886. (car args) (cdr args))))
  1887. ((eq func '*)
  1888. (math-define-binop 'math-mul 1
  1889. (car args) (cdr args)))
  1890. ((eq func '/)
  1891. (math-define-binop 'math-div 1
  1892. (car args) (cdr args)))
  1893. ((eq func 'min)
  1894. (math-define-binop 'math-min 0
  1895. (car args) (cdr args)))
  1896. ((eq func 'max)
  1897. (math-define-binop 'math-max 0
  1898. (car args) (cdr args)))
  1899. ((eq func '<)
  1900. (if (and (math-numberp (nth 1 args))
  1901. (math-zerop (nth 1 args)))
  1902. (list 'math-negp (car args))
  1903. (cons 'math-lessp args)))
  1904. ((eq func '>)
  1905. (if (and (math-numberp (nth 1 args))
  1906. (math-zerop (nth 1 args)))
  1907. (list 'math-posp (car args))
  1908. (list 'math-lessp (nth 1 args) (nth 0 args))))
  1909. ((eq func '<=)
  1910. (list 'not
  1911. (if (and (math-numberp (nth 1 args))
  1912. (math-zerop (nth 1 args)))
  1913. (list 'math-posp (car args))
  1914. (list 'math-lessp
  1915. (nth 1 args) (nth 0 args)))))
  1916. ((eq func '>=)
  1917. (list 'not
  1918. (if (and (math-numberp (nth 1 args))
  1919. (math-zerop (nth 1 args)))
  1920. (list 'math-negp (car args))
  1921. (cons 'math-lessp args))))
  1922. ((eq func '=)
  1923. (if (and (math-numberp (nth 1 args))
  1924. (math-zerop (nth 1 args)))
  1925. (list 'math-zerop (nth 0 args))
  1926. (if (and (integerp (nth 1 args))
  1927. (/= (% (nth 1 args) 10) 0))
  1928. (cons 'math-equal-int args)
  1929. (cons 'math-equal args))))
  1930. ((eq func '/=)
  1931. (list 'not
  1932. (if (and (math-numberp (nth 1 args))
  1933. (math-zerop (nth 1 args)))
  1934. (list 'math-zerop (nth 0 args))
  1935. (if (and (integerp (nth 1 args))
  1936. (/= (% (nth 1 args) 10) 0))
  1937. (cons 'math-equal-int args)
  1938. (cons 'math-equal args)))))
  1939. ((eq func '1+)
  1940. (list 'math-add (car args) 1))
  1941. ((eq func '1-)
  1942. (list 'math-add (car args) -1))
  1943. ((eq func 'not) ; optimize (not (not x)) => x
  1944. (if (eq (car-safe args) func)
  1945. (car (nth 1 args))
  1946. (cons func args)))
  1947. ((and (eq func 'elt) (cdr (cdr args)))
  1948. (math-define-elt (car args) (cdr args)))
  1949. (t
  1950. (macroexpand
  1951. (let* ((name (symbol-name func))
  1952. (cfunc (intern (concat "calcFunc-" name)))
  1953. (mfunc (intern (concat "math-" name))))
  1954. (cond ((fboundp cfunc)
  1955. (cons cfunc args))
  1956. ((fboundp mfunc)
  1957. (cons mfunc args))
  1958. ((or (fboundp func)
  1959. (string-match "\\`calcFunc-.*" name))
  1960. (cons func args))
  1961. (t
  1962. (cons cfunc args)))))))))
  1963. (t (cons func (math-define-list (cdr exp))))))) ;;args
  1964. ((symbolp exp)
  1965. (let ((prim (assq exp math-prim-vars))
  1966. (name (symbol-name exp)))
  1967. (cond (prim
  1968. (cdr prim))
  1969. ((memq exp math-exp-env)
  1970. exp)
  1971. ((string-match "-" name)
  1972. exp)
  1973. (t
  1974. (intern (concat "var-" name))))))
  1975. ((integerp exp)
  1976. (if (or (<= exp -1000000) (>= exp 1000000))
  1977. (list 'quote (math-normalize exp))
  1978. exp))
  1979. (t exp)))
  1980. (defun math-define-cond (forms)
  1981. (and forms
  1982. (cons (math-define-list (car forms))
  1983. (math-define-cond (cdr forms)))))
  1984. (defun math-complicated-lhs (body)
  1985. (and body
  1986. (or (not (symbolp (car body)))
  1987. (math-complicated-lhs (cdr (cdr body))))))
  1988. (defun math-define-setf-list (body)
  1989. (and body
  1990. (cons (math-define-setf (nth 0 body) (nth 1 body))
  1991. (math-define-setf-list (cdr (cdr body))))))
  1992. (defun math-define-setf (place value)
  1993. (setq place (math-define-exp place)
  1994. value (math-define-exp value))
  1995. (cond ((symbolp place)
  1996. (list 'setq place value))
  1997. ((eq (car-safe place) 'nth)
  1998. (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value))
  1999. ((eq (car-safe place) 'elt)
  2000. (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value))
  2001. ((eq (car-safe place) 'car)
  2002. (list 'setcar (nth 1 place) value))
  2003. ((eq (car-safe place) 'cdr)
  2004. (list 'setcdr (nth 1 place) value))
  2005. (t
  2006. (error "Bad place form for setf: %s" place))))
  2007. (defun math-define-binop (op ident arg1 rest)
  2008. (if rest
  2009. (math-define-binop op ident
  2010. (list op arg1 (car rest))
  2011. (cdr rest))
  2012. (or arg1 ident)))
  2013. (defun math-define-let (vlist)
  2014. (and vlist
  2015. (cons (if (consp (car vlist))
  2016. (cons (car (car vlist))
  2017. (math-define-list (cdr (car vlist))))
  2018. (car vlist))
  2019. (math-define-let (cdr vlist)))))
  2020. (defun math-define-let-env (vlist)
  2021. (and vlist
  2022. (cons (if (consp (car vlist))
  2023. (car (car vlist))
  2024. (car vlist))
  2025. (math-define-let-env (cdr vlist)))))
  2026. (defun math-define-lambda (exp exp-env)
  2027. (nconc (list (nth 0 exp) ; 'lambda
  2028. (nth 1 exp)) ; arg list
  2029. (math-define-function-body (cdr (cdr exp))
  2030. (append (nth 1 exp) exp-env))))
  2031. (defun math-define-elt (seq idx)
  2032. (if idx
  2033. (math-define-elt (list 'elt seq (car idx)) (cdr idx))
  2034. seq))
  2035. ;;; Useful programming macros.
  2036. (defmacro math-while (head &rest body)
  2037. (let ((body (cons 'while (cons head body))))
  2038. (if (math-body-refers-to body 'math-break)
  2039. (cons 'catch (cons '(quote math-break) (list body)))
  2040. body)))
  2041. ;; (put 'math-while 'lisp-indent-hook 1)
  2042. (defmacro math-for (head &rest body)
  2043. (let ((body (if head
  2044. (math-handle-for head body)
  2045. (cons 'while (cons t body)))))
  2046. (if (math-body-refers-to body 'math-break)
  2047. (cons 'catch (cons '(quote math-break) (list body)))
  2048. body)))
  2049. ;; (put 'math-for 'lisp-indent-hook 1)
  2050. (defun math-handle-for (head body)
  2051. (let* ((var (nth 0 (car head)))
  2052. (init (nth 1 (car head)))
  2053. (limit (nth 2 (car head)))
  2054. (step (or (nth 3 (car head)) 1))
  2055. (body (if (cdr head)
  2056. (list (math-handle-for (cdr head) body))
  2057. body))
  2058. (all-ints (and (integerp init) (integerp limit) (integerp step)))
  2059. (const-limit (or (integerp limit)
  2060. (and (eq (car-safe limit) 'quote)
  2061. (math-realp (nth 1 limit)))))
  2062. (const-step (or (integerp step)
  2063. (and (eq (car-safe step) 'quote)
  2064. (math-realp (nth 1 step)))))
  2065. (save-limit (if const-limit limit (make-symbol "<limit>")))
  2066. (save-step (if const-step step (make-symbol "<step>"))))
  2067. (cons 'let
  2068. (cons (append (if const-limit nil (list (list save-limit limit)))
  2069. (if const-step nil (list (list save-step step)))
  2070. (list (list var init)))
  2071. (list
  2072. (cons 'while
  2073. (cons (if all-ints
  2074. (if (> step 0)
  2075. (list '<= var save-limit)
  2076. (list '>= var save-limit))
  2077. (list 'not
  2078. (if const-step
  2079. (if (or (math-posp step)
  2080. (math-posp
  2081. (cdr-safe step)))
  2082. (list 'math-lessp
  2083. save-limit
  2084. var)
  2085. (list 'math-lessp
  2086. var
  2087. save-limit))
  2088. (list 'if
  2089. (list 'math-posp
  2090. save-step)
  2091. (list 'math-lessp
  2092. save-limit
  2093. var)
  2094. (list 'math-lessp
  2095. var
  2096. save-limit)))))
  2097. (append body
  2098. (list (list 'setq
  2099. var
  2100. (list (if all-ints
  2101. '+
  2102. 'math-add)
  2103. var
  2104. save-step)))))))))))
  2105. (defmacro math-foreach (head &rest body)
  2106. (let ((body (math-handle-foreach head body)))
  2107. (if (math-body-refers-to body 'math-break)
  2108. (cons 'catch (cons '(quote math-break) (list body)))
  2109. body)))
  2110. ;; (put 'math-foreach 'lisp-indent-hook 1)
  2111. (defun math-handle-foreach (head body)
  2112. (let ((var (nth 0 (car head)))
  2113. (data (nth 1 (car head)))
  2114. (body (if (cdr head)
  2115. (list (math-handle-foreach (cdr head) body))
  2116. body)))
  2117. (cons 'let
  2118. (cons (list (list var data))
  2119. (list
  2120. (cons 'while
  2121. (cons var
  2122. (append body
  2123. (list (list 'setq
  2124. var
  2125. (list 'cdr var)))))))))))
  2126. (defun math-body-refers-to (body thing)
  2127. (or (equal body thing)
  2128. (and (consp body)
  2129. (or (math-body-refers-to (car body) thing)
  2130. (math-body-refers-to (cdr body) thing)))))
  2131. (defun math-break (&optional value)
  2132. (throw 'math-break value))
  2133. (defun math-return (&optional value)
  2134. (throw 'math-return value))
  2135. (defun math-composite-inequalities (x op)
  2136. (if (memq (nth 1 op) '(calcFunc-eq calcFunc-neq))
  2137. (if (eq (car x) (nth 1 op))
  2138. (append x (list (math-read-expr-level (nth 3 op))))
  2139. (throw 'syntax "Syntax error"))
  2140. (list 'calcFunc-in
  2141. (nth 2 x)
  2142. (if (memq (nth 1 op) '(calcFunc-lt calcFunc-leq))
  2143. (if (memq (car x) '(calcFunc-lt calcFunc-leq))
  2144. (math-make-intv
  2145. (+ (if (eq (car x) 'calcFunc-leq) 2 0)
  2146. (if (eq (nth 1 op) 'calcFunc-leq) 1 0))
  2147. (nth 1 x) (math-read-expr-level (nth 3 op)))
  2148. (throw 'syntax "Syntax error"))
  2149. (if (memq (car x) '(calcFunc-gt calcFunc-geq))
  2150. (math-make-intv
  2151. (+ (if (eq (nth 1 op) 'calcFunc-geq) 2 0)
  2152. (if (eq (car x) 'calcFunc-geq) 1 0))
  2153. (math-read-expr-level (nth 3 op)) (nth 1 x))
  2154. (throw 'syntax "Syntax error"))))))
  2155. (provide 'calc-prog)
  2156. ;;; calc-prog.el ends here