calc-misc.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. ;;; calc-misc.el --- miscellaneous functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2015 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. (unless (member stuff calc-next-why)
  275. (setq calc-next-why (cons stuff calc-next-why)))
  276. nil)
  277. ;; True if A is a constant or vector of constants. [P x] [Public]
  278. ;;;###autoload
  279. (defun math-constp (a)
  280. (or (Math-scalarp a)
  281. (and (memq (car a) '(sdev intv mod vec))
  282. (progn
  283. (while (and (setq a (cdr a))
  284. (or (Math-scalarp (car a)) ; optimization
  285. (math-constp (car a)))))
  286. (null a)))))
  287. ;;;###autoload
  288. (defun calc-roll-down-stack (n &optional m)
  289. (if (< n 0)
  290. (calc-roll-up-stack (- n) m)
  291. (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
  292. (or m (setq m 1))
  293. (and (> n 1)
  294. (< m n)
  295. (if (and calc-any-selections
  296. (not calc-use-selections))
  297. (calc-roll-down-with-selections n m)
  298. (calc-pop-push-list n
  299. (append (calc-top-list m 1)
  300. (calc-top-list (- n m) (1+ m))))))))
  301. ;;;###autoload
  302. (defun calc-roll-up-stack (n &optional m)
  303. (if (< n 0)
  304. (calc-roll-down-stack (- n) m)
  305. (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
  306. (or m (setq m 1))
  307. (and (> n 1)
  308. (< m n)
  309. (if (and calc-any-selections
  310. (not calc-use-selections))
  311. (calc-roll-up-with-selections n m)
  312. (calc-pop-push-list n
  313. (append (calc-top-list (- n m) 1)
  314. (calc-top-list m (- n m -1))))))))
  315. ;;;###autoload
  316. (defun calc-do-refresh ()
  317. (if calc-hyperbolic-flag
  318. (progn
  319. (setq calc-display-dirty t)
  320. nil)
  321. (calc-refresh)
  322. t))
  323. ;;;###autoload
  324. (defun calc-record-list (vals &optional prefix)
  325. (while vals
  326. (or (eq (car vals) 'top-of-stack)
  327. (progn
  328. (calc-record (car vals) prefix)
  329. (setq prefix "...")))
  330. (setq vals (cdr vals))))
  331. ;;;###autoload
  332. (defun calc-last-args-stub (arg)
  333. (interactive "p")
  334. (require 'calc-ext)
  335. (calc-last-args arg))
  336. ;;;###autoload
  337. (defun calc-power (arg)
  338. (interactive "P")
  339. (calc-slow-wrapper
  340. (if (and (featurep 'calc-ext)
  341. (calc-is-inverse))
  342. (calc-binary-op "root" 'calcFunc-nroot arg nil nil)
  343. (calc-binary-op "^" 'calcFunc-pow arg nil nil '^))))
  344. ;;;###autoload
  345. (defun calc-mod (arg)
  346. (interactive "P")
  347. (calc-slow-wrapper
  348. (calc-binary-op "%" 'calcFunc-mod arg nil nil '%)))
  349. ;;;###autoload
  350. (defun calc-inv (arg)
  351. (interactive "P")
  352. (calc-slow-wrapper
  353. (calc-unary-op "inv" 'calcFunc-inv arg)))
  354. ;;;###autoload
  355. (defun calc-percent ()
  356. (interactive)
  357. (calc-slow-wrapper
  358. (calc-pop-push-record-list
  359. 1 "%" (list (list 'calcFunc-percent (calc-top-n 1))))))
  360. ;;;###autoload
  361. (defun calc-over (n)
  362. (interactive "P")
  363. (if n
  364. (calc-enter (- (prefix-numeric-value n)))
  365. (calc-enter -2)))
  366. ;;;###autoload
  367. (defun calc-pop-above (n)
  368. (interactive "P")
  369. (if n
  370. (calc-pop (- (prefix-numeric-value n)))
  371. (calc-pop -2)))
  372. ;;;###autoload
  373. (defun calc-roll-down (n)
  374. (interactive "P")
  375. (calc-wrapper
  376. (let ((nn (prefix-numeric-value n)))
  377. (cond ((null n)
  378. (calc-roll-down-stack 2))
  379. ((> nn 0)
  380. (calc-roll-down-stack nn))
  381. ((= nn 0)
  382. (calc-pop-push-list (calc-stack-size)
  383. (reverse
  384. (calc-top-list (calc-stack-size)))))
  385. (t
  386. (calc-roll-down-stack (calc-stack-size) (- nn)))))))
  387. ;;;###autoload
  388. (defun calc-roll-up (n)
  389. (interactive "P")
  390. (calc-wrapper
  391. (let ((nn (prefix-numeric-value n)))
  392. (cond ((null n)
  393. (calc-roll-up-stack 3))
  394. ((> nn 0)
  395. (calc-roll-up-stack nn))
  396. ((= nn 0)
  397. (calc-pop-push-list (calc-stack-size)
  398. (reverse
  399. (calc-top-list (calc-stack-size)))))
  400. (t
  401. (calc-roll-up-stack (calc-stack-size) (- nn)))))))
  402. ;;;###autoload
  403. (defun calc-transpose-lines (&optional arg)
  404. "Transpose previous line and current line.
  405. With argument ARG, move previous line past ARG lines.
  406. With argument 0, switch line point is in with line mark is in."
  407. (interactive "p")
  408. (setq arg (or arg 1))
  409. (let (bot-line mid-line end-line
  410. old-top-list new-top-list
  411. bot-cell mid-cell
  412. prev-mid-cell post-mid-cell post-bot-cell)
  413. (calc-wrapper
  414. (when (eq major-mode 'calc-mode)
  415. (cond
  416. ;; exchange point and mark
  417. ((= 0 arg)
  418. (setq bot-line (calc-locate-cursor-element (point))
  419. mid-line (mark))
  420. (if mid-line
  421. (setq mid-line (calc-locate-cursor-element mid-line)
  422. end-line (1+ mid-line))
  423. (error "No mark set"))
  424. (if (< bot-line mid-line)
  425. (let ((temp mid-line))
  426. (setq mid-line bot-line
  427. bot-line temp))))
  428. ;; move bot-line to mid-line that is above bot-line on stack (that is
  429. ;; to say mid-line displayed below bot-line in *Calculator* buffer)
  430. ((> arg 0)
  431. (setq bot-line (1+ (calc-locate-cursor-element (point)))
  432. mid-line (- bot-line arg)
  433. end-line mid-line))
  434. ;; move bot-line to mid-line that is above bot-line on stack (that is
  435. ;; to say mid-line displayed below bot-line in *Calculator* buffer)
  436. ((< arg 0)
  437. (setq mid-line (1+ (calc-locate-cursor-element (point)))
  438. bot-line (- mid-line arg)
  439. end-line bot-line)))
  440. (calc-check-stack bot-line)
  441. (if (= 0 mid-line)
  442. (error "Can't transpose beyond top"))
  443. (setq old-top-list (nreverse (calc-top-list bot-line)))
  444. ;; example: (arg = 2)
  445. ;; old-top-list =
  446. ;; 1 <-- top of stack (bottom of *Calculator* buffer)
  447. ;; 2
  448. ;; 3 <-- mid-line = 3
  449. ;; 4 <-- point
  450. ;; 5 <-- bot-line = 5
  451. (dotimes (i mid-line)
  452. (setq mid-cell old-top-list
  453. old-top-list (cdr old-top-list))
  454. (setcdr mid-cell new-top-list)
  455. (setq new-top-list mid-cell))
  456. ;; example follow-up:
  457. ;; old-top-list =
  458. ;; 4
  459. ;; 5
  460. ;; new-top-list =
  461. ;; 3 <-- mid-cell
  462. ;; 2
  463. ;; 1
  464. (setq prev-mid-cell old-top-list)
  465. (dotimes (i (- bot-line mid-line))
  466. (setq bot-cell old-top-list
  467. old-top-list (cdr old-top-list))
  468. (setcdr bot-cell new-top-list)
  469. (setq new-top-list bot-cell))
  470. (setq post-mid-cell (cdr mid-cell)
  471. post-bot-cell (cdr bot-cell))
  472. ;; example follow-up:
  473. ;; new-top-list =
  474. ;; 5 <-- bot-cell
  475. ;; 4 <-- prev-mid-cell & post-bot-cell
  476. ;; 3 <-- mid-cell
  477. ;; 2 <-- post-mid-cell
  478. ;; 1
  479. (cond
  480. ((= 0 arg); swap bot and mid
  481. (setcdr mid-cell post-bot-cell)
  482. (setcdr bot-cell post-mid-cell)
  483. (setcdr prev-mid-cell bot-cell)
  484. ;; example follow-up:
  485. ;; 3 <-- mid-cell
  486. ;; 4 <-- post-bot-cell & prev-mid-cell
  487. ;; 5 <-- bot-cell
  488. ;; 2 <-- post-mid-cell
  489. ;; 1
  490. (setq new-top-list mid-cell))
  491. ((< 0 arg) ; move bot just after mid
  492. (setcdr mid-cell bot-cell)
  493. (setcdr bot-cell post-mid-cell)
  494. ;; example follow-up:
  495. ;; new-top-list =
  496. ;; 4 <-- post-bot-cell
  497. ;; 3 <-- mid-cell
  498. ;; 5 <-- bot-cell
  499. ;; 2 <-- post-mid-cell
  500. ;; 1
  501. (setq new-top-list post-bot-cell))
  502. ((> 0 arg) ; move mid just before bot
  503. (setcdr mid-cell bot-cell)
  504. (setcdr prev-mid-cell post-mid-cell)
  505. ;; example follow-up:
  506. ;; new-top-list =
  507. ;; 3 <-- mid-cell
  508. ;; 5 <-- bot-cell
  509. ;; 4 <-- prev-mid-cell
  510. ;; 2 <-- post-mid-cell
  511. ;; 1
  512. (setq new-top-list mid-cell)))
  513. (calc-pop-push-list bot-line new-top-list)))
  514. (calc-cursor-stack-index (1- end-line))))
  515. ;;; Other commands.
  516. ;;;###autoload
  517. (defun calc-num-prefix-name (n)
  518. (cond ((eq n '-) "- ")
  519. ((equal n '(4)) "C-u ")
  520. ((consp n) (format "%d " (car n)))
  521. ((integerp n) (format "%d " n))
  522. (t "")))
  523. ;;;###autoload
  524. (defun calc-missing-key (n)
  525. "This is a placeholder for a command which needs to be loaded from calc-ext.
  526. When this key is used, calc-ext (the Calculator extensions module) will be
  527. loaded and the keystroke automatically re-typed."
  528. (interactive "P")
  529. (require 'calc-ext)
  530. (if (keymapp (key-binding (char-to-string last-command-event)))
  531. (message "%s%c-" (calc-num-prefix-name n) last-command-event))
  532. (calc-unread-command)
  533. (setq prefix-arg n))
  534. ;;;###autoload
  535. (defun calc-shift-Y-prefix-help ()
  536. (interactive)
  537. (require 'calc-ext)
  538. (calc-do-prefix-help calc-Y-help-msgs "other" ?Y))
  539. ;;;###autoload
  540. (defun calcDigit-letter ()
  541. (interactive)
  542. (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
  543. (progn
  544. (setq last-command-event (upcase last-command-event))
  545. (calcDigit-key))
  546. (calcDigit-nondigit)))
  547. ;; A Lisp version of temp_minibuffer_message from minibuf.c.
  548. ;;;###autoload
  549. (defun calc-temp-minibuffer-message (m)
  550. (let ((savemax (point-max)))
  551. (save-excursion
  552. (goto-char (point-max))
  553. (insert m))
  554. (let ((okay nil))
  555. (unwind-protect
  556. (progn
  557. (sit-for 2)
  558. (identity 1) ; this forces a call to QUIT; in bytecode.c.
  559. (setq okay t))
  560. (progn
  561. (delete-region savemax (point-max))
  562. (or okay (abort-recursive-edit)))))))
  563. (put 'math-with-extra-prec 'lisp-indent-hook 1)
  564. ;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
  565. ;;;###autoload
  566. (defun math-concat (v1 v2)
  567. (if (stringp v1)
  568. (concat v1 v2)
  569. (require 'calc-ext)
  570. (if (and (or (math-objvecp v1) (math-known-scalarp v1))
  571. (or (math-objvecp v2) (math-known-scalarp v2)))
  572. (append (if (and (math-vectorp v1)
  573. (or (math-matrixp v1)
  574. (not (math-matrixp v2))))
  575. v1
  576. (list 'vec v1))
  577. (if (and (math-vectorp v2)
  578. (or (math-matrixp v2)
  579. (not (math-matrixp v1))))
  580. (cdr v2)
  581. (list v2)))
  582. (list '| v1 v2))))
  583. ;; True if A is zero. Works for un-normalized values. [P n] [Public]
  584. ;;;###autoload
  585. (defun math-zerop (a)
  586. (if (consp a)
  587. (cond ((memq (car a) '(bigpos bigneg))
  588. (while (eq (car (setq a (cdr a))) 0))
  589. (null a))
  590. ((memq (car a) '(frac float polar mod))
  591. (math-zerop (nth 1 a)))
  592. ((eq (car a) 'cplx)
  593. (and (math-zerop (nth 1 a)) (math-zerop (nth 2 a))))
  594. ((eq (car a) 'hms)
  595. (and (math-zerop (nth 1 a))
  596. (math-zerop (nth 2 a))
  597. (math-zerop (nth 3 a)))))
  598. (eq a 0)))
  599. ;; True if A is real and negative. [P n] [Public]
  600. ;;;###autoload
  601. (defun math-negp (a)
  602. (if (consp a)
  603. (cond ((eq (car a) 'bigpos) nil)
  604. ((eq (car a) 'bigneg) (cdr a))
  605. ((memq (car a) '(float frac))
  606. (Math-integer-negp (nth 1 a)))
  607. ((eq (car a) 'hms)
  608. (if (math-zerop (nth 1 a))
  609. (if (math-zerop (nth 2 a))
  610. (math-negp (nth 3 a))
  611. (math-negp (nth 2 a)))
  612. (math-negp (nth 1 a))))
  613. ((eq (car a) 'date)
  614. (math-negp (nth 1 a)))
  615. ((eq (car a) 'intv)
  616. (or (math-negp (nth 3 a))
  617. (and (math-zerop (nth 3 a))
  618. (memq (nth 1 a) '(0 2)))))
  619. ((equal a '(neg (var inf var-inf))) t))
  620. (< a 0)))
  621. ;; True if A is a negative number or an expression the starts with '-'.
  622. ;;;###autoload
  623. (defun math-looks-negp (a) ; [P x] [Public]
  624. (or (Math-negp a)
  625. (eq (car-safe a) 'neg)
  626. (and (memq (car-safe a) '(* /))
  627. (or (math-looks-negp (nth 1 a))
  628. (math-looks-negp (nth 2 a))))
  629. (and (eq (car-safe a) '-)
  630. (math-looks-negp (nth 1 a)))))
  631. ;; True if A is real and positive. [P n] [Public]
  632. ;;;###autoload
  633. (defun math-posp (a)
  634. (if (consp a)
  635. (cond ((eq (car a) 'bigpos) (cdr a))
  636. ((eq (car a) 'bigneg) nil)
  637. ((memq (car a) '(float frac))
  638. (Math-integer-posp (nth 1 a)))
  639. ((eq (car a) 'hms)
  640. (if (math-zerop (nth 1 a))
  641. (if (math-zerop (nth 2 a))
  642. (math-posp (nth 3 a))
  643. (math-posp (nth 2 a)))
  644. (math-posp (nth 1 a))))
  645. ((eq (car a) 'date)
  646. (math-posp (nth 1 a)))
  647. ((eq (car a) 'mod)
  648. (not (math-zerop (nth 1 a))))
  649. ((eq (car a) 'intv)
  650. (or (math-posp (nth 2 a))
  651. (and (math-zerop (nth 2 a))
  652. (memq (nth 1 a) '(0 1)))))
  653. ((equal a '(var inf var-inf)) t))
  654. (> a 0)))
  655. ;;;###autoload
  656. (defalias 'math-fixnump 'integerp)
  657. ;;;###autoload
  658. (defalias 'math-fixnatnump 'natnump)
  659. ;; True if A is an even integer. [P R R] [Public]
  660. ;;;###autoload
  661. (defun math-evenp (a)
  662. (if (consp a)
  663. (and (memq (car a) '(bigpos bigneg))
  664. (= (% (nth 1 a) 2) 0))
  665. (= (% a 2) 0)))
  666. ;; Compute A / 2, for small or big integer A. [I i]
  667. ;; If A is negative, type of truncation is undefined.
  668. ;;;###autoload
  669. (defun math-div2 (a)
  670. (if (consp a)
  671. (if (cdr a)
  672. (math-normalize (cons (car a) (math-div2-bignum (cdr a))))
  673. 0)
  674. (/ a 2)))
  675. ;;;###autoload
  676. (defun math-div2-bignum (a) ; [l l]
  677. (if (cdr a)
  678. (cons (+ (/ (car a) 2) (* (% (nth 1 a) 2) (/ math-bignum-digit-size 2)))
  679. (math-div2-bignum (cdr a)))
  680. (list (/ (car a) 2))))
  681. ;; Reject an argument to a calculator function. [Public]
  682. ;;;###autoload
  683. (defun math-reject-arg (&optional a p option)
  684. (if option
  685. (calc-record-why option p a)
  686. (if p
  687. (calc-record-why p a)))
  688. (signal 'wrong-type-argument (and a (if p (list p a) (list a)))))
  689. ;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
  690. ;; The variable math-trunc-prec is local to math-trunc, but used by
  691. ;; math-trunc-fancy in calc-arith.el, which is called by math-trunc.
  692. ;;;###autoload
  693. (defun math-trunc (a &optional math-trunc-prec)
  694. (cond (math-trunc-prec
  695. (require 'calc-ext)
  696. (math-trunc-special a math-trunc-prec))
  697. ((Math-integerp a) a)
  698. ((Math-looks-negp a)
  699. (math-neg (math-trunc (math-neg a))))
  700. ((eq (car a) 'float)
  701. (math-scale-int (nth 1 a) (nth 2 a)))
  702. (t (require 'calc-ext)
  703. (math-trunc-fancy a))))
  704. ;;;###autoload
  705. (defalias 'calcFunc-trunc 'math-trunc)
  706. ;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
  707. ;; The variable math-floor-prec is local to math-floor, but used by
  708. ;; math-floor-fancy in calc-arith.el, which is called by math-floor.
  709. ;;;###autoload
  710. (defun math-floor (a &optional math-floor-prec) ; [Public]
  711. (cond (math-floor-prec
  712. (require 'calc-ext)
  713. (math-floor-special a math-floor-prec))
  714. ((Math-integerp a) a)
  715. ((Math-messy-integerp a) (math-trunc a))
  716. ((Math-realp a)
  717. (if (Math-negp a)
  718. (math-add (math-trunc a) -1)
  719. (math-trunc a)))
  720. (t (require 'calc-ext)
  721. (math-floor-fancy a))))
  722. ;;;###autoload
  723. (defalias 'calcFunc-floor 'math-floor)
  724. ;;;###autoload
  725. (defun math-imod (a b) ; [I I I] [Public]
  726. (if (and (not (consp a)) (not (consp b)))
  727. (if (= b 0)
  728. (math-reject-arg a "*Division by zero")
  729. (% a b))
  730. (cdr (math-idivmod a b))))
  731. ;;;###autoload
  732. (defun calcFunc-inv (m)
  733. (if (Math-vectorp m)
  734. (progn
  735. (require 'calc-ext)
  736. (if (math-square-matrixp m)
  737. (or (math-with-extra-prec 2 (math-matrix-inv-raw m))
  738. (math-reject-arg m "*Singular matrix"))
  739. (math-reject-arg m 'square-matrixp)))
  740. (if (and
  741. (require 'calc-arith)
  742. (math-known-matrixp m))
  743. (math-pow m -1)
  744. (math-div 1 m))))
  745. ;;;###autoload
  746. (defun math-do-working (msg arg)
  747. (or executing-kbd-macro
  748. (progn
  749. (calc-set-command-flag 'clear-message)
  750. (if math-working-step
  751. (if math-working-step-2
  752. (setq msg (format "[%d/%d] %s"
  753. math-working-step math-working-step-2 msg))
  754. (setq msg (format "[%d] %s" math-working-step msg))))
  755. (message "Working... %s = %s" msg
  756. (math-showing-full-precision (math-format-number arg))))))
  757. ;; Compute A modulo B, defined in terms of truncation toward minus infinity.
  758. ;;;###autoload
  759. (defun math-mod (a b) ; [R R R] [Public]
  760. (cond ((and (Math-zerop a) (not (eq (car-safe a) 'mod))) a)
  761. ((Math-zerop b)
  762. (math-reject-arg a "*Division by zero"))
  763. ((and (Math-natnump a) (Math-natnump b))
  764. (math-imod a b))
  765. ((and (Math-anglep a) (Math-anglep b))
  766. (math-sub a (math-mul (math-floor (math-div a b)) b)))
  767. (t (require 'calc-ext)
  768. (math-mod-fancy a b))))
  769. ;;; General exponentiation.
  770. ;;;###autoload
  771. (defun math-pow (a b) ; [O O N] [Public]
  772. (cond ((equal b '(var nan var-nan))
  773. b)
  774. ((Math-zerop a)
  775. (if (and (Math-scalarp b) (Math-posp b))
  776. (if (math-floatp b) (math-float a) a)
  777. (require 'calc-ext)
  778. (math-pow-of-zero a b)))
  779. ((or (eq a 1) (eq b 1)) a)
  780. ((or (equal a '(float 1 0)) (equal b '(float 1 0))) a)
  781. ((Math-zerop b)
  782. (if (Math-scalarp a)
  783. (if (or (math-floatp a) (math-floatp b))
  784. '(float 1 0) 1)
  785. (require 'calc-ext)
  786. (math-pow-zero a b)))
  787. ((and (Math-integerp b) (or (Math-numberp a) (Math-vectorp a)))
  788. (if (and (equal a '(float 1 1)) (integerp b))
  789. (math-make-float 1 b)
  790. (math-with-extra-prec 2
  791. (math-ipow a b))))
  792. (t
  793. (require 'calc-ext)
  794. (math-pow-fancy a b))))
  795. ;;;###autoload
  796. (defun math-ipow (a n) ; [O O I] [Public]
  797. (cond ((Math-integer-negp n)
  798. (math-ipow (math-div 1 a) (Math-integer-neg n)))
  799. ((not (consp n))
  800. (if (and (Math-ratp a) (> n 20))
  801. (math-iipow-show a n)
  802. (math-iipow a n)))
  803. ((math-evenp n)
  804. (math-ipow (math-mul a a) (math-div2 n)))
  805. (t
  806. (math-mul a (math-ipow (math-mul a a)
  807. (math-div2 (math-add n -1)))))))
  808. (defun math-iipow (a n) ; [O O S]
  809. (cond ((= n 0) 1)
  810. ((= n 1) a)
  811. ((= (% n 2) 0) (math-iipow (math-mul a a) (/ n 2)))
  812. (t (math-mul a (math-iipow (math-mul a a) (/ n 2))))))
  813. (defun math-iipow-show (a n) ; [O O S]
  814. (math-working "pow" a)
  815. (let ((val (cond
  816. ((= n 0) 1)
  817. ((= n 1) a)
  818. ((= (% n 2) 0) (math-iipow-show (math-mul a a) (/ n 2)))
  819. (t (math-mul a (math-iipow-show (math-mul a a) (/ n 2)))))))
  820. (math-working "pow" val)
  821. val))
  822. ;;;###autoload
  823. (defun math-read-radix-digit (dig) ; [D S; Z S]
  824. (if (> dig ?9)
  825. (if (< dig ?A)
  826. nil
  827. (- dig 55))
  828. (if (>= dig ?0)
  829. (- dig ?0)
  830. nil)))
  831. ;;; Bug reporting
  832. ;;;###autoload
  833. (defun report-calc-bug ()
  834. "Report a bug in Calc, the GNU Emacs calculator.
  835. Prompts for bug subject. Leaves you in a mail buffer."
  836. (interactive)
  837. (let ((reporter-prompt-for-summary-p t))
  838. (reporter-submit-bug-report calc-bug-address "Calc"
  839. nil nil nil
  840. "Please describe exactly what actions triggered the bug and the
  841. precise symptoms of the bug. If possible, include a backtrace by
  842. doing ‘\\[toggle-debug-on-error]’, then reproducing the bug.
  843. " )))
  844. ;;;###autoload
  845. (defalias 'calc-report-bug 'report-calc-bug)
  846. (provide 'calc-misc)
  847. ;; Local variables:
  848. ;; generated-autoload-file: "calc-loaddefs.el"
  849. ;; End:
  850. ;;; calc-misc.el ends here