1 ;;; calc-misc.el --- miscellaenous functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc.el.
34 (defun calc-Need-calc-misc () nil
)
37 (defun calc-dispatch-help (arg)
38 "M-# is a prefix key; follow it with one of these letters:
40 For turning Calc on and off:
41 C calc. Start the Calculator in a window at the bottom of the screen.
42 O calc-other-window. Start the Calculator but don't select its window.
43 B calc-big-or-small. Control whether to use the full Emacs screen for Calc.
44 Q quick-calc. Use the Calculator in the minibuffer.
45 K calc-keypad. Start the Calculator in keypad mode (X window system only).
46 E calc-embedded. Use the Calculator on a formula in this editing buffer.
47 J calc-embedded-select. Like E, but select appropriate half of => or :=.
48 W calc-embedded-word. Like E, but activate a single word, i.e., a number.
49 Z calc-user-invocation. Invoke Calc in the way you defined with `Z I' cmd.
50 X calc-quit. Turn Calc off.
52 For moving data into and out of Calc:
53 G calc-grab-region. Grab the region defined by mark and point into Calc.
54 R calc-grab-rectangle. Grab the rectangle defined by mark, point into Calc.
55 : calc-grab-sum-down. Grab a rectangle and sum the columns.
56 _ calc-grab-sum-across. Grab a rectangle and sum the rows.
57 Y calc-copy-to-buffer. Copy a value from the stack into the editing buffer.
59 For use with Embedded mode:
60 A calc-embedded-activate. Find and activate all :='s and =>'s in buffer.
61 D calc-embedded-duplicate. Make a copy of this formula and select it.
62 F calc-embedded-new-formula. Insert a new formula at current point.
63 N calc-embedded-next. Advance cursor to next known formula in buffer.
64 P calc-embedded-previous. Advance cursor to previous known formula.
65 U calc-embedded-update-formula. Re-evaluate formula at point.
66 ` calc-embedded-edit. Use calc-edit to edit formula at point.
69 I calc-info. Read the Calculator manual in the Emacs Info system.
70 T calc-tutorial. Run the Calculator Tutorial using the Emacs Info system.
71 S calc-summary. Read the Summary from the Calculator manual in Info.
74 L calc-load-everything. Load all parts of the Calculator into memory.
75 M read-kbd-macro. Read a region of keystroke names as a keyboard macro.
76 0 (zero) calc-reset. Reset Calc stack and modes to default state.
78 Press twice (`M-# M-#' or `M-# #') to turn Calc on or off using the same
79 Calc user interface as before (either M-# C or M-# K; initially M-# C)."
82 (if calc-dispatch-help
84 (save-window-excursion
85 (describe-function 'calc-dispatch-help
)
86 (let ((win (get-buffer-window "*Help*")))
91 (message "Calc options: Calc, Keypad, ... %s"
92 "press SPC, DEL to scroll, C-g to cancel")
93 (memq (car (setq key
(calc-read-key t
)))
94 '(? ?\C-h ?\C-? ?\C-v ?\M-v
)))
96 (if (memq (car key
) '(? ?\C-v
))
100 (calc-unread-command (cdr key
))))))
101 (calc-do-dispatch nil
))
102 (let ((calc-dispatch-help t
))
103 (calc-do-dispatch arg
))))
106 (defun calc-big-or-small (arg)
107 "Toggle Calc between full-screen and regular mode."
109 (let ((cwin (get-buffer-window "*Calculator*"))
110 (twin (get-buffer-window "*Calc Trail*"))
111 (kwin (get-buffer-window "*Calc Keypad*")))
115 (and twin
(eq (window-width twin
) (frame-width)))
116 (eq (window-height cwin
) (1- (frame-height))))))
117 (setq calc-full-mode
(if arg
118 (> (prefix-numeric-value arg
) 0)
119 (not calc-full-mode
)))
123 (calc-do-keypad calc-full-mode nil
))
127 (calc nil calc-full-mode nil
))))
128 (message (if calc-full-mode
129 "Now using full screen for Calc"
130 "Now using partial screen for Calc"))))
132 (defun calc-other-window ()
133 "Invoke the Calculator in another window."
135 (if (memq major-mode
'(calc-mode calc-trail-mode
))
138 (if (memq major-mode
'(calc-mode calc-trail-mode
))
140 (if (get-buffer-window "*Calculator*")
142 (let ((win (selected-window)))
143 (calc nil win
(interactive-p))))))
145 (defun another-calc ()
146 "Create another, independent Calculator buffer."
148 (if (eq major-mode
'calc-mode
)
151 (set-default v
(symbol-value v
)))) calc-local-var-list
))
152 (set-buffer (generate-new-buffer "*Calculator*"))
153 (pop-to-buffer (current-buffer))
157 "Run the Emacs Info system on the Calculator documentation."
159 (select-window (get-largest-window))
162 (defun calc-tutorial ()
163 "Run the Emacs Info system on the Calculator Tutorial."
165 (if (get-buffer-window "*Calculator*")
168 (Info-goto-node "Interactive Tutorial")
170 (message "Welcome to the Calc Tutorial!"))
172 (defun calc-info-summary ()
173 "Run the Emacs Info system on the Calculator Summary."
176 (Info-goto-node "Summary"))
181 '("Press `h' for complete help; press `?' repeatedly for a summary"
182 "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit"
183 "Letter keys: SHIFT + Undo, reDo; Keep-args; Inverse, Hyperbolic"
184 "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB"
185 "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi"
186 "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro"
187 "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)"
188 "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)"
189 "Other keys: ' (alg-entry), = (eval), ` (edit); M-RET (last-args)"
190 "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)"
191 "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)"
192 "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)"
193 "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)"
194 "Prefix keys: Algebra, Binary/business, Convert, Display"
195 "Prefix keys: Functions, Graphics, Help, J (select)"
196 "Prefix keys: Kombinatorics/statistics, Modes, Store/recall"
197 "Prefix keys: Trail/time, Units/statistics, Vector/matrix"
198 "Prefix keys: Z (user), SHIFT + Z (define)"
199 "Prefix keys: prefix + ? gives further help for that prefix")
201 " Calc %s by Dave Gillespie, daveg@synaptics.com"
203 (if calc-full-help-flag
205 (if (or calc-inverse-flag calc-hyperbolic-flag
)
206 (if calc-inverse-flag
207 (if calc-hyperbolic-flag
208 (calc-inv-hyp-prefix-help)
209 (calc-inverse-prefix-help))
210 (calc-hyperbolic-prefix-help))
211 (setq calc-help-phase
212 (if (eq this-command last-command
)
213 (%
(1+ calc-help-phase
) (1+ (length msgs
)))
215 (let ((msg (nth calc-help-phase msgs
)))
216 (message "%s" (if msg
218 (make-string (- (apply 'max
228 ;;;; Stack and buffer management.
231 (defun calc-do-handle-whys ()
232 (setq calc-why
(sort calc-next-why
235 (and (eq (car x
) '*) (not (eq (car y
) '*))))))
237 (if (and calc-why
(or (eq calc-auto-why t
)
238 (and (eq (car (car calc-why
)) '*)
242 (calc-explain-why (car calc-why
)
243 (if (eq calc-auto-why t
)
246 (eq (car (nth 1 calc-why
)) '*))))
247 (setq calc-last-why-command this-command
)
248 (calc-clear-command-flag 'clear-message
))))
250 (defun calc-record-why (&rest stuff
)
251 (if (eq (car stuff
) 'quiet
)
252 (setq stuff
(cdr stuff
))
253 (if (and (symbolp (car stuff
))
255 (or (Math-objectp (nth 1 stuff
))
256 (and (Math-vectorp (nth 1 stuff
))
257 (math-constp (nth 1 stuff
)))
258 (math-infinitep (nth 1 stuff
))))
259 (setq stuff
(cons '* stuff
))
260 (if (and (stringp (car stuff
))
261 (string-match "\\`\\*" (car stuff
)))
262 (setq stuff
(cons '* (cons (substring (car stuff
) 1)
264 (setq calc-next-why
(cons stuff calc-next-why
))
267 ;;; True if A is a constant or vector of constants. [P x] [Public]
268 (defun math-constp (a)
270 (and (memq (car a
) '(sdev intv mod vec
))
272 (while (and (setq a
(cdr a
))
273 (or (Math-scalarp (car a
)) ; optimization
274 (math-constp (car a
)))))
278 (defun calc-roll-down-stack (n &optional m
)
280 (calc-roll-up-stack (- n
) m
)
281 (if (or (= n
0) (> n
(calc-stack-size))) (setq n
(calc-stack-size)))
285 (if (and calc-any-selections
286 (not calc-use-selections
))
287 (calc-roll-down-with-selections n m
)
288 (calc-pop-push-list n
289 (append (calc-top-list m
1)
290 (calc-top-list (- n m
) (1+ m
))))))))
292 (defun calc-roll-up-stack (n &optional m
)
294 (calc-roll-down-stack (- n
) m
)
295 (if (or (= n
0) (> n
(calc-stack-size))) (setq n
(calc-stack-size)))
299 (if (and calc-any-selections
300 (not calc-use-selections
))
301 (calc-roll-up-with-selections n m
)
302 (calc-pop-push-list n
303 (append (calc-top-list (- n m
) 1)
304 (calc-top-list m
(- n m -
1))))))))
307 (defun calc-do-refresh ()
308 (if calc-hyperbolic-flag
310 (setq calc-display-dirty t
)
316 (defun calc-record-list (vals &optional prefix
)
318 (or (eq (car vals
) 'top-of-stack
)
320 (calc-record (car vals
) prefix
)
321 (setq prefix
"...")))
322 (setq vals
(cdr vals
))))
325 (defun calc-last-args-stub (arg)
328 (calc-last-args arg
))
331 (defun calc-power (arg)
334 (if (and calc-extensions-loaded
336 (calc-binary-op "root" 'calcFunc-nroot arg nil nil
)
337 (calc-binary-op "^" 'calcFunc-pow arg nil nil
'^
))))
339 (defun calc-mod (arg)
342 (calc-binary-op "%" 'calcFunc-mod arg nil nil
'%
)))
344 (defun calc-inv (arg)
347 (calc-unary-op "inv" 'calcFunc-inv arg
)))
349 (defun calc-percent ()
352 (calc-pop-push-record-list
353 1 "%" (list (list 'calcFunc-percent
(calc-top-n 1))))))
359 (calc-enter (- (prefix-numeric-value n
)))
363 (defun calc-pop-above (n)
366 (calc-pop (- (prefix-numeric-value n
)))
369 (defun calc-roll-down (n)
372 (let ((nn (prefix-numeric-value n
)))
374 (calc-roll-down-stack 2))
376 (calc-roll-down-stack nn
))
378 (calc-pop-push-list (calc-stack-size)
380 (calc-top-list (calc-stack-size)))))
382 (calc-roll-down-stack (calc-stack-size) (- nn
)))))))
384 (defun calc-roll-up (n)
387 (let ((nn (prefix-numeric-value n
)))
389 (calc-roll-up-stack 3))
391 (calc-roll-up-stack nn
))
393 (calc-pop-push-list (calc-stack-size)
395 (calc-top-list (calc-stack-size)))))
397 (calc-roll-up-stack (calc-stack-size) (- nn
)))))))
404 (defun calc-num-prefix-name (n)
405 (cond ((eq n
'-
) "- ")
406 ((equal n
'(4)) "C-u ")
407 ((consp n
) (format "%d " (car n
)))
408 ((integerp n
) (format "%d " n
))
411 (defun calc-missing-key (n)
412 "This is a placeholder for a command which needs to be loaded from calc-ext.
413 When this key is used, calc-ext (the Calculator extensions module) will be
414 loaded and the keystroke automatically re-typed."
417 (if (keymapp (key-binding (char-to-string last-command-char
)))
418 (message "%s%c-" (calc-num-prefix-name n
) last-command-char
))
419 (calc-unread-command)
422 (defun calc-shift-Y-prefix-help ()
425 (calc-do-prefix-help calc-Y-help-msgs
"other" ?Y
))
430 (defun calcDigit-letter ()
432 (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
434 (setq last-command-char
(upcase last-command-char
))
436 (calcDigit-nondigit)))
439 ;; A Lisp version of temp_minibuffer_message from minibuf.c.
440 (defun calc-temp-minibuffer-message (m)
441 (let ((savemax (point-max)))
443 (goto-char (point-max))
449 (identity 1) ; this forces a call to QUIT; in bytecode.c.
452 (delete-region savemax
(point-max))
453 (or okay
(abort-recursive-edit)))))))
456 (put 'math-with-extra-prec
'lisp-indent-hook
1)
459 ;;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
460 (defun math-concat (v1 v2
)
464 (if (and (or (math-objvecp v1
) (math-known-scalarp v1
))
465 (or (math-objvecp v2
) (math-known-scalarp v2
)))
466 (append (if (and (math-vectorp v1
)
467 (or (math-matrixp v1
)
468 (not (math-matrixp v2
))))
471 (if (and (math-vectorp v2
)
472 (or (math-matrixp v2
)
473 (not (math-matrixp v1
))))
479 ;;; True if A is zero. Works for un-normalized values. [P n] [Public]
480 (defun math-zerop (a)
482 (cond ((memq (car a
) '(bigpos bigneg
))
483 (while (eq (car (setq a
(cdr a
))) 0))
485 ((memq (car a
) '(frac float polar mod
))
486 (math-zerop (nth 1 a
)))
488 (and (math-zerop (nth 1 a
)) (math-zerop (nth 2 a
))))
490 (and (math-zerop (nth 1 a
))
491 (math-zerop (nth 2 a
))
492 (math-zerop (nth 3 a
)))))
496 ;;; True if A is real and negative. [P n] [Public]
500 (cond ((eq (car a
) 'bigpos
) nil
)
501 ((eq (car a
) 'bigneg
) (cdr a
))
502 ((memq (car a
) '(float frac
))
503 (Math-integer-negp (nth 1 a
)))
505 (if (math-zerop (nth 1 a
))
506 (if (math-zerop (nth 2 a
))
507 (math-negp (nth 3 a
))
508 (math-negp (nth 2 a
)))
509 (math-negp (nth 1 a
))))
511 (math-negp (nth 1 a
)))
513 (or (math-negp (nth 3 a
))
514 (and (math-zerop (nth 3 a
))
515 (memq (nth 1 a
) '(0 2)))))
516 ((equal a
'(neg (var inf var-inf
))) t
))
519 ;;; True if A is a negative number or an expression the starts with '-'.
520 (defun math-looks-negp (a) ; [P x] [Public]
522 (eq (car-safe a
) 'neg
)
523 (and (memq (car-safe a
) '(* /))
524 (or (math-looks-negp (nth 1 a
))
525 (math-looks-negp (nth 2 a
))))
526 (and (eq (car-safe a
) '-
)
527 (math-looks-negp (nth 1 a
)))))
530 ;;; True if A is real and positive. [P n] [Public]
533 (cond ((eq (car a
) 'bigpos
) (cdr a
))
534 ((eq (car a
) 'bigneg
) nil
)
535 ((memq (car a
) '(float frac
))
536 (Math-integer-posp (nth 1 a
)))
538 (if (math-zerop (nth 1 a
))
539 (if (math-zerop (nth 2 a
))
540 (math-posp (nth 3 a
))
541 (math-posp (nth 2 a
)))
542 (math-posp (nth 1 a
))))
544 (math-posp (nth 1 a
)))
546 (not (math-zerop (nth 1 a
))))
548 (or (math-posp (nth 2 a
))
549 (and (math-zerop (nth 2 a
))
550 (memq (nth 1 a
) '(0 1)))))
551 ((equal a
'(var inf var-inf
)) t
))
554 (defalias 'math-fixnump
'integerp
)
555 (defalias 'math-fixnatnump
'natnump
)
558 ;;; True if A is an even integer. [P R R] [Public]
559 (defun math-evenp (a)
561 (and (memq (car a
) '(bigpos bigneg
))
562 (= (%
(nth 1 a
) 2) 0))
565 ;;; Compute A / 2, for small or big integer A. [I i]
566 ;;; If A is negative, type of truncation is undefined.
570 (math-normalize (cons (car a
) (math-div2-bignum (cdr a
))))
574 (defun math-div2-bignum (a) ; [l l]
576 (cons (+ (/ (car a
) 2) (* (%
(nth 1 a
) 2) 500))
577 (math-div2-bignum (cdr a
)))
578 (list (/ (car a
) 2))))
581 ;;; Reject an argument to a calculator function. [Public]
582 (defun math-reject-arg (&optional a p option
)
584 (calc-record-why option p a
)
586 (calc-record-why p a
)))
587 (signal 'wrong-type-argument
(and a
(if p
(list p a
) (list a
)))))
590 ;;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
591 (defun math-trunc (a &optional prec
)
594 (math-trunc-special a prec
))
595 ((Math-integerp a
) a
)
597 (math-neg (math-trunc (math-neg a
))))
599 (math-scale-int (nth 1 a
) (nth 2 a
)))
601 (math-trunc-fancy a
))))
602 (defalias 'calcFunc-trunc
'math-trunc
)
604 ;;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
605 (defun math-floor (a &optional prec
) ; [Public]
608 (math-floor-special a prec
))
609 ((Math-integerp a
) a
)
610 ((Math-messy-integerp a
) (math-trunc a
))
613 (math-add (math-trunc a
) -
1)
616 (math-floor-fancy a
))))
617 (defalias 'calcFunc-floor
'math-floor
)
620 (defun math-imod (a b
) ; [I I I] [Public]
621 (if (and (not (consp a
)) (not (consp b
)))
623 (math-reject-arg a
"*Division by zero")
625 (cdr (math-idivmod a b
))))
628 (defun calcFunc-inv (m)
632 (if (math-square-matrixp m
)
633 (or (math-with-extra-prec 2 (math-matrix-inv-raw m
))
634 (math-reject-arg m
"*Singular matrix"))
635 (math-reject-arg m
'square-matrixp
)))
639 (defun math-do-working (msg arg
)
640 (or executing-kbd-macro
642 (calc-set-command-flag 'clear-message
)
643 (if math-working-step
644 (if math-working-step-2
645 (setq msg
(format "[%d/%d] %s"
646 math-working-step math-working-step-2 msg
))
647 (setq msg
(format "[%d] %s" math-working-step msg
))))
648 (message "Working... %s = %s" msg
649 (math-showing-full-precision (math-format-number arg
))))))
652 ;;; Compute A modulo B, defined in terms of truncation toward minus infinity.
653 (defun math-mod (a b
) ; [R R R] [Public]
654 (cond ((and (Math-zerop a
) (not (eq (car-safe a
) 'mod
))) a
)
656 (math-reject-arg a
"*Division by zero"))
657 ((and (Math-natnump a
) (Math-natnump b
))
659 ((and (Math-anglep a
) (Math-anglep b
))
660 (math-sub a
(math-mul (math-floor (math-div a b
)) b
)))
662 (math-mod-fancy a b
))))
666 ;;; General exponentiation.
668 (defun math-pow (a b
) ; [O O N] [Public]
669 (cond ((equal b
'(var nan var-nan
))
672 (if (and (Math-scalarp b
) (Math-posp b
))
673 (if (math-floatp b
) (math-float a
) a
)
675 (math-pow-of-zero a b
)))
676 ((or (eq a
1) (eq b
1)) a
)
677 ((or (equal a
'(float 1 0)) (equal b
'(float 1 0))) a
)
680 (if (or (math-floatp a
) (math-floatp b
))
683 (math-pow-zero a b
)))
684 ((and (Math-integerp b
) (or (Math-numberp a
) (Math-vectorp a
)))
685 (if (and (equal a
'(float 1 1)) (integerp b
))
686 (math-make-float 1 b
)
687 (math-with-extra-prec 2
691 (math-pow-fancy a b
))))
693 (defun math-ipow (a n
) ; [O O I] [Public]
694 (cond ((Math-integer-negp n
)
695 (math-ipow (math-div 1 a
) (Math-integer-neg n
)))
697 (if (and (Math-ratp a
) (> n
20))
698 (math-iipow-show a n
)
701 (math-ipow (math-mul a a
) (math-div2 n
)))
703 (math-mul a
(math-ipow (math-mul a a
)
704 (math-div2 (math-add n -
1)))))))
706 (defun math-iipow (a n
) ; [O O S]
709 ((= (% n
2) 0) (math-iipow (math-mul a a
) (/ n
2)))
710 (t (math-mul a
(math-iipow (math-mul a a
) (/ n
2))))))
712 (defun math-iipow-show (a n
) ; [O O S]
713 (math-working "pow" a
)
717 ((= (% n
2) 0) (math-iipow-show (math-mul a a
) (/ n
2)))
718 (t (math-mul a
(math-iipow-show (math-mul a a
) (/ n
2)))))))
719 (math-working "pow" val
)
723 (defun math-read-radix-digit (dig) ; [D S; Z S]
735 (defun report-calc-bug ()
736 "Report a bug in Calc, the GNU Emacs calculator.
737 Prompts for bug subject. Leaves you in a mail buffer."
739 (let ((reporter-prompt-for-summary-p t
))
740 (reporter-submit-bug-report calc-bug-address
"Calc" '(calc-version)
742 "Please describe exactly what actions triggered the bug and the
743 precise symptoms of the bug. If possible, include a backtrace by
744 doing 'M-x toggle-debug-on-error', then reproducing the bug.
746 (defalias 'calc-report-bug
'report-calc-bug
)
748 ;;; calc-misc.el ends here