Use Core Text types/functions/variables/enumerators directly
[emacs.git] / lisp / calc / calc-misc.el
blobe141589ab7b8fd4526c6e9acd471293a38741cf8
1 ;;; calc-misc.el --- miscellaneous functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 ;; This file is autoloaded from calc.el.
29 (require 'calc)
30 (require 'calc-macs)
32 ;; Declare functions which are defined elsewhere.
33 (declare-function calc-do-keypad "calc-keypd" (&optional full-display interactive))
34 (declare-function calc-inv-hyp-prefix-help "calc-help" ())
35 (declare-function calc-inverse-prefix-help "calc-help" ())
36 (declare-function calc-hyperbolic-prefix-help "calc-help" ())
37 (declare-function calc-option-prefix-help "calc-help" ())
38 (declare-function calc-explain-why "calc-stuff" (why &optional more))
39 (declare-function calc-clear-command-flag "calc-ext" (f))
40 (declare-function calc-roll-down-with-selections "calc-sel" (n m))
41 (declare-function calc-roll-up-with-selections "calc-sel" (n m))
42 (declare-function calc-last-args "calc-undo" (n))
43 (declare-function calc-is-inverse "calc-ext" ())
44 (declare-function calc-do-prefix-help "calc-ext" (msgs group key))
45 (declare-function math-objvecp "calc-ext" (a))
46 (declare-function math-known-scalarp "calc-arith" (a &optional assume-scalar))
47 (declare-function math-vectorp "calc-ext" (a))
48 (declare-function math-matrixp "calc-ext" (a))
49 (declare-function math-trunc-special "calc-arith" (a prec))
50 (declare-function math-trunc-fancy "calc-arith" (a))
51 (declare-function math-floor-special "calc-arith" (a prec))
52 (declare-function math-floor-fancy "calc-arith" (a))
53 (declare-function math-square-matrixp "calc-ext" (a))
54 (declare-function math-matrix-inv-raw "calc-mtx" (m))
55 (declare-function math-known-matrixp "calc-arith" (a))
56 (declare-function math-mod-fancy "calc-arith" (a b))
57 (declare-function math-pow-of-zero "calc-arith" (a b))
58 (declare-function math-pow-zero "calc-arith" (a b))
59 (declare-function math-pow-fancy "calc-arith" (a b))
60 (declare-function calc-locate-cursor-element "calc-yank" (pt))
62 ;;;###autoload
63 (defun calc-dispatch-help (arg)
64 "C-x* is a prefix key sequence; follow it with one of these letters:
66 For turning Calc on and off:
67 C calc. Start the Calculator in a window at the bottom of the screen.
68 O calc-other-window. Start the Calculator but don't select its window.
69 B calc-big-or-small. Control whether to use the full Emacs screen for Calc.
70 Q quick-calc. Use the Calculator in the minibuffer.
71 K calc-keypad. Start the Calculator in keypad mode (X window system only).
72 E calc-embedded. Use the Calculator on a formula in this editing buffer.
73 J calc-embedded-select. Like E, but select appropriate half of => or :=.
74 W calc-embedded-word. Like E, but activate a single word, i.e., a number.
75 Z calc-user-invocation. Invoke Calc in the way you defined with `Z I' cmd.
76 X calc-quit. Turn Calc off.
78 For moving data into and out of Calc:
79 G calc-grab-region. Grab the region defined by mark and point into Calc.
80 R calc-grab-rectangle. Grab the rectangle defined by mark, point into Calc.
81 : calc-grab-sum-down. Grab a rectangle and sum the columns.
82 _ calc-grab-sum-across. Grab a rectangle and sum the rows.
83 Y calc-copy-to-buffer. Copy a value from the stack into the editing buffer.
85 For use with Embedded mode:
86 A calc-embedded-activate. Find and activate all :='s and =>'s in buffer.
87 D calc-embedded-duplicate. Make a copy of this formula and select it.
88 F calc-embedded-new-formula. Insert a new formula at current point.
89 N calc-embedded-next. Advance cursor to next known formula in buffer.
90 P calc-embedded-previous. Advance cursor to previous known formula.
91 U calc-embedded-update-formula. Re-evaluate formula at point.
92 \\=` calc-embedded-edit. Use calc-edit to edit formula at point.
94 Documentation:
95 I calc-info. Read the Calculator manual in the Emacs Info system.
96 T calc-tutorial. Run the Calculator Tutorial using the Emacs Info system.
97 S calc-summary. Read the Summary from the Calculator manual in Info.
99 Miscellaneous:
100 L calc-load-everything. Load all parts of the Calculator into memory.
101 M read-kbd-macro. Read a region of keystroke names as a keyboard macro.
102 0 (zero) calc-reset. Reset Calc stack and modes to default state.
104 Press `*' twice (`C-x * *') to turn Calc on or off using the same
105 Calc user interface as before (either C-x * C or C-x * K; initially C-x * C).
107 (interactive "P")
108 (calc-check-defines)
109 (if calc-dispatch-help
110 (progn
111 (save-window-excursion
112 (describe-function 'calc-dispatch-help)
113 (let ((win (get-buffer-window "*Help*")))
114 (if win
115 (let (key)
116 (select-window win)
117 (while (progn
118 (message "Calc options: Calc, Keypad, ... %s"
119 "press SPC, DEL to scroll, C-g to cancel")
120 (memq (car (setq key (calc-read-key t)))
121 '(? ?\C-h ?\C-? ?\C-v ?\M-v)))
122 (condition-case err
123 (if (memq (car key) '(? ?\C-v))
124 (scroll-up)
125 (scroll-down))
126 (error (beep))))
127 (calc-unread-command (cdr key))))))
128 (calc-do-dispatch nil))
129 (let ((calc-dispatch-help t))
130 (calc-do-dispatch arg))))
133 ;;;###autoload
134 (defun calc-big-or-small (arg)
135 "Toggle Calc between full-screen and regular mode."
136 (interactive "P")
137 (let ((cwin (get-buffer-window "*Calculator*"))
138 (twin (get-buffer-window "*Calc Trail*"))
139 (kwin (get-buffer-window "*Calc Keypad*")))
140 (if cwin
141 (setq calc-full-mode
142 (if kwin
143 (and twin (window-full-width-p twin))
144 (window-full-height-p cwin))))
145 (setq calc-full-mode (if arg
146 (> (prefix-numeric-value arg) 0)
147 (not calc-full-mode)))
148 (if kwin
149 (progn
150 (calc-quit)
151 (calc-do-keypad calc-full-mode nil))
152 (if cwin
153 (progn
154 (calc-quit)
155 (calc nil calc-full-mode nil))))
156 (message (if calc-full-mode
157 "Now using full screen for Calc"
158 "Now using partial screen for Calc"))))
160 ;;;###autoload
161 (defun calc-other-window (&optional interactive)
162 "Invoke the Calculator in another window."
163 (interactive "p")
164 (if (memq major-mode '(calc-mode calc-trail-mode))
165 (progn
166 (other-window 1)
167 (if (memq major-mode '(calc-mode calc-trail-mode))
168 (other-window 1)))
169 (if (get-buffer-window "*Calculator*")
170 (calc-quit)
171 (let ((win (selected-window)))
172 (calc nil win interactive)))))
174 ;;;###autoload
175 (defun another-calc ()
176 "Create another, independent Calculator buffer."
177 (interactive)
178 (if (eq major-mode 'calc-mode)
179 (mapc (function
180 (lambda (v)
181 (set-default v (symbol-value v)))) calc-local-var-list))
182 (set-buffer (generate-new-buffer "*Calculator*"))
183 (pop-to-buffer (current-buffer))
184 (calc-mode))
186 ;;;###autoload
187 (defun calc-info ()
188 "Run the Emacs Info system on the Calculator documentation."
189 (interactive)
190 (select-window (get-largest-window))
191 (info "Calc"))
193 ;;;###autoload
194 (defun calc-info-goto-node (node)
195 "Go to a node in the Calculator info documentation."
196 (interactive)
197 (select-window (get-largest-window))
198 (info (concat "(Calc)" node)))
200 ;;;###autoload
201 (defun calc-tutorial ()
202 "Run the Emacs Info system on the Calculator Tutorial."
203 (interactive)
204 (if (get-buffer-window "*Calculator*")
205 (calc-quit))
206 (calc-info-goto-node "Interactive Tutorial")
207 (calc-other-window)
208 (message "Welcome to the Calc Tutorial!"))
210 ;;;###autoload
211 (defun calc-info-summary ()
212 "Run the Emacs Info system on the Calculator Summary."
213 (interactive)
214 (calc-info-goto-node "Summary"))
216 ;;;###autoload
217 (defun calc-help ()
218 (interactive)
219 (let ((msgs
220 '("Press ‘h’ for complete help; press ‘?’ repeatedly for a summary"
221 "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit"
222 "Letter keys: SHIFT + Undo, reDo; Inverse, Hyperbolic, Option"
223 "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB"
224 "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi"
225 "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro; Keep-args"
226 "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)"
227 "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)"
228 "Other keys: \\=' (alg-entry), = (eval), \\=` (edit); M-RET (last-args)"
229 "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)"
230 "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)"
231 "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)"
232 "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)"
233 "Prefix keys: Algebra, Binary/business, Convert, Display"
234 "Prefix keys: Functions, Graphics, Help, J (select)"
235 "Prefix keys: Kombinatorics/statistics, Modes, Store/recall"
236 "Prefix keys: Trail/time, Units/statistics, Vector/matrix"
237 "Prefix keys: Z (user), SHIFT + Z (define)"
238 "Prefix keys: prefix + ? gives further help for that prefix"
239 " Calc by Dave Gillespie, daveg@synaptics.com")))
240 (if calc-full-help-flag
241 msgs
242 (if (or calc-inverse-flag calc-hyperbolic-flag)
243 (if calc-inverse-flag
244 (if calc-hyperbolic-flag
245 (calc-inv-hyp-prefix-help)
246 (calc-inverse-prefix-help))
247 (calc-hyperbolic-prefix-help))
248 (if calc-option-flag
249 (calc-option-prefix-help)
250 (setq calc-help-phase
251 (if (eq this-command last-command)
252 (% (1+ calc-help-phase) (1+ (length msgs)))
254 (let ((msg (nth calc-help-phase msgs)))
255 (message "%s" (if msg
256 (concat (substitute-command-keys msg) ":"
257 (make-string (- (apply 'max
258 (mapcar 'length
259 msgs))
260 (length msg))
261 ?\ )
262 " [?=MORE]")
263 ""))))))))
268 ;;;; Stack and buffer management.
270 ;; The variable calc-last-why-command is set in calc-do-handle-whys
271 ;; and used in calc-why (in calc-stuff.el).
272 (defvar calc-last-why-command)
274 ;;;###autoload
275 (defun calc-do-handle-whys ()
276 (setq calc-why (sort calc-next-why
277 (function
278 (lambda (x y)
279 (and (eq (car x) '*) (not (eq (car y) '*))))))
280 calc-next-why nil)
281 (if (and calc-why (or (eq calc-auto-why t)
282 (and (eq (car (car calc-why)) '*)
283 calc-auto-why)))
284 (progn
285 (require 'calc-ext)
286 (calc-explain-why (car calc-why)
287 (if (eq calc-auto-why t)
288 (cdr calc-why)
289 (if calc-auto-why
290 (eq (car (nth 1 calc-why)) '*))))
291 (setq calc-last-why-command this-command)
292 (calc-clear-command-flag 'clear-message))))
294 ;;;###autoload
295 (defun calc-record-why (&rest stuff)
296 (if (eq (car stuff) 'quiet)
297 (setq stuff (cdr stuff))
298 (if (and (symbolp (car stuff))
299 (cdr stuff)
300 (or (Math-objectp (nth 1 stuff))
301 (and (Math-vectorp (nth 1 stuff))
302 (math-constp (nth 1 stuff)))
303 (math-infinitep (nth 1 stuff))))
304 (setq stuff (cons '* stuff))
305 (if (and (stringp (car stuff))
306 (string-match "\\`\\*" (car stuff)))
307 (setq stuff (cons '* (cons (substring (car stuff) 1)
308 (cdr stuff)))))))
309 (unless (member stuff calc-next-why)
310 (setq calc-next-why (cons stuff calc-next-why)))
311 nil)
313 ;; True if A is a constant or vector of constants. [P x] [Public]
314 ;;;###autoload
315 (defun math-constp (a)
316 (or (Math-scalarp a)
317 (and (memq (car a) '(sdev intv mod vec))
318 (progn
319 (while (and (setq a (cdr a))
320 (or (Math-scalarp (car a)) ; optimization
321 (math-constp (car a)))))
322 (null a)))))
325 ;;;###autoload
326 (defun calc-roll-down-stack (n &optional m)
327 (if (< n 0)
328 (calc-roll-up-stack (- n) m)
329 (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
330 (or m (setq m 1))
331 (and (> n 1)
332 (< m n)
333 (if (and calc-any-selections
334 (not calc-use-selections))
335 (calc-roll-down-with-selections n m)
336 (calc-pop-push-list n
337 (append (calc-top-list m 1)
338 (calc-top-list (- n m) (1+ m))))))))
340 ;;;###autoload
341 (defun calc-roll-up-stack (n &optional m)
342 (if (< n 0)
343 (calc-roll-down-stack (- n) m)
344 (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
345 (or m (setq m 1))
346 (and (> n 1)
347 (< m n)
348 (if (and calc-any-selections
349 (not calc-use-selections))
350 (calc-roll-up-with-selections n m)
351 (calc-pop-push-list n
352 (append (calc-top-list (- n m) 1)
353 (calc-top-list m (- n m -1))))))))
356 ;;;###autoload
357 (defun calc-do-refresh ()
358 (if calc-hyperbolic-flag
359 (progn
360 (setq calc-display-dirty t)
361 nil)
362 (calc-refresh)
366 ;;;###autoload
367 (defun calc-record-list (vals &optional prefix)
368 (while vals
369 (or (eq (car vals) 'top-of-stack)
370 (progn
371 (calc-record (car vals) prefix)
372 (setq prefix "...")))
373 (setq vals (cdr vals))))
376 ;;;###autoload
377 (defun calc-last-args-stub (arg)
378 (interactive "p")
379 (require 'calc-ext)
380 (calc-last-args arg))
383 ;;;###autoload
384 (defun calc-power (arg)
385 (interactive "P")
386 (calc-slow-wrapper
387 (if (and (featurep 'calc-ext)
388 (calc-is-inverse))
389 (calc-binary-op "root" 'calcFunc-nroot arg nil nil)
390 (calc-binary-op "^" 'calcFunc-pow arg nil nil '^))))
392 ;;;###autoload
393 (defun calc-mod (arg)
394 (interactive "P")
395 (calc-slow-wrapper
396 (calc-binary-op "%" 'calcFunc-mod arg nil nil '%)))
398 ;;;###autoload
399 (defun calc-inv (arg)
400 (interactive "P")
401 (calc-slow-wrapper
402 (calc-unary-op "inv" 'calcFunc-inv arg)))
404 ;;;###autoload
405 (defun calc-percent ()
406 (interactive)
407 (calc-slow-wrapper
408 (calc-pop-push-record-list
409 1 "%" (list (list 'calcFunc-percent (calc-top-n 1))))))
412 ;;;###autoload
413 (defun calc-over (n)
414 (interactive "P")
415 (if n
416 (calc-enter (- (prefix-numeric-value n)))
417 (calc-enter -2)))
420 ;;;###autoload
421 (defun calc-pop-above (n)
422 (interactive "P")
423 (if n
424 (calc-pop (- (prefix-numeric-value n)))
425 (calc-pop -2)))
427 ;;;###autoload
428 (defun calc-roll-down (n)
429 (interactive "P")
430 (calc-wrapper
431 (let ((nn (prefix-numeric-value n)))
432 (cond ((null n)
433 (calc-roll-down-stack 2))
434 ((> nn 0)
435 (calc-roll-down-stack nn))
436 ((= nn 0)
437 (calc-pop-push-list (calc-stack-size)
438 (reverse
439 (calc-top-list (calc-stack-size)))))
441 (calc-roll-down-stack (calc-stack-size) (- nn)))))))
443 ;;;###autoload
444 (defun calc-roll-up (n)
445 (interactive "P")
446 (calc-wrapper
447 (let ((nn (prefix-numeric-value n)))
448 (cond ((null n)
449 (calc-roll-up-stack 3))
450 ((> nn 0)
451 (calc-roll-up-stack nn))
452 ((= nn 0)
453 (calc-pop-push-list (calc-stack-size)
454 (reverse
455 (calc-top-list (calc-stack-size)))))
457 (calc-roll-up-stack (calc-stack-size) (- nn)))))))
459 ;;;###autoload
460 (defun calc-transpose-lines (&optional arg)
461 "Transpose previous line and current line.
462 With argument ARG, move previous line past ARG lines.
463 With argument 0, switch line point is in with line mark is in."
464 (interactive "p")
465 (setq arg (or arg 1))
466 (let (bot-line mid-line end-line
467 old-top-list new-top-list
468 bot-cell mid-cell
469 prev-mid-cell post-mid-cell post-bot-cell)
470 (calc-wrapper
471 (when (eq major-mode 'calc-mode)
472 (cond
473 ;; exchange point and mark
474 ((= 0 arg)
475 (setq bot-line (calc-locate-cursor-element (point))
476 mid-line (mark))
477 (if mid-line
478 (setq mid-line (calc-locate-cursor-element mid-line)
479 end-line (1+ mid-line))
480 (error "No mark set"))
481 (if (< bot-line mid-line)
482 (let ((temp mid-line))
483 (setq mid-line bot-line
484 bot-line temp))))
485 ;; move bot-line to mid-line that is above bot-line on stack (that is
486 ;; to say mid-line displayed below bot-line in *Calculator* buffer)
487 ((> arg 0)
488 (setq bot-line (1+ (calc-locate-cursor-element (point)))
489 mid-line (- bot-line arg)
490 end-line mid-line))
491 ;; move bot-line to mid-line that is above bot-line on stack (that is
492 ;; to say mid-line displayed below bot-line in *Calculator* buffer)
493 ((< arg 0)
494 (setq mid-line (1+ (calc-locate-cursor-element (point)))
495 bot-line (- mid-line arg)
496 end-line bot-line)))
497 (calc-check-stack bot-line)
498 (if (= 0 mid-line)
499 (error "Can't transpose beyond top"))
500 (setq old-top-list (nreverse (calc-top-list bot-line)))
501 ;; example: (arg = 2)
502 ;; old-top-list =
503 ;; 1 <-- top of stack (bottom of *Calculator* buffer)
504 ;; 2
505 ;; 3 <-- mid-line = 3
506 ;; 4 <-- point
507 ;; 5 <-- bot-line = 5
508 (dotimes (i mid-line)
509 (setq mid-cell old-top-list
510 old-top-list (cdr old-top-list))
511 (setcdr mid-cell new-top-list)
512 (setq new-top-list mid-cell))
513 ;; example follow-up:
514 ;; old-top-list =
515 ;; 4
516 ;; 5
517 ;; new-top-list =
518 ;; 3 <-- mid-cell
519 ;; 2
520 ;; 1
521 (setq prev-mid-cell old-top-list)
522 (dotimes (i (- bot-line mid-line))
523 (setq bot-cell old-top-list
524 old-top-list (cdr old-top-list))
525 (setcdr bot-cell new-top-list)
526 (setq new-top-list bot-cell))
527 (setq post-mid-cell (cdr mid-cell)
528 post-bot-cell (cdr bot-cell))
529 ;; example follow-up:
530 ;; new-top-list =
531 ;; 5 <-- bot-cell
532 ;; 4 <-- prev-mid-cell & post-bot-cell
533 ;; 3 <-- mid-cell
534 ;; 2 <-- post-mid-cell
535 ;; 1
536 (cond
537 ((= 0 arg); swap bot and mid
538 (setcdr mid-cell post-bot-cell)
539 (setcdr bot-cell post-mid-cell)
540 (setcdr prev-mid-cell bot-cell)
541 ;; example follow-up:
542 ;; 3 <-- mid-cell
543 ;; 4 <-- post-bot-cell & prev-mid-cell
544 ;; 5 <-- bot-cell
545 ;; 2 <-- post-mid-cell
546 ;; 1
547 (setq new-top-list mid-cell))
548 ((< 0 arg) ; move bot just after mid
549 (setcdr mid-cell bot-cell)
550 (setcdr bot-cell post-mid-cell)
551 ;; example follow-up:
552 ;; new-top-list =
553 ;; 4 <-- post-bot-cell
554 ;; 3 <-- mid-cell
555 ;; 5 <-- bot-cell
556 ;; 2 <-- post-mid-cell
557 ;; 1
558 (setq new-top-list post-bot-cell))
559 ((> 0 arg) ; move mid just before bot
560 (setcdr mid-cell bot-cell)
561 (setcdr prev-mid-cell post-mid-cell)
562 ;; example follow-up:
563 ;; new-top-list =
564 ;; 3 <-- mid-cell
565 ;; 5 <-- bot-cell
566 ;; 4 <-- prev-mid-cell
567 ;; 2 <-- post-mid-cell
568 ;; 1
569 (setq new-top-list mid-cell)))
570 (calc-pop-push-list bot-line new-top-list)))
571 (calc-cursor-stack-index (1- end-line))))
575 ;;; Other commands.
577 ;;;###autoload
578 (defun calc-num-prefix-name (n)
579 (cond ((eq n '-) "- ")
580 ((equal n '(4)) "C-u ")
581 ((consp n) (format "%d " (car n)))
582 ((integerp n) (format "%d " n))
583 (t "")))
585 ;;;###autoload
586 (defun calc-missing-key (n)
587 "This is a placeholder for a command which needs to be loaded from calc-ext.
588 When this key is used, calc-ext (the Calculator extensions module) will be
589 loaded and the keystroke automatically re-typed."
590 (interactive "P")
591 (require 'calc-ext)
592 (if (keymapp (key-binding (char-to-string last-command-event)))
593 (message "%s%c-" (calc-num-prefix-name n) last-command-event))
594 (calc-unread-command)
595 (setq prefix-arg n))
597 ;;;###autoload
598 (defun calc-shift-Y-prefix-help ()
599 (interactive)
600 (require 'calc-ext)
601 (calc-do-prefix-help calc-Y-help-msgs "other" ?Y))
606 ;;;###autoload
607 (defun calcDigit-letter ()
608 (interactive)
609 (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
610 (progn
611 (setq last-command-event (upcase last-command-event))
612 (calcDigit-key))
613 (calcDigit-nondigit)))
616 ;; A Lisp version of temp_minibuffer_message from minibuf.c.
617 ;;;###autoload
618 (defun calc-temp-minibuffer-message (m)
619 (let ((savemax (point-max)))
620 (save-excursion
621 (goto-char (point-max))
622 (insert m))
623 (let ((okay nil))
624 (unwind-protect
625 (progn
626 (sit-for 2)
627 (identity 1) ; this forces a call to QUIT; in bytecode.c.
628 (setq okay t))
629 (progn
630 (delete-region savemax (point-max))
631 (or okay (abort-recursive-edit)))))))
634 (put 'math-with-extra-prec 'lisp-indent-hook 1)
637 ;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
638 ;;;###autoload
639 (defun math-concat (v1 v2)
640 (if (stringp v1)
641 (concat v1 v2)
642 (require 'calc-ext)
643 (if (and (or (math-objvecp v1) (math-known-scalarp v1))
644 (or (math-objvecp v2) (math-known-scalarp v2)))
645 (append (if (and (math-vectorp v1)
646 (or (math-matrixp v1)
647 (not (math-matrixp v2))))
649 (list 'vec v1))
650 (if (and (math-vectorp v2)
651 (or (math-matrixp v2)
652 (not (math-matrixp v1))))
653 (cdr v2)
654 (list v2)))
655 (list '| v1 v2))))
658 ;; True if A is zero. Works for un-normalized values. [P n] [Public]
659 ;;;###autoload
660 (defun math-zerop (a)
661 (if (consp a)
662 (cond ((memq (car a) '(bigpos bigneg))
663 (while (eq (car (setq a (cdr a))) 0))
664 (null a))
665 ((memq (car a) '(frac float polar mod))
666 (math-zerop (nth 1 a)))
667 ((eq (car a) 'cplx)
668 (and (math-zerop (nth 1 a)) (math-zerop (nth 2 a))))
669 ((eq (car a) 'hms)
670 (and (math-zerop (nth 1 a))
671 (math-zerop (nth 2 a))
672 (math-zerop (nth 3 a)))))
673 (eq a 0)))
676 ;; True if A is real and negative. [P n] [Public]
678 ;;;###autoload
679 (defun math-negp (a)
680 (if (consp a)
681 (cond ((eq (car a) 'bigpos) nil)
682 ((eq (car a) 'bigneg) (cdr a))
683 ((memq (car a) '(float frac))
684 (Math-integer-negp (nth 1 a)))
685 ((eq (car a) 'hms)
686 (if (math-zerop (nth 1 a))
687 (if (math-zerop (nth 2 a))
688 (math-negp (nth 3 a))
689 (math-negp (nth 2 a)))
690 (math-negp (nth 1 a))))
691 ((eq (car a) 'date)
692 (math-negp (nth 1 a)))
693 ((eq (car a) 'intv)
694 (or (math-negp (nth 3 a))
695 (and (math-zerop (nth 3 a))
696 (memq (nth 1 a) '(0 2)))))
697 ((equal a '(neg (var inf var-inf))) t))
698 (< a 0)))
700 ;; True if A is a negative number or an expression the starts with '-'.
701 ;;;###autoload
702 (defun math-looks-negp (a) ; [P x] [Public]
703 (or (Math-negp a)
704 (eq (car-safe a) 'neg)
705 (and (memq (car-safe a) '(* /))
706 (or (math-looks-negp (nth 1 a))
707 (math-looks-negp (nth 2 a))))
708 (and (eq (car-safe a) '-)
709 (math-looks-negp (nth 1 a)))))
712 ;; True if A is real and positive. [P n] [Public]
713 ;;;###autoload
714 (defun math-posp (a)
715 (if (consp a)
716 (cond ((eq (car a) 'bigpos) (cdr a))
717 ((eq (car a) 'bigneg) nil)
718 ((memq (car a) '(float frac))
719 (Math-integer-posp (nth 1 a)))
720 ((eq (car a) 'hms)
721 (if (math-zerop (nth 1 a))
722 (if (math-zerop (nth 2 a))
723 (math-posp (nth 3 a))
724 (math-posp (nth 2 a)))
725 (math-posp (nth 1 a))))
726 ((eq (car a) 'date)
727 (math-posp (nth 1 a)))
728 ((eq (car a) 'mod)
729 (not (math-zerop (nth 1 a))))
730 ((eq (car a) 'intv)
731 (or (math-posp (nth 2 a))
732 (and (math-zerop (nth 2 a))
733 (memq (nth 1 a) '(0 1)))))
734 ((equal a '(var inf var-inf)) t))
735 (> a 0)))
737 ;;;###autoload
738 (defalias 'math-fixnump 'integerp)
739 ;;;###autoload
740 (defalias 'math-fixnatnump 'natnump)
743 ;; True if A is an even integer. [P R R] [Public]
744 ;;;###autoload
745 (defun math-evenp (a)
746 (if (consp a)
747 (and (memq (car a) '(bigpos bigneg))
748 (= (% (nth 1 a) 2) 0))
749 (= (% a 2) 0)))
751 ;; Compute A / 2, for small or big integer A. [I i]
752 ;; If A is negative, type of truncation is undefined.
753 ;;;###autoload
754 (defun math-div2 (a)
755 (if (consp a)
756 (if (cdr a)
757 (math-normalize (cons (car a) (math-div2-bignum (cdr a))))
759 (/ a 2)))
761 ;;;###autoload
762 (defun math-div2-bignum (a) ; [l l]
763 (if (cdr a)
764 (cons (+ (/ (car a) 2) (* (% (nth 1 a) 2) (/ math-bignum-digit-size 2)))
765 (math-div2-bignum (cdr a)))
766 (list (/ (car a) 2))))
769 ;; Reject an argument to a calculator function. [Public]
770 ;;;###autoload
771 (defun math-reject-arg (&optional a p option)
772 (if option
773 (calc-record-why option p a)
774 (if p
775 (calc-record-why p a)))
776 (signal 'wrong-type-argument (and a (if p (list p a) (list a)))))
779 ;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
781 ;; The variable math-trunc-prec is local to math-trunc, but used by
782 ;; math-trunc-fancy in calc-arith.el, which is called by math-trunc.
784 ;;;###autoload
785 (defun math-trunc (a &optional math-trunc-prec)
786 (cond (math-trunc-prec
787 (require 'calc-ext)
788 (math-trunc-special a math-trunc-prec))
789 ((Math-integerp a) a)
790 ((Math-looks-negp a)
791 (math-neg (math-trunc (math-neg a))))
792 ((eq (car a) 'float)
793 (math-scale-int (nth 1 a) (nth 2 a)))
794 (t (require 'calc-ext)
795 (math-trunc-fancy a))))
796 ;;;###autoload
797 (defalias 'calcFunc-trunc 'math-trunc)
799 ;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
801 ;; The variable math-floor-prec is local to math-floor, but used by
802 ;; math-floor-fancy in calc-arith.el, which is called by math-floor.
804 ;;;###autoload
805 (defun math-floor (a &optional math-floor-prec) ; [Public]
806 (cond (math-floor-prec
807 (require 'calc-ext)
808 (math-floor-special a math-floor-prec))
809 ((Math-integerp a) a)
810 ((Math-messy-integerp a) (math-trunc a))
811 ((Math-realp a)
812 (if (Math-negp a)
813 (math-add (math-trunc a) -1)
814 (math-trunc a)))
815 (t (require 'calc-ext)
816 (math-floor-fancy a))))
817 ;;;###autoload
818 (defalias 'calcFunc-floor 'math-floor)
821 ;;;###autoload
822 (defun math-imod (a b) ; [I I I] [Public]
823 (if (and (not (consp a)) (not (consp b)))
824 (if (= b 0)
825 (math-reject-arg a "*Division by zero")
826 (% a b))
827 (cdr (math-idivmod a b))))
830 ;;;###autoload
831 (defun calcFunc-inv (m)
832 (if (Math-vectorp m)
833 (progn
834 (require 'calc-ext)
835 (if (math-square-matrixp m)
836 (or (math-with-extra-prec 2 (math-matrix-inv-raw m))
837 (math-reject-arg m "*Singular matrix"))
838 (math-reject-arg m 'square-matrixp)))
839 (if (and
840 (require 'calc-arith)
841 (math-known-matrixp m))
842 (math-pow m -1)
843 (math-div 1 m))))
845 ;;;###autoload
846 (defun math-do-working (msg arg)
847 (or executing-kbd-macro
848 (progn
849 (calc-set-command-flag 'clear-message)
850 (if math-working-step
851 (if math-working-step-2
852 (setq msg (format "[%d/%d] %s"
853 math-working-step math-working-step-2 msg))
854 (setq msg (format "[%d] %s" math-working-step msg))))
855 (message "Working... %s = %s" msg
856 (math-showing-full-precision (math-format-number arg))))))
859 ;; Compute A modulo B, defined in terms of truncation toward minus infinity.
860 ;;;###autoload
861 (defun math-mod (a b) ; [R R R] [Public]
862 (cond ((and (Math-zerop a) (not (eq (car-safe a) 'mod))) a)
863 ((Math-zerop b)
864 (math-reject-arg a "*Division by zero"))
865 ((and (Math-natnump a) (Math-natnump b))
866 (math-imod a b))
867 ((and (Math-anglep a) (Math-anglep b))
868 (math-sub a (math-mul (math-floor (math-div a b)) b)))
869 (t (require 'calc-ext)
870 (math-mod-fancy a b))))
874 ;;; General exponentiation.
876 ;;;###autoload
877 (defun math-pow (a b) ; [O O N] [Public]
878 (cond ((equal b '(var nan var-nan))
880 ((Math-zerop a)
881 (if (and (Math-scalarp b) (Math-posp b))
882 (if (math-floatp b) (math-float a) a)
883 (require 'calc-ext)
884 (math-pow-of-zero a b)))
885 ((or (eq a 1) (eq b 1)) a)
886 ((or (equal a '(float 1 0)) (equal b '(float 1 0))) a)
887 ((Math-zerop b)
888 (if (Math-scalarp a)
889 (if (or (math-floatp a) (math-floatp b))
890 '(float 1 0) 1)
891 (require 'calc-ext)
892 (math-pow-zero a b)))
893 ((and (Math-integerp b) (or (Math-numberp a) (Math-vectorp a)))
894 (if (and (equal a '(float 1 1)) (integerp b))
895 (math-make-float 1 b)
896 (math-with-extra-prec 2
897 (math-ipow a b))))
899 (require 'calc-ext)
900 (math-pow-fancy a b))))
902 ;;;###autoload
903 (defun math-ipow (a n) ; [O O I] [Public]
904 (cond ((Math-integer-negp n)
905 (math-ipow (math-div 1 a) (Math-integer-neg n)))
906 ((not (consp n))
907 (if (and (Math-ratp a) (> n 20))
908 (math-iipow-show a n)
909 (math-iipow a n)))
910 ((math-evenp n)
911 (math-ipow (math-mul a a) (math-div2 n)))
913 (math-mul a (math-ipow (math-mul a a)
914 (math-div2 (math-add n -1)))))))
916 (defun math-iipow (a n) ; [O O S]
917 (cond ((= n 0) 1)
918 ((= n 1) a)
919 ((= (% n 2) 0) (math-iipow (math-mul a a) (/ n 2)))
920 (t (math-mul a (math-iipow (math-mul a a) (/ n 2))))))
922 (defun math-iipow-show (a n) ; [O O S]
923 (math-working "pow" a)
924 (let ((val (cond
925 ((= n 0) 1)
926 ((= n 1) a)
927 ((= (% n 2) 0) (math-iipow-show (math-mul a a) (/ n 2)))
928 (t (math-mul a (math-iipow-show (math-mul a a) (/ n 2)))))))
929 (math-working "pow" val)
930 val))
933 ;;;###autoload
934 (defun math-read-radix-digit (dig) ; [D S; Z S]
935 (if (> dig ?9)
936 (if (< dig ?A)
938 (- dig 55))
939 (if (>= dig ?0)
940 (- dig ?0)
941 nil)))
944 ;;; Bug reporting
946 ;;;###autoload
947 (defun report-calc-bug ()
948 "Report a bug in Calc, the GNU Emacs calculator.
949 Prompts for bug subject. Leaves you in a mail buffer."
950 (interactive)
951 (let ((reporter-prompt-for-summary-p t))
952 (reporter-submit-bug-report calc-bug-address "Calc"
953 nil nil nil
954 "Please describe exactly what actions triggered the bug and the
955 precise symptoms of the bug. If possible, include a backtrace by
956 doing ‘\\[toggle-debug-on-error]’, then reproducing the bug.
957 " )))
958 ;;;###autoload
959 (defalias 'calc-report-bug 'report-calc-bug)
961 (provide 'calc-misc)
963 ;; Local variables:
964 ;; generated-autoload-file: "calc-loaddefs.el"
965 ;; End:
967 ;;; calc-misc.el ends here