1 ;;; calc-misc.el --- miscellaneous functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc.el.
35 (defun calc-dispatch-help (arg)
36 "M-# is a prefix key; follow it with one of these letters:
38 For turning Calc on and off:
39 C calc. Start the Calculator in a window at the bottom of the screen.
40 O calc-other-window. Start the Calculator but don't select its window.
41 B calc-big-or-small. Control whether to use the full Emacs screen for Calc.
42 Q quick-calc. Use the Calculator in the minibuffer.
43 K calc-keypad. Start the Calculator in keypad mode (X window system only).
44 E calc-embedded. Use the Calculator on a formula in this editing buffer.
45 J calc-embedded-select. Like E, but select appropriate half of => or :=.
46 W calc-embedded-word. Like E, but activate a single word, i.e., a number.
47 Z calc-user-invocation. Invoke Calc in the way you defined with `Z I' cmd.
48 X calc-quit. Turn Calc off.
50 For moving data into and out of Calc:
51 G calc-grab-region. Grab the region defined by mark and point into Calc.
52 R calc-grab-rectangle. Grab the rectangle defined by mark, point into Calc.
53 : calc-grab-sum-down. Grab a rectangle and sum the columns.
54 _ calc-grab-sum-across. Grab a rectangle and sum the rows.
55 Y calc-copy-to-buffer. Copy a value from the stack into the editing buffer.
57 For use with Embedded mode:
58 A calc-embedded-activate. Find and activate all :='s and =>'s in buffer.
59 D calc-embedded-duplicate. Make a copy of this formula and select it.
60 F calc-embedded-new-formula. Insert a new formula at current point.
61 N calc-embedded-next. Advance cursor to next known formula in buffer.
62 P calc-embedded-previous. Advance cursor to previous known formula.
63 U calc-embedded-update-formula. Re-evaluate formula at point.
64 ` calc-embedded-edit. Use calc-edit to edit formula at point.
67 I calc-info. Read the Calculator manual in the Emacs Info system.
68 T calc-tutorial. Run the Calculator Tutorial using the Emacs Info system.
69 S calc-summary. Read the Summary from the Calculator manual in Info.
72 L calc-load-everything. Load all parts of the Calculator into memory.
73 M read-kbd-macro. Read a region of keystroke names as a keyboard macro.
74 0 (zero) calc-reset. Reset Calc stack and modes to default state.
76 Press twice (`M-# M-#' or `M-# #') to turn Calc on or off using the same
77 Calc user interface as before (either M-# C or M-# K; initially M-# C)."
80 (if calc-dispatch-help
82 (save-window-excursion
83 (describe-function 'calc-dispatch-help)
84 (let ((win (get-buffer-window "*Help*")))
89 (message "Calc options: Calc, Keypad, ... %s"
90 "press SPC, DEL to scroll, C-g to cancel")
91 (memq (car (setq key (calc-read-key t)))
92 '(? ?\C-h ?\C-? ?\C-v ?\M-v)))
94 (if (memq (car key) '(? ?\C-v))
98 (calc-unread-command (cdr key))))))
99 (calc-do-dispatch nil))
100 (let ((calc-dispatch-help t))
101 (calc-do-dispatch arg))))
104 (defun calc-big-or-small (arg)
105 "Toggle Calc between full-screen and regular mode."
107 (let ((cwin (get-buffer-window "*Calculator*"))
108 (twin (get-buffer-window "*Calc Trail*"))
109 (kwin (get-buffer-window "*Calc Keypad*")))
113 (and twin (eq (window-width twin) (frame-width)))
114 (eq (window-height cwin) (1- (frame-height))))))
115 (setq calc-full-mode (if arg
116 (> (prefix-numeric-value arg) 0)
117 (not calc-full-mode)))
121 (calc-do-keypad calc-full-mode nil))
125 (calc nil calc-full-mode nil))))
126 (message (if calc-full-mode
127 "Now using full screen for Calc"
128 "Now using partial screen for Calc"))))
130 (defun calc-other-window (&optional interactive)
131 "Invoke the Calculator in another window."
133 (if (memq major-mode '(calc-mode calc-trail-mode))
136 (if (memq major-mode '(calc-mode calc-trail-mode))
138 (if (get-buffer-window "*Calculator*")
140 (let ((win (selected-window)))
141 (calc nil win interactive)))))
143 (defun another-calc ()
144 "Create another, independent Calculator buffer."
146 (if (eq major-mode 'calc-mode)
149 (set-default v (symbol-value v)))) calc-local-var-list))
150 (set-buffer (generate-new-buffer "*Calculator*"))
151 (pop-to-buffer (current-buffer))
155 "Run the Emacs Info system on the Calculator documentation."
157 (select-window (get-largest-window))
160 (defun calc-info-goto-node (node)
161 "Go to a node in the Calculator info documentation."
163 (select-window (get-largest-window))
164 (info (concat "(Calc)" node)))
166 (defun calc-tutorial ()
167 "Run the Emacs Info system on the Calculator Tutorial."
169 (if (get-buffer-window "*Calculator*")
171 (calc-info-goto-node "Interactive Tutorial")
173 (message "Welcome to the Calc Tutorial!"))
175 (defun calc-info-summary ()
176 "Run the Emacs Info system on the Calculator Summary."
178 (calc-info-goto-node "Summary"))
183 '("Press `h' for complete help; press `?' repeatedly for a summary"
184 "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit"
185 "Letter keys: SHIFT + Undo, reDo; Keep-args; Inverse, Hyperbolic"
186 "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB"
187 "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi"
188 "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro"
189 "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)"
190 "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)"
191 "Other keys: ' (alg-entry), = (eval), ` (edit); M-RET (last-args)"
192 "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)"
193 "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)"
194 "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)"
195 "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)"
196 "Prefix keys: Algebra, Binary/business, Convert, Display"
197 "Prefix keys: Functions, Graphics, Help, J (select)"
198 "Prefix keys: Kombinatorics/statistics, Modes, Store/recall"
199 "Prefix keys: Trail/time, Units/statistics, Vector/matrix"
200 "Prefix keys: Z (user), SHIFT + Z (define)"
201 "Prefix keys: prefix + ? gives further help for that prefix")
203 " Calc %s by Dave Gillespie, daveg@synaptics.com"
205 (if calc-full-help-flag
207 (if (or calc-inverse-flag calc-hyperbolic-flag)
208 (if calc-inverse-flag
209 (if calc-hyperbolic-flag
210 (calc-inv-hyp-prefix-help)
211 (calc-inverse-prefix-help))
212 (calc-hyperbolic-prefix-help))
213 (setq calc-help-phase
214 (if (eq this-command last-command)
215 (% (1+ calc-help-phase) (1+ (length msgs)))
217 (let ((msg (nth calc-help-phase msgs)))
218 (message "%s" (if msg
220 (make-string (- (apply 'max
230 ;;;; Stack and buffer management.
232 ;; The variable calc-last-why-command is set in calc-do-handly-whys
233 ;; and used in calc-why (in calc-stuff.el).
234 (defvar calc-last-why-command)
236 (defun calc-do-handle-whys ()
237 (setq calc-why (sort calc-next-why
240 (and (eq (car x) '*) (not (eq (car y) '*))))))
242 (if (and calc-why (or (eq calc-auto-why t)
243 (and (eq (car (car calc-why)) '*)
247 (calc-explain-why (car calc-why)
248 (if (eq calc-auto-why t)
251 (eq (car (nth 1 calc-why)) '*))))
252 (setq calc-last-why-command this-command)
253 (calc-clear-command-flag 'clear-message))))
255 (defun calc-record-why (&rest stuff)
256 (if (eq (car stuff) 'quiet)
257 (setq stuff (cdr stuff))
258 (if (and (symbolp (car stuff))
260 (or (Math-objectp (nth 1 stuff))
261 (and (Math-vectorp (nth 1 stuff))
262 (math-constp (nth 1 stuff)))
263 (math-infinitep (nth 1 stuff))))
264 (setq stuff (cons '* stuff))
265 (if (and (stringp (car stuff))
266 (string-match "\\`\\*" (car stuff)))
267 (setq stuff (cons '* (cons (substring (car stuff) 1)
269 (setq calc-next-why (cons stuff calc-next-why))
272 ;;; True if A is a constant or vector of constants. [P x] [Public]
273 (defun math-constp (a)
275 (and (memq (car a) '(sdev intv mod vec))
277 (while (and (setq a (cdr a))
278 (or (Math-scalarp (car a)) ; optimization
279 (math-constp (car a)))))
283 (defun calc-roll-down-stack (n &optional m)
285 (calc-roll-up-stack (- n) m)
286 (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
290 (if (and calc-any-selections
291 (not calc-use-selections))
292 (calc-roll-down-with-selections n m)
293 (calc-pop-push-list n
294 (append (calc-top-list m 1)
295 (calc-top-list (- n m) (1+ m))))))))
297 (defun calc-roll-up-stack (n &optional m)
299 (calc-roll-down-stack (- n) m)
300 (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
304 (if (and calc-any-selections
305 (not calc-use-selections))
306 (calc-roll-up-with-selections n m)
307 (calc-pop-push-list n
308 (append (calc-top-list (- n m) 1)
309 (calc-top-list m (- n m -1))))))))
312 (defun calc-do-refresh ()
313 (if calc-hyperbolic-flag
315 (setq calc-display-dirty t)
321 (defun calc-record-list (vals &optional prefix)
323 (or (eq (car vals) 'top-of-stack)
325 (calc-record (car vals) prefix)
326 (setq prefix "...")))
327 (setq vals (cdr vals))))
330 (defun calc-last-args-stub (arg)
333 (calc-last-args arg))
336 (defun calc-power (arg)
339 (if (and (featurep 'calc-ext)
341 (calc-binary-op "root" 'calcFunc-nroot arg nil nil)
342 (calc-binary-op "^" 'calcFunc-pow arg nil nil '^))))
344 (defun calc-mod (arg)
347 (calc-binary-op "%" 'calcFunc-mod arg nil nil '%)))
349 (defun calc-inv (arg)
352 (calc-unary-op "inv" 'calcFunc-inv arg)))
354 (defun calc-percent ()
357 (calc-pop-push-record-list
358 1 "%" (list (list 'calcFunc-percent (calc-top-n 1))))))
364 (calc-enter (- (prefix-numeric-value n)))
368 (defun calc-pop-above (n)
371 (calc-pop (- (prefix-numeric-value n)))
374 (defun calc-roll-down (n)
377 (let ((nn (prefix-numeric-value n)))
379 (calc-roll-down-stack 2))
381 (calc-roll-down-stack nn))
383 (calc-pop-push-list (calc-stack-size)
385 (calc-top-list (calc-stack-size)))))
387 (calc-roll-down-stack (calc-stack-size) (- nn)))))))
389 (defun calc-roll-up (n)
392 (let ((nn (prefix-numeric-value n)))
394 (calc-roll-up-stack 3))
396 (calc-roll-up-stack nn))
398 (calc-pop-push-list (calc-stack-size)
400 (calc-top-list (calc-stack-size)))))
402 (calc-roll-up-stack (calc-stack-size) (- nn)))))))
409 (defun calc-num-prefix-name (n)
410 (cond ((eq n '-) "- ")
411 ((equal n '(4)) "C-u ")
412 ((consp n) (format "%d " (car n)))
413 ((integerp n) (format "%d " n))
416 (defun calc-missing-key (n)
417 "This is a placeholder for a command which needs to be loaded from calc-ext.
418 When this key is used, calc-ext (the Calculator extensions module) will be
419 loaded and the keystroke automatically re-typed."
422 (if (keymapp (key-binding (char-to-string last-command-char)))
423 (message "%s%c-" (calc-num-prefix-name n) last-command-char))
424 (calc-unread-command)
427 (defun calc-shift-Y-prefix-help ()
430 (calc-do-prefix-help calc-Y-help-msgs "other" ?Y))
435 (defun calcDigit-letter ()
437 (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
439 (setq last-command-char (upcase last-command-char))
441 (calcDigit-nondigit)))
444 ;; A Lisp version of temp_minibuffer_message from minibuf.c.
445 (defun calc-temp-minibuffer-message (m)
446 (let ((savemax (point-max)))
448 (goto-char (point-max))
454 (identity 1) ; this forces a call to QUIT; in bytecode.c.
457 (delete-region savemax (point-max))
458 (or okay (abort-recursive-edit)))))))
461 (put 'math-with-extra-prec 'lisp-indent-hook 1)
464 ;;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
465 (defun math-concat (v1 v2)
469 (if (and (or (math-objvecp v1) (math-known-scalarp v1))
470 (or (math-objvecp v2) (math-known-scalarp v2)))
471 (append (if (and (math-vectorp v1)
472 (or (math-matrixp v1)
473 (not (math-matrixp v2))))
476 (if (and (math-vectorp v2)
477 (or (math-matrixp v2)
478 (not (math-matrixp v1))))
484 ;;; True if A is zero. Works for un-normalized values. [P n] [Public]
485 (defun math-zerop (a)
487 (cond ((memq (car a) '(bigpos bigneg))
488 (while (eq (car (setq a (cdr a))) 0))
490 ((memq (car a) '(frac float polar mod))
491 (math-zerop (nth 1 a)))
493 (and (math-zerop (nth 1 a)) (math-zerop (nth 2 a))))
495 (and (math-zerop (nth 1 a))
496 (math-zerop (nth 2 a))
497 (math-zerop (nth 3 a)))))
501 ;;; True if A is real and negative. [P n] [Public]
505 (cond ((eq (car a) 'bigpos) nil)
506 ((eq (car a) 'bigneg) (cdr a))
507 ((memq (car a) '(float frac))
508 (Math-integer-negp (nth 1 a)))
510 (if (math-zerop (nth 1 a))
511 (if (math-zerop (nth 2 a))
512 (math-negp (nth 3 a))
513 (math-negp (nth 2 a)))
514 (math-negp (nth 1 a))))
516 (math-negp (nth 1 a)))
518 (or (math-negp (nth 3 a))
519 (and (math-zerop (nth 3 a))
520 (memq (nth 1 a) '(0 2)))))
521 ((equal a '(neg (var inf var-inf))) t))
524 ;;; True if A is a negative number or an expression the starts with '-'.
525 (defun math-looks-negp (a) ; [P x] [Public]
527 (eq (car-safe a) 'neg)
528 (and (memq (car-safe a) '(* /))
529 (or (math-looks-negp (nth 1 a))
530 (math-looks-negp (nth 2 a))))
531 (and (eq (car-safe a) '-)
532 (math-looks-negp (nth 1 a)))))
535 ;;; True if A is real and positive. [P n] [Public]
538 (cond ((eq (car a) 'bigpos) (cdr a))
539 ((eq (car a) 'bigneg) nil)
540 ((memq (car a) '(float frac))
541 (Math-integer-posp (nth 1 a)))
543 (if (math-zerop (nth 1 a))
544 (if (math-zerop (nth 2 a))
545 (math-posp (nth 3 a))
546 (math-posp (nth 2 a)))
547 (math-posp (nth 1 a))))
549 (math-posp (nth 1 a)))
551 (not (math-zerop (nth 1 a))))
553 (or (math-posp (nth 2 a))
554 (and (math-zerop (nth 2 a))
555 (memq (nth 1 a) '(0 1)))))
556 ((equal a '(var inf var-inf)) t))
559 (defalias 'math-fixnump 'integerp)
560 (defalias 'math-fixnatnump 'natnump)
563 ;;; True if A is an even integer. [P R R] [Public]
564 (defun math-evenp (a)
566 (and (memq (car a) '(bigpos bigneg))
567 (= (% (nth 1 a) 2) 0))
570 ;;; Compute A / 2, for small or big integer A. [I i]
571 ;;; If A is negative, type of truncation is undefined.
575 (math-normalize (cons (car a) (math-div2-bignum (cdr a))))
579 (defun math-div2-bignum (a) ; [l l]
581 (cons (+ (/ (car a) 2) (* (% (nth 1 a) 2) 500))
582 (math-div2-bignum (cdr a)))
583 (list (/ (car a) 2))))
586 ;;; Reject an argument to a calculator function. [Public]
587 (defun math-reject-arg (&optional a p option)
589 (calc-record-why option p a)
591 (calc-record-why p a)))
592 (signal 'wrong-type-argument (and a (if p (list p a) (list a)))))
595 ;;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
597 ;; The variable math-trunc-prec is local to math-trunc, but used by
598 ;; math-trunc-fancy in calc-arith.el, which is called by math-trunc.
600 (defun math-trunc (a &optional math-trunc-prec)
601 (cond (math-trunc-prec
603 (math-trunc-special a math-trunc-prec))
604 ((Math-integerp a) a)
606 (math-neg (math-trunc (math-neg a))))
608 (math-scale-int (nth 1 a) (nth 2 a)))
609 (t (require 'calc-ext)
610 (math-trunc-fancy a))))
611 (defalias 'calcFunc-trunc 'math-trunc)
613 ;;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
615 ;; The variable math-floor-prec is local to math-floor, but used by
616 ;; math-floor-fancy in calc-arith.el, which is called by math-floor.
618 (defun math-floor (a &optional math-floor-prec) ; [Public]
619 (cond (math-floor-prec
621 (math-floor-special a math-floor-prec))
622 ((Math-integerp a) a)
623 ((Math-messy-integerp a) (math-trunc a))
626 (math-add (math-trunc a) -1)
628 (t (require 'calc-ext)
629 (math-floor-fancy a))))
630 (defalias 'calcFunc-floor 'math-floor)
633 (defun math-imod (a b) ; [I I I] [Public]
634 (if (and (not (consp a)) (not (consp b)))
636 (math-reject-arg a "*Division by zero")
638 (cdr (math-idivmod a b))))
641 (defun calcFunc-inv (m)
645 (if (math-square-matrixp m)
646 (or (math-with-extra-prec 2 (math-matrix-inv-raw m))
647 (math-reject-arg m "*Singular matrix"))
648 (math-reject-arg m 'square-matrixp)))
650 (require 'calc-arith)
651 (math-known-matrixp m))
655 (defun math-do-working (msg arg)
656 (or executing-kbd-macro
658 (calc-set-command-flag 'clear-message)
659 (if math-working-step
660 (if math-working-step-2
661 (setq msg (format "[%d/%d] %s"
662 math-working-step math-working-step-2 msg))
663 (setq msg (format "[%d] %s" math-working-step msg))))
664 (message "Working... %s = %s" msg
665 (math-showing-full-precision (math-format-number arg))))))
668 ;;; Compute A modulo B, defined in terms of truncation toward minus infinity.
669 (defun math-mod (a b) ; [R R R] [Public]
670 (cond ((and (Math-zerop a) (not (eq (car-safe a) 'mod))) a)
672 (math-reject-arg a "*Division by zero"))
673 ((and (Math-natnump a) (Math-natnump b))
675 ((and (Math-anglep a) (Math-anglep b))
676 (math-sub a (math-mul (math-floor (math-div a b)) b)))
677 (t (require 'calc-ext)
678 (math-mod-fancy a b))))
682 ;;; General exponentiation.
684 (defun math-pow (a b) ; [O O N] [Public]
685 (cond ((equal b '(var nan var-nan))
688 (if (and (Math-scalarp b) (Math-posp b))
689 (if (math-floatp b) (math-float a) a)
691 (math-pow-of-zero a b)))
692 ((or (eq a 1) (eq b 1)) a)
693 ((or (equal a '(float 1 0)) (equal b '(float 1 0))) a)
696 (if (or (math-floatp a) (math-floatp b))
699 (math-pow-zero a b)))
700 ((and (Math-integerp b) (or (Math-numberp a) (Math-vectorp a)))
701 (if (and (equal a '(float 1 1)) (integerp b))
702 (math-make-float 1 b)
703 (math-with-extra-prec 2
707 (math-pow-fancy a b))))
709 (defun math-ipow (a n) ; [O O I] [Public]
710 (cond ((Math-integer-negp n)
711 (math-ipow (math-div 1 a) (Math-integer-neg n)))
713 (if (and (Math-ratp a) (> n 20))
714 (math-iipow-show a n)
717 (math-ipow (math-mul a a) (math-div2 n)))
719 (math-mul a (math-ipow (math-mul a a)
720 (math-div2 (math-add n -1)))))))
722 (defun math-iipow (a n) ; [O O S]
725 ((= (% n 2) 0) (math-iipow (math-mul a a) (/ n 2)))
726 (t (math-mul a (math-iipow (math-mul a a) (/ n 2))))))
728 (defun math-iipow-show (a n) ; [O O S]
729 (math-working "pow" a)
733 ((= (% n 2) 0) (math-iipow-show (math-mul a a) (/ n 2)))
734 (t (math-mul a (math-iipow-show (math-mul a a) (/ n 2)))))))
735 (math-working "pow" val)
739 (defun math-read-radix-digit (dig) ; [D S; Z S]
751 (defun report-calc-bug ()
752 "Report a bug in Calc, the GNU Emacs calculator.
753 Prompts for bug subject. Leaves you in a mail buffer."
755 (let ((reporter-prompt-for-summary-p t))
756 (reporter-submit-bug-report calc-bug-address "Calc" '(calc-version)
758 "Please describe exactly what actions triggered the bug and the
759 precise symptoms of the bug. If possible, include a backtrace by
760 doing 'M-x toggle-debug-on-error', then reproducing the bug.
762 (defalias 'calc-report-bug 'report-calc-bug)
766 ;;; arch-tag: 7984d9d0-62e5-41dc-afb8-e904b975f250
767 ;;; calc-misc.el ends here