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