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 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
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-Need-calc-misc () nil
)
38 (defun calc-dispatch-help (arg)
39 "M-# is a prefix key; follow it with one of these letters:
41 For turning Calc on and off:
42 C calc. Start the Calculator in a window at the bottom of the screen.
43 O calc-other-window. Start the Calculator but don't select its window.
44 B calc-big-or-small. Control whether to use the full Emacs screen for Calc.
45 Q quick-calc. Use the Calculator in the minibuffer.
46 K calc-keypad. Start the Calculator in keypad mode (X window system only).
47 E calc-embedded. Use the Calculator on a formula in this editing buffer.
48 J calc-embedded-select. Like E, but select appropriate half of => or :=.
49 W calc-embedded-word. Like E, but activate a single word, i.e., a number.
50 Z calc-user-invocation. Invoke Calc in the way you defined with `Z I' cmd.
51 X calc-quit. Turn Calc off.
53 For moving data into and out of Calc:
54 G calc-grab-region. Grab the region defined by mark and point into Calc.
55 R calc-grab-rectangle. Grab the rectangle defined by mark, point into Calc.
56 : calc-grab-sum-down. Grab a rectangle and sum the columns.
57 _ calc-grab-sum-across. Grab a rectangle and sum the rows.
58 Y calc-copy-to-buffer. Copy a value from the stack into the editing buffer.
60 For use with Embedded mode:
61 A calc-embedded-activate. Find and activate all :='s and =>'s in buffer.
62 D calc-embedded-duplicate. Make a copy of this formula and select it.
63 F calc-embedded-new-formula. Insert a new formula at current point.
64 N calc-embedded-next. Advance cursor to next known formula in buffer.
65 P calc-embedded-previous. Advance cursor to previous known formula.
66 U calc-embedded-update-formula. Re-evaluate formula at point.
67 ` calc-embedded-edit. Use calc-edit to edit formula at point.
70 I calc-info. Read the Calculator manual in the Emacs Info system.
71 T calc-tutorial. Run the Calculator Tutorial using the Emacs Info system.
72 S calc-summary. Read the Summary from the Calculator manual in Info.
75 L calc-load-everything. Load all parts of the Calculator into memory.
76 M read-kbd-macro. Read a region of keystroke names as a keyboard macro.
77 0 (zero) calc-reset. Reset Calc stack and modes to default state.
79 Press twice (`M-# M-#' or `M-# #') to turn Calc on or off using the same
80 Calc user interface as before (either M-# C or M-# K; initially M-# C)."
83 (if calc-dispatch-help
85 (save-window-excursion
86 (describe-function 'calc-dispatch-help
)
87 (let ((win (get-buffer-window "*Help*")))
92 (message "Calc options: Calc, Keypad, ... %s"
93 "press SPC, DEL to scroll, C-g to cancel")
94 (memq (car (setq key
(calc-read-key t
)))
95 '(? ?\C-h ?\C-? ?\C-v ?\M-v
)))
97 (if (memq (car key
) '(? ?\C-v
))
101 (calc-unread-command (cdr key
))))))
102 (calc-do-dispatch nil
))
103 (let ((calc-dispatch-help t
))
104 (calc-do-dispatch arg
))))
107 (defun calc-big-or-small (arg)
108 "Toggle Calc between full-screen and regular mode."
110 (let ((cwin (get-buffer-window "*Calculator*"))
111 (twin (get-buffer-window "*Calc Trail*"))
112 (kwin (get-buffer-window "*Calc Keypad*")))
116 (and twin
(eq (window-width twin
) (frame-width)))
117 (eq (window-height cwin
) (1- (frame-height))))))
118 (setq calc-full-mode
(if arg
119 (> (prefix-numeric-value arg
) 0)
120 (not calc-full-mode
)))
124 (calc-do-keypad calc-full-mode nil
))
128 (calc nil calc-full-mode nil
))))
129 (message (if calc-full-mode
130 "Now using full screen for Calc"
131 "Now using partial screen for Calc"))))
133 (defun calc-other-window ()
134 "Invoke the Calculator in another window."
136 (if (memq major-mode
'(calc-mode calc-trail-mode
))
139 (if (memq major-mode
'(calc-mode calc-trail-mode
))
141 (if (get-buffer-window "*Calculator*")
143 (let ((win (selected-window)))
144 (calc nil win
(interactive-p))))))
146 (defun another-calc ()
147 "Create another, independent Calculator buffer."
149 (if (eq major-mode
'calc-mode
)
152 (set-default v
(symbol-value v
)))) calc-local-var-list
))
153 (set-buffer (generate-new-buffer "*Calculator*"))
154 (pop-to-buffer (current-buffer))
158 "Run the Emacs Info system on the Calculator documentation."
160 (select-window (get-largest-window))
163 (defun calc-tutorial ()
164 "Run the Emacs Info system on the Calculator Tutorial."
166 (if (get-buffer-window "*Calculator*")
169 (Info-goto-node "Interactive Tutorial")
171 (message "Welcome to the Calc Tutorial!"))
173 (defun calc-info-summary ()
174 "Run the Emacs Info system on the Calculator Summary."
177 (Info-goto-node "Summary"))
182 '("Press `h' for complete help; press `?' repeatedly for a summary"
183 "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit"
184 "Letter keys: SHIFT + Undo, reDo; Keep-args; Inverse, Hyperbolic"
185 "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB"
186 "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi"
187 "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro"
188 "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)"
189 "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)"
190 "Other keys: ' (alg-entry), = (eval), ` (edit); M-RET (last-args)"
191 "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)"
192 "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)"
193 "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)"
194 "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)"
195 "Prefix keys: Algebra, Binary/business, Convert, Display"
196 "Prefix keys: Functions, Graphics, Help, J (select)"
197 "Prefix keys: Kombinatorics/statistics, Modes, Store/recall"
198 "Prefix keys: Trail/time, Units/statistics, Vector/matrix"
199 "Prefix keys: Z (user), SHIFT + Z (define)"
200 "Prefix keys: prefix + ? gives further help for that prefix")
202 " Calc %s by Dave Gillespie, daveg@synaptics.com"
204 (if calc-full-help-flag
206 (if (or calc-inverse-flag calc-hyperbolic-flag
)
207 (if calc-inverse-flag
208 (if calc-hyperbolic-flag
209 (calc-inv-hyp-prefix-help)
210 (calc-inverse-prefix-help))
211 (calc-hyperbolic-prefix-help))
212 (setq calc-help-phase
213 (if (eq this-command last-command
)
214 (%
(1+ calc-help-phase
) (1+ (length msgs
)))
216 (let ((msg (nth calc-help-phase msgs
)))
217 (message "%s" (if msg
219 (make-string (- (apply 'max
229 ;;;; Stack and buffer management.
232 (defun calc-do-handle-whys ()
233 (setq calc-why
(sort calc-next-why
236 (and (eq (car x
) '*) (not (eq (car y
) '*))))))
238 (if (and calc-why
(or (eq calc-auto-why t
)
239 (and (eq (car (car calc-why
)) '*)
243 (calc-explain-why (car calc-why
)
244 (if (eq calc-auto-why t
)
247 (eq (car (nth 1 calc-why
)) '*))))
248 (setq calc-last-why-command this-command
)
249 (calc-clear-command-flag 'clear-message
))))
251 (defun calc-record-why (&rest stuff
)
252 (if (eq (car stuff
) 'quiet
)
253 (setq stuff
(cdr stuff
))
254 (if (and (symbolp (car stuff
))
256 (or (Math-objectp (nth 1 stuff
))
257 (and (Math-vectorp (nth 1 stuff
))
258 (math-constp (nth 1 stuff
)))
259 (math-infinitep (nth 1 stuff
))))
260 (setq stuff
(cons '* stuff
))
261 (if (and (stringp (car stuff
))
262 (string-match "\\`\\*" (car stuff
)))
263 (setq stuff
(cons '* (cons (substring (car stuff
) 1)
265 (setq calc-next-why
(cons stuff calc-next-why
))
268 ;;; True if A is a constant or vector of constants. [P x] [Public]
269 (defun math-constp (a)
271 (and (memq (car a
) '(sdev intv mod vec
))
273 (while (and (setq a
(cdr a
))
274 (or (Math-scalarp (car a
)) ; optimization
275 (math-constp (car a
)))))
279 (defun calc-roll-down-stack (n &optional m
)
281 (calc-roll-up-stack (- n
) m
)
282 (if (or (= n
0) (> n
(calc-stack-size))) (setq n
(calc-stack-size)))
286 (if (and calc-any-selections
287 (not calc-use-selections
))
288 (calc-roll-down-with-selections n m
)
289 (calc-pop-push-list n
290 (append (calc-top-list m
1)
291 (calc-top-list (- n m
) (1+ m
))))))))
293 (defun calc-roll-up-stack (n &optional m
)
295 (calc-roll-down-stack (- n
) m
)
296 (if (or (= n
0) (> n
(calc-stack-size))) (setq n
(calc-stack-size)))
300 (if (and calc-any-selections
301 (not calc-use-selections
))
302 (calc-roll-up-with-selections n m
)
303 (calc-pop-push-list n
304 (append (calc-top-list (- n m
) 1)
305 (calc-top-list m
(- n m -
1))))))))
308 (defun calc-do-refresh ()
309 (if calc-hyperbolic-flag
311 (setq calc-display-dirty t
)
317 (defun calc-record-list (vals &optional prefix
)
319 (or (eq (car vals
) 'top-of-stack
)
321 (calc-record (car vals
) prefix
)
322 (setq prefix
"...")))
323 (setq vals
(cdr vals
))))
326 (defun calc-last-args-stub (arg)
329 (calc-last-args arg
))
332 (defun calc-power (arg)
335 (if (and calc-extensions-loaded
337 (calc-binary-op "root" 'calcFunc-nroot arg nil nil
)
338 (calc-binary-op "^" 'calcFunc-pow arg nil nil
'^
))))
340 (defun calc-mod (arg)
343 (calc-binary-op "%" 'calcFunc-mod arg nil nil
'%
)))
345 (defun calc-inv (arg)
348 (calc-unary-op "inv" 'calcFunc-inv arg
)))
350 (defun calc-percent ()
353 (calc-pop-push-record-list
354 1 "%" (list (list 'calcFunc-percent
(calc-top-n 1))))))
360 (calc-enter (- (prefix-numeric-value n
)))
364 (defun calc-pop-above (n)
367 (calc-pop (- (prefix-numeric-value n
)))
370 (defun calc-roll-down (n)
373 (let ((nn (prefix-numeric-value n
)))
375 (calc-roll-down-stack 2))
377 (calc-roll-down-stack nn
))
379 (calc-pop-push-list (calc-stack-size)
381 (calc-top-list (calc-stack-size)))))
383 (calc-roll-down-stack (calc-stack-size) (- nn
)))))))
385 (defun calc-roll-up (n)
388 (let ((nn (prefix-numeric-value n
)))
390 (calc-roll-up-stack 3))
392 (calc-roll-up-stack nn
))
394 (calc-pop-push-list (calc-stack-size)
396 (calc-top-list (calc-stack-size)))))
398 (calc-roll-up-stack (calc-stack-size) (- nn
)))))))
405 (defun calc-num-prefix-name (n)
406 (cond ((eq n
'-
) "- ")
407 ((equal n
'(4)) "C-u ")
408 ((consp n
) (format "%d " (car n
)))
409 ((integerp n
) (format "%d " n
))
412 (defun calc-missing-key (n)
413 "This is a placeholder for a command which needs to be loaded from calc-ext.
414 When this key is used, calc-ext (the Calculator extensions module) will be
415 loaded and the keystroke automatically re-typed."
418 (if (keymapp (key-binding (char-to-string last-command-char
)))
419 (message "%s%c-" (calc-num-prefix-name n
) last-command-char
))
420 (calc-unread-command)
423 (defun calc-shift-Y-prefix-help ()
426 (calc-do-prefix-help calc-Y-help-msgs
"other" ?Y
))
431 (defun calcDigit-letter ()
433 (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
435 (setq last-command-char
(upcase last-command-char
))
437 (calcDigit-nondigit)))
440 ;; A Lisp version of temp_minibuffer_message from minibuf.c.
441 (defun calc-temp-minibuffer-message (m)
442 (let ((savemax (point-max)))
444 (goto-char (point-max))
450 (identity 1) ; this forces a call to QUIT; in bytecode.c.
453 (delete-region savemax
(point-max))
454 (or okay
(abort-recursive-edit)))))))
457 (put 'math-with-extra-prec
'lisp-indent-hook
1)
460 ;;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
461 (defun math-concat (v1 v2
)
465 (if (and (or (math-objvecp v1
) (math-known-scalarp v1
))
466 (or (math-objvecp v2
) (math-known-scalarp v2
)))
467 (append (if (and (math-vectorp v1
)
468 (or (math-matrixp v1
)
469 (not (math-matrixp v2
))))
472 (if (and (math-vectorp v2
)
473 (or (math-matrixp v2
)
474 (not (math-matrixp v1
))))
480 ;;; True if A is zero. Works for un-normalized values. [P n] [Public]
481 (defun math-zerop (a)
483 (cond ((memq (car a
) '(bigpos bigneg
))
484 (while (eq (car (setq a
(cdr a
))) 0))
486 ((memq (car a
) '(frac float polar mod
))
487 (math-zerop (nth 1 a
)))
489 (and (math-zerop (nth 1 a
)) (math-zerop (nth 2 a
))))
491 (and (math-zerop (nth 1 a
))
492 (math-zerop (nth 2 a
))
493 (math-zerop (nth 3 a
)))))
497 ;;; True if A is real and negative. [P n] [Public]
501 (cond ((eq (car a
) 'bigpos
) nil
)
502 ((eq (car a
) 'bigneg
) (cdr a
))
503 ((memq (car a
) '(float frac
))
504 (Math-integer-negp (nth 1 a
)))
506 (if (math-zerop (nth 1 a
))
507 (if (math-zerop (nth 2 a
))
508 (math-negp (nth 3 a
))
509 (math-negp (nth 2 a
)))
510 (math-negp (nth 1 a
))))
512 (math-negp (nth 1 a
)))
514 (or (math-negp (nth 3 a
))
515 (and (math-zerop (nth 3 a
))
516 (memq (nth 1 a
) '(0 2)))))
517 ((equal a
'(neg (var inf var-inf
))) t
))
520 ;;; True if A is a negative number or an expression the starts with '-'.
521 (defun math-looks-negp (a) ; [P x] [Public]
523 (eq (car-safe a
) 'neg
)
524 (and (memq (car-safe a
) '(* /))
525 (or (math-looks-negp (nth 1 a
))
526 (math-looks-negp (nth 2 a
))))
527 (and (eq (car-safe a
) '-
)
528 (math-looks-negp (nth 1 a
)))))
531 ;;; True if A is real and positive. [P n] [Public]
534 (cond ((eq (car a
) 'bigpos
) (cdr a
))
535 ((eq (car a
) 'bigneg
) nil
)
536 ((memq (car a
) '(float frac
))
537 (Math-integer-posp (nth 1 a
)))
539 (if (math-zerop (nth 1 a
))
540 (if (math-zerop (nth 2 a
))
541 (math-posp (nth 3 a
))
542 (math-posp (nth 2 a
)))
543 (math-posp (nth 1 a
))))
545 (math-posp (nth 1 a
)))
547 (not (math-zerop (nth 1 a
))))
549 (or (math-posp (nth 2 a
))
550 (and (math-zerop (nth 2 a
))
551 (memq (nth 1 a
) '(0 1)))))
552 ((equal a
'(var inf var-inf
)) t
))
555 (defalias 'math-fixnump
'integerp
)
556 (defalias 'math-fixnatnump
'natnump
)
559 ;;; True if A is an even integer. [P R R] [Public]
560 (defun math-evenp (a)
562 (and (memq (car a
) '(bigpos bigneg
))
563 (= (%
(nth 1 a
) 2) 0))
566 ;;; Compute A / 2, for small or big integer A. [I i]
567 ;;; If A is negative, type of truncation is undefined.
571 (math-normalize (cons (car a
) (math-div2-bignum (cdr a
))))
575 (defun math-div2-bignum (a) ; [l l]
577 (cons (+ (/ (car a
) 2) (* (%
(nth 1 a
) 2) 500))
578 (math-div2-bignum (cdr a
)))
579 (list (/ (car a
) 2))))
582 ;;; Reject an argument to a calculator function. [Public]
583 (defun math-reject-arg (&optional a p option
)
585 (calc-record-why option p a
)
587 (calc-record-why p a
)))
588 (signal 'wrong-type-argument
(and a
(if p
(list p a
) (list a
)))))
591 ;;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
592 (defun math-trunc (a &optional prec
)
595 (math-trunc-special a prec
))
596 ((Math-integerp a
) a
)
598 (math-neg (math-trunc (math-neg a
))))
600 (math-scale-int (nth 1 a
) (nth 2 a
)))
602 (math-trunc-fancy a
))))
603 (defalias 'calcFunc-trunc
'math-trunc
)
605 ;;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
606 (defun math-floor (a &optional prec
) ; [Public]
609 (math-floor-special a prec
))
610 ((Math-integerp a
) a
)
611 ((Math-messy-integerp a
) (math-trunc a
))
614 (math-add (math-trunc a
) -
1)
617 (math-floor-fancy a
))))
618 (defalias 'calcFunc-floor
'math-floor
)
621 (defun math-imod (a b
) ; [I I I] [Public]
622 (if (and (not (consp a
)) (not (consp b
)))
624 (math-reject-arg a
"*Division by zero")
626 (cdr (math-idivmod a b
))))
629 (defun calcFunc-inv (m)
633 (if (math-square-matrixp m
)
634 (or (math-with-extra-prec 2 (math-matrix-inv-raw m
))
635 (math-reject-arg m
"*Singular matrix"))
636 (math-reject-arg m
'square-matrixp
)))
640 (defun math-do-working (msg arg
)
641 (or executing-kbd-macro
643 (calc-set-command-flag 'clear-message
)
644 (if math-working-step
645 (if math-working-step-2
646 (setq msg
(format "[%d/%d] %s"
647 math-working-step math-working-step-2 msg
))
648 (setq msg
(format "[%d] %s" math-working-step msg
))))
649 (message "Working... %s = %s" msg
650 (math-showing-full-precision (math-format-number arg
))))))
653 ;;; Compute A modulo B, defined in terms of truncation toward minus infinity.
654 (defun math-mod (a b
) ; [R R R] [Public]
655 (cond ((and (Math-zerop a
) (not (eq (car-safe a
) 'mod
))) a
)
657 (math-reject-arg a
"*Division by zero"))
658 ((and (Math-natnump a
) (Math-natnump b
))
660 ((and (Math-anglep a
) (Math-anglep b
))
661 (math-sub a
(math-mul (math-floor (math-div a b
)) b
)))
663 (math-mod-fancy a b
))))
667 ;;; General exponentiation.
669 (defun math-pow (a b
) ; [O O N] [Public]
670 (cond ((equal b
'(var nan var-nan
))
673 (if (and (Math-scalarp b
) (Math-posp b
))
674 (if (math-floatp b
) (math-float a
) a
)
676 (math-pow-of-zero a b
)))
677 ((or (eq a
1) (eq b
1)) a
)
678 ((or (equal a
'(float 1 0)) (equal b
'(float 1 0))) a
)
681 (if (or (math-floatp a
) (math-floatp b
))
684 (math-pow-zero a b
)))
685 ((and (Math-integerp b
) (or (Math-numberp a
) (Math-vectorp a
)))
686 (if (and (equal a
'(float 1 1)) (integerp b
))
687 (math-make-float 1 b
)
688 (math-with-extra-prec 2
692 (math-pow-fancy a b
))))
694 (defun math-ipow (a n
) ; [O O I] [Public]
695 (cond ((Math-integer-negp n
)
696 (math-ipow (math-div 1 a
) (Math-integer-neg n
)))
698 (if (and (Math-ratp a
) (> n
20))
699 (math-iipow-show a n
)
702 (math-ipow (math-mul a a
) (math-div2 n
)))
704 (math-mul a
(math-ipow (math-mul a a
)
705 (math-div2 (math-add n -
1)))))))
707 (defun math-iipow (a n
) ; [O O S]
710 ((= (% n
2) 0) (math-iipow (math-mul a a
) (/ n
2)))
711 (t (math-mul a
(math-iipow (math-mul a a
) (/ n
2))))))
713 (defun math-iipow-show (a n
) ; [O O S]
714 (math-working "pow" a
)
718 ((= (% n
2) 0) (math-iipow-show (math-mul a a
) (/ n
2)))
719 (t (math-mul a
(math-iipow-show (math-mul a a
) (/ n
2)))))))
720 (math-working "pow" val
)
724 (defun math-read-radix-digit (dig) ; [D S; Z S]
736 (defun report-calc-bug ()
737 "Report a bug in Calc, the GNU Emacs calculator.
738 Prompts for bug subject. Leaves you in a mail buffer."
740 (let ((reporter-prompt-for-summary-p t
))
741 (reporter-submit-bug-report calc-bug-address
"Calc" '(calc-version)
743 "Please describe exactly what actions triggered the bug and the
744 precise symptoms of the bug. If possible, include a backtrace by
745 doing 'M-x toggle-debug-on-error', then reproducing the bug.
747 (defalias 'calc-report-bug
'report-calc-bug
)
749 ;;; calc-misc.el ends here