1 ;;; calc-misc.el --- miscellaneous functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004
4 ;; 2005, 2006 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 "C-x* is a prefix key sequence; 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 (`C-x * *') to turn Calc on or off using the same
77 Calc user interface as before (either C-x * C or C-x * K; initially C-x * C).
81 (if calc-dispatch-help
83 (save-window-excursion
84 (describe-function 'calc-dispatch-help
)
85 (let ((win (get-buffer-window "*Help*")))
90 (message "Calc options: Calc, Keypad, ... %s"
91 "press SPC, DEL to scroll, C-g to cancel")
92 (memq (car (setq key
(calc-read-key t
)))
93 '(? ?\C-h ?\C-? ?\C-v ?\M-v
)))
95 (if (memq (car key
) '(? ?\C-v
))
99 (calc-unread-command (cdr key
))))))
100 (calc-do-dispatch nil
))
101 (let ((calc-dispatch-help t
))
102 (calc-do-dispatch arg
))))
105 (defun calc-big-or-small (arg)
106 "Toggle Calc between full-screen and regular mode."
108 (let ((cwin (get-buffer-window "*Calculator*"))
109 (twin (get-buffer-window "*Calc Trail*"))
110 (kwin (get-buffer-window "*Calc Keypad*")))
114 (and twin
(eq (window-width twin
) (frame-width)))
115 (eq (window-height cwin
) (1- (frame-height))))))
116 (setq calc-full-mode
(if arg
117 (> (prefix-numeric-value arg
) 0)
118 (not calc-full-mode
)))
122 (calc-do-keypad calc-full-mode nil
))
126 (calc nil calc-full-mode nil
))))
127 (message (if calc-full-mode
128 "Now using full screen for Calc"
129 "Now using partial screen for Calc"))))
131 (defun calc-other-window (&optional interactive
)
132 "Invoke the Calculator in another window."
134 (if (memq major-mode
'(calc-mode calc-trail-mode
))
137 (if (memq major-mode
'(calc-mode calc-trail-mode
))
139 (if (get-buffer-window "*Calculator*")
141 (let ((win (selected-window)))
142 (calc nil win interactive
)))))
144 (defun another-calc ()
145 "Create another, independent Calculator buffer."
147 (if (eq major-mode
'calc-mode
)
150 (set-default v
(symbol-value v
)))) calc-local-var-list
))
151 (set-buffer (generate-new-buffer "*Calculator*"))
152 (pop-to-buffer (current-buffer))
156 "Run the Emacs Info system on the Calculator documentation."
158 (select-window (get-largest-window))
161 (defun calc-info-goto-node (node)
162 "Go to a node in the Calculator info documentation."
164 (select-window (get-largest-window))
165 (info (concat "(Calc)" node
)))
167 (defun calc-tutorial ()
168 "Run the Emacs Info system on the Calculator Tutorial."
170 (if (get-buffer-window "*Calculator*")
172 (calc-info-goto-node "Interactive Tutorial")
174 (message "Welcome to the Calc Tutorial!"))
176 (defun calc-info-summary ()
177 "Run the Emacs Info system on the Calculator Summary."
179 (calc-info-goto-node "Summary"))
184 '("Press `h' for complete help; press `?' repeatedly for a summary"
185 "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit"
186 "Letter keys: SHIFT + Undo, reDo; Keep-args; Inverse, Hyperbolic"
187 "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB"
188 "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi"
189 "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro"
190 "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)"
191 "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)"
192 "Other keys: ' (alg-entry), = (eval), ` (edit); M-RET (last-args)"
193 "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)"
194 "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)"
195 "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)"
196 "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)"
197 "Prefix keys: Algebra, Binary/business, Convert, Display"
198 "Prefix keys: Functions, Graphics, Help, J (select)"
199 "Prefix keys: Kombinatorics/statistics, Modes, Store/recall"
200 "Prefix keys: Trail/time, Units/statistics, Vector/matrix"
201 "Prefix keys: Z (user), SHIFT + Z (define)"
202 "Prefix keys: prefix + ? gives further help for that prefix")
204 " Calc %s by Dave Gillespie, daveg@synaptics.com"
206 (if calc-full-help-flag
208 (if (or calc-inverse-flag calc-hyperbolic-flag
)
209 (if calc-inverse-flag
210 (if calc-hyperbolic-flag
211 (calc-inv-hyp-prefix-help)
212 (calc-inverse-prefix-help))
213 (calc-hyperbolic-prefix-help))
214 (setq calc-help-phase
215 (if (eq this-command last-command
)
216 (%
(1+ calc-help-phase
) (1+ (length msgs
)))
218 (let ((msg (nth calc-help-phase msgs
)))
219 (message "%s" (if msg
221 (make-string (- (apply 'max
231 ;;;; Stack and buffer management.
233 ;; The variable calc-last-why-command is set in calc-do-handly-whys
234 ;; and used in calc-why (in calc-stuff.el).
235 (defvar calc-last-why-command
)
237 (defun calc-do-handle-whys ()
238 (setq calc-why
(sort calc-next-why
241 (and (eq (car x
) '*) (not (eq (car y
) '*))))))
243 (if (and calc-why
(or (eq calc-auto-why t
)
244 (and (eq (car (car calc-why
)) '*)
248 (calc-explain-why (car calc-why
)
249 (if (eq calc-auto-why t
)
252 (eq (car (nth 1 calc-why
)) '*))))
253 (setq calc-last-why-command this-command
)
254 (calc-clear-command-flag 'clear-message
))))
256 (defun calc-record-why (&rest stuff
)
257 (if (eq (car stuff
) 'quiet
)
258 (setq stuff
(cdr stuff
))
259 (if (and (symbolp (car stuff
))
261 (or (Math-objectp (nth 1 stuff
))
262 (and (Math-vectorp (nth 1 stuff
))
263 (math-constp (nth 1 stuff
)))
264 (math-infinitep (nth 1 stuff
))))
265 (setq stuff
(cons '* stuff
))
266 (if (and (stringp (car stuff
))
267 (string-match "\\`\\*" (car stuff
)))
268 (setq stuff
(cons '* (cons (substring (car stuff
) 1)
270 (setq calc-next-why
(cons stuff calc-next-why
))
273 ;;; True if A is a constant or vector of constants. [P x] [Public]
274 (defun math-constp (a)
276 (and (memq (car a
) '(sdev intv mod vec
))
278 (while (and (setq a
(cdr a
))
279 (or (Math-scalarp (car a
)) ; optimization
280 (math-constp (car a
)))))
284 (defun calc-roll-down-stack (n &optional m
)
286 (calc-roll-up-stack (- n
) m
)
287 (if (or (= n
0) (> n
(calc-stack-size))) (setq n
(calc-stack-size)))
291 (if (and calc-any-selections
292 (not calc-use-selections
))
293 (calc-roll-down-with-selections n m
)
294 (calc-pop-push-list n
295 (append (calc-top-list m
1)
296 (calc-top-list (- n m
) (1+ m
))))))))
298 (defun calc-roll-up-stack (n &optional m
)
300 (calc-roll-down-stack (- n
) m
)
301 (if (or (= n
0) (> n
(calc-stack-size))) (setq n
(calc-stack-size)))
305 (if (and calc-any-selections
306 (not calc-use-selections
))
307 (calc-roll-up-with-selections n m
)
308 (calc-pop-push-list n
309 (append (calc-top-list (- n m
) 1)
310 (calc-top-list m
(- n m -
1))))))))
313 (defun calc-do-refresh ()
314 (if calc-hyperbolic-flag
316 (setq calc-display-dirty t
)
322 (defun calc-record-list (vals &optional prefix
)
324 (or (eq (car vals
) 'top-of-stack
)
326 (calc-record (car vals
) prefix
)
327 (setq prefix
"...")))
328 (setq vals
(cdr vals
))))
331 (defun calc-last-args-stub (arg)
334 (calc-last-args arg
))
337 (defun calc-power (arg)
340 (if (and (featurep 'calc-ext
)
342 (calc-binary-op "root" 'calcFunc-nroot arg nil nil
)
343 (calc-binary-op "^" 'calcFunc-pow arg nil nil
'^
))))
345 (defun calc-mod (arg)
348 (calc-binary-op "%" 'calcFunc-mod arg nil nil
'%
)))
350 (defun calc-inv (arg)
353 (calc-unary-op "inv" 'calcFunc-inv arg
)))
355 (defun calc-percent ()
358 (calc-pop-push-record-list
359 1 "%" (list (list 'calcFunc-percent
(calc-top-n 1))))))
365 (calc-enter (- (prefix-numeric-value n
)))
369 (defun calc-pop-above (n)
372 (calc-pop (- (prefix-numeric-value n
)))
375 (defun calc-roll-down (n)
378 (let ((nn (prefix-numeric-value n
)))
380 (calc-roll-down-stack 2))
382 (calc-roll-down-stack nn
))
384 (calc-pop-push-list (calc-stack-size)
386 (calc-top-list (calc-stack-size)))))
388 (calc-roll-down-stack (calc-stack-size) (- nn
)))))))
390 (defun calc-roll-up (n)
393 (let ((nn (prefix-numeric-value n
)))
395 (calc-roll-up-stack 3))
397 (calc-roll-up-stack nn
))
399 (calc-pop-push-list (calc-stack-size)
401 (calc-top-list (calc-stack-size)))))
403 (calc-roll-up-stack (calc-stack-size) (- nn
)))))))
410 (defun calc-num-prefix-name (n)
411 (cond ((eq n
'-
) "- ")
412 ((equal n
'(4)) "C-u ")
413 ((consp n
) (format "%d " (car n
)))
414 ((integerp n
) (format "%d " n
))
417 (defun calc-missing-key (n)
418 "This is a placeholder for a command which needs to be loaded from calc-ext.
419 When this key is used, calc-ext (the Calculator extensions module) will be
420 loaded and the keystroke automatically re-typed."
423 (if (keymapp (key-binding (char-to-string last-command-char
)))
424 (message "%s%c-" (calc-num-prefix-name n
) last-command-char
))
425 (calc-unread-command)
428 (defun calc-shift-Y-prefix-help ()
431 (calc-do-prefix-help calc-Y-help-msgs
"other" ?Y
))
436 (defun calcDigit-letter ()
438 (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
440 (setq last-command-char
(upcase last-command-char
))
442 (calcDigit-nondigit)))
445 ;; A Lisp version of temp_minibuffer_message from minibuf.c.
446 (defun calc-temp-minibuffer-message (m)
447 (let ((savemax (point-max)))
449 (goto-char (point-max))
455 (identity 1) ; this forces a call to QUIT; in bytecode.c.
458 (delete-region savemax
(point-max))
459 (or okay
(abort-recursive-edit)))))))
462 (put 'math-with-extra-prec
'lisp-indent-hook
1)
465 ;;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
466 (defun math-concat (v1 v2
)
470 (if (and (or (math-objvecp v1
) (math-known-scalarp v1
))
471 (or (math-objvecp v2
) (math-known-scalarp v2
)))
472 (append (if (and (math-vectorp v1
)
473 (or (math-matrixp v1
)
474 (not (math-matrixp v2
))))
477 (if (and (math-vectorp v2
)
478 (or (math-matrixp v2
)
479 (not (math-matrixp v1
))))
485 ;;; True if A is zero. Works for un-normalized values. [P n] [Public]
486 (defun math-zerop (a)
488 (cond ((memq (car a
) '(bigpos bigneg
))
489 (while (eq (car (setq a
(cdr a
))) 0))
491 ((memq (car a
) '(frac float polar mod
))
492 (math-zerop (nth 1 a
)))
494 (and (math-zerop (nth 1 a
)) (math-zerop (nth 2 a
))))
496 (and (math-zerop (nth 1 a
))
497 (math-zerop (nth 2 a
))
498 (math-zerop (nth 3 a
)))))
502 ;;; True if A is real and negative. [P n] [Public]
506 (cond ((eq (car a
) 'bigpos
) nil
)
507 ((eq (car a
) 'bigneg
) (cdr a
))
508 ((memq (car a
) '(float frac
))
509 (Math-integer-negp (nth 1 a
)))
511 (if (math-zerop (nth 1 a
))
512 (if (math-zerop (nth 2 a
))
513 (math-negp (nth 3 a
))
514 (math-negp (nth 2 a
)))
515 (math-negp (nth 1 a
))))
517 (math-negp (nth 1 a
)))
519 (or (math-negp (nth 3 a
))
520 (and (math-zerop (nth 3 a
))
521 (memq (nth 1 a
) '(0 2)))))
522 ((equal a
'(neg (var inf var-inf
))) t
))
525 ;;; True if A is a negative number or an expression the starts with '-'.
526 (defun math-looks-negp (a) ; [P x] [Public]
528 (eq (car-safe a
) 'neg
)
529 (and (memq (car-safe a
) '(* /))
530 (or (math-looks-negp (nth 1 a
))
531 (math-looks-negp (nth 2 a
))))
532 (and (eq (car-safe a
) '-
)
533 (math-looks-negp (nth 1 a
)))))
536 ;;; True if A is real and positive. [P n] [Public]
539 (cond ((eq (car a
) 'bigpos
) (cdr a
))
540 ((eq (car a
) 'bigneg
) nil
)
541 ((memq (car a
) '(float frac
))
542 (Math-integer-posp (nth 1 a
)))
544 (if (math-zerop (nth 1 a
))
545 (if (math-zerop (nth 2 a
))
546 (math-posp (nth 3 a
))
547 (math-posp (nth 2 a
)))
548 (math-posp (nth 1 a
))))
550 (math-posp (nth 1 a
)))
552 (not (math-zerop (nth 1 a
))))
554 (or (math-posp (nth 2 a
))
555 (and (math-zerop (nth 2 a
))
556 (memq (nth 1 a
) '(0 1)))))
557 ((equal a
'(var inf var-inf
)) t
))
560 (defalias 'math-fixnump
'integerp
)
561 (defalias 'math-fixnatnump
'natnump
)
564 ;;; True if A is an even integer. [P R R] [Public]
565 (defun math-evenp (a)
567 (and (memq (car a
) '(bigpos bigneg
))
568 (= (%
(nth 1 a
) 2) 0))
571 ;;; Compute A / 2, for small or big integer A. [I i]
572 ;;; If A is negative, type of truncation is undefined.
576 (math-normalize (cons (car a
) (math-div2-bignum (cdr a
))))
580 (defun math-div2-bignum (a) ; [l l]
582 (cons (+ (/ (car a
) 2) (* (%
(nth 1 a
) 2) 500))
583 (math-div2-bignum (cdr a
)))
584 (list (/ (car a
) 2))))
587 ;;; Reject an argument to a calculator function. [Public]
588 (defun math-reject-arg (&optional a p option
)
590 (calc-record-why option p a
)
592 (calc-record-why p a
)))
593 (signal 'wrong-type-argument
(and a
(if p
(list p a
) (list a
)))))
596 ;;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
598 ;; The variable math-trunc-prec is local to math-trunc, but used by
599 ;; math-trunc-fancy in calc-arith.el, which is called by math-trunc.
601 (defun math-trunc (a &optional math-trunc-prec
)
602 (cond (math-trunc-prec
604 (math-trunc-special a math-trunc-prec
))
605 ((Math-integerp a
) a
)
607 (math-neg (math-trunc (math-neg a
))))
609 (math-scale-int (nth 1 a
) (nth 2 a
)))
610 (t (require 'calc-ext
)
611 (math-trunc-fancy a
))))
612 (defalias 'calcFunc-trunc
'math-trunc
)
614 ;;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
616 ;; The variable math-floor-prec is local to math-floor, but used by
617 ;; math-floor-fancy in calc-arith.el, which is called by math-floor.
619 (defun math-floor (a &optional math-floor-prec
) ; [Public]
620 (cond (math-floor-prec
622 (math-floor-special a math-floor-prec
))
623 ((Math-integerp a
) a
)
624 ((Math-messy-integerp a
) (math-trunc a
))
627 (math-add (math-trunc a
) -
1)
629 (t (require 'calc-ext
)
630 (math-floor-fancy a
))))
631 (defalias 'calcFunc-floor
'math-floor
)
634 (defun math-imod (a b
) ; [I I I] [Public]
635 (if (and (not (consp a
)) (not (consp b
)))
637 (math-reject-arg a
"*Division by zero")
639 (cdr (math-idivmod a b
))))
642 (defun calcFunc-inv (m)
646 (if (math-square-matrixp m
)
647 (or (math-with-extra-prec 2 (math-matrix-inv-raw m
))
648 (math-reject-arg m
"*Singular matrix"))
649 (math-reject-arg m
'square-matrixp
)))
651 (require 'calc-arith
)
652 (math-known-matrixp m
))
656 (defun math-do-working (msg arg
)
657 (or executing-kbd-macro
659 (calc-set-command-flag 'clear-message
)
660 (if math-working-step
661 (if math-working-step-2
662 (setq msg
(format "[%d/%d] %s"
663 math-working-step math-working-step-2 msg
))
664 (setq msg
(format "[%d] %s" math-working-step msg
))))
665 (message "Working... %s = %s" msg
666 (math-showing-full-precision (math-format-number arg
))))))
669 ;;; Compute A modulo B, defined in terms of truncation toward minus infinity.
670 (defun math-mod (a b
) ; [R R R] [Public]
671 (cond ((and (Math-zerop a
) (not (eq (car-safe a
) 'mod
))) a
)
673 (math-reject-arg a
"*Division by zero"))
674 ((and (Math-natnump a
) (Math-natnump b
))
676 ((and (Math-anglep a
) (Math-anglep b
))
677 (math-sub a
(math-mul (math-floor (math-div a b
)) b
)))
678 (t (require 'calc-ext
)
679 (math-mod-fancy a b
))))
683 ;;; General exponentiation.
685 (defun math-pow (a b
) ; [O O N] [Public]
686 (cond ((equal b
'(var nan var-nan
))
689 (if (and (Math-scalarp b
) (Math-posp b
))
690 (if (math-floatp b
) (math-float a
) a
)
692 (math-pow-of-zero a b
)))
693 ((or (eq a
1) (eq b
1)) a
)
694 ((or (equal a
'(float 1 0)) (equal b
'(float 1 0))) a
)
697 (if (or (math-floatp a
) (math-floatp b
))
700 (math-pow-zero a b
)))
701 ((and (Math-integerp b
) (or (Math-numberp a
) (Math-vectorp a
)))
702 (if (and (equal a
'(float 1 1)) (integerp b
))
703 (math-make-float 1 b
)
704 (math-with-extra-prec 2
708 (math-pow-fancy a b
))))
710 (defun math-ipow (a n
) ; [O O I] [Public]
711 (cond ((Math-integer-negp n
)
712 (math-ipow (math-div 1 a
) (Math-integer-neg n
)))
714 (if (and (Math-ratp a
) (> n
20))
715 (math-iipow-show a n
)
718 (math-ipow (math-mul a a
) (math-div2 n
)))
720 (math-mul a
(math-ipow (math-mul a a
)
721 (math-div2 (math-add n -
1)))))))
723 (defun math-iipow (a n
) ; [O O S]
726 ((= (% n
2) 0) (math-iipow (math-mul a a
) (/ n
2)))
727 (t (math-mul a
(math-iipow (math-mul a a
) (/ n
2))))))
729 (defun math-iipow-show (a n
) ; [O O S]
730 (math-working "pow" a
)
734 ((= (% n
2) 0) (math-iipow-show (math-mul a a
) (/ n
2)))
735 (t (math-mul a
(math-iipow-show (math-mul a a
) (/ n
2)))))))
736 (math-working "pow" val
)
740 (defun math-read-radix-digit (dig) ; [D S; Z S]
752 (defun report-calc-bug ()
753 "Report a bug in Calc, the GNU Emacs calculator.
754 Prompts for bug subject. Leaves you in a mail buffer."
756 (let ((reporter-prompt-for-summary-p t
))
757 (reporter-submit-bug-report calc-bug-address
"Calc" '(calc-version)
759 "Please describe exactly what actions triggered the bug and the
760 precise symptoms of the bug. If possible, include a backtrace by
761 doing 'M-x toggle-debug-on-error', then reproducing the bug.
763 (defalias 'calc-report-bug
'report-calc-bug
)
767 ;;; arch-tag: 7984d9d0-62e5-41dc-afb8-e904b975f250
768 ;;; calc-misc.el ends here