Commit | Line | Data |
---|---|---|
dac12d80 CW |
1 | ;;; calc-misc.el --- miscellaenous functions for Calc |
2 | ||
8f66f479 | 3 | ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
dac12d80 CW |
4 | |
5 | ;; Author: David Gillespie <daveg@synaptics.com> | |
6 | ;; Maintainer: Colin Walters <walters@debian.org> | |
136211a9 EZ |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
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. | |
16 | ||
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. | |
24 | ||
dac12d80 | 25 | ;;; Commentary: |
136211a9 | 26 | |
dac12d80 | 27 | ;;; Code: |
136211a9 EZ |
28 | |
29 | ;; This file is autoloaded from calc.el. | |
30 | (require 'calc) | |
31 | ||
32 | (require 'calc-macs) | |
33 | ||
34 | (defun calc-Need-calc-misc () nil) | |
35 | ||
36 | ||
37 | (defun calc-dispatch-help (arg) | |
38 | "M-# is a prefix key; follow it with one of these letters: | |
39 | ||
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. | |
51 | ||
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. | |
58 | ||
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. | |
67 | ||
68 | Documentation: | |
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. | |
72 | ||
73 | Miscellaneous: | |
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. | |
77 | ||
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)." | |
80 | (interactive "P") | |
81 | (calc-check-defines) | |
82 | (if calc-dispatch-help | |
83 | (progn | |
84 | (save-window-excursion | |
85 | (describe-function 'calc-dispatch-help) | |
86 | (let ((win (get-buffer-window "*Help*"))) | |
87 | (if win | |
88 | (let (key) | |
89 | (select-window win) | |
90 | (while (progn | |
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))) | |
95 | (condition-case err | |
96 | (if (memq (car key) '(? ?\C-v)) | |
97 | (scroll-up) | |
98 | (scroll-down)) | |
99 | (error (beep)))) | |
100 | (calc-unread-command (cdr key)))))) | |
101 | (calc-do-dispatch nil)) | |
102 | (let ((calc-dispatch-help t)) | |
60e4c5a7 | 103 | (calc-do-dispatch arg)))) |
136211a9 EZ |
104 | |
105 | ||
106 | (defun calc-big-or-small (arg) | |
107 | "Toggle Calc between full-screen and regular mode." | |
108 | (interactive "P") | |
109 | (let ((cwin (get-buffer-window "*Calculator*")) | |
110 | (twin (get-buffer-window "*Calc Trail*")) | |
111 | (kwin (get-buffer-window "*Calc Keypad*"))) | |
112 | (if cwin | |
113 | (setq calc-full-mode | |
114 | (if kwin | |
8f66f479 EZ |
115 | (and twin (eq (window-width twin) (frame-width))) |
116 | (eq (window-height cwin) (1- (frame-height)))))) | |
136211a9 EZ |
117 | (setq calc-full-mode (if arg |
118 | (> (prefix-numeric-value arg) 0) | |
119 | (not calc-full-mode))) | |
120 | (if kwin | |
121 | (progn | |
122 | (calc-quit) | |
123 | (calc-do-keypad calc-full-mode nil)) | |
124 | (if cwin | |
125 | (progn | |
126 | (calc-quit) | |
127 | (calc nil calc-full-mode nil)))) | |
128 | (message (if calc-full-mode | |
dac12d80 CW |
129 | "Now using full screen for Calc" |
130 | "Now using partial screen for Calc")))) | |
136211a9 EZ |
131 | |
132 | (defun calc-other-window () | |
133 | "Invoke the Calculator in another window." | |
134 | (interactive) | |
135 | (if (memq major-mode '(calc-mode calc-trail-mode)) | |
136 | (progn | |
137 | (other-window 1) | |
138 | (if (memq major-mode '(calc-mode calc-trail-mode)) | |
139 | (other-window 1))) | |
140 | (if (get-buffer-window "*Calculator*") | |
141 | (calc-quit) | |
142 | (let ((win (selected-window))) | |
60e4c5a7 | 143 | (calc nil win (interactive-p)))))) |
136211a9 EZ |
144 | |
145 | (defun another-calc () | |
146 | "Create another, independent Calculator buffer." | |
147 | (interactive) | |
148 | (if (eq major-mode 'calc-mode) | |
149 | (mapcar (function | |
150 | (lambda (v) | |
151 | (set-default v (symbol-value v)))) calc-local-var-list)) | |
152 | (set-buffer (generate-new-buffer "*Calculator*")) | |
153 | (pop-to-buffer (current-buffer)) | |
60e4c5a7 | 154 | (calc-mode)) |
136211a9 EZ |
155 | |
156 | ||
157 | ;;; Make an attempt to preserve the window configuration, while deleting | |
158 | ;;; windows on "bufs". Emacs 19's delete-window function will probably | |
159 | ;;; make this kludgery unnecessary, but Emacs 18's tendency to grow all | |
160 | ;;; windows on the screen to take up the slack from the deleted windows | |
161 | ;;; can be annoying when Calc was called during another multi-window | |
162 | ;;; application, such as GNUS. | |
163 | ||
164 | (defun calc-delete-windows-keep (&rest bufs) | |
165 | (if (one-window-p) | |
166 | (mapcar 'delete-windows-on bufs) | |
167 | (let* ((w (car calc-was-split)) | |
168 | (e (window-edges w)) | |
169 | (wins nil) | |
170 | w2 e2) | |
171 | (while (progn | |
172 | (setq w2 (previous-window w) | |
173 | e2 (window-edges w2)) | |
174 | (and (= (car e2) (car e)) | |
175 | (= (nth 2 e2) (nth 2 e)) | |
176 | (< (nth 1 e2) (nth 1 e)))) | |
177 | (setq w w2 e e2)) | |
178 | (setq w2 w e2 e) | |
179 | (while (progn | |
180 | (setq wins (cons (list w (nth 1 e) (window-buffer w) | |
181 | (window-point w) (window-start w)) | |
182 | wins) | |
183 | w (next-window w) | |
184 | e (window-edges w)) | |
185 | (and (not (eq w w2)) | |
186 | (= (car e2) (car e)) | |
187 | (= (nth 2 e2) (nth 2 e))))) | |
188 | (setq wins (nreverse wins)) | |
189 | (mapcar 'delete-windows-on bufs) | |
190 | (or (one-window-p) | |
191 | (let ((w wins) | |
192 | (main nil) | |
193 | (mainpos 0) | |
194 | (sel (if (window-point (nth 2 calc-was-split)) | |
195 | (nth 2 calc-was-split) | |
196 | (selected-window)))) | |
197 | (while w | |
198 | (if (window-point (car (car w))) | |
199 | (if main | |
200 | (delete-window (car (car w))) | |
201 | (setq main (car (car w)) | |
202 | mainpos (nth 1 (car w)) | |
203 | wins (cdr wins))) | |
204 | (setq wins (delq (car w) wins))) | |
205 | (setq w (cdr w))) | |
206 | (while wins | |
207 | (setq w (split-window main | |
208 | (if (eq main (car calc-was-split)) | |
209 | (nth 1 calc-was-split) | |
210 | (- (nth 1 (car wins)) mainpos)))) | |
211 | (set-window-buffer w (nth 2 (car wins))) | |
212 | (set-window-point w (nth 3 (car wins))) | |
213 | (set-window-start w (nth 4 (car wins))) | |
214 | (if (eq sel (car (car wins))) | |
215 | (select-window w)) | |
216 | (setq main w | |
217 | mainpos (nth 1 (car wins)) | |
218 | wins (cdr wins))) | |
219 | (if (window-point sel) | |
60e4c5a7 | 220 | (select-window sel))))))) |
136211a9 EZ |
221 | |
222 | ||
223 | (defun calc-info () | |
224 | "Run the Emacs Info system on the Calculator documentation." | |
225 | (interactive) | |
136211a9 | 226 | (select-window (get-largest-window)) |
b23d58ff | 227 | (info "Calc")) |
136211a9 EZ |
228 | |
229 | (defun calc-tutorial () | |
230 | "Run the Emacs Info system on the Calculator Tutorial." | |
231 | (interactive) | |
232 | (if (get-buffer-window "*Calculator*") | |
233 | (calc-quit)) | |
234 | (calc-info) | |
235 | (Info-goto-node "Interactive Tutorial") | |
236 | (calc-other-window) | |
60e4c5a7 | 237 | (message "Welcome to the Calc Tutorial!")) |
136211a9 EZ |
238 | |
239 | (defun calc-info-summary () | |
240 | "Run the Emacs Info system on the Calculator Summary." | |
241 | (interactive) | |
242 | (calc-info) | |
60e4c5a7 | 243 | (Info-goto-node "Summary")) |
136211a9 EZ |
244 | |
245 | (defun calc-help () | |
246 | (interactive) | |
247 | (let ((msgs (append | |
248 | '("Press `h' for complete help; press `?' repeatedly for a summary" | |
249 | "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit" | |
250 | "Letter keys: SHIFT + Undo, reDo; Keep-args; Inverse, Hyperbolic" | |
251 | "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB" | |
252 | "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi" | |
253 | "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro" | |
254 | "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)" | |
255 | "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)" | |
256 | "Other keys: ' (alg-entry), = (eval), ` (edit); M-RET (last-args)" | |
257 | "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)" | |
258 | "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)" | |
259 | "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)" | |
260 | "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)" | |
261 | "Prefix keys: Algebra, Binary/business, Convert, Display" | |
262 | "Prefix keys: Functions, Graphics, Help, J (select)" | |
263 | "Prefix keys: Kombinatorics/statistics, Modes, Store/recall" | |
264 | "Prefix keys: Trail/time, Units/statistics, Vector/matrix" | |
265 | "Prefix keys: Z (user), SHIFT + Z (define)" | |
266 | "Prefix keys: prefix + ? gives further help for that prefix") | |
267 | (list (format | |
268 | " Calc %s by Dave Gillespie, daveg@synaptics.com" | |
269 | calc-version))))) | |
270 | (if calc-full-help-flag | |
271 | msgs | |
272 | (if (or calc-inverse-flag calc-hyperbolic-flag) | |
273 | (if calc-inverse-flag | |
274 | (if calc-hyperbolic-flag | |
275 | (calc-inv-hyp-prefix-help) | |
276 | (calc-inverse-prefix-help)) | |
277 | (calc-hyperbolic-prefix-help)) | |
278 | (setq calc-help-phase | |
279 | (if (eq this-command last-command) | |
280 | (% (1+ calc-help-phase) (1+ (length msgs))) | |
281 | 0)) | |
282 | (let ((msg (nth calc-help-phase msgs))) | |
283 | (message "%s" (if msg | |
284 | (concat msg ":" | |
285 | (make-string (- (apply 'max | |
286 | (mapcar 'length | |
287 | msgs)) | |
288 | (length msg)) 32) | |
289 | " [?=MORE]") | |
60e4c5a7 | 290 | ""))))))) |
136211a9 EZ |
291 | |
292 | ||
293 | ||
294 | ||
295 | ;;;; Stack and buffer management. | |
296 | ||
297 | ||
298 | (defun calc-do-handle-whys () | |
299 | (setq calc-why (sort calc-next-why | |
300 | (function | |
301 | (lambda (x y) | |
302 | (and (eq (car x) '*) (not (eq (car y) '*)))))) | |
303 | calc-next-why nil) | |
304 | (if (and calc-why (or (eq calc-auto-why t) | |
305 | (and (eq (car (car calc-why)) '*) | |
306 | calc-auto-why))) | |
307 | (progn | |
308 | (calc-extensions) | |
309 | (calc-explain-why (car calc-why) | |
310 | (if (eq calc-auto-why t) | |
311 | (cdr calc-why) | |
312 | (if calc-auto-why | |
313 | (eq (car (nth 1 calc-why)) '*)))) | |
314 | (setq calc-last-why-command this-command) | |
60e4c5a7 | 315 | (calc-clear-command-flag 'clear-message)))) |
136211a9 EZ |
316 | |
317 | (defun calc-record-why (&rest stuff) | |
318 | (if (eq (car stuff) 'quiet) | |
319 | (setq stuff (cdr stuff)) | |
320 | (if (and (symbolp (car stuff)) | |
321 | (cdr stuff) | |
322 | (or (Math-objectp (nth 1 stuff)) | |
323 | (and (Math-vectorp (nth 1 stuff)) | |
324 | (math-constp (nth 1 stuff))) | |
325 | (math-infinitep (nth 1 stuff)))) | |
326 | (setq stuff (cons '* stuff)) | |
327 | (if (and (stringp (car stuff)) | |
328 | (string-match "\\`\\*" (car stuff))) | |
329 | (setq stuff (cons '* (cons (substring (car stuff) 1) | |
330 | (cdr stuff))))))) | |
331 | (setq calc-next-why (cons stuff calc-next-why)) | |
60e4c5a7 | 332 | nil) |
136211a9 EZ |
333 | |
334 | ;;; True if A is a constant or vector of constants. [P x] [Public] | |
335 | (defun math-constp (a) | |
336 | (or (Math-scalarp a) | |
337 | (and (memq (car a) '(sdev intv mod vec)) | |
338 | (progn | |
339 | (while (and (setq a (cdr a)) | |
340 | (or (Math-scalarp (car a)) ; optimization | |
341 | (math-constp (car a))))) | |
60e4c5a7 | 342 | (null a))))) |
136211a9 EZ |
343 | |
344 | ||
345 | (defun calc-roll-down-stack (n &optional m) | |
346 | (if (< n 0) | |
347 | (calc-roll-up-stack (- n) m) | |
348 | (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size))) | |
349 | (or m (setq m 1)) | |
350 | (and (> n 1) | |
351 | (< m n) | |
352 | (if (and calc-any-selections | |
353 | (not calc-use-selections)) | |
354 | (calc-roll-down-with-selections n m) | |
355 | (calc-pop-push-list n | |
356 | (append (calc-top-list m 1) | |
60e4c5a7 | 357 | (calc-top-list (- n m) (1+ m)))))))) |
136211a9 EZ |
358 | |
359 | (defun calc-roll-up-stack (n &optional m) | |
360 | (if (< n 0) | |
361 | (calc-roll-down-stack (- n) m) | |
362 | (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size))) | |
363 | (or m (setq m 1)) | |
364 | (and (> n 1) | |
365 | (< m n) | |
366 | (if (and calc-any-selections | |
367 | (not calc-use-selections)) | |
368 | (calc-roll-up-with-selections n m) | |
369 | (calc-pop-push-list n | |
370 | (append (calc-top-list (- n m) 1) | |
60e4c5a7 | 371 | (calc-top-list m (- n m -1)))))))) |
136211a9 EZ |
372 | |
373 | ||
374 | (defun calc-do-refresh () | |
375 | (if calc-hyperbolic-flag | |
376 | (progn | |
377 | (setq calc-display-dirty t) | |
378 | nil) | |
379 | (calc-refresh) | |
60e4c5a7 | 380 | t)) |
136211a9 EZ |
381 | |
382 | ||
383 | (defun calc-record-list (vals &optional prefix) | |
384 | (while vals | |
385 | (or (eq (car vals) 'top-of-stack) | |
386 | (progn | |
387 | (calc-record (car vals) prefix) | |
388 | (setq prefix "..."))) | |
60e4c5a7 | 389 | (setq vals (cdr vals)))) |
136211a9 EZ |
390 | |
391 | ||
392 | (defun calc-last-args-stub (arg) | |
393 | (interactive "p") | |
394 | (calc-extensions) | |
60e4c5a7 | 395 | (calc-last-args arg)) |
136211a9 EZ |
396 | |
397 | ||
398 | (defun calc-power (arg) | |
399 | (interactive "P") | |
400 | (calc-slow-wrapper | |
401 | (if (and calc-extensions-loaded | |
402 | (calc-is-inverse)) | |
403 | (calc-binary-op "root" 'calcFunc-nroot arg nil nil) | |
60e4c5a7 | 404 | (calc-binary-op "^" 'calcFunc-pow arg nil nil '^)))) |
136211a9 EZ |
405 | |
406 | (defun calc-mod (arg) | |
407 | (interactive "P") | |
408 | (calc-slow-wrapper | |
60e4c5a7 | 409 | (calc-binary-op "%" 'calcFunc-mod arg nil nil '%))) |
136211a9 EZ |
410 | |
411 | (defun calc-inv (arg) | |
412 | (interactive "P") | |
413 | (calc-slow-wrapper | |
60e4c5a7 | 414 | (calc-unary-op "inv" 'calcFunc-inv arg))) |
136211a9 EZ |
415 | |
416 | (defun calc-percent () | |
417 | (interactive) | |
418 | (calc-slow-wrapper | |
419 | (calc-pop-push-record-list | |
60e4c5a7 | 420 | 1 "%" (list (list 'calcFunc-percent (calc-top-n 1)))))) |
136211a9 EZ |
421 | |
422 | ||
423 | (defun calc-over (n) | |
424 | (interactive "P") | |
425 | (if n | |
426 | (calc-enter (- (prefix-numeric-value n))) | |
60e4c5a7 | 427 | (calc-enter -2))) |
136211a9 EZ |
428 | |
429 | ||
430 | (defun calc-pop-above (n) | |
431 | (interactive "P") | |
432 | (if n | |
433 | (calc-pop (- (prefix-numeric-value n))) | |
60e4c5a7 | 434 | (calc-pop -2))) |
136211a9 EZ |
435 | |
436 | (defun calc-roll-down (n) | |
437 | (interactive "P") | |
438 | (calc-wrapper | |
439 | (let ((nn (prefix-numeric-value n))) | |
440 | (cond ((null n) | |
441 | (calc-roll-down-stack 2)) | |
442 | ((> nn 0) | |
443 | (calc-roll-down-stack nn)) | |
444 | ((= nn 0) | |
445 | (calc-pop-push-list (calc-stack-size) | |
446 | (reverse | |
447 | (calc-top-list (calc-stack-size))))) | |
448 | (t | |
60e4c5a7 | 449 | (calc-roll-down-stack (calc-stack-size) (- nn))))))) |
136211a9 EZ |
450 | |
451 | (defun calc-roll-up (n) | |
452 | (interactive "P") | |
453 | (calc-wrapper | |
454 | (let ((nn (prefix-numeric-value n))) | |
455 | (cond ((null n) | |
456 | (calc-roll-up-stack 3)) | |
457 | ((> nn 0) | |
458 | (calc-roll-up-stack nn)) | |
459 | ((= nn 0) | |
460 | (calc-pop-push-list (calc-stack-size) | |
461 | (reverse | |
462 | (calc-top-list (calc-stack-size))))) | |
463 | (t | |
60e4c5a7 | 464 | (calc-roll-up-stack (calc-stack-size) (- nn))))))) |
136211a9 EZ |
465 | |
466 | ||
467 | ||
468 | ||
469 | ;;; Other commands. | |
470 | ||
471 | (defun calc-num-prefix-name (n) | |
472 | (cond ((eq n '-) "- ") | |
473 | ((equal n '(4)) "C-u ") | |
474 | ((consp n) (format "%d " (car n))) | |
475 | ((integerp n) (format "%d " n)) | |
60e4c5a7 | 476 | (t ""))) |
136211a9 EZ |
477 | |
478 | (defun calc-missing-key (n) | |
479 | "This is a placeholder for a command which needs to be loaded from calc-ext. | |
480 | When this key is used, calc-ext (the Calculator extensions module) will be | |
481 | loaded and the keystroke automatically re-typed." | |
482 | (interactive "P") | |
483 | (calc-extensions) | |
484 | (if (keymapp (key-binding (char-to-string last-command-char))) | |
485 | (message "%s%c-" (calc-num-prefix-name n) last-command-char)) | |
486 | (calc-unread-command) | |
60e4c5a7 | 487 | (setq prefix-arg n)) |
136211a9 EZ |
488 | |
489 | (defun calc-shift-Y-prefix-help () | |
490 | (interactive) | |
491 | (calc-extensions) | |
60e4c5a7 | 492 | (calc-do-prefix-help calc-Y-help-msgs "other" ?Y)) |
136211a9 EZ |
493 | |
494 | ||
495 | ||
496 | ||
497 | (defun calcDigit-letter () | |
498 | (interactive) | |
499 | (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*") | |
500 | (progn | |
501 | (setq last-command-char (upcase last-command-char)) | |
502 | (calcDigit-key)) | |
60e4c5a7 | 503 | (calcDigit-nondigit))) |
136211a9 EZ |
504 | |
505 | ||
506 | ;; A Lisp version of temp_minibuffer_message from minibuf.c. | |
507 | (defun calc-temp-minibuffer-message (m) | |
508 | (let ((savemax (point-max))) | |
509 | (save-excursion | |
510 | (goto-char (point-max)) | |
511 | (insert m)) | |
512 | (let ((okay nil)) | |
513 | (unwind-protect | |
514 | (progn | |
515 | (sit-for 2) | |
516 | (identity 1) ; this forces a call to QUIT; in bytecode.c. | |
517 | (setq okay t)) | |
518 | (progn | |
519 | (delete-region savemax (point-max)) | |
60e4c5a7 | 520 | (or okay (abort-recursive-edit))))))) |
136211a9 EZ |
521 | |
522 | ||
523 | (put 'math-with-extra-prec 'lisp-indent-hook 1) | |
524 | ||
525 | ||
526 | ;;; Concatenate two vectors, or a vector and an object. [V O O] [Public] | |
527 | (defun math-concat (v1 v2) | |
528 | (if (stringp v1) | |
529 | (concat v1 v2) | |
530 | (calc-extensions) | |
531 | (if (and (or (math-objvecp v1) (math-known-scalarp v1)) | |
532 | (or (math-objvecp v2) (math-known-scalarp v2))) | |
533 | (append (if (and (math-vectorp v1) | |
534 | (or (math-matrixp v1) | |
535 | (not (math-matrixp v2)))) | |
536 | v1 | |
537 | (list 'vec v1)) | |
538 | (if (and (math-vectorp v2) | |
539 | (or (math-matrixp v2) | |
540 | (not (math-matrixp v1)))) | |
541 | (cdr v2) | |
542 | (list v2))) | |
60e4c5a7 | 543 | (list '| v1 v2)))) |
136211a9 EZ |
544 | |
545 | ||
546 | ;;; True if A is zero. Works for un-normalized values. [P n] [Public] | |
547 | (defun math-zerop (a) | |
548 | (if (consp a) | |
549 | (cond ((memq (car a) '(bigpos bigneg)) | |
550 | (while (eq (car (setq a (cdr a))) 0)) | |
551 | (null a)) | |
552 | ((memq (car a) '(frac float polar mod)) | |
553 | (math-zerop (nth 1 a))) | |
554 | ((eq (car a) 'cplx) | |
555 | (and (math-zerop (nth 1 a)) (math-zerop (nth 2 a)))) | |
556 | ((eq (car a) 'hms) | |
557 | (and (math-zerop (nth 1 a)) | |
558 | (math-zerop (nth 2 a)) | |
559 | (math-zerop (nth 3 a))))) | |
60e4c5a7 | 560 | (eq a 0))) |
136211a9 EZ |
561 | |
562 | ||
563 | ;;; True if A is real and negative. [P n] [Public] | |
564 | ||
565 | (defun math-negp (a) | |
566 | (if (consp a) | |
567 | (cond ((eq (car a) 'bigpos) nil) | |
568 | ((eq (car a) 'bigneg) (cdr a)) | |
569 | ((memq (car a) '(float frac)) | |
570 | (Math-integer-negp (nth 1 a))) | |
571 | ((eq (car a) 'hms) | |
572 | (if (math-zerop (nth 1 a)) | |
573 | (if (math-zerop (nth 2 a)) | |
574 | (math-negp (nth 3 a)) | |
575 | (math-negp (nth 2 a))) | |
576 | (math-negp (nth 1 a)))) | |
577 | ((eq (car a) 'date) | |
578 | (math-negp (nth 1 a))) | |
579 | ((eq (car a) 'intv) | |
580 | (or (math-negp (nth 3 a)) | |
581 | (and (math-zerop (nth 3 a)) | |
582 | (memq (nth 1 a) '(0 2))))) | |
583 | ((equal a '(neg (var inf var-inf))) t)) | |
60e4c5a7 | 584 | (< a 0))) |
136211a9 EZ |
585 | |
586 | ;;; True if A is a negative number or an expression the starts with '-'. | |
587 | (defun math-looks-negp (a) ; [P x] [Public] | |
588 | (or (Math-negp a) | |
589 | (eq (car-safe a) 'neg) | |
590 | (and (memq (car-safe a) '(* /)) | |
591 | (or (math-looks-negp (nth 1 a)) | |
592 | (math-looks-negp (nth 2 a)))) | |
593 | (and (eq (car-safe a) '-) | |
60e4c5a7 | 594 | (math-looks-negp (nth 1 a))))) |
136211a9 EZ |
595 | |
596 | ||
597 | ;;; True if A is real and positive. [P n] [Public] | |
598 | (defun math-posp (a) | |
599 | (if (consp a) | |
600 | (cond ((eq (car a) 'bigpos) (cdr a)) | |
601 | ((eq (car a) 'bigneg) nil) | |
602 | ((memq (car a) '(float frac)) | |
603 | (Math-integer-posp (nth 1 a))) | |
604 | ((eq (car a) 'hms) | |
605 | (if (math-zerop (nth 1 a)) | |
606 | (if (math-zerop (nth 2 a)) | |
607 | (math-posp (nth 3 a)) | |
608 | (math-posp (nth 2 a))) | |
609 | (math-posp (nth 1 a)))) | |
610 | ((eq (car a) 'date) | |
611 | (math-posp (nth 1 a))) | |
612 | ((eq (car a) 'mod) | |
613 | (not (math-zerop (nth 1 a)))) | |
614 | ((eq (car a) 'intv) | |
615 | (or (math-posp (nth 2 a)) | |
616 | (and (math-zerop (nth 2 a)) | |
617 | (memq (nth 1 a) '(0 1))))) | |
618 | ((equal a '(var inf var-inf)) t)) | |
60e4c5a7 | 619 | (> a 0))) |
136211a9 | 620 | |
dac12d80 CW |
621 | (defalias 'math-fixnump 'integerp) |
622 | (defalias 'math-fixnatnump 'natnump) | |
136211a9 EZ |
623 | |
624 | ||
625 | ;;; True if A is an even integer. [P R R] [Public] | |
626 | (defun math-evenp (a) | |
627 | (if (consp a) | |
628 | (and (memq (car a) '(bigpos bigneg)) | |
629 | (= (% (nth 1 a) 2) 0)) | |
60e4c5a7 | 630 | (= (% a 2) 0))) |
136211a9 EZ |
631 | |
632 | ;;; Compute A / 2, for small or big integer A. [I i] | |
633 | ;;; If A is negative, type of truncation is undefined. | |
634 | (defun math-div2 (a) | |
635 | (if (consp a) | |
636 | (if (cdr a) | |
637 | (math-normalize (cons (car a) (math-div2-bignum (cdr a)))) | |
638 | 0) | |
60e4c5a7 | 639 | (/ a 2))) |
136211a9 EZ |
640 | |
641 | (defun math-div2-bignum (a) ; [l l] | |
642 | (if (cdr a) | |
643 | (cons (+ (/ (car a) 2) (* (% (nth 1 a) 2) 500)) | |
644 | (math-div2-bignum (cdr a))) | |
60e4c5a7 | 645 | (list (/ (car a) 2)))) |
136211a9 EZ |
646 | |
647 | ||
648 | ;;; Reject an argument to a calculator function. [Public] | |
649 | (defun math-reject-arg (&optional a p option) | |
650 | (if option | |
651 | (calc-record-why option p a) | |
652 | (if p | |
653 | (calc-record-why p a))) | |
60e4c5a7 | 654 | (signal 'wrong-type-argument (and a (if p (list p a) (list a))))) |
136211a9 EZ |
655 | |
656 | ||
657 | ;;; Coerce A to be an integer (by truncation toward zero). [I N] [Public] | |
658 | (defun math-trunc (a &optional prec) | |
659 | (cond (prec | |
660 | (calc-extensions) | |
661 | (math-trunc-special a prec)) | |
662 | ((Math-integerp a) a) | |
663 | ((Math-looks-negp a) | |
664 | (math-neg (math-trunc (math-neg a)))) | |
665 | ((eq (car a) 'float) | |
666 | (math-scale-int (nth 1 a) (nth 2 a))) | |
667 | (t (calc-extensions) | |
60e4c5a7 | 668 | (math-trunc-fancy a)))) |
dac12d80 | 669 | (defalias 'calcFunc-trunc 'math-trunc) |
136211a9 EZ |
670 | |
671 | ;;; Coerce A to be an integer (by truncation toward minus infinity). [I N] | |
672 | (defun math-floor (a &optional prec) ; [Public] | |
673 | (cond (prec | |
674 | (calc-extensions) | |
675 | (math-floor-special a prec)) | |
676 | ((Math-integerp a) a) | |
677 | ((Math-messy-integerp a) (math-trunc a)) | |
678 | ((Math-realp a) | |
679 | (if (Math-negp a) | |
680 | (math-add (math-trunc a) -1) | |
681 | (math-trunc a))) | |
682 | (t (calc-extensions) | |
60e4c5a7 | 683 | (math-floor-fancy a)))) |
dac12d80 | 684 | (defalias 'calcFunc-floor 'math-floor) |
136211a9 EZ |
685 | |
686 | ||
687 | (defun math-imod (a b) ; [I I I] [Public] | |
688 | (if (and (not (consp a)) (not (consp b))) | |
689 | (if (= b 0) | |
690 | (math-reject-arg a "*Division by zero") | |
691 | (% a b)) | |
60e4c5a7 | 692 | (cdr (math-idivmod a b)))) |
136211a9 EZ |
693 | |
694 | ||
695 | (defun calcFunc-inv (m) | |
696 | (if (Math-vectorp m) | |
697 | (progn | |
698 | (calc-extensions) | |
699 | (if (math-square-matrixp m) | |
700 | (or (math-with-extra-prec 2 (math-matrix-inv-raw m)) | |
701 | (math-reject-arg m "*Singular matrix")) | |
702 | (math-reject-arg m 'square-matrixp))) | |
60e4c5a7 | 703 | (math-div 1 m))) |
136211a9 EZ |
704 | |
705 | ||
706 | (defun math-do-working (msg arg) | |
8f66f479 | 707 | (or executing-kbd-macro |
136211a9 EZ |
708 | (progn |
709 | (calc-set-command-flag 'clear-message) | |
710 | (if math-working-step | |
711 | (if math-working-step-2 | |
712 | (setq msg (format "[%d/%d] %s" | |
713 | math-working-step math-working-step-2 msg)) | |
714 | (setq msg (format "[%d] %s" math-working-step msg)))) | |
715 | (message "Working... %s = %s" msg | |
60e4c5a7 | 716 | (math-showing-full-precision (math-format-number arg)))))) |
136211a9 EZ |
717 | |
718 | ||
719 | ;;; Compute A modulo B, defined in terms of truncation toward minus infinity. | |
720 | (defun math-mod (a b) ; [R R R] [Public] | |
721 | (cond ((and (Math-zerop a) (not (eq (car-safe a) 'mod))) a) | |
722 | ((Math-zerop b) | |
723 | (math-reject-arg a "*Division by zero")) | |
724 | ((and (Math-natnump a) (Math-natnump b)) | |
725 | (math-imod a b)) | |
726 | ((and (Math-anglep a) (Math-anglep b)) | |
727 | (math-sub a (math-mul (math-floor (math-div a b)) b))) | |
728 | (t (calc-extensions) | |
60e4c5a7 | 729 | (math-mod-fancy a b)))) |
136211a9 EZ |
730 | |
731 | ||
732 | ||
733 | ;;; General exponentiation. | |
734 | ||
735 | (defun math-pow (a b) ; [O O N] [Public] | |
736 | (cond ((equal b '(var nan var-nan)) | |
737 | b) | |
738 | ((Math-zerop a) | |
739 | (if (and (Math-scalarp b) (Math-posp b)) | |
740 | (if (math-floatp b) (math-float a) a) | |
741 | (calc-extensions) | |
742 | (math-pow-of-zero a b))) | |
743 | ((or (eq a 1) (eq b 1)) a) | |
744 | ((or (equal a '(float 1 0)) (equal b '(float 1 0))) a) | |
745 | ((Math-zerop b) | |
746 | (if (Math-scalarp a) | |
747 | (if (or (math-floatp a) (math-floatp b)) | |
748 | '(float 1 0) 1) | |
749 | (calc-extensions) | |
750 | (math-pow-zero a b))) | |
751 | ((and (Math-integerp b) (or (Math-numberp a) (Math-vectorp a))) | |
752 | (if (and (equal a '(float 1 1)) (integerp b)) | |
753 | (math-make-float 1 b) | |
754 | (math-with-extra-prec 2 | |
755 | (math-ipow a b)))) | |
756 | (t | |
757 | (calc-extensions) | |
60e4c5a7 | 758 | (math-pow-fancy a b)))) |
136211a9 EZ |
759 | |
760 | (defun math-ipow (a n) ; [O O I] [Public] | |
761 | (cond ((Math-integer-negp n) | |
762 | (math-ipow (math-div 1 a) (Math-integer-neg n))) | |
763 | ((not (consp n)) | |
764 | (if (and (Math-ratp a) (> n 20)) | |
765 | (math-iipow-show a n) | |
766 | (math-iipow a n))) | |
767 | ((math-evenp n) | |
768 | (math-ipow (math-mul a a) (math-div2 n))) | |
769 | (t | |
770 | (math-mul a (math-ipow (math-mul a a) | |
60e4c5a7 | 771 | (math-div2 (math-add n -1))))))) |
136211a9 EZ |
772 | |
773 | (defun math-iipow (a n) ; [O O S] | |
774 | (cond ((= n 0) 1) | |
775 | ((= n 1) a) | |
776 | ((= (% n 2) 0) (math-iipow (math-mul a a) (/ n 2))) | |
60e4c5a7 | 777 | (t (math-mul a (math-iipow (math-mul a a) (/ n 2)))))) |
136211a9 EZ |
778 | |
779 | (defun math-iipow-show (a n) ; [O O S] | |
780 | (math-working "pow" a) | |
781 | (let ((val (cond | |
782 | ((= n 0) 1) | |
783 | ((= n 1) a) | |
784 | ((= (% n 2) 0) (math-iipow-show (math-mul a a) (/ n 2))) | |
785 | (t (math-mul a (math-iipow-show (math-mul a a) (/ n 2))))))) | |
786 | (math-working "pow" val) | |
60e4c5a7 | 787 | val)) |
136211a9 EZ |
788 | |
789 | ||
790 | (defun math-read-radix-digit (dig) ; [D S; Z S] | |
791 | (if (> dig ?9) | |
792 | (if (< dig ?A) | |
793 | nil | |
794 | (- dig 55)) | |
795 | (if (>= dig ?0) | |
796 | (- dig ?0) | |
60e4c5a7 | 797 | nil))) |
136211a9 EZ |
798 | |
799 | ||
136211a9 EZ |
800 | ;;; Bug reporting |
801 | ||
802 | (defun report-calc-bug (topic) | |
803 | "Report a bug in Calc, the GNU Emacs calculator. | |
804 | Prompts for bug subject. Leaves you in a mail buffer." | |
805 | (interactive "sBug Subject: ") | |
8f148852 CW |
806 | (reporter-submit-bug-report calc-bug-address "Calc" '(calc-version) |
807 | nil nil | |
808 | "Please describe exactly what actions triggered the bug and the | |
809 | precise symptoms of the bug. If possible, include a backtrace by | |
810 | doing 'M-x toggle-debug-on-error', then reproducing the bug. | |
811 | " )) | |
dac12d80 | 812 | (defalias 'calc-report-bug 'report-calc-bug) |
136211a9 | 813 | |
60e4c5a7 | 814 | ;;; calc-misc.el ends here |