calc-misc.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. ;;; calc-misc.el --- miscellaneous functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; This file is autoloaded from calc.el.
  19. (require 'calc)
  20. (require 'calc-macs)
  21. ;; Declare functions which are defined elsewhere.
  22. (declare-function calc-do-keypad "calc-keypd" (&optional full-display interactive))
  23. (declare-function calc-inv-hyp-prefix-help "calc-help" ())
  24. (declare-function calc-inverse-prefix-help "calc-help" ())
  25. (declare-function calc-hyperbolic-prefix-help "calc-help" ())
  26. (declare-function calc-option-prefix-help "calc-help" ())
  27. (declare-function calc-explain-why "calc-stuff" (why &optional more))
  28. (declare-function calc-clear-command-flag "calc-ext" (f))
  29. (declare-function calc-roll-down-with-selections "calc-sel" (n m))
  30. (declare-function calc-roll-up-with-selections "calc-sel" (n m))
  31. (declare-function calc-last-args "calc-undo" (n))
  32. (declare-function calc-is-inverse "calc-ext" ())
  33. (declare-function calc-do-prefix-help "calc-ext" (msgs group key))
  34. (declare-function math-objvecp "calc-ext" (a))
  35. (declare-function math-known-scalarp "calc-arith" (a &optional assume-scalar))
  36. (declare-function math-vectorp "calc-ext" (a))
  37. (declare-function math-matrixp "calc-ext" (a))
  38. (declare-function math-trunc-special "calc-arith" (a prec))
  39. (declare-function math-trunc-fancy "calc-arith" (a))
  40. (declare-function math-floor-special "calc-arith" (a prec))
  41. (declare-function math-floor-fancy "calc-arith" (a))
  42. (declare-function math-square-matrixp "calc-ext" (a))
  43. (declare-function math-matrix-inv-raw "calc-mtx" (m))
  44. (declare-function math-known-matrixp "calc-arith" (a))
  45. (declare-function math-mod-fancy "calc-arith" (a b))
  46. (declare-function math-pow-of-zero "calc-arith" (a b))
  47. (declare-function math-pow-zero "calc-arith" (a b))
  48. (declare-function math-pow-fancy "calc-arith" (a b))
  49. (declare-function calc-locate-cursor-element "calc-yank" (pt))
  50. ;;;###autoload
  51. (defun calc-dispatch-help (arg)
  52. "C-x* is a prefix key sequence; follow it with one of these letters:
  53. For turning Calc on and off:
  54. C calc. Start the Calculator in a window at the bottom of the screen.
  55. O calc-other-window. Start the Calculator but don't select its window.
  56. B calc-big-or-small. Control whether to use the full Emacs screen for Calc.
  57. Q quick-calc. Use the Calculator in the minibuffer.
  58. K calc-keypad. Start the Calculator in keypad mode (X window system only).
  59. E calc-embedded. Use the Calculator on a formula in this editing buffer.
  60. J calc-embedded-select. Like E, but select appropriate half of => or :=.
  61. W calc-embedded-word. Like E, but activate a single word, i.e., a number.
  62. Z calc-user-invocation. Invoke Calc in the way you defined with `Z I' cmd.
  63. X calc-quit. Turn Calc off.
  64. For moving data into and out of Calc:
  65. G calc-grab-region. Grab the region defined by mark and point into Calc.
  66. R calc-grab-rectangle. Grab the rectangle defined by mark, point into Calc.
  67. : calc-grab-sum-down. Grab a rectangle and sum the columns.
  68. _ calc-grab-sum-across. Grab a rectangle and sum the rows.
  69. Y calc-copy-to-buffer. Copy a value from the stack into the editing buffer.
  70. For use with Embedded mode:
  71. A calc-embedded-activate. Find and activate all :='s and =>'s in buffer.
  72. D calc-embedded-duplicate. Make a copy of this formula and select it.
  73. F calc-embedded-new-formula. Insert a new formula at current point.
  74. N calc-embedded-next. Advance cursor to next known formula in buffer.
  75. P calc-embedded-previous. Advance cursor to previous known formula.
  76. U calc-embedded-update-formula. Re-evaluate formula at point.
  77. ` calc-embedded-edit. Use calc-edit to edit formula at point.
  78. Documentation:
  79. I calc-info. Read the Calculator manual in the Emacs Info system.
  80. T calc-tutorial. Run the Calculator Tutorial using the Emacs Info system.
  81. S calc-summary. Read the Summary from the Calculator manual in Info.
  82. Miscellaneous:
  83. L calc-load-everything. Load all parts of the Calculator into memory.
  84. M read-kbd-macro. Read a region of keystroke names as a keyboard macro.
  85. 0 (zero) calc-reset. Reset Calc stack and modes to default state.
  86. Press `*' twice (`C-x * *') to turn Calc on or off using the same
  87. Calc user interface as before (either C-x * C or C-x * K; initially C-x * C).
  88. "
  89. (interactive "P")
  90. (calc-check-defines)
  91. (if calc-dispatch-help
  92. (progn
  93. (save-window-excursion
  94. (describe-function 'calc-dispatch-help)
  95. (let ((win (get-buffer-window "*Help*")))
  96. (if win
  97. (let (key)
  98. (select-window win)
  99. (while (progn
  100. (message "Calc options: Calc, Keypad, ... %s"
  101. "press SPC, DEL to scroll, C-g to cancel")
  102. (memq (car (setq key (calc-read-key t)))
  103. '(? ?\C-h ?\C-? ?\C-v ?\M-v)))
  104. (condition-case err
  105. (if (memq (car key) '(? ?\C-v))
  106. (scroll-up)
  107. (scroll-down))
  108. (error (beep))))
  109. (calc-unread-command (cdr key))))))
  110. (calc-do-dispatch nil))
  111. (let ((calc-dispatch-help t))
  112. (calc-do-dispatch arg))))
  113. ;;;###autoload
  114. (defun calc-big-or-small (arg)
  115. "Toggle Calc between full-screen and regular mode."
  116. (interactive "P")
  117. (let ((cwin (get-buffer-window "*Calculator*"))
  118. (twin (get-buffer-window "*Calc Trail*"))
  119. (kwin (get-buffer-window "*Calc Keypad*")))
  120. (if cwin
  121. (setq calc-full-mode
  122. (if kwin
  123. (and twin (window-full-width-p twin))
  124. (window-full-height-p cwin))))
  125. (setq calc-full-mode (if arg
  126. (> (prefix-numeric-value arg) 0)
  127. (not calc-full-mode)))
  128. (if kwin
  129. (progn
  130. (calc-quit)
  131. (calc-do-keypad calc-full-mode nil))
  132. (if cwin
  133. (progn
  134. (calc-quit)
  135. (calc nil calc-full-mode nil))))
  136. (message (if calc-full-mode
  137. "Now using full screen for Calc"
  138. "Now using partial screen for Calc"))))
  139. ;;;###autoload
  140. (defun calc-other-window (&optional interactive)
  141. "Invoke the Calculator in another window."
  142. (interactive "p")
  143. (if (memq major-mode '(calc-mode calc-trail-mode))
  144. (progn
  145. (other-window 1)
  146. (if (memq major-mode '(calc-mode calc-trail-mode))
  147. (other-window 1)))
  148. (if (get-buffer-window "*Calculator*")
  149. (calc-quit)
  150. (let ((win (selected-window)))
  151. (calc nil win interactive)))))
  152. ;;;###autoload
  153. (defun another-calc ()
  154. "Create another, independent Calculator buffer."
  155. (interactive)
  156. (if (eq major-mode 'calc-mode)
  157. (mapc (function
  158. (lambda (v)
  159. (set-default v (symbol-value v)))) calc-local-var-list))
  160. (set-buffer (generate-new-buffer "*Calculator*"))
  161. (pop-to-buffer (current-buffer))
  162. (calc-mode))
  163. ;;;###autoload
  164. (defun calc-info ()
  165. "Run the Emacs Info system on the Calculator documentation."
  166. (interactive)
  167. (select-window (get-largest-window))
  168. (info "Calc"))
  169. ;;;###autoload
  170. (defun calc-info-goto-node (node)
  171. "Go to a node in the Calculator info documentation."
  172. (interactive)
  173. (select-window (get-largest-window))
  174. (info (concat "(Calc)" node)))
  175. ;;;###autoload
  176. (defun calc-tutorial ()
  177. "Run the Emacs Info system on the Calculator Tutorial."
  178. (interactive)
  179. (if (get-buffer-window "*Calculator*")
  180. (calc-quit))
  181. (calc-info-goto-node "Interactive Tutorial")
  182. (calc-other-window)
  183. (message "Welcome to the Calc Tutorial!"))
  184. ;;;###autoload
  185. (defun calc-info-summary ()
  186. "Run the Emacs Info system on the Calculator Summary."
  187. (interactive)
  188. (calc-info-goto-node "Summary"))
  189. ;;;###autoload
  190. (defun calc-help ()
  191. (interactive)
  192. (let ((msgs
  193. '("Press `h' for complete help; press `?' repeatedly for a summary"
  194. "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit"
  195. "Letter keys: SHIFT + Undo, reDo; Inverse, Hyperbolic, Option"
  196. "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB"
  197. "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi"
  198. "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro; Keep-args"
  199. "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)"
  200. "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)"
  201. "Other keys: ' (alg-entry), = (eval), ` (edit); M-RET (last-args)"
  202. "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)"
  203. "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)"
  204. "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)"
  205. "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)"
  206. "Prefix keys: Algebra, Binary/business, Convert, Display"
  207. "Prefix keys: Functions, Graphics, Help, J (select)"
  208. "Prefix keys: Kombinatorics/statistics, Modes, Store/recall"
  209. "Prefix keys: Trail/time, Units/statistics, Vector/matrix"
  210. "Prefix keys: Z (user), SHIFT + Z (define)"
  211. "Prefix keys: prefix + ? gives further help for that prefix"
  212. " Calc by Dave Gillespie, daveg@synaptics.com")))
  213. (if calc-full-help-flag
  214. msgs
  215. (if (or calc-inverse-flag calc-hyperbolic-flag)
  216. (if calc-inverse-flag
  217. (if calc-hyperbolic-flag
  218. (calc-inv-hyp-prefix-help)
  219. (calc-inverse-prefix-help))
  220. (calc-hyperbolic-prefix-help))
  221. (if calc-option-flag
  222. (calc-option-prefix-help)
  223. (setq calc-help-phase
  224. (if (eq this-command last-command)
  225. (% (1+ calc-help-phase) (1+ (length msgs)))
  226. 0))
  227. (let ((msg (nth calc-help-phase msgs)))
  228. (message "%s" (if msg
  229. (concat msg ":"
  230. (make-string (- (apply 'max
  231. (mapcar 'length
  232. msgs))
  233. (length msg)) 32)
  234. " [?=MORE]")
  235. ""))))))))
  236. ;;;; Stack and buffer management.
  237. ;; The variable calc-last-why-command is set in calc-do-handle-whys
  238. ;; and used in calc-why (in calc-stuff.el).
  239. (defvar calc-last-why-command)
  240. ;;;###autoload
  241. (defun calc-do-handle-whys ()
  242. (setq calc-why (sort calc-next-why
  243. (function
  244. (lambda (x y)
  245. (and (eq (car x) '*) (not (eq (car y) '*))))))
  246. calc-next-why nil)
  247. (if (and calc-why (or (eq calc-auto-why t)
  248. (and (eq (car (car calc-why)) '*)
  249. calc-auto-why)))
  250. (progn
  251. (require 'calc-ext)
  252. (calc-explain-why (car calc-why)
  253. (if (eq calc-auto-why t)
  254. (cdr calc-why)
  255. (if calc-auto-why
  256. (eq (car (nth 1 calc-why)) '*))))
  257. (setq calc-last-why-command this-command)
  258. (calc-clear-command-flag 'clear-message))))
  259. ;;;###autoload
  260. (defun calc-record-why (&rest stuff)
  261. (if (eq (car stuff) 'quiet)
  262. (setq stuff (cdr stuff))
  263. (if (and (symbolp (car stuff))
  264. (cdr stuff)
  265. (or (Math-objectp (nth 1 stuff))
  266. (and (Math-vectorp (nth 1 stuff))
  267. (math-constp (nth 1 stuff)))
  268. (math-infinitep (nth 1 stuff))))
  269. (setq stuff (cons '* stuff))
  270. (if (and (stringp (car stuff))
  271. (string-match "\\`\\*" (car stuff)))
  272. (setq stuff (cons '* (cons (substring (car stuff) 1)
  273. (cdr stuff)))))))
  274. (setq calc-next-why (cons stuff calc-next-why))
  275. nil)
  276. ;; True if A is a constant or vector of constants. [P x] [Public]
  277. ;;;###autoload
  278. (defun math-constp (a)
  279. (or (Math-scalarp a)
  280. (and (memq (car a) '(sdev intv mod vec))
  281. (progn
  282. (while (and (setq a (cdr a))
  283. (or (Math-scalarp (car a)) ; optimization
  284. (math-constp (car a)))))
  285. (null a)))))
  286. ;;;###autoload
  287. (defun calc-roll-down-stack (n &optional m)
  288. (if (< n 0)
  289. (calc-roll-up-stack (- n) m)
  290. (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
  291. (or m (setq m 1))
  292. (and (> n 1)
  293. (< m n)
  294. (if (and calc-any-selections
  295. (not calc-use-selections))
  296. (calc-roll-down-with-selections n m)
  297. (calc-pop-push-list n
  298. (append (calc-top-list m 1)
  299. (calc-top-list (- n m) (1+ m))))))))
  300. ;;;###autoload
  301. (defun calc-roll-up-stack (n &optional m)
  302. (if (< n 0)
  303. (calc-roll-down-stack (- n) m)
  304. (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
  305. (or m (setq m 1))
  306. (and (> n 1)
  307. (< m n)
  308. (if (and calc-any-selections
  309. (not calc-use-selections))
  310. (calc-roll-up-with-selections n m)
  311. (calc-pop-push-list n
  312. (append (calc-top-list (- n m) 1)
  313. (calc-top-list m (- n m -1))))))))
  314. ;;;###autoload
  315. (defun calc-do-refresh ()
  316. (if calc-hyperbolic-flag
  317. (progn
  318. (setq calc-display-dirty t)
  319. nil)
  320. (calc-refresh)
  321. t))
  322. ;;;###autoload
  323. (defun calc-record-list (vals &optional prefix)
  324. (while vals
  325. (or (eq (car vals) 'top-of-stack)
  326. (progn
  327. (calc-record (car vals) prefix)
  328. (setq prefix "...")))
  329. (setq vals (cdr vals))))
  330. ;;;###autoload
  331. (defun calc-last-args-stub (arg)
  332. (interactive "p")
  333. (require 'calc-ext)
  334. (calc-last-args arg))
  335. ;;;###autoload
  336. (defun calc-power (arg)
  337. (interactive "P")
  338. (calc-slow-wrapper
  339. (if (and (featurep 'calc-ext)
  340. (calc-is-inverse))
  341. (calc-binary-op "root" 'calcFunc-nroot arg nil nil)
  342. (calc-binary-op "^" 'calcFunc-pow arg nil nil '^))))
  343. ;;;###autoload
  344. (defun calc-mod (arg)
  345. (interactive "P")
  346. (calc-slow-wrapper
  347. (calc-binary-op "%" 'calcFunc-mod arg nil nil '%)))
  348. ;;;###autoload
  349. (defun calc-inv (arg)
  350. (interactive "P")
  351. (calc-slow-wrapper
  352. (calc-unary-op "inv" 'calcFunc-inv arg)))
  353. ;;;###autoload
  354. (defun calc-percent ()
  355. (interactive)
  356. (calc-slow-wrapper
  357. (calc-pop-push-record-list
  358. 1 "%" (list (list 'calcFunc-percent (calc-top-n 1))))))
  359. ;;;###autoload
  360. (defun calc-over (n)
  361. (interactive "P")
  362. (if n
  363. (calc-enter (- (prefix-numeric-value n)))
  364. (calc-enter -2)))
  365. ;;;###autoload
  366. (defun calc-pop-above (n)
  367. (interactive "P")
  368. (if n
  369. (calc-pop (- (prefix-numeric-value n)))
  370. (calc-pop -2)))
  371. ;;;###autoload
  372. (defun calc-roll-down (n)
  373. (interactive "P")
  374. (calc-wrapper
  375. (let ((nn (prefix-numeric-value n)))
  376. (cond ((null n)
  377. (calc-roll-down-stack 2))
  378. ((> nn 0)
  379. (calc-roll-down-stack nn))
  380. ((= nn 0)
  381. (calc-pop-push-list (calc-stack-size)
  382. (reverse
  383. (calc-top-list (calc-stack-size)))))
  384. (t
  385. (calc-roll-down-stack (calc-stack-size) (- nn)))))))
  386. ;;;###autoload
  387. (defun calc-roll-up (n)
  388. (interactive "P")
  389. (calc-wrapper
  390. (let ((nn (prefix-numeric-value n)))
  391. (cond ((null n)
  392. (calc-roll-up-stack 3))
  393. ((> nn 0)
  394. (calc-roll-up-stack nn))
  395. ((= nn 0)
  396. (calc-pop-push-list (calc-stack-size)
  397. (reverse
  398. (calc-top-list (calc-stack-size)))))
  399. (t
  400. (calc-roll-up-stack (calc-stack-size) (- nn)))))))
  401. ;;;###autoload
  402. (defun calc-transpose-lines (&optional arg)
  403. "Transpose previous line and current line.
  404. With argument ARG, move previous line past ARG lines.
  405. With argument 0, switch line point is in with line mark is in."
  406. (interactive "p")
  407. (setq arg (or arg 1))
  408. (let (bot-line mid-line end-line
  409. old-top-list new-top-list
  410. bot-cell mid-cell
  411. prev-mid-cell post-mid-cell post-bot-cell)
  412. (calc-wrapper
  413. (when (eq major-mode 'calc-mode)
  414. (cond
  415. ;; exchange point and mark
  416. ((= 0 arg)
  417. (setq bot-line (calc-locate-cursor-element (point))
  418. mid-line (mark))
  419. (if mid-line
  420. (setq mid-line (calc-locate-cursor-element mid-line)
  421. end-line (1+ mid-line))
  422. (error "No mark set"))
  423. (if (< bot-line mid-line)
  424. (let ((temp mid-line))
  425. (setq mid-line bot-line
  426. bot-line temp))))
  427. ;; move bot-line to mid-line that is above bot-line on stack (that is
  428. ;; to say mid-line displayed below bot-line in *Calculator* buffer)
  429. ((> arg 0)
  430. (setq bot-line (1+ (calc-locate-cursor-element (point)))
  431. mid-line (- bot-line arg)
  432. end-line mid-line))
  433. ;; move bot-line to mid-line that is above bot-line on stack (that is
  434. ;; to say mid-line displayed below bot-line in *Calculator* buffer)
  435. ((< arg 0)
  436. (setq mid-line (1+ (calc-locate-cursor-element (point)))
  437. bot-line (- mid-line arg)
  438. end-line bot-line)))
  439. (calc-check-stack bot-line)
  440. (if (= 0 mid-line)
  441. (error "Can't transpose beyond top"))
  442. (setq old-top-list (nreverse (calc-top-list bot-line)))
  443. ;; example: (arg = 2)
  444. ;; old-top-list =
  445. ;; 1 <-- top of stack (bottom of *Calculator* buffer)
  446. ;; 2
  447. ;; 3 <-- mid-line = 3
  448. ;; 4 <-- point
  449. ;; 5 <-- bot-line = 5
  450. (dotimes (i mid-line)
  451. (setq mid-cell old-top-list
  452. old-top-list (cdr old-top-list))
  453. (setcdr mid-cell new-top-list)
  454. (setq new-top-list mid-cell))
  455. ;; example follow-up:
  456. ;; old-top-list =
  457. ;; 4
  458. ;; 5
  459. ;; new-top-list =
  460. ;; 3 <-- mid-cell
  461. ;; 2
  462. ;; 1
  463. (setq prev-mid-cell old-top-list)
  464. (dotimes (i (- bot-line mid-line))
  465. (setq bot-cell old-top-list
  466. old-top-list (cdr old-top-list))
  467. (setcdr bot-cell new-top-list)
  468. (setq new-top-list bot-cell))
  469. (setq post-mid-cell (cdr mid-cell)
  470. post-bot-cell (cdr bot-cell))
  471. ;; example follow-up:
  472. ;; new-top-list =
  473. ;; 5 <-- bot-cell
  474. ;; 4 <-- prev-mid-cell & post-bot-cell
  475. ;; 3 <-- mid-cell
  476. ;; 2 <-- post-mid-cell
  477. ;; 1
  478. (cond
  479. ((= 0 arg); swap bot and mid
  480. (setcdr mid-cell post-bot-cell)
  481. (setcdr bot-cell post-mid-cell)
  482. (setcdr prev-mid-cell bot-cell)
  483. ;; example follow-up:
  484. ;; 3 <-- mid-cell
  485. ;; 4 <-- post-bot-cell & prev-mid-cell
  486. ;; 5 <-- bot-cell
  487. ;; 2 <-- post-mid-cell
  488. ;; 1
  489. (setq new-top-list mid-cell))
  490. ((< 0 arg) ; move bot just after mid
  491. (setcdr mid-cell bot-cell)
  492. (setcdr bot-cell post-mid-cell)
  493. ;; example follow-up:
  494. ;; new-top-list =
  495. ;; 4 <-- post-bot-cell
  496. ;; 3 <-- mid-cell
  497. ;; 5 <-- bot-cell
  498. ;; 2 <-- post-mid-cell
  499. ;; 1
  500. (setq new-top-list post-bot-cell))
  501. ((> 0 arg) ; move mid just before bot
  502. (setcdr mid-cell bot-cell)
  503. (setcdr prev-mid-cell post-mid-cell)
  504. ;; example follow-up:
  505. ;; new-top-list =
  506. ;; 3 <-- mid-cell
  507. ;; 5 <-- bot-cell
  508. ;; 4 <-- prev-mid-cell
  509. ;; 2 <-- post-mid-cell
  510. ;; 1
  511. (setq new-top-list mid-cell)))
  512. (calc-pop-push-list bot-line new-top-list)))
  513. (calc-cursor-stack-index (1- end-line))))
  514. ;;; Other commands.
  515. ;;;###autoload
  516. (defun calc-num-prefix-name (n)
  517. (cond ((eq n '-) "- ")
  518. ((equal n '(4)) "C-u ")
  519. ((consp n) (format "%d " (car n)))
  520. ((integerp n) (format "%d " n))
  521. (t "")))
  522. ;;;###autoload
  523. (defun calc-missing-key (n)
  524. "This is a placeholder for a command which needs to be loaded from calc-ext.
  525. When this key is used, calc-ext (the Calculator extensions module) will be
  526. loaded and the keystroke automatically re-typed."
  527. (interactive "P")
  528. (require 'calc-ext)
  529. (if (keymapp (key-binding (char-to-string last-command-event)))
  530. (message "%s%c-" (calc-num-prefix-name n) last-command-event))
  531. (calc-unread-command)
  532. (setq prefix-arg n))
  533. ;;;###autoload
  534. (defun calc-shift-Y-prefix-help ()
  535. (interactive)
  536. (require 'calc-ext)
  537. (calc-do-prefix-help calc-Y-help-msgs "other" ?Y))
  538. ;;;###autoload
  539. (defun calcDigit-letter ()
  540. (interactive)
  541. (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
  542. (progn
  543. (setq last-command-event (upcase last-command-event))
  544. (calcDigit-key))
  545. (calcDigit-nondigit)))
  546. ;; A Lisp version of temp_minibuffer_message from minibuf.c.
  547. ;;;###autoload
  548. (defun calc-temp-minibuffer-message (m)
  549. (let ((savemax (point-max)))
  550. (save-excursion
  551. (goto-char (point-max))
  552. (insert m))
  553. (let ((okay nil))
  554. (unwind-protect
  555. (progn
  556. (sit-for 2)
  557. (identity 1) ; this forces a call to QUIT; in bytecode.c.
  558. (setq okay t))
  559. (progn
  560. (delete-region savemax (point-max))
  561. (or okay (abort-recursive-edit)))))))
  562. (put 'math-with-extra-prec 'lisp-indent-hook 1)
  563. ;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
  564. ;;;###autoload
  565. (defun math-concat (v1 v2)
  566. (if (stringp v1)
  567. (concat v1 v2)
  568. (require 'calc-ext)
  569. (if (and (or (math-objvecp v1) (math-known-scalarp v1))
  570. (or (math-objvecp v2) (math-known-scalarp v2)))
  571. (append (if (and (math-vectorp v1)
  572. (or (math-matrixp v1)
  573. (not (math-matrixp v2))))
  574. v1
  575. (list 'vec v1))
  576. (if (and (math-vectorp v2)
  577. (or (math-matrixp v2)
  578. (not (math-matrixp v1))))
  579. (cdr v2)
  580. (list v2)))
  581. (list '| v1 v2))))
  582. ;; True if A is zero. Works for un-normalized values. [P n] [Public]
  583. ;;;###autoload
  584. (defun math-zerop (a)
  585. (if (consp a)
  586. (cond ((memq (car a) '(bigpos bigneg))
  587. (while (eq (car (setq a (cdr a))) 0))
  588. (null a))
  589. ((memq (car a) '(frac float polar mod))
  590. (math-zerop (nth 1 a)))
  591. ((eq (car a) 'cplx)
  592. (and (math-zerop (nth 1 a)) (math-zerop (nth 2 a))))
  593. ((eq (car a) 'hms)
  594. (and (math-zerop (nth 1 a))
  595. (math-zerop (nth 2 a))
  596. (math-zerop (nth 3 a)))))
  597. (eq a 0)))
  598. ;; True if A is real and negative. [P n] [Public]
  599. ;;;###autoload
  600. (defun math-negp (a)
  601. (if (consp a)
  602. (cond ((eq (car a) 'bigpos) nil)
  603. ((eq (car a) 'bigneg) (cdr a))
  604. ((memq (car a) '(float frac))
  605. (Math-integer-negp (nth 1 a)))
  606. ((eq (car a) 'hms)
  607. (if (math-zerop (nth 1 a))
  608. (if (math-zerop (nth 2 a))
  609. (math-negp (nth 3 a))
  610. (math-negp (nth 2 a)))
  611. (math-negp (nth 1 a))))
  612. ((eq (car a) 'date)
  613. (math-negp (nth 1 a)))
  614. ((eq (car a) 'intv)
  615. (or (math-negp (nth 3 a))
  616. (and (math-zerop (nth 3 a))
  617. (memq (nth 1 a) '(0 2)))))
  618. ((equal a '(neg (var inf var-inf))) t))
  619. (< a 0)))
  620. ;; True if A is a negative number or an expression the starts with '-'.
  621. ;;;###autoload
  622. (defun math-looks-negp (a) ; [P x] [Public]
  623. (or (Math-negp a)
  624. (eq (car-safe a) 'neg)
  625. (and (memq (car-safe a) '(* /))
  626. (or (math-looks-negp (nth 1 a))
  627. (math-looks-negp (nth 2 a))))
  628. (and (eq (car-safe a) '-)
  629. (math-looks-negp (nth 1 a)))))
  630. ;; True if A is real and positive. [P n] [Public]
  631. ;;;###autoload
  632. (defun math-posp (a)
  633. (if (consp a)
  634. (cond ((eq (car a) 'bigpos) (cdr a))
  635. ((eq (car a) 'bigneg) nil)
  636. ((memq (car a) '(float frac))
  637. (Math-integer-posp (nth 1 a)))
  638. ((eq (car a) 'hms)
  639. (if (math-zerop (nth 1 a))
  640. (if (math-zerop (nth 2 a))
  641. (math-posp (nth 3 a))
  642. (math-posp (nth 2 a)))
  643. (math-posp (nth 1 a))))
  644. ((eq (car a) 'date)
  645. (math-posp (nth 1 a)))
  646. ((eq (car a) 'mod)
  647. (not (math-zerop (nth 1 a))))
  648. ((eq (car a) 'intv)
  649. (or (math-posp (nth 2 a))
  650. (and (math-zerop (nth 2 a))
  651. (memq (nth 1 a) '(0 1)))))
  652. ((equal a '(var inf var-inf)) t))
  653. (> a 0)))
  654. ;;;###autoload
  655. (defalias 'math-fixnump 'integerp)
  656. ;;;###autoload
  657. (defalias 'math-fixnatnump 'natnump)
  658. ;; True if A is an even integer. [P R R] [Public]
  659. ;;;###autoload
  660. (defun math-evenp (a)
  661. (if (consp a)
  662. (and (memq (car a) '(bigpos bigneg))
  663. (= (% (nth 1 a) 2) 0))
  664. (= (% a 2) 0)))
  665. ;; Compute A / 2, for small or big integer A. [I i]
  666. ;; If A is negative, type of truncation is undefined.
  667. ;;;###autoload
  668. (defun math-div2 (a)
  669. (if (consp a)
  670. (if (cdr a)
  671. (math-normalize (cons (car a) (math-div2-bignum (cdr a))))
  672. 0)
  673. (/ a 2)))
  674. ;;;###autoload
  675. (defun math-div2-bignum (a) ; [l l]
  676. (if (cdr a)
  677. (cons (+ (/ (car a) 2) (* (% (nth 1 a) 2) (/ math-bignum-digit-size 2)))
  678. (math-div2-bignum (cdr a)))
  679. (list (/ (car a) 2))))
  680. ;; Reject an argument to a calculator function. [Public]
  681. ;;;###autoload
  682. (defun math-reject-arg (&optional a p option)
  683. (if option
  684. (calc-record-why option p a)
  685. (if p
  686. (calc-record-why p a)))
  687. (signal 'wrong-type-argument (and a (if p (list p a) (list a)))))
  688. ;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
  689. ;; The variable math-trunc-prec is local to math-trunc, but used by
  690. ;; math-trunc-fancy in calc-arith.el, which is called by math-trunc.
  691. ;;;###autoload
  692. (defun math-trunc (a &optional math-trunc-prec)
  693. (cond (math-trunc-prec
  694. (require 'calc-ext)
  695. (math-trunc-special a math-trunc-prec))
  696. ((Math-integerp a) a)
  697. ((Math-looks-negp a)
  698. (math-neg (math-trunc (math-neg a))))
  699. ((eq (car a) 'float)
  700. (math-scale-int (nth 1 a) (nth 2 a)))
  701. (t (require 'calc-ext)
  702. (math-trunc-fancy a))))
  703. ;;;###autoload
  704. (defalias 'calcFunc-trunc 'math-trunc)
  705. ;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
  706. ;; The variable math-floor-prec is local to math-floor, but used by
  707. ;; math-floor-fancy in calc-arith.el, which is called by math-floor.
  708. ;;;###autoload
  709. (defun math-floor (a &optional math-floor-prec) ; [Public]
  710. (cond (math-floor-prec
  711. (require 'calc-ext)
  712. (math-floor-special a math-floor-prec))
  713. ((Math-integerp a) a)
  714. ((Math-messy-integerp a) (math-trunc a))
  715. ((Math-realp a)
  716. (if (Math-negp a)
  717. (math-add (math-trunc a) -1)
  718. (math-trunc a)))
  719. (t (require 'calc-ext)
  720. (math-floor-fancy a))))
  721. ;;;###autoload
  722. (defalias 'calcFunc-floor 'math-floor)
  723. ;;;###autoload
  724. (defun math-imod (a b) ; [I I I] [Public]
  725. (if (and (not (consp a)) (not (consp b)))
  726. (if (= b 0)
  727. (math-reject-arg a "*Division by zero")
  728. (% a b))
  729. (cdr (math-idivmod a b))))
  730. ;;;###autoload
  731. (defun calcFunc-inv (m)
  732. (if (Math-vectorp m)
  733. (progn
  734. (require 'calc-ext)
  735. (if (math-square-matrixp m)
  736. (or (math-with-extra-prec 2 (math-matrix-inv-raw m))
  737. (math-reject-arg m "*Singular matrix"))
  738. (math-reject-arg m 'square-matrixp)))
  739. (if (and
  740. (require 'calc-arith)
  741. (math-known-matrixp m))
  742. (math-pow m -1)
  743. (math-div 1 m))))
  744. ;;;###autoload
  745. (defun math-do-working (msg arg)
  746. (or executing-kbd-macro
  747. (progn
  748. (calc-set-command-flag 'clear-message)
  749. (if math-working-step
  750. (if math-working-step-2
  751. (setq msg (format "[%d/%d] %s"
  752. math-working-step math-working-step-2 msg))
  753. (setq msg (format "[%d] %s" math-working-step msg))))
  754. (message "Working... %s = %s" msg
  755. (math-showing-full-precision (math-format-number arg))))))
  756. ;; Compute A modulo B, defined in terms of truncation toward minus infinity.
  757. ;;;###autoload
  758. (defun math-mod (a b) ; [R R R] [Public]
  759. (cond ((and (Math-zerop a) (not (eq (car-safe a) 'mod))) a)
  760. ((Math-zerop b)
  761. (math-reject-arg a "*Division by zero"))
  762. ((and (Math-natnump a) (Math-natnump b))
  763. (math-imod a b))
  764. ((and (Math-anglep a) (Math-anglep b))
  765. (math-sub a (math-mul (math-floor (math-div a b)) b)))
  766. (t (require 'calc-ext)
  767. (math-mod-fancy a b))))
  768. ;;; General exponentiation.
  769. ;;;###autoload
  770. (defun math-pow (a b) ; [O O N] [Public]
  771. (cond ((equal b '(var nan var-nan))
  772. b)
  773. ((Math-zerop a)
  774. (if (and (Math-scalarp b) (Math-posp b))
  775. (if (math-floatp b) (math-float a) a)
  776. (require 'calc-ext)
  777. (math-pow-of-zero a b)))
  778. ((or (eq a 1) (eq b 1)) a)
  779. ((or (equal a '(float 1 0)) (equal b '(float 1 0))) a)
  780. ((Math-zerop b)
  781. (if (Math-scalarp a)
  782. (if (or (math-floatp a) (math-floatp b))
  783. '(float 1 0) 1)
  784. (require 'calc-ext)
  785. (math-pow-zero a b)))
  786. ((and (Math-integerp b) (or (Math-numberp a) (Math-vectorp a)))
  787. (if (and (equal a '(float 1 1)) (integerp b))
  788. (math-make-float 1 b)
  789. (math-with-extra-prec 2
  790. (math-ipow a b))))
  791. (t
  792. (require 'calc-ext)
  793. (math-pow-fancy a b))))
  794. ;;;###autoload
  795. (defun math-ipow (a n) ; [O O I] [Public]
  796. (cond ((Math-integer-negp n)
  797. (math-ipow (math-div 1 a) (Math-integer-neg n)))
  798. ((not (consp n))
  799. (if (and (Math-ratp a) (> n 20))
  800. (math-iipow-show a n)
  801. (math-iipow a n)))
  802. ((math-evenp n)
  803. (math-ipow (math-mul a a) (math-div2 n)))
  804. (t
  805. (math-mul a (math-ipow (math-mul a a)
  806. (math-div2 (math-add n -1)))))))
  807. (defun math-iipow (a n) ; [O O S]
  808. (cond ((= n 0) 1)
  809. ((= n 1) a)
  810. ((= (% n 2) 0) (math-iipow (math-mul a a) (/ n 2)))
  811. (t (math-mul a (math-iipow (math-mul a a) (/ n 2))))))
  812. (defun math-iipow-show (a n) ; [O O S]
  813. (math-working "pow" a)
  814. (let ((val (cond
  815. ((= n 0) 1)
  816. ((= n 1) a)
  817. ((= (% n 2) 0) (math-iipow-show (math-mul a a) (/ n 2)))
  818. (t (math-mul a (math-iipow-show (math-mul a a) (/ n 2)))))))
  819. (math-working "pow" val)
  820. val))
  821. ;;;###autoload
  822. (defun math-read-radix-digit (dig) ; [D S; Z S]
  823. (if (> dig ?9)
  824. (if (< dig ?A)
  825. nil
  826. (- dig 55))
  827. (if (>= dig ?0)
  828. (- dig ?0)
  829. nil)))
  830. ;;; Bug reporting
  831. ;;;###autoload
  832. (defun report-calc-bug ()
  833. "Report a bug in Calc, the GNU Emacs calculator.
  834. Prompts for bug subject. Leaves you in a mail buffer."
  835. (interactive)
  836. (let ((reporter-prompt-for-summary-p t))
  837. (reporter-submit-bug-report calc-bug-address "Calc"
  838. nil nil nil
  839. "Please describe exactly what actions triggered the bug and the
  840. precise symptoms of the bug. If possible, include a backtrace by
  841. doing 'M-x toggle-debug-on-error', then reproducing the bug.
  842. " )))
  843. ;;;###autoload
  844. (defalias 'calc-report-bug 'report-calc-bug)
  845. (provide 'calc-misc)
  846. ;; Local variables:
  847. ;; generated-autoload-file: "calc-loaddefs.el"
  848. ;; End:
  849. ;;; calc-misc.el ends here