(calcDigit-key): Use minibuffer-prompt-end instead of point-min.
[bpt/emacs.git] / lisp / calc / calc.el
1 ;;; calc.el --- the GNU Emacs calculator
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
7 ;; Keywords: convenience, extensions
8 ;; Version: 2.02g
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY. No author or distributor
14 ;; accepts responsibility to anyone for the consequences of using it
15 ;; or for whether it serves any particular purpose or works at all,
16 ;; unless he says so in writing. Refer to the GNU Emacs General Public
17 ;; License for full details.
18
19 ;; Everyone is granted permission to copy, modify and redistribute
20 ;; GNU Emacs, but only under the conditions described in the
21 ;; GNU Emacs General Public License. A copy of this license is
22 ;; supposed to have been given to you along with GNU Emacs so you
23 ;; can know your rights and responsibilities. It should be in a
24 ;; file named COPYING. Among other things, the copyright notice
25 ;; and this notice must be preserved on all copies.
26
27 ;;; Commentary:
28
29 ;; Calc is split into many files. This file is the main entry point.
30 ;; This file includes autoload commands for various other basic Calc
31 ;; facilities. The more advanced features are based in calc-ext, which
32 ;; in turn contains autoloads for the rest of the Calc files. This
33 ;; odd set of interactions is designed to make Calc's loading time
34 ;; be as short as possible when only simple calculations are needed.
35
36 ;; Original author's address:
37 ;; Dave Gillespie, daveg@synaptics.com, uunet!synaptx!daveg.
38 ;; Synaptics, Inc., 2698 Orchard Parkway, San Jose, CA 95134.
39 ;;
40 ;; The old address daveg@csvax.cs.caltech.edu will continue to
41 ;; work for the foreseeable future.
42 ;;
43 ;; Bug reports and suggestions are always welcome! (Type M-x
44 ;; report-calc-bug to send them).
45
46 ;; All functions, macros, and Lisp variables defined here begin with one
47 ;; of the prefixes "math", "Math", or "calc", with the exceptions of
48 ;; "full-calc", "full-calc-keypad", "another-calc", "quick-calc",
49 ;; "report-calc-bug", and "defmath". User-accessible variables begin
50 ;; with "var-".
51
52 ;;; TODO:
53
54 ;; Fix rewrite mechanism to do less gratuitous rearrangement of terms.
55 ;; Implement a pattern-based "refers" predicate.
56 ;;
57 ;; Make it possible to Undo a selection command.
58 ;; Figure out how to allow selecting rows of matrices.
59 ;; If cursor was in selection before, move it after j n, j p, j L, etc.
60 ;; Consider reimplementing calc-delete-selection using rewrites.
61 ;;
62 ;; Implement line-breaking in non-flat compositions (is this desirable?).
63 ;; Implement matrix formatting with multi-line components.
64 ;;
65 ;; Have "Z R" define a user command based on a set of rewrite rules.
66 ;; Support "incf" and "decf" in defmath definitions.
67 ;; Have defmath generate calls to calc-binary-op or calc-unary-op.
68 ;; Make some way to define algebraic functions using keyboard macros.
69 ;;
70 ;; Allow calc-word-size=0 => Common Lisp-style signed bitwise arithmetic.
71 ;; Consider digamma function (and thus arb. prec. Euler's gamma constant).
72 ;; May as well make continued-fractions stuff available to the user.
73 ;;
74 ;; How about matrix eigenvalues, SVD, pseudo-inverse, etc.?
75 ;; Should cache matrix inverses as well as decompositions.
76 ;; If dividing by a non-square matrix, use least-squares automatically.
77 ;; Consider supporting matrix exponentials.
78 ;;
79 ;; Have ninteg detect and work around singularities at the endpoints.
80 ;; Use an adaptive subdivision algorithm for ninteg.
81 ;; Provide nsum and nprod to go along with ninteg.
82 ;;
83 ;; Handle TeX-mode parsing of \matrix{ ... } where ... contains braces.
84 ;; Support AmS-TeX's \{d,t,}frac, \{d,t,}binom notations.
85 ;; Format and parse sums and products in Eqn and Math modes.
86 ;;
87 ;; Get math-read-big-expr to read sums, products, etc.
88 ;; Change calc-grab-region to use math-read-big-expr.
89 ;; Have a way to define functions using := in Embedded Mode.
90 ;;
91 ;; Support polar plotting with GNUPLOT.
92 ;; Make a calc-graph-histogram function.
93 ;;
94 ;; Replace hokey formulas for complex functions with formulas designed
95 ;; to minimize roundoff while maintaining the proper branch cuts.
96 ;; Test accuracy of advanced math functions over whole complex plane.
97 ;; Extend Bessel functions to provide arbitrary precision.
98 ;; Extend advanced math functions to handle error forms and intervals.
99 ;; Provide a better implementation for math-sin-cos-raw.
100 ;; Provide a better implementation for math-hypot.
101 ;; Provide a better implementation for math-make-frac.
102 ;; Provide a better implementation for calcFunc-prfac.
103 ;; Provide a better implementation for calcFunc-factor.
104 ;;
105 ;; Provide more examples in the tutorial section of the manual.
106 ;; Cover in the tutorial: simplification modes, declarations,
107 ;; bitwise stuff, selections, matrix mapping, financial functions.
108 ;; Provide more Lisp programming examples in the manual.
109 ;; Finish the Internals section of the manual (and bring it up to date).
110 ;;
111 ;; Tim suggests adding spreadsheet-like features.
112 ;; Implement language modes for Gnuplot, Lisp, Ada, APL, ...?
113 ;;
114 ;; For atan series, if x > tan(pi/12) (about 0.268) reduce using the identity
115 ;; atan(x) = atan((x * sqrt(3) - 1) / (sqrt(3) + x)) + pi/6.
116 ;;
117 ;; A better integration algorithm:
118 ;; Use breadth-first instead of depth-first search, as follows:
119 ;; The integral cache allows unfinished integrals in symbolic notation
120 ;; on the righthand side. An entry with no unfinished integrals on the
121 ;; RHS is "complete"; references to it elsewhere are replaced by the
122 ;; integrated value. More than one cache entry for the same integral
123 ;; may exist, though if one becomes complete, the others may be deleted.
124 ;; The integrator works by using every applicable rule (such as
125 ;; substitution, parts, linearity, etc.) to generate possible righthand
126 ;; sides, all of which are entered into the cache. Now, as long as the
127 ;; target integral is not complete (and the time limit has not run out)
128 ;; choose an incomplete integral from the cache and, for every integral
129 ;; appearing in its RHS's, add those integrals to the cache using the
130 ;; same substitition, parts, etc. rules. The cache should be organized
131 ;; as a priority queue, choosing the "simplest" incomplete integral at
132 ;; each step, or choosing randomly among equally simple integrals.
133 ;; Simplicity equals small size, and few steps removed from the original
134 ;; target integral. Note that when the integrator finishes, incomplete
135 ;; integrals can be left in the cache, so the algorithm can start where
136 ;; it left off if another similar integral is later requested.
137 ;; Breadth-first search would avoid the nagging problem of, e.g., whether
138 ;; to use parts or substitution first, and which decomposition is best.
139 ;; All are tried, and any path that diverges will quickly be put on the
140 ;; back burner by the priority queue.
141 ;; Note: Probably a good idea to call math-simplify-extended before
142 ;; measuring a formula's simplicity.
143
144 ;; From: "Robert J. Chassell" <bob@rattlesnake.com>
145 ;; Subject: Re: fix for `Cannot open load file: calc-alg-3'
146 ;; To: walters@debian.org
147 ;; Date: Sat, 24 Nov 2001 21:44:21 +0000 (UTC)
148 ;;
149 ;; Could you add logistic curve fitting to the current list?
150 ;;
151 ;; (I guess the key binding for a logistic curve would have to be `s'
152 ;; since a logistic curve is an `s' curve; both `l' and `L' are already
153 ;; taken for logarithms.)
154 ;;
155 ;; Here is the current list for curve fitting;
156 ;;
157 ;; `1'
158 ;; Linear or multilinear. a + b x + c y + d z.
159 ;;
160 ;; `2-9'
161 ;; Polynomials. a + b x + c x^2 + d x^3.
162 ;;
163 ;; `e'
164 ;; Exponential. a exp(b x) exp(c y).
165 ;;
166 ;; `E'
167 ;; Base-10 exponential. a 10^(b x) 10^(c y).
168 ;;
169 ;; `x'
170 ;; Exponential (alternate notation). exp(a + b x + c y).
171 ;;
172 ;; `X'
173 ;; Base-10 exponential (alternate). 10^(a + b x + c y).
174 ;;
175 ;; `l'
176 ;; Logarithmic. a + b ln(x) + c ln(y).
177 ;;
178 ;; `L'
179 ;; Base-10 logarithmic. a + b log10(x) + c log10(y).
180 ;;
181 ;; `^'
182 ;; General exponential. a b^x c^y.
183 ;;
184 ;; `p'
185 ;; Power law. a x^b y^c.
186 ;;
187 ;; `q'
188 ;; Quadratic. a + b (x-c)^2 + d (x-e)^2.
189 ;;
190 ;; `g'
191 ;; Gaussian. (a / b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2).
192 ;;
193 ;;
194 ;; Logistic curves are used a great deal in ecology, and in predicting
195 ;; human actions, such as use of different kinds of energy in a country
196 ;; (wood, coal, oil, natural gas, etc.) or the number of scientific
197 ;; papers a person publishes, or the number of movies made.
198 ;;
199 ;; (The less information on which to base the curve, the higher the error
200 ;; rate. Theodore Modis ran some Monte Carlo simulations and produced
201 ;; what may be useful set of confidence levels for different amounts of
202 ;; initial information.)
203
204 ;;; Code:
205
206 (provide 'calc)
207 (require 'calc-macs)
208
209 ;;; The "###autoload" comment will be used by Emacs version 19 for
210 ;;; maintaining the loaddefs.el file automatically.
211
212 ;;;###autoload
213 (defvar calc-info-filename "calc.info"
214 "*File name in which to look for the Calculator's Info documentation.")
215
216 ;;;###autoload
217 (defvar calc-settings-file user-init-file
218 "*File in which to record permanent settings; default is `user-init-file'.")
219
220 ;;;###autoload
221 (defvar calc-autoload-directory nil
222 "Name of directory from which additional \".elc\" files for Calc should be
223 loaded. Should include a trailing \"/\".
224 If nil, use original installation directory.
225 This can safely be nil as long as the Calc files are on the load-path.")
226
227 ;;;###autoload
228 (defvar calc-gnuplot-name "gnuplot"
229 "*Name of GNUPLOT program, for calc-graph features.")
230
231 ;;;###autoload
232 (defvar calc-gnuplot-plot-command nil
233 "*Name of command for displaying GNUPLOT output; %s = file name to print.")
234
235 ;;;###autoload
236 (defvar calc-gnuplot-print-command "lp %s"
237 "*Name of command for printing GNUPLOT output; %s = file name to print.")
238
239 ;; Address of the author of Calc, for use by report-calc-bug.
240 (defvar calc-bug-address "walters@debian.org")
241
242 ;; If t, scan keymaps to find all DEL-like keys.
243 ;; if nil, only DEL itself is mapped to calc-pop.
244 (defvar calc-scan-for-dels t)
245
246 (defvar calc-extensions-loaded nil)
247
248 ;; Calculator stack.
249 ;; Entries are 3-lists: Formula, Height (in lines), Selection (or nil).
250 (defvar calc-stack '((top-of-stack 1 nil)))
251
252 ;; Index into calc-stack of "top" of stack.
253 ;; This is 1 unless calc-truncate-stack has been used.
254 ;;(defvar calc-stack-top 1)
255
256 ;; If non-nil, load the calc-ext module automatically when calc is loaded.
257 ;;(defvar calc-always-load-extensions nil)
258
259 ;; If non-nil, display line numbers in Calculator stack.
260 ;;(defvar calc-line-numbering t)
261
262 ;; If non-nil, break long values across multiple lines in Calculator stack.
263 ;;(defvar calc-line-breaking t)
264
265 ;; If nil, stack display is left-justified.
266 ;; If 'right, stack display is right-justified.
267 ;; If 'center, stack display is centered."
268 ;;(defvar calc-display-just nil)
269
270 ;; Horizontal origin of displayed stack entries.
271 ;; In left-justified mode, this is effectively indentation. (Default 0).
272 ;; In right-justified mode, this is effectively window width.
273 ;; In centered mode, center of stack entry is placed here.
274 ;;(defvar calc-display-origin nil)
275
276 ;; Radix for entry and display of numbers in calc-mode, 2-36.
277 ;;(defvar calc-number-radix 10)
278
279 ;; If non-nil, leading zeros are provided to pad integers to calc-word-size.
280 ;;(defvar calc-leading-zeros nil)
281
282 ;; If non-nil, group digits in large displayed integers by inserting spaces.
283 ;; If an integer, group that many digits at a time.
284 ;; If 't', use 4 for binary and hex, 3 otherwise.
285 ;;(defvar calc-group-digits nil)
286
287 ;; The character (in the form of a string) to be used for grouping digits.
288 ;; This is used only when calc-group-digits mode is on.
289 ;;(defvar calc-group-char ",")
290
291 ;; The character (in the form of a string) to be used as a decimal point.
292 ;;(defvar calc-point-char ".")
293
294 ;; Format of displayed fractions; a string of one or two of ":" or "/".
295 ;;(defvar calc-frac-format '(":" nil))
296
297 ;; If non-nil, prefer fractional over floating-point results.
298 ;;(defvar calc-prefer-frac nil)
299
300 ;; Format of displayed hours-minutes-seconds angles, a format string.
301 ;; String must contain three %s marks for hours, minutes, seconds respectively.
302 ;;(defvar calc-hms-format "%s@ %s' %s\"")
303
304 ;; Format of displayed date forms.
305 ;;(defvar calc-date-format '((H ":" mm ":" SS pp " ") Www " " Mmm " " D ", " YYYY))
306
307 ;; Format to use for display of floating-point numbers in calc-mode.
308 ;; Must be a list of one of the following forms:
309 ;; (float 0) Floating point format, display full precision.
310 ;; (float N) N > 0: Floating point format, at most N significant figures.
311 ;; (float -N) -N < 0: Floating point format, calc-internal-prec - N figs.
312 ;; (fix N) N >= 0: Fixed point format, N places after decimal point.
313 ;; (sci 0) Scientific notation, full precision.
314 ;; (sci N) N > 0: Scientific notation, N significant figures.
315 ;; (sci -N) -N < 0: Scientific notation, calc-internal-prec - N figs.
316 ;; (eng 0) Engineering notation, full precision.
317 ;; (eng N) N > 0: Engineering notation, N significant figures.
318 ;; (eng -N) -N < 0: Engineering notation, calc-internal-prec - N figs.
319 ;;(defvar calc-float-format '(float 0))
320
321 ;; Format to use when full precision must be displayed.
322 ;;(defvar calc-full-float-format '(float 0))
323
324 ;; Format to use for display of complex numbers in calc-mode. Must be one of:
325 ;; nil Use (x, y) form.
326 ;; i Use x + yi form.
327 ;; j Use x + yj form.
328 ;;(defvar calc-complex-format nil)
329
330 ;; Preferred form, either 'cplx or 'polar, for complex numbers.
331 ;;(defvar calc-complex-mode 'cplx)
332
333 ;; If nil, 1 / 0 is left unsimplified.
334 ;; If 0, 1 / 0 is changed to inf (zeros are considered positive).
335 ;; Otherwise, 1 / 0 is changed to uinf (undirected infinity).
336 ;;(defvar calc-infinite-mode nil)
337
338 ;; If non-nil, display vectors of byte-sized integers as strings.
339 ;;(defvar calc-display-strings nil)
340
341 ;; If nil, vector elements are left-justified.
342 ;; If 'right, vector elements are right-justified.
343 ;; If 'center, vector elements are centered."
344 ;;(defvar calc-matrix-just 'center)
345
346 ;; If non-nil, display vectors one element per line.
347 ;;(defvar calc-break-vectors nil)
348
349 ;; If non-nil, display long vectors in full. If nil, use abbreviated form.
350 ;;(defvar calc-full-vectors t)
351
352 ;; If non-nil, display long vectors in full in the trail.
353 ;;(defvar calc-full-trail-vectors t)
354
355 ;; If non-nil, separate elements of displayed vectors with this string.
356 ;;(defvar calc-vector-commas ",")
357
358 ;; If non-nil, surround displayed vectors with these characters.
359 ;;(defvar calc-vector-brackets "[]")
360
361 ;; A list of code-letter symbols that control "big" matrix display.
362 ;; If 'R is present, display inner brackets for matrices.
363 ;; If 'O is present, display outer brackets for matrices (above/below).
364 ;; If 'C is present, display outer brackets for matrices (centered).
365 ;;(defvar calc-matrix-brackets '(R O))
366
367 ;; Language or format for entry and display of stack values. Must be one of:
368 ;; nil Use standard Calc notation.
369 ;; flat Use standard Calc notation, one-line format.
370 ;; big Display formulas in 2-d notation (enter w/std notation).
371 ;; unform Use unformatted display: add(a, mul(b,c)).
372 ;; c Use C language notation.
373 ;; pascal Use Pascal language notation.
374 ;; fortran Use Fortran language notation.
375 ;; tex Use TeX notation.
376 ;; eqn Use eqn notation.
377 ;; math Use Mathematica(tm) notation.
378 ;; maple Use Maple notation.
379 ;;(defvar calc-language nil)
380
381 ;; Numeric prefix argument for the command that set calc-language.
382 ;;(defvar calc-language-option nil)
383
384 ;; Open-parenthesis string for function call notation.
385 ;;(defvar calc-function-open "(")
386
387 ;; Close-parenthesis string for function call notation.
388 ;;(defvar calc-function-close ")")
389
390 ;; Function through which to pass strings after formatting.
391 ;;(defvar calc-language-output-filter nil)
392
393 ;; Function through which to pass strings before parsing.
394 ;;(defvar calc-language-input-filter nil)
395
396 ;; Formatting function used for non-decimal numbers.
397 ;;(defvar calc-radix-formatter nil)
398
399 ;; Label to display at left of formula.
400 ;;(defvar calc-left-label "")
401
402 ;; Label to display at right of formula.
403 ;;(defvar calc-right-label "")
404
405 ;; Minimum number of bits per word, if any, for binary operations in calc-mode.
406 ;;(defvar calc-word-size 32)
407
408 ;; Most recently used value of M in a modulo form.
409 ;;(defvar calc-previous-modulo nil)
410
411 ;; Type of simplification applied to results.
412 ;; If 'none, results are not simplified when pushed on the stack.
413 ;; If 'num, functions are simplified only when args are constant.
414 ;; If nil, only fast simplifications are applied.
415 ;; If 'binary, math-clip is applied if appropriate.
416 ;; If 'alg, math-simplify is applied.
417 ;; If 'ext, math-simplify-extended is applied.
418 ;; If 'units, math-simplify-units is applied.
419 ;;(defvar calc-simplify-mode nil)
420
421 ;; If non-nil, recompute evalto's automatically when necessary.
422 ;;(defvar calc-auto-recompute t)
423
424 ;; If non-nil, display shows unformatted Lisp exprs. (For debugging)
425 ;;(defvar calc-display-raw nil)
426
427 ;; Number of digits of internal precision for calc-mode calculations.
428 ;;(defvar calc-internal-prec 12)
429
430 ;; If non-nil, next operation is Inverse.
431 ;;(defvar calc-inverse-flag nil)
432
433 ;; If non-nil, next operation is Hyperbolic.
434 ;;(defvar calc-hyperbolic-flag nil)
435
436 ;; If non-nil, next operation should not remove its arguments from stack.
437 ;;(defvar calc-keep-args-flag nil)
438
439 ;; If deg, angles are in degrees; if rad, angles are in radians.
440 ;; If hms, angles are in degrees-minutes-seconds.
441 ;;(defvar calc-angle-mode 'deg)
442
443 ;; If non-nil, numeric entry accepts whole algebraic expressions.
444 ;; If nil, algebraic expressions must be preceded by "'".
445 ;;(defvar calc-algebraic-mode nil)
446
447 ;; Like calc-algebraic-mode except only affects ( and [ keys.
448 ;;(defvar calc-incomplete-algebraic-mode nil)
449
450 ;; If non-nil, inexact numeric computations like sqrt(2) are postponed.
451 ;; If nil, computations on numbers always yield numbers where possible.
452 ;;(defvar calc-symbolic-mode nil)
453
454 ;; If 'matrix, variables are assumed to be matrix-valued.
455 ;; If a number, variables are assumed to be NxN matrices.
456 ;; If 'scalar, variables are assumed to be scalar-valued.
457 ;; If nil, symbolic math routines make no assumptions about variables.
458 ;;(defvar calc-matrix-mode nil)
459
460 ;; If non-nil, shifted letter keys are prefix keys rather than normal meanings.
461 ;;(defvar calc-shift-prefix nil)
462
463 ;; Initial height of Calculator window.
464 ;;(defvar calc-window-height 7)
465
466 ;; If non-nil, M-x calc creates a window to display Calculator trail.
467 ;;(defvar calc-display-trail t)
468
469 ;; If non-nil, selected sub-formulas are shown by obscuring rest of formula.
470 ;; If nil, selected sub-formulas are highlighted by obscuring the sub-formulas.
471 ;;(defvar calc-show-selections t)
472
473 ;; If non-nil, commands operate only on selected portions of formulas.
474 ;; If nil, selections displayed but ignored.
475 ;;(defvar calc-use-selections t)
476
477 ;; If non-nil, selection hides deep structure of associative formulas.
478 ;;(defvar calc-assoc-selections t)
479
480 ;; If non-nil, display "Working..." for potentially slow Calculator commands.
481 ;;(defvar calc-display-working-message 'lots)
482
483 ;; If non-nil, automatically execute a "why" command to explain odd results.
484 ;;(defvar calc-auto-why nil)
485
486 ;; If non-nil, display timing information on each slow command.
487 ;;(defvar calc-timing nil)
488
489 ;; Floating-point numbers with this positive exponent or higher above the
490 ;; current precision are displayed in scientific notation in calc-mode.
491 (defvar calc-display-sci-high 0)
492
493 ;; Floating-point numbers with this negative exponent or lower are displayed
494 ;; scientific notation in calc-mode.
495 (defvar calc-display-sci-low -3)
496
497
498 ;; List of used-defined strings to append to Calculator mode line.
499 (defvar calc-other-modes nil)
500
501 ;; List of strings for Y prefix help.
502 (defvar calc-Y-help-msgs nil)
503
504 ;; t if calc-settings-file has been loaded yet.
505 (defvar calc-loaded-settings-file nil)
506
507
508
509 (defconst calc-mode-var-list '((calc-always-load-extensions nil)
510 (calc-mode-save-mode local)
511 (calc-line-numbering t)
512 (calc-line-breaking t)
513 (calc-display-just nil)
514 (calc-display-origin nil)
515 (calc-left-label "")
516 (calc-right-label "")
517 (calc-number-radix 10)
518 (calc-leading-zeros nil)
519 (calc-group-digits nil)
520 (calc-group-char ",")
521 (calc-point-char ".")
522 (calc-frac-format (":" nil))
523 (calc-prefer-frac nil)
524 (calc-hms-format "%s@ %s' %s\"")
525 (calc-date-format ((H ":" mm C SS pp " ")
526 Www " " Mmm " " D ", " YYYY))
527 (calc-standard-date-formats
528 ("N"
529 "<H:mm:SSpp >Www Mmm D, YYYY"
530 "D Mmm YYYY<, h:mm:SS>"
531 "Www Mmm BD< hh:mm:ss> YYYY"
532 "M/D/Y< H:mm:SSpp>"
533 "D.M.Y< h:mm:SS>"
534 "M-D-Y< H:mm:SSpp>"
535 "D-M-Y< h:mm:SS>"
536 "j<, h:mm:SS>"
537 "YYddd< hh:mm:ss>"))
538 (calc-float-format (float 0))
539 (calc-full-float-format (float 0))
540 (calc-complex-format nil)
541 (calc-matrix-just center)
542 (calc-full-vectors t)
543 (calc-full-trail-vectors t)
544 (calc-break-vectors nil)
545 (calc-vector-commas ",")
546 (calc-vector-brackets "[]")
547 (calc-matrix-brackets (R O))
548 (calc-complex-mode cplx)
549 (calc-infinite-mode nil)
550 (calc-display-strings nil)
551 (calc-simplify-mode nil)
552 (calc-auto-recompute t)
553 (calc-word-size 32)
554 (calc-previous-modulo nil)
555 (calc-display-raw nil)
556 (calc-internal-prec 12)
557 (calc-angle-mode deg)
558 (calc-algebraic-mode nil)
559 (calc-incomplete-algebraic-mode nil)
560 (calc-symbolic-mode nil)
561 (calc-matrix-mode nil)
562 (calc-autorange-units nil)
563 (calc-shift-prefix nil)
564 (calc-window-height 7)
565 (calc-was-keypad-mode nil)
566 (calc-full-mode nil)
567 (calc-language nil)
568 (calc-language-option nil)
569 (calc-user-parse-tables nil)
570 (calc-show-selections t)
571 (calc-use-selections t)
572 (calc-assoc-selections t)
573 (calc-display-trail t)
574 (calc-display-working-message lots)
575 (calc-auto-why 'maybe)
576 (calc-timing nil)
577 (calc-gnuplot-default-device "default")
578 (calc-gnuplot-default-output "STDOUT")
579 (calc-gnuplot-print-device "postscript")
580 (calc-gnuplot-print-output "auto")
581 (calc-gnuplot-geometry nil)
582 (calc-graph-default-resolution 15)
583 (calc-graph-default-resolution-3d 5)
584 (calc-invocation-macro nil)))
585
586 (defconst calc-local-var-list '(calc-stack
587 calc-stack-top
588 calc-undo-list
589 calc-redo-list
590 calc-always-load-extensions
591 calc-mode-save-mode
592 calc-display-raw
593 calc-line-numbering
594 calc-line-breaking
595 calc-display-just
596 calc-display-origin
597 calc-left-label
598 calc-right-label
599 calc-auto-why
600 calc-algebraic-mode
601 calc-incomplete-algebraic-mode
602 calc-symbolic-mode
603 calc-matrix-mode
604 calc-inverse-flag
605 calc-hyperbolic-flag
606 calc-keep-args-flag
607 calc-angle-mode
608 calc-number-radix
609 calc-leading-zeros
610 calc-group-digits
611 calc-group-char
612 calc-point-char
613 calc-frac-format
614 calc-prefer-frac
615 calc-hms-format
616 calc-date-format
617 calc-standard-date-formats
618 calc-float-format
619 calc-full-float-format
620 calc-complex-format
621 calc-matrix-just
622 calc-full-vectors
623 calc-full-trail-vectors
624 calc-break-vectors
625 calc-vector-commas
626 calc-vector-brackets
627 calc-matrix-brackets
628 calc-complex-mode
629 calc-infinite-mode
630 calc-display-strings
631 calc-simplify-mode
632 calc-auto-recompute
633 calc-autorange-units
634 calc-show-plain
635 calc-show-selections
636 calc-use-selections
637 calc-assoc-selections
638 calc-word-size
639 calc-internal-prec))
640
641
642 (defun calc-init-base ()
643
644 ;; Verify that Calc is running on the right kind of system.
645 (setq calc-emacs-type-epoch (and (fboundp 'epoch::version) epoch::version)
646 calc-emacs-type-19 (not (or calc-emacs-type-epoch
647 (string-lessp emacs-version "19")))
648 calc-emacs-type-lucid (not (not (string-match "Lucid" emacs-version)))
649 calc-emacs-type-gnu19 (and calc-emacs-type-19
650 (not calc-emacs-type-lucid)))
651
652 ;; Set up the standard keystroke (M-#) to run the Calculator, if that key
653 ;; has not yet been bound to anything. For best results, the user should
654 ;; do this before Calc is even loaded, so that M-# can auto-load Calc.
655 (or (global-key-binding "\e#")
656 (global-set-key "\e#" 'calc-dispatch))
657
658 ;; Set up the autoloading linkage.
659 (let ((name (and (fboundp 'calc-dispatch)
660 (eq (car-safe (symbol-function 'calc-dispatch)) 'autoload)
661 (nth 1 (symbol-function 'calc-dispatch))))
662 (p load-path))
663
664 ;; If Calc files exist on the load-path, we're all set.
665 (while (and p (not (file-exists-p
666 (expand-file-name "calc-misc.elc" (car p)))))
667 (setq p (cdr p)))
668 (or p
669
670 ;; If Calc is autoloaded using a path name, look there for Calc files.
671 ;; This works for both relative ("calc/calc.elc") and absolute paths.
672 (and name (file-name-directory name)
673 (let ((p2 load-path)
674 (name2 (concat (file-name-directory name)
675 "calc-misc.elc")))
676 (while (and p2 (not (file-exists-p
677 (expand-file-name name2 (car p2)))))
678 (setq p2 (cdr p2)))
679 (if p2
680 (setq load-path (nconc load-path
681 (list
682 (directory-file-name
683 (file-name-directory
684 (expand-file-name
685 name (car p2))))))))))
686
687 ;; If calc-autoload-directory is given, use that (and hope it works!).
688 (and calc-autoload-directory
689 (not (equal calc-autoload-directory ""))
690 (setq load-path (nconc load-path
691 (list (directory-file-name
692 calc-autoload-directory)))))))
693
694 ;; The following modes use specially-formatted data.
695 (put 'calc-mode 'mode-class 'special)
696 (put 'calc-trail-mode 'mode-class 'special)
697
698 ;; Define "inexact-result" as an e-lisp error symbol.
699 (put 'inexact-result 'error-conditions '(error inexact-result calc-error))
700 (put 'inexact-result 'error-message "Calc internal error (inexact-result)")
701
702 ;; Define "math-overflow" and "math-underflow" as e-lisp error symbols.
703 (put 'math-overflow 'error-conditions '(error math-overflow calc-error))
704 (put 'math-overflow 'error-message "Floating-point overflow occurred")
705 (put 'math-underflow 'error-conditions '(error math-underflow calc-error))
706 (put 'math-underflow 'error-message "Floating-point underflow occurred")
707
708 (setq calc-version "2.02g"
709 calc-version-date "Mon Nov 19 2001"
710 calc-trail-pointer nil ; "Current" entry in trail buffer.
711 calc-trail-overlay nil ; Value of overlay-arrow-string.
712 calc-was-split nil ; Had multiple windows before Calc.
713 calc-undo-list nil ; List of previous operations for undo.
714 calc-redo-list nil ; List of recent undo operations.
715 calc-main-buffer nil ; Pointer to Calculator buffer.
716 calc-trail-buffer nil ; Pointer to Calc Trail buffer.
717 calc-why nil ; Explanations of most recent errors.
718 calc-next-why nil
719 calc-inverse-flag nil
720 calc-hyperbolic-flag nil
721 calc-keep-args-flag nil
722 calc-function-open "("
723 calc-function-close ")"
724 calc-language-output-filter nil
725 calc-language-input-filter nil
726 calc-radix-formatter nil
727 calc-last-kill nil ; Last number killed in calc-mode.
728 calc-previous-alg-entry nil ; Previous algebraic entry.
729 calc-dollar-values nil ; Values to be used for '$'.
730 calc-dollar-used nil ; Highest order of '$' that occurred.
731 calc-hashes-used nil ; Highest order of '#' that occurred.
732 calc-quick-prev-results nil ; Previous results from Quick Calc.
733 calc-said-hello nil ; Has welcome message been said yet?
734 calc-executing-macro nil ; Kbd macro executing from "K" key.
735 calc-any-selections nil ; Nil means no selections present.
736 calc-help-phase 0 ; Count of consecutive "?" keystrokes.
737 calc-full-help-flag nil ; Executing calc-full-help?
738 calc-refresh-count 0 ; Count of calc-refresh calls.
739 calc-display-dirty nil
740 calc-prepared-composition nil
741 calc-selection-cache-default-entry nil
742 calc-embedded-info nil
743 calc-embedded-active nil
744 calc-standalone-flag nil
745 var-EvalRules nil
746 math-eval-rules-cache-tag t
747 math-radix-explicit-format t
748 math-expr-function-mapping nil
749 math-expr-variable-mapping nil
750 math-read-expr-quotes nil
751 math-working-step nil
752 math-working-step-2 nil
753 var-i '(special-const (math-imaginary 1))
754 var-pi '(special-const (math-pi))
755 var-e '(special-const (math-e))
756 var-phi '(special-const (math-phi))
757 var-gamma '(special-const (math-gamma-const))
758 var-Modes '(special-const (math-get-modes-vec)))
759
760 (mapcar (function (lambda (v) (or (boundp (car v)) (set (car v) (nth 1 v)))))
761 calc-mode-var-list)
762 (mapcar (function (lambda (v) (or (boundp v) (set v nil))))
763 calc-local-var-list)
764
765 (unless (boundp 'calc-mode-map)
766 (setq calc-mode-map (make-keymap))
767 (suppress-keymap calc-mode-map t)
768 (define-key calc-mode-map "+" 'calc-plus)
769 (define-key calc-mode-map "-" 'calc-minus)
770 (define-key calc-mode-map "*" 'calc-times)
771 (define-key calc-mode-map "/" 'calc-divide)
772 (define-key calc-mode-map "%" 'calc-mod)
773 (define-key calc-mode-map "&" 'calc-inv)
774 (define-key calc-mode-map "^" 'calc-power)
775 (define-key calc-mode-map "\M-%" 'calc-percent)
776 (define-key calc-mode-map "e" 'calcDigit-start)
777 (define-key calc-mode-map "i" 'calc-info)
778 (define-key calc-mode-map "n" 'calc-change-sign)
779 (define-key calc-mode-map "q" 'calc-quit)
780 (define-key calc-mode-map "Y" 'nil)
781 (define-key calc-mode-map "Y?" 'calc-shift-Y-prefix-help)
782 (define-key calc-mode-map "?" 'calc-help)
783 (define-key calc-mode-map " " 'calc-enter)
784 (define-key calc-mode-map "'" 'calc-algebraic-entry)
785 (define-key calc-mode-map "$" 'calc-auto-algebraic-entry)
786 (define-key calc-mode-map "\"" 'calc-auto-algebraic-entry)
787 (define-key calc-mode-map "\t" 'calc-roll-down)
788 (define-key calc-mode-map "\M-\t" 'calc-roll-up)
789 (define-key calc-mode-map "\C-m" 'calc-enter)
790 (define-key calc-mode-map "\M-\C-m" 'calc-last-args-stub)
791 (define-key calc-mode-map "\C-j" 'calc-over)
792
793 (mapcar (function
794 (lambda (x)
795 (define-key calc-mode-map (char-to-string x) 'undefined)))
796 "lOW")
797 (mapcar (function
798 (lambda (x)
799 (define-key calc-mode-map (char-to-string x)
800 'calc-missing-key)))
801 (concat "ABCDEFGHIJKLMNPQRSTUVXZabcdfghjkmoprstuvwxyz"
802 ":\\|!()[]<>{},;=~`\C-k\M-k\C-w\M-w\C-y\C-_"))
803 (mapcar (function
804 (lambda (x)
805 (define-key calc-mode-map (char-to-string x) 'calcDigit-start)))
806 "_0123456789.#@")
807
808 (setq calc-digit-map (make-keymap))
809 (if calc-emacs-type-lucid
810 (map-keymap (function
811 (lambda (keys bind)
812 (define-key calc-digit-map keys
813 (if (eq bind 'undefined)
814 'undefined 'calcDigit-nondigit))))
815 calc-mode-map)
816 (let ((cmap (if calc-emacs-type-19 (nth 1 calc-mode-map) calc-mode-map))
817 (dmap (if calc-emacs-type-19 (nth 1 calc-digit-map)
818 calc-digit-map))
819 (i 0))
820 (while (< i 128)
821 (aset dmap i
822 (if (eq (aref cmap i) 'undefined)
823 'undefined 'calcDigit-nondigit))
824 (setq i (1+ i)))))
825 (mapcar (function
826 (lambda (x)
827 (define-key calc-digit-map (char-to-string x)
828 'calcDigit-key)))
829 "_0123456789.e+-:n#@oh'\"mspM")
830 (mapcar (function
831 (lambda (x)
832 (define-key calc-digit-map (char-to-string x)
833 'calcDigit-letter)))
834 "abcdfgijklqrtuvwxyzABCDEFGHIJKLNOPQRSTUVWXYZ")
835 (define-key calc-digit-map "'" 'calcDigit-algebraic)
836 (define-key calc-digit-map "`" 'calcDigit-edit)
837 (define-key calc-digit-map "\C-g" 'abort-recursive-edit)
838
839 (mapcar (function
840 (lambda (x)
841 (condition-case err
842 (progn
843 (define-key calc-digit-map x 'calcDigit-backspace)
844 (define-key calc-mode-map x 'calc-pop)
845 (define-key calc-mode-map
846 (if (vectorp x)
847 (if calc-emacs-type-lucid
848 (if (= (length x) 1)
849 (vector (if (consp (aref x 0))
850 (cons 'meta (aref x 0))
851 (list 'meta (aref x 0))))
852 "\e\C-d")
853 (vconcat "\e" x))
854 (concat "\e" x))
855 'calc-pop-above))
856 (error nil))))
857 (if calc-scan-for-dels
858 (append (where-is-internal 'delete-backward-char global-map)
859 (where-is-internal 'backward-delete-char global-map)
860 '("\C-d"))
861 '("\177" "\C-d")))
862
863 (setq calc-dispatch-map (make-keymap))
864 (mapcar (function
865 (lambda (x)
866 (define-key calc-dispatch-map (char-to-string (car x)) (cdr x))
867 (if (string-match "abcdefhijklnopqrstuwxyz"
868 (char-to-string (car x)))
869 (define-key calc-dispatch-map
870 (char-to-string (- (car x) ?a -1)) (cdr x)))
871 (define-key calc-dispatch-map (format "\e%c" (car x)) (cdr x))))
872 '( ( ?a . calc-embedded-activate )
873 ( ?b . calc-big-or-small )
874 ( ?c . calc )
875 ( ?d . calc-embedded-duplicate )
876 ( ?e . calc-embedded )
877 ( ?f . calc-embedded-new-formula )
878 ( ?g . calc-grab-region )
879 ( ?h . calc-dispatch-help )
880 ( ?i . calc-info )
881 ( ?j . calc-embedded-select )
882 ( ?k . calc-keypad )
883 ( ?l . calc-load-everything )
884 ( ?m . read-kbd-macro )
885 ( ?n . calc-embedded-next )
886 ( ?o . calc-other-window )
887 ( ?p . calc-embedded-previous )
888 ( ?q . quick-calc )
889 ( ?r . calc-grab-rectangle )
890 ( ?s . calc-info-summary )
891 ( ?t . calc-tutorial )
892 ( ?u . calc-embedded-update-formula )
893 ( ?w . calc-embedded-word )
894 ( ?x . calc-quit )
895 ( ?y . calc-copy-to-buffer )
896 ( ?z . calc-user-invocation )
897 ( ?= . calc-embedded-update-formula )
898 ( ?\' . calc-embedded-new-formula )
899 ( ?\` . calc-embedded-edit )
900 ( ?: . calc-grab-sum-down )
901 ( ?_ . calc-grab-sum-across )
902 ( ?0 . calc-reset )
903 ( ?# . calc-same-interface )
904 ( ?? . calc-dispatch-help ) ))
905 )
906
907 (autoload 'calc-extensions "calc-ext")
908 (autoload 'calc-need-macros "calc-macs")
909
910 ;;;; (Autoloads here)
911 (mapcar (function (lambda (x)
912 (mapcar (function (lambda (func)
913 (autoload func (car x)))) (cdr x))))
914 '(
915
916 ("calc-aent" calc-Need-calc-aent calc-alg-digit-entry calc-alg-entry
917 calc-check-user-syntax calc-do-alg-entry calc-do-calc-eval
918 calc-do-quick-calc calc-match-user-syntax math-build-parse-table
919 math-find-user-tokens math-read-expr-list math-read-exprs math-read-if
920 math-read-token math-remove-dashes)
921
922 ("calc-misc" calc-Need-calc-misc calc-delete-windows-keep
923 calc-do-handle-whys calc-do-refresh calc-num-prefix-name
924 calc-record-list calc-record-why calc-report-bug calc-roll-down-stack
925 calc-roll-up-stack calc-temp-minibuffer-message calcFunc-floor
926 calcFunc-inv calcFunc-trunc math-concat math-constp math-div2
927 math-div2-bignum math-do-working math-evenp math-fixnatnump
928 math-fixnump math-floor math-imod math-ipow math-looks-negp math-mod
929 math-negp math-posp math-pow math-read-radix-digit math-reject-arg
930 math-trunc math-zerop)
931
932 ))
933
934 (mapcar (function (lambda (x)
935 (mapcar (function (lambda (cmd)
936 (autoload cmd (car x) nil t))) (cdr x))))
937 '(
938
939 ("calc-aent" calc-algebraic-entry calc-auto-algebraic-entry
940 calcDigit-algebraic calcDigit-edit)
941
942 ("calc-misc" another-calc calc-big-or-small calc-dispatch-help
943 calc-help calc-info calc-info-summary calc-inv calc-last-args-stub
944 calc-missing-key calc-mod calc-other-window calc-over calc-percent
945 calc-pop-above calc-power calc-roll-down calc-roll-up
946 calc-shift-Y-prefix-help calc-tutorial calcDigit-letter
947 report-calc-bug))))
948
949 (calc-init-base)
950
951
952 ;;;###autoload (global-set-key "\e#" 'calc-dispatch)
953
954 ;;;###autoload
955 (defun calc-dispatch (&optional arg)
956 "Invoke the GNU Emacs Calculator. See `calc-dispatch-help' for details."
957 (interactive "P")
958 (sit-for echo-keystrokes)
959 (condition-case err ; look for other keys bound to calc-dispatch
960 (let ((keys (this-command-keys)))
961 (or (not (stringp keys))
962 (string-match "\\`\C-u\\|\\`\e[-0-9#]\\|`[\M--\M-0-\M-9]" keys)
963 (eq (lookup-key calc-dispatch-map keys) 'calc-same-interface)
964 (progn
965 (and (string-match "\\`[\C-@-\C-_]" keys)
966 (symbolp
967 (lookup-key calc-dispatch-map (substring keys 0 1)))
968 (define-key calc-dispatch-map (substring keys 0 1) nil))
969 (define-key calc-dispatch-map keys 'calc-same-interface))))
970 (error nil))
971 (calc-do-dispatch arg))
972
973 (defvar calc-dispatch-help nil)
974 (defun calc-do-dispatch (arg)
975 (let ((key (calc-read-key-sequence
976 (if calc-dispatch-help
977 "Calc options: Calc, Keypad, Quick, Embed; eXit; Info, Tutorial; Grab; ?=more"
978 (format "%s (Type ? for a list of Calc options)"
979 (key-description (this-command-keys))))
980 calc-dispatch-map)))
981 (setq key (lookup-key calc-dispatch-map key))
982 (message "")
983 (if key
984 (progn
985 (or (commandp key) (calc-extensions))
986 (call-interactively key))
987 (beep))))
988
989 (defun calc-read-key-sequence (prompt map)
990 (let ((prompt2 (format "%s " (key-description (this-command-keys))))
991 (glob (current-global-map))
992 (loc (current-local-map)))
993 (or (input-pending-p) (message prompt))
994 (let ((key (calc-read-key t)))
995 (calc-unread-command (cdr key))
996 (unwind-protect
997 (progn
998 (use-global-map map)
999 (use-local-map nil)
1000 (read-key-sequence
1001 (if (commandp (key-binding (if calc-emacs-type-19
1002 (vector (cdr key))
1003 (char-to-string (cdr key)))))
1004 "" prompt2)))
1005 (use-global-map glob)
1006 (use-local-map loc)))))
1007
1008
1009
1010 (defun calc-mode ()
1011 "Calculator major mode.
1012
1013 This is an RPN calculator featuring arbitrary-precision integer, rational,
1014 floating-point, complex, matrix, and symbolic arithmetic.
1015
1016 RPN calculation: 2 RET 3 + produces 5.
1017 Algebraic style: ' 2+3 RET produces 5.
1018
1019 Basic operators are +, -, *, /, ^, & (reciprocal), % (modulo), n (change-sign).
1020
1021 Press ? repeatedly for more complete help. Press `h i' to read the
1022 Calc manual on-line, `h s' to read the summary, or `h t' for the tutorial.
1023
1024 Notations: 3.14e6 3.14 * 10^6
1025 _23 negative number -23 (or type `23 n')
1026 17:3 the fraction 17/3
1027 5:2:3 the fraction 5 and 2/3
1028 16#12C the integer 12C base 16 = 300 base 10
1029 8#177:100 the fraction 177:100 base 8 = 127:64 base 10
1030 (2, 4) complex number 2 + 4i
1031 (2; 4) polar complex number (r; theta)
1032 [1, 2, 3] vector ([[1, 2], [3, 4]] is a matrix)
1033 [1 .. 4) semi-open interval, 1 <= x < 4
1034 2 +/- 3 (p key) number with mean 2, standard deviation 3
1035 2 mod 3 (M key) number 2 computed modulo 3
1036 <1 jan 91> Date form (enter using ' key)
1037
1038
1039 \\{calc-mode-map}
1040 "
1041 (interactive)
1042 (mapcar (function
1043 (lambda (v) (set-default v (symbol-value v)))) calc-local-var-list)
1044 (kill-all-local-variables)
1045 (use-local-map (if (eq calc-algebraic-mode 'total)
1046 (progn (calc-extensions) calc-alg-map) calc-mode-map))
1047 (mapcar (function (lambda (v) (make-local-variable v))) calc-local-var-list)
1048 (make-local-variable 'overlay-arrow-position)
1049 (make-local-variable 'overlay-arrow-string)
1050 (setq truncate-lines t)
1051 (setq buffer-read-only t)
1052 (setq major-mode 'calc-mode)
1053 (setq mode-name "Calculator")
1054 (setq calc-stack-top (length (or (memq (assq 'top-of-stack calc-stack)
1055 calc-stack)
1056 (setq calc-stack (list (list 'top-of-stack
1057 1 nil))))))
1058 (setq calc-stack-top (- (length calc-stack) calc-stack-top -1))
1059 (or calc-loaded-settings-file
1060 (null calc-settings-file)
1061 (string-match "\\.emacs" calc-settings-file)
1062 (progn
1063 (setq calc-loaded-settings-file t)
1064 (load calc-settings-file t))) ; t = missing-ok
1065 (if (and (eq window-system 'x) (boundp 'mouse-map))
1066 (substitute-key-definition 'x-paste-text 'calc-x-paste-text
1067 mouse-map))
1068 (let ((p command-line-args))
1069 (while p
1070 (and (equal (car p) "-f")
1071 (string-match "calc" (nth 1 p))
1072 (string-match "full" (nth 1 p))
1073 (setq calc-standalone-flag t))
1074 (setq p (cdr p))))
1075 (run-hooks 'calc-mode-hook)
1076 (calc-refresh t)
1077 (calc-set-mode-line)
1078 ;; The calc-defs variable is a relic. Use calc-define properties instead.
1079 (if (and (boundp 'calc-defs)
1080 calc-defs)
1081 (progn
1082 (message "Evaluating calc-defs...")
1083 (calc-need-macros)
1084 (eval (cons 'progn calc-defs))
1085 (setq calc-defs nil)
1086 (calc-set-mode-line)))
1087 (calc-check-defines))
1088
1089 (defvar calc-check-defines 'calc-check-defines) ; suitable for run-hooks
1090 (defun calc-check-defines ()
1091 (if (symbol-plist 'calc-define)
1092 (let ((plist (copy-sequence (symbol-plist 'calc-define))))
1093 (while (and plist (null (nth 1 plist)))
1094 (setq plist (cdr (cdr plist))))
1095 (if plist
1096 (save-excursion
1097 (calc-extensions)
1098 (calc-need-macros)
1099 (set-buffer "*Calculator*")
1100 (while plist
1101 (put 'calc-define (car plist) nil)
1102 (eval (nth 1 plist))
1103 (setq plist (cdr (cdr plist))))
1104 ;; See if this has added any more calc-define properties.
1105 (calc-check-defines))
1106 (setplist 'calc-define nil)))))
1107
1108 (defun calc-trail-mode (&optional buf)
1109 "Calc Trail mode.
1110 This mode is used by the *Calc Trail* buffer, which records all results
1111 obtained by the GNU Emacs Calculator.
1112
1113 Calculator commands beginning with the `t' key are used to manipulate
1114 the Trail.
1115
1116 This buffer uses the same key map as the *Calculator* buffer; calculator
1117 commands given here will actually operate on the *Calculator* stack."
1118 (interactive)
1119 (fundamental-mode)
1120 (use-local-map calc-mode-map)
1121 (setq major-mode 'calc-trail-mode)
1122 (setq mode-name "Calc Trail")
1123 (setq truncate-lines t)
1124 (setq buffer-read-only t)
1125 (make-local-variable 'overlay-arrow-position)
1126 (make-local-variable 'overlay-arrow-string)
1127 (if buf
1128 (progn
1129 (make-local-variable 'calc-main-buffer)
1130 (setq calc-main-buffer buf)))
1131 (if (= (buffer-size) 0)
1132 (let ((buffer-read-only nil))
1133 (insert "Emacs Calculator v" calc-version " by Dave Gillespie\n")))
1134 (run-hooks 'calc-trail-mode-hook))
1135
1136 (defun calc-create-buffer ()
1137 (set-buffer (get-buffer-create "*Calculator*"))
1138 (or (eq major-mode 'calc-mode)
1139 (calc-mode))
1140 (setq max-lisp-eval-depth (max max-lisp-eval-depth 1000))
1141 (if calc-always-load-extensions
1142 (calc-extensions))
1143 (if calc-language
1144 (progn
1145 (calc-extensions)
1146 (calc-set-language calc-language calc-language-option t))))
1147
1148 ;;;###autoload
1149 (defun calc (&optional arg full-display interactive)
1150 "The Emacs Calculator. Full documentation is listed under \"calc-mode\"."
1151 (interactive "P")
1152 (if arg
1153 (or (eq arg 0)
1154 (progn
1155 (calc-extensions)
1156 (if (= (prefix-numeric-value arg) -1)
1157 (calc-grab-region (region-beginning) (region-end) nil)
1158 (if (= (prefix-numeric-value arg) -2)
1159 (calc-keypad)))))
1160 (if (get-buffer-window "*Calc Keypad*")
1161 (progn
1162 (calc-keypad)
1163 (set-buffer (window-buffer (selected-window)))))
1164 (if (eq major-mode 'calc-mode)
1165 (calc-quit)
1166 (let ((oldbuf (current-buffer)))
1167 (calc-create-buffer)
1168 (setq calc-was-keypad-mode nil)
1169 (if (or (eq full-display t)
1170 (and (null full-display) calc-full-mode))
1171 (switch-to-buffer (current-buffer) t)
1172 (if (get-buffer-window (current-buffer))
1173 (select-window (get-buffer-window (current-buffer)))
1174 (setq calc-was-split nil)
1175 (if (and (boundp 'calc-window-hook) calc-window-hook)
1176 (run-hooks 'calc-window-hook)
1177 (let ((w (get-largest-window)))
1178 (if (and pop-up-windows
1179 (> (window-height w)
1180 (+ window-min-height calc-window-height 2)))
1181 (progn
1182 (or (one-window-p)
1183 (setq calc-was-split (list w (window-height w)
1184 (selected-window))))
1185 (setq w (split-window w
1186 (- (window-height w)
1187 calc-window-height 2)
1188 nil))
1189 (set-window-buffer w (current-buffer))
1190 (select-window w))
1191 (pop-to-buffer (current-buffer)))))))
1192 (save-excursion
1193 (set-buffer (calc-trail-buffer))
1194 (and calc-display-trail
1195 (= (window-width) (frame-width))
1196 (calc-trail-display 1 t)))
1197 (message "Welcome to the GNU Emacs Calculator! Press `?' or `h' for help, `q' to quit")
1198 (run-hooks 'calc-start-hook)
1199 (and (windowp full-display)
1200 (window-point full-display)
1201 (select-window full-display))
1202 (calc-check-defines)
1203 (and calc-said-hello
1204 (or (interactive-p) interactive)
1205 (progn
1206 (sit-for 2)
1207 (message "")))
1208 (setq calc-said-hello t)))))
1209
1210 ;;;###autoload
1211 (defun full-calc ()
1212 "Invoke the Calculator and give it a full-sized window."
1213 (interactive)
1214 (calc nil t (interactive-p)))
1215
1216 (defun calc-same-interface (arg)
1217 "Invoke the Calculator using the most recent interface (calc or calc-keypad)."
1218 (interactive "P")
1219 (if (and (equal (buffer-name) "*Gnuplot Trail*")
1220 (> (recursion-depth) 0))
1221 (exit-recursive-edit)
1222 (if (eq major-mode 'calc-edit-mode)
1223 (calc-edit-finish arg)
1224 (if (eq major-mode 'MacEdit-mode)
1225 (MacEdit-finish-edit)
1226 (if calc-was-keypad-mode
1227 (calc-keypad)
1228 (calc arg calc-full-mode t))))))
1229
1230
1231 (defun calc-quit (&optional non-fatal)
1232 (interactive)
1233 (and calc-standalone-flag (not non-fatal)
1234 (save-buffers-kill-emacs nil))
1235 (if (and (equal (buffer-name) "*Gnuplot Trail*")
1236 (> (recursion-depth) 0))
1237 (exit-recursive-edit))
1238 (if (eq major-mode 'calc-edit-mode)
1239 (calc-edit-cancel)
1240 (if (eq major-mode 'MacEdit-mode)
1241 (MacEdit-cancel-edit)
1242 (if (and (interactive-p)
1243 calc-embedded-info
1244 (eq (current-buffer) (aref calc-embedded-info 0)))
1245 (calc-embedded nil)
1246 (unless (eq major-mode 'calc-mode)
1247 (calc-create-buffer))
1248 (run-hooks 'calc-end-hook)
1249 (setq calc-undo-list nil calc-redo-list nil)
1250 (mapcar (function (lambda (v) (set-default v (symbol-value v))))
1251 calc-local-var-list)
1252 (let ((buf (current-buffer))
1253 (win (get-buffer-window (current-buffer)))
1254 (kbuf (get-buffer "*Calc Keypad*")))
1255 (delete-windows-on (calc-trail-buffer))
1256 (if (and win
1257 (< (window-height win) (1- (frame-height)))
1258 (= (window-width win) (frame-width)) ; avoid calc-keypad
1259 (not (get-buffer-window "*Calc Keypad*")))
1260 (setq calc-window-height (- (window-height win) 2)))
1261 (if calc-was-split
1262 (calc-delete-windows-keep buf kbuf)
1263 (delete-windows-on buf)
1264 (delete-windows-on kbuf))
1265 (bury-buffer buf)
1266 (bury-buffer calc-trail-buffer)
1267 (and kbuf (bury-buffer kbuf)))))))
1268
1269 ;;;###autoload
1270 (defun quick-calc ()
1271 "Do a quick calculation in the minibuffer without invoking full Calculator."
1272 (interactive)
1273 (calc-do-quick-calc))
1274
1275 ;;;###autoload
1276 (defun calc-eval (str &optional separator &rest args)
1277 "Do a quick calculation and return the result as a string.
1278 Return value will either be the formatted result in string form,
1279 or a list containing a character position and an error message in string form."
1280 (calc-do-calc-eval str separator args))
1281
1282 ;;;###autoload
1283 (defun calc-keypad ()
1284 "Invoke the Calculator in \"visual keypad\" mode.
1285 This is most useful in the X window system.
1286 In this mode, click on the Calc \"buttons\" using the left mouse button.
1287 Or, position the cursor manually and do M-x calc-keypad-press."
1288 (interactive)
1289 (calc-extensions)
1290 (calc-do-keypad calc-full-mode (interactive-p)))
1291
1292 ;;;###autoload
1293 (defun full-calc-keypad ()
1294 "Invoke the Calculator in full-screen \"visual keypad\" mode.
1295 See calc-keypad for details."
1296 (interactive)
1297 (calc-extensions)
1298 (calc-do-keypad t (interactive-p)))
1299
1300
1301 (defvar calc-aborted-prefix nil)
1302 (defvar calc-start-time nil)
1303 ;;; Note that modifications to this function may break calc-pass-errors.
1304 (defun calc-do (do-body &optional do-slow)
1305 (calc-check-defines)
1306 (let* ((calc-command-flags nil)
1307 (calc-start-time (and calc-timing (not calc-start-time)
1308 (calc-extensions)
1309 (current-time-string)))
1310 (gc-cons-threshold (max gc-cons-threshold
1311 (if calc-timing 2000000 100000))))
1312 (setq calc-aborted-prefix "")
1313 (unwind-protect
1314 (condition-case err
1315 (save-excursion
1316 (if calc-embedded-info
1317 (calc-embedded-select-buffer)
1318 (calc-select-buffer))
1319 (and (eq calc-algebraic-mode 'total)
1320 (calc-extensions)
1321 (use-local-map calc-alg-map))
1322 (when (and do-slow calc-display-working-message)
1323 (message "Working...")
1324 (calc-set-command-flag 'clear-message))
1325 (funcall do-body)
1326 (setq calc-aborted-prefix nil)
1327 (when (memq 'renum-stack calc-command-flags)
1328 (calc-renumber-stack))
1329 (when (memq 'clear-message calc-command-flags)
1330 (message "")))
1331 (error
1332 (if (and (eq (car err) 'error)
1333 (stringp (nth 1 err))
1334 (string-match "max-specpdl-size\\|max-lisp-eval-depth"
1335 (nth 1 err)))
1336 (error "Computation got stuck or ran too long. Type `M' to increase the limit")
1337 (setq calc-aborted-prefix nil)
1338 (signal (car err) (cdr err)))))
1339 (setq calc-old-aborted-prefix calc-aborted-prefix)
1340 (when calc-aborted-prefix
1341 (calc-record "<Aborted>" calc-aborted-prefix))
1342 (and calc-start-time
1343 (let* ((calc-internal-prec 12)
1344 (calc-date-format nil)
1345 (end-time (current-time-string))
1346 (time (if (equal calc-start-time end-time)
1347 0
1348 (math-sub
1349 (calcFunc-unixtime (math-parse-date end-time) 0)
1350 (calcFunc-unixtime (math-parse-date calc-start-time)
1351 0)))))
1352 (if (math-lessp 1 time)
1353 (calc-record time "(t)"))))
1354 (or (memq 'no-align calc-command-flags)
1355 (eq major-mode 'calc-trail-mode)
1356 (calc-align-stack-window))
1357 (and (memq 'position-point calc-command-flags)
1358 (if (eq major-mode 'calc-mode)
1359 (progn
1360 (goto-line calc-final-point-line)
1361 (move-to-column calc-final-point-column))
1362 (save-excursion
1363 (calc-select-buffer)
1364 (goto-line calc-final-point-line)
1365 (move-to-column calc-final-point-column))))
1366 (unless (memq 'keep-flags calc-command-flags)
1367 (save-excursion
1368 (calc-select-buffer)
1369 (setq calc-inverse-flag nil
1370 calc-hyperbolic-flag nil
1371 calc-keep-args-flag nil)))
1372 (when (memq 'do-edit calc-command-flags)
1373 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
1374 (calc-set-mode-line)
1375 (when calc-embedded-info
1376 (calc-embedded-finish-command))))
1377 (identity nil)) ; allow a GC after timing is done
1378
1379
1380 (defun calc-set-command-flag (f)
1381 (unless (memq f calc-command-flags)
1382 (setq calc-command-flags (cons f calc-command-flags))))
1383
1384 (defun calc-select-buffer ()
1385 (or (eq major-mode 'calc-mode)
1386 (if calc-main-buffer
1387 (set-buffer calc-main-buffer)
1388 (let ((buf (get-buffer "*Calculator*")))
1389 (if buf
1390 (set-buffer buf)
1391 (error "Calculator buffer not available"))))))
1392
1393 (defun calc-cursor-stack-index (&optional index)
1394 (goto-char (point-max))
1395 (forward-line (- (calc-substack-height (or index 1)))))
1396
1397 (defun calc-stack-size ()
1398 (- (length calc-stack) calc-stack-top))
1399
1400 (defun calc-substack-height (n)
1401 (let ((sum 0)
1402 (stack calc-stack))
1403 (setq n (+ n calc-stack-top))
1404 (while (and (> n 0) stack)
1405 (setq sum (+ sum (nth 1 (car stack)))
1406 n (1- n)
1407 stack (cdr stack)))
1408 sum))
1409
1410 (defun calc-set-mode-line ()
1411 (save-excursion
1412 (calc-select-buffer)
1413 (let* ((fmt (car calc-float-format))
1414 (figs (nth 1 calc-float-format))
1415 (new-mode-string
1416 (format "Calc%s%s: %d %s %-14s"
1417 (if calc-embedded-info "Embed" "")
1418 (if (and (> (length (buffer-name)) 12)
1419 (equal (substring (buffer-name) 0 12)
1420 "*Calculator*"))
1421 (substring (buffer-name) 12)
1422 "")
1423 calc-internal-prec
1424 (capitalize (symbol-name calc-angle-mode))
1425 (concat
1426
1427 ;; Input-related modes
1428 (if (eq calc-algebraic-mode 'total) "Alg* "
1429 (if calc-algebraic-mode "Alg "
1430 (if calc-incomplete-algebraic-mode "Alg[( " "")))
1431
1432 ;; Computational modes
1433 (if calc-symbolic-mode "Symb " "")
1434 (cond ((eq calc-matrix-mode 'matrix) "Matrix ")
1435 ((integerp calc-matrix-mode)
1436 (format "Matrix%d " calc-matrix-mode))
1437 ((eq calc-matrix-mode 'scalar) "Scalar ")
1438 (t ""))
1439 (if (eq calc-complex-mode 'polar) "Polar " "")
1440 (if calc-prefer-frac "Frac " "")
1441 (cond ((null calc-infinite-mode) "")
1442 ((eq calc-infinite-mode 1) "+Inf ")
1443 (t "Inf "))
1444 (cond ((eq calc-simplify-mode 'none) "NoSimp ")
1445 ((eq calc-simplify-mode 'num) "NumSimp ")
1446 ((eq calc-simplify-mode 'binary)
1447 (format "BinSimp%d " calc-word-size))
1448 ((eq calc-simplify-mode 'alg) "AlgSimp ")
1449 ((eq calc-simplify-mode 'ext) "ExtSimp ")
1450 ((eq calc-simplify-mode 'units) "UnitSimp ")
1451 (t ""))
1452
1453 ;; Display modes
1454 (cond ((= calc-number-radix 10) "")
1455 ((= calc-number-radix 2) "Bin ")
1456 ((= calc-number-radix 8) "Oct ")
1457 ((= calc-number-radix 16) "Hex ")
1458 (t (format "Radix%d " calc-number-radix)))
1459 (if calc-leading-zeros "Zero " "")
1460 (cond ((null calc-language) "")
1461 ((eq calc-language 'tex) "TeX ")
1462 (t (concat
1463 (capitalize (symbol-name calc-language))
1464 " ")))
1465 (cond ((eq fmt 'float)
1466 (if (zerop figs) "" (format "Norm%d " figs)))
1467 ((eq fmt 'fix) (format "Fix%d " figs))
1468 ((eq fmt 'sci)
1469 (if (zerop figs) "Sci " (format "Sci%d " figs)))
1470 ((eq fmt 'eng)
1471 (if (zerop figs) "Eng " (format "Eng%d " figs))))
1472 (cond ((not calc-display-just)
1473 (if calc-display-origin
1474 (format "Left%d " calc-display-origin) ""))
1475 ((eq calc-display-just 'right)
1476 (if calc-display-origin
1477 (format "Right%d " calc-display-origin)
1478 "Right "))
1479 (t
1480 (if calc-display-origin
1481 (format "Center%d " calc-display-origin)
1482 "Center ")))
1483 (cond ((integerp calc-line-breaking)
1484 (format "Wid%d " calc-line-breaking))
1485 (calc-line-breaking "")
1486 (t "Wide "))
1487
1488 ;; Miscellaneous other modes/indicators
1489 (if calc-assoc-selections "" "Break ")
1490 (cond ((eq calc-mode-save-mode 'save) "Save ")
1491 ((not calc-embedded-info) "")
1492 ((eq calc-mode-save-mode 'local) "Local ")
1493 ((eq calc-mode-save-mode 'edit) "LocEdit ")
1494 ((eq calc-mode-save-mode 'perm) "LocPerm ")
1495 ((eq calc-mode-save-mode 'global) "Global ")
1496 (t ""))
1497 (if calc-auto-recompute "" "Manual ")
1498 (if (and (fboundp 'calc-gnuplot-alive)
1499 (calc-gnuplot-alive)) "Graph " "")
1500 (if (and calc-embedded-info
1501 (> (calc-stack-size) 0)
1502 (calc-top 1 'sel)) "Sel " "")
1503 (if calc-display-dirty "Dirty " "")
1504 (if calc-inverse-flag "Inv " "")
1505 (if calc-hyperbolic-flag "Hyp " "")
1506 (if calc-keep-args-flag "Keep " "")
1507 (if (/= calc-stack-top 1) "Narrow " "")
1508 (apply 'concat calc-other-modes)))))
1509 (if (equal new-mode-string mode-line-buffer-identification)
1510 nil
1511 (setq mode-line-buffer-identification new-mode-string)
1512 (set-buffer-modified-p (buffer-modified-p))
1513 (and calc-embedded-info (calc-embedded-mode-line-change))))))
1514
1515 (defun calc-align-stack-window ()
1516 (if (eq major-mode 'calc-mode)
1517 (progn
1518 (let ((win (get-buffer-window (current-buffer))))
1519 (if win
1520 (progn
1521 (calc-cursor-stack-index 0)
1522 (vertical-motion (- 2 (window-height win)))
1523 (set-window-start win (point)))))
1524 (calc-cursor-stack-index 0)
1525 (if (looking-at " *\\.$")
1526 (goto-char (1- (match-end 0)))))
1527 (save-excursion
1528 (calc-select-buffer)
1529 (calc-align-stack-window))))
1530
1531 (defun calc-check-stack (n)
1532 (if (> n (calc-stack-size))
1533 (error "Too few elements on stack"))
1534 (if (< n 0)
1535 (error "Invalid argument")))
1536
1537 (defun calc-push-list (vals &optional m sels)
1538 (while vals
1539 (if calc-executing-macro
1540 (calc-push-list-in-macro vals m sels)
1541 (save-excursion
1542 (calc-select-buffer)
1543 (let* ((val (car vals))
1544 (entry (list val 1 (car sels)))
1545 (mm (+ (or m 1) calc-stack-top)))
1546 (calc-cursor-stack-index (1- (or m 1)))
1547 (if (> mm 1)
1548 (setcdr (nthcdr (- mm 2) calc-stack)
1549 (cons entry (nthcdr (1- mm) calc-stack)))
1550 (setq calc-stack (cons entry calc-stack)))
1551 (let ((buffer-read-only nil))
1552 (insert (math-format-stack-value entry) "\n"))
1553 (calc-record-undo (list 'push mm))
1554 (calc-set-command-flag 'renum-stack))))
1555 (setq vals (cdr vals)
1556 sels (cdr sels))))
1557
1558 (defun calc-pop-push-list (n vals &optional m sels)
1559 (if (and calc-any-selections (null sels))
1560 (calc-replace-selections n vals m)
1561 (calc-pop-stack n m sels)
1562 (calc-push-list vals m sels)))
1563
1564 (defun calc-pop-push-record-list (n prefix vals &optional m sels)
1565 (or (and (consp vals)
1566 (or (integerp (car vals))
1567 (consp (car vals))))
1568 (and vals (setq vals (list vals)
1569 sels (and sels (list sels)))))
1570 (calc-check-stack (+ n (or m 1) -1))
1571 (if prefix
1572 (if (cdr vals)
1573 (calc-record-list vals prefix)
1574 (calc-record (car vals) prefix)))
1575 (calc-pop-push-list n vals m sels))
1576
1577 (defun calc-enter-result (n prefix vals &optional m)
1578 (setq calc-aborted-prefix prefix)
1579 (if (and (consp vals)
1580 (or (integerp (car vals))
1581 (consp (car vals))))
1582 (setq vals (mapcar 'calc-normalize vals))
1583 (setq vals (calc-normalize vals)))
1584 (or (and (consp vals)
1585 (or (integerp (car vals))
1586 (consp (car vals))))
1587 (setq vals (list vals)))
1588 (if (equal vals '((nil)))
1589 (setq vals nil))
1590 (calc-pop-push-record-list n prefix vals m)
1591 (calc-handle-whys))
1592
1593 (defun calc-normalize (val)
1594 (if (memq calc-simplify-mode '(nil none num))
1595 (math-normalize val)
1596 (calc-extensions)
1597 (calc-normalize-fancy val)))
1598
1599 (defun calc-handle-whys ()
1600 (if calc-next-why
1601 (calc-do-handle-whys)))
1602
1603
1604 (defun calc-pop-stack (&optional n m sel-ok) ; pop N objs at level M of stack.
1605 (or n (setq n 1))
1606 (or m (setq m 1))
1607 (or calc-keep-args-flag
1608 (let ((mm (+ m calc-stack-top)))
1609 (if (and calc-any-selections (not sel-ok)
1610 (calc-top-selected n m))
1611 (calc-sel-error))
1612 (if calc-executing-macro
1613 (calc-pop-stack-in-macro n mm)
1614 (calc-record-undo (list 'pop mm (calc-top-list n m 'full)))
1615 (save-excursion
1616 (calc-select-buffer)
1617 (let ((buffer-read-only nil))
1618 (if (> mm 1)
1619 (progn
1620 (calc-cursor-stack-index (1- m))
1621 (let ((bot (point)))
1622 (calc-cursor-stack-index (+ n m -1))
1623 (delete-region (point) bot))
1624 (setcdr (nthcdr (- mm 2) calc-stack)
1625 (nthcdr (+ n mm -1) calc-stack)))
1626 (calc-cursor-stack-index n)
1627 (setq calc-stack (nthcdr n calc-stack))
1628 (delete-region (point) (point-max))))
1629 (calc-set-command-flag 'renum-stack))))))
1630
1631 (defun calc-get-stack-element (x)
1632 (cond ((eq sel-mode 'entry)
1633 x)
1634 ((eq sel-mode 'sel)
1635 (nth 2 x))
1636 ((or (null (nth 2 x))
1637 (eq sel-mode 'full)
1638 (not calc-use-selections))
1639 (car x))
1640 (sel-mode
1641 (calc-sel-error))
1642 (t (nth 2 x))))
1643
1644 ;; Get the Nth element of the stack (N=1 is the top element).
1645 (defun calc-top (&optional n sel-mode)
1646 (or n (setq n 1))
1647 (calc-check-stack n)
1648 (calc-get-stack-element (nth (+ n calc-stack-top -1) calc-stack)))
1649
1650 (defun calc-top-n (&optional n sel-mode) ; in case precision has changed
1651 (math-check-complete (calc-normalize (calc-top n sel-mode))))
1652
1653 (defun calc-top-list (&optional n m sel-mode)
1654 (or n (setq n 1))
1655 (or m (setq m 1))
1656 (calc-check-stack (+ n m -1))
1657 (and (> n 0)
1658 (let ((top (copy-sequence (nthcdr (+ m calc-stack-top -1)
1659 calc-stack))))
1660 (setcdr (nthcdr (1- n) top) nil)
1661 (nreverse (mapcar 'calc-get-stack-element top)))))
1662
1663 (defun calc-top-list-n (&optional n m sel-mode)
1664 (mapcar 'math-check-complete
1665 (mapcar 'calc-normalize (calc-top-list n m sel-mode))))
1666
1667
1668 (defun calc-renumber-stack ()
1669 (if calc-line-numbering
1670 (save-excursion
1671 (calc-cursor-stack-index 0)
1672 (let ((lnum 1)
1673 (buffer-read-only nil)
1674 (stack (nthcdr calc-stack-top calc-stack)))
1675 (if (re-search-forward "^[0-9]+[:*]" nil t)
1676 (progn
1677 (beginning-of-line)
1678 (while (re-search-forward "^[0-9]+[:*]" nil t)
1679 (let ((buffer-read-only nil))
1680 (beginning-of-line)
1681 (delete-char 4)
1682 (insert " ")))
1683 (calc-cursor-stack-index 0)))
1684 (while (re-search-backward "^[0-9]+[:*]" nil t)
1685 (delete-char 4)
1686 (if (> lnum 999)
1687 (insert (format "%03d%s" (% lnum 1000)
1688 (if (and (nth 2 (car stack))
1689 calc-use-selections) "*" ":")))
1690 (let ((prefix (int-to-string lnum)))
1691 (insert prefix (if (and (nth 2 (car stack))
1692 calc-use-selections) "*" ":")
1693 (make-string (- 3 (length prefix)) 32))))
1694 (beginning-of-line)
1695 (setq lnum (1+ lnum)
1696 stack (cdr stack))))))
1697 (and calc-embedded-info (calc-embedded-stack-change)))
1698
1699 (defun calc-refresh (&optional align)
1700 (interactive)
1701 (and (eq major-mode 'calc-mode)
1702 (not calc-executing-macro)
1703 (let* ((buffer-read-only nil)
1704 (save-point (point))
1705 (save-mark (condition-case err (mark) (error nil)))
1706 (save-aligned (looking-at "\\.$"))
1707 (thing calc-stack))
1708 (setq calc-any-selections nil
1709 calc-any-evaltos nil)
1710 (erase-buffer)
1711 (insert "--- Emacs Calculator Mode ---\n")
1712 (while thing
1713 (goto-char (point-min))
1714 (forward-line 1)
1715 (insert (math-format-stack-value (car thing)) "\n")
1716 (setq thing (cdr thing)))
1717 (calc-renumber-stack)
1718 (if calc-display-dirty
1719 (calc-wrapper (setq calc-display-dirty nil)))
1720 (and calc-any-evaltos calc-auto-recompute
1721 (calc-wrapper (calc-refresh-evaltos)))
1722 (if (or align save-aligned)
1723 (calc-align-stack-window)
1724 (goto-char save-point))
1725 (if save-mark (set-mark save-mark))))
1726 (and calc-embedded-info (not (eq major-mode 'calc-mode))
1727 (save-excursion
1728 (set-buffer (aref calc-embedded-info 1))
1729 (calc-refresh align)))
1730 (setq calc-refresh-count (1+ calc-refresh-count)))
1731
1732
1733 (defun calc-x-paste-text (arg)
1734 "Move point to mouse position and insert window system cut buffer contents.
1735 If mouse is pressed in Calc window, push cut buffer contents onto the stack."
1736 (x-mouse-select arg)
1737 (if (memq major-mode '(calc-mode calc-trail-mode))
1738 (progn
1739 (calc-wrapper
1740 (calc-extensions)
1741 (let* ((buf (x-get-cut-buffer))
1742 (val (math-read-exprs (calc-clean-newlines buf))))
1743 (if (eq (car-safe val) 'error)
1744 (progn
1745 (setq val (math-read-exprs buf))
1746 (if (eq (car-safe val) 'error)
1747 (error "%s in yanked data" (nth 2 val)))))
1748 (calc-enter-result 0 "Xynk" val))))
1749 (x-paste-text arg)))
1750
1751
1752
1753 ;;;; The Calc Trail buffer.
1754
1755 (defun calc-check-trail-aligned ()
1756 (save-excursion
1757 (let ((win (get-buffer-window (current-buffer))))
1758 (and win
1759 (pos-visible-in-window-p (1- (point-max)) win)))))
1760
1761 (defun calc-trail-buffer ()
1762 (and (or (null calc-trail-buffer)
1763 (null (buffer-name calc-trail-buffer)))
1764 (save-excursion
1765 (setq calc-trail-buffer (get-buffer-create "*Calc Trail*"))
1766 (let ((buf (or (and (not (eq major-mode 'calc-mode))
1767 (get-buffer "*Calculator*"))
1768 (current-buffer))))
1769 (set-buffer calc-trail-buffer)
1770 (or (eq major-mode 'calc-trail-mode)
1771 (calc-trail-mode buf)))))
1772 (or (and calc-trail-pointer
1773 (eq (marker-buffer calc-trail-pointer) calc-trail-buffer))
1774 (save-excursion
1775 (set-buffer calc-trail-buffer)
1776 (goto-line 2)
1777 (setq calc-trail-pointer (point-marker))))
1778 calc-trail-buffer)
1779
1780 (defun calc-record (val &optional prefix)
1781 (setq calc-aborted-prefix nil)
1782 (or calc-executing-macro
1783 (let* ((mainbuf (current-buffer))
1784 (buf (calc-trail-buffer))
1785 (calc-display-raw nil)
1786 (calc-can-abbrev-vectors t)
1787 (fval (if val
1788 (if (stringp val)
1789 val
1790 (math-showing-full-precision
1791 (math-format-flat-expr val 0)))
1792 "")))
1793 (save-excursion
1794 (set-buffer buf)
1795 (let ((aligned (calc-check-trail-aligned))
1796 (buffer-read-only nil))
1797 (goto-char (point-max))
1798 (cond ((null prefix) (insert " "))
1799 ((and (> (length prefix) 4)
1800 (string-match " " prefix 4))
1801 (insert (substring prefix 0 4) " "))
1802 (t (insert (format "%4s " prefix))))
1803 (insert fval "\n")
1804 (let ((win (get-buffer-window buf)))
1805 (if (and aligned win (not (memq 'hold-trail calc-command-flags)))
1806 (calc-trail-here))
1807 (goto-char (1- (point-max))))))))
1808 val)
1809
1810
1811 (defun calc-trail-display (flag &optional no-refresh)
1812 (interactive "P")
1813 (let ((win (get-buffer-window (calc-trail-buffer))))
1814 (if (setq calc-display-trail
1815 (not (if flag (memq flag '(nil 0)) win)))
1816 (if (null win)
1817 (progn
1818 (if (and (boundp 'calc-trail-window-hook) calc-trail-window-hook)
1819 (run-hooks 'calc-trail-window-hook)
1820 (let ((w (split-window nil (/ (* (window-width) 2) 3) t)))
1821 (set-window-buffer w calc-trail-buffer)))
1822 (calc-wrapper
1823 (setq overlay-arrow-string calc-trail-overlay
1824 overlay-arrow-position calc-trail-pointer)
1825 (or no-refresh
1826 (if (interactive-p)
1827 (calc-do-refresh)
1828 (calc-refresh))))))
1829 (if win
1830 (progn
1831 (delete-window win)
1832 (calc-wrapper
1833 (or no-refresh
1834 (if (interactive-p)
1835 (calc-do-refresh)
1836 (calc-refresh))))))))
1837 calc-trail-buffer)
1838
1839 (defun calc-trail-here ()
1840 (interactive)
1841 (if (eq major-mode 'calc-trail-mode)
1842 (progn
1843 (beginning-of-line)
1844 (if (bobp)
1845 (forward-line 1)
1846 (if (eobp)
1847 (forward-line -1)))
1848 (if (or (bobp) (eobp))
1849 (setq overlay-arrow-position nil) ; trail is empty
1850 (set-marker calc-trail-pointer (point) (current-buffer))
1851 (setq calc-trail-overlay (concat (buffer-substring (point)
1852 (+ (point) 4))
1853 ">")
1854 overlay-arrow-string calc-trail-overlay
1855 overlay-arrow-position calc-trail-pointer)
1856 (forward-char 4)
1857 (let ((win (get-buffer-window (current-buffer))))
1858 (if win
1859 (save-excursion
1860 (forward-line (/ (window-height win) 2))
1861 (forward-line (- 1 (window-height win)))
1862 (set-window-start win (point))
1863 (set-window-point win (+ calc-trail-pointer 4))
1864 (set-buffer calc-main-buffer)
1865 (setq overlay-arrow-string calc-trail-overlay
1866 overlay-arrow-position calc-trail-pointer))))))
1867 (error "Not in Calc Trail buffer")))
1868
1869
1870
1871
1872 ;;;; The Undo list.
1873
1874 (defun calc-record-undo (rec)
1875 (or calc-executing-macro
1876 (if (memq 'undo calc-command-flags)
1877 (setq calc-undo-list (cons (cons rec (car calc-undo-list))
1878 (cdr calc-undo-list)))
1879 (setq calc-undo-list (cons (list rec) calc-undo-list)
1880 calc-redo-list nil)
1881 (calc-set-command-flag 'undo))))
1882
1883
1884
1885
1886 ;;; Arithmetic commands.
1887
1888 (defun calc-binary-op (name func arg &optional ident unary func2)
1889 (setq calc-aborted-prefix name)
1890 (if (null arg)
1891 (calc-enter-result 2 name (cons (or func2 func)
1892 (mapcar 'math-check-complete
1893 (calc-top-list 2))))
1894 (calc-extensions)
1895 (calc-binary-op-fancy name func arg ident unary)))
1896
1897 (defun calc-unary-op (name func arg &optional func2)
1898 (setq calc-aborted-prefix name)
1899 (if (null arg)
1900 (calc-enter-result 1 name (list (or func2 func)
1901 (math-check-complete (calc-top 1))))
1902 (calc-extensions)
1903 (calc-unary-op-fancy name func arg)))
1904
1905
1906 (defun calc-plus (arg)
1907 (interactive "P")
1908 (calc-slow-wrapper
1909 (calc-binary-op "+" 'calcFunc-add arg 0 nil '+)))
1910
1911 (defun calc-minus (arg)
1912 (interactive "P")
1913 (calc-slow-wrapper
1914 (calc-binary-op "-" 'calcFunc-sub arg 0 'neg '-)))
1915
1916 (defun calc-times (arg)
1917 (interactive "P")
1918 (calc-slow-wrapper
1919 (calc-binary-op "*" 'calcFunc-mul arg 1 nil '*)))
1920
1921 (defun calc-divide (arg)
1922 (interactive "P")
1923 (calc-slow-wrapper
1924 (calc-binary-op "/" 'calcFunc-div arg 0 'calcFunc-inv '/)))
1925
1926
1927 (defun calc-change-sign (arg)
1928 (interactive "P")
1929 (calc-wrapper
1930 (calc-unary-op "chs" 'neg arg)))
1931
1932
1933
1934 ;;; Stack management commands.
1935
1936 (defun calc-enter (n)
1937 (interactive "p")
1938 (calc-wrapper
1939 (cond ((< n 0)
1940 (calc-push-list (calc-top-list 1 (- n))))
1941 ((= n 0)
1942 (calc-push-list (calc-top-list (calc-stack-size))))
1943 (t
1944 (calc-push-list (calc-top-list n))))))
1945
1946
1947 (defun calc-pop (n)
1948 (interactive "P")
1949 (calc-wrapper
1950 (let* ((nn (prefix-numeric-value n))
1951 (top (and (null n) (calc-top 1))))
1952 (cond ((and (null n)
1953 (eq (car-safe top) 'incomplete)
1954 (> (length top) (if (eq (nth 1 top) 'intv) 3 2)))
1955 (calc-pop-push-list 1 (let ((tt (copy-sequence top)))
1956 (setcdr (nthcdr (- (length tt) 2) tt) nil)
1957 (list tt))))
1958 ((< nn 0)
1959 (if (and calc-any-selections
1960 (calc-top-selected 1 (- nn)))
1961 (calc-delete-selection (- nn))
1962 (calc-pop-stack 1 (- nn) t)))
1963 ((= nn 0)
1964 (calc-pop-stack (calc-stack-size) 1 t))
1965 (t
1966 (if (and calc-any-selections
1967 (= nn 1)
1968 (calc-top-selected 1 1))
1969 (calc-delete-selection 1)
1970 (calc-pop-stack nn)))))))
1971
1972
1973
1974
1975 ;;;; Reading a number using the minibuffer.
1976
1977 (defun calcDigit-start ()
1978 (interactive)
1979 (calc-wrapper
1980 (if (or calc-algebraic-mode
1981 (and (> calc-number-radix 14) (eq last-command-char ?e)))
1982 (calc-alg-digit-entry)
1983 (calc-unread-command)
1984 (setq calc-aborted-prefix nil)
1985 (let* ((calc-digit-value nil)
1986 (calc-prev-char nil)
1987 (calc-prev-prev-char nil)
1988 (calc-buffer (current-buffer))
1989 (buf (if calc-emacs-type-lucid
1990 (catch 'calc-foo
1991 (catch 'execute-kbd-macro
1992 (throw 'calc-foo
1993 (read-from-minibuffer
1994 "Calc: " "" calc-digit-map)))
1995 (error "Lucid Emacs requires RET after %s"
1996 "digit entry in kbd macro"))
1997 (let ((old-esc (lookup-key global-map "\e")))
1998 (unwind-protect
1999 (progn
2000 (define-key global-map "\e" nil)
2001 (read-from-minibuffer "Calc: " "" calc-digit-map))
2002 (define-key global-map "\e" old-esc))))))
2003 (or calc-digit-value (setq calc-digit-value (math-read-number buf)))
2004 (if (stringp calc-digit-value)
2005 (calc-alg-entry calc-digit-value)
2006 (if calc-digit-value
2007 (calc-push-list (list (calc-record (calc-normalize
2008 calc-digit-value))))))
2009 (if (eq calc-prev-char 'dots)
2010 (progn
2011 (calc-extensions)
2012 (calc-dots)))))))
2013
2014 (defsubst calc-minibuffer-size ()
2015 (- (point-max) (minibuffer-prompt-end)))
2016
2017 (defun calcDigit-nondigit ()
2018 (interactive)
2019 ;; Exercise for the reader: Figure out why this is a good precaution!
2020 (or (boundp 'calc-buffer)
2021 (use-local-map minibuffer-local-map))
2022 (let ((str (minibuffer-contents)))
2023 (setq calc-digit-value (save-excursion
2024 (set-buffer calc-buffer)
2025 (math-read-number str))))
2026 (if (and (null calc-digit-value) (> (calc-minibuffer-size) 0))
2027 (progn
2028 (beep)
2029 (calc-temp-minibuffer-message " [Bad format]"))
2030 (or (memq last-command-char '(32 13))
2031 (progn (setq prefix-arg current-prefix-arg)
2032 (calc-unread-command (if (and (eq last-command-char 27)
2033 (>= last-input-char 128))
2034 last-input-char
2035 nil))))
2036 (exit-minibuffer)))
2037
2038
2039 (defun calc-minibuffer-contains (rex)
2040 (save-excursion
2041 (goto-char (minibuffer-prompt-end))
2042 (looking-at rex)))
2043
2044 (defun calcDigit-key ()
2045 (interactive)
2046 (goto-char (point-max))
2047 (if (or (and (memq last-command-char '(?+ ?-))
2048 (> (buffer-size) 0)
2049 (/= (preceding-char) ?e))
2050 (and (memq last-command-char '(?m ?s))
2051 (not (calc-minibuffer-contains "[-+]?[0-9]+\\.?0*[@oh].*"))
2052 (not (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*"))))
2053 (calcDigit-nondigit)
2054 (if (calc-minibuffer-contains "\\([-+]?\\|.* \\)\\'")
2055 (cond ((memq last-command-char '(?. ?@)) (insert "0"))
2056 ((and (memq last-command-char '(?o ?h ?m))
2057 (not (calc-minibuffer-contains ".*#.*"))) (insert "0"))
2058 ((memq last-command-char '(?: ?e)) (insert "1"))
2059 ((eq last-command-char ?#)
2060 (insert (int-to-string calc-number-radix)))))
2061 (if (and (calc-minibuffer-contains "\\([-+]?[0-9]+#\\|[^:]*:\\)\\'")
2062 (eq last-command-char ?:))
2063 (insert "1"))
2064 (if (and (calc-minibuffer-contains "[-+]?[0-9]+#\\'")
2065 (eq last-command-char ?.))
2066 (insert "0"))
2067 (if (and (calc-minibuffer-contains "[-+]?0*\\([2-9]\\|1[0-4]\\)#\\'")
2068 (eq last-command-char ?e))
2069 (insert "1"))
2070 (if (or (and (memq last-command-char '(?h ?o ?m ?s ?p))
2071 (calc-minibuffer-contains ".*#.*"))
2072 (and (eq last-command-char ?e)
2073 (calc-minibuffer-contains "[-+]?\\(1[5-9]\\|[2-9][0-9]\\)#.*"))
2074 (and (eq last-command-char ?n)
2075 (calc-minibuffer-contains "[-+]?\\(2[4-9]\\|[3-9][0-9]\\)#.*")))
2076 (setq last-command-char (upcase last-command-char)))
2077 (cond
2078 ((memq last-command-char '(?_ ?n))
2079 (goto-char (minibuffer-prompt-end))
2080 (if (and (search-forward " +/- " nil t)
2081 (not (search-forward "e" nil t)))
2082 (beep)
2083 (and (not (calc-minibuffer-contains "[-+]?\\(1[5-9]\\|[2-9][0-9]\\)#.*"))
2084 (search-forward "e" nil t))
2085 (if (looking-at "+")
2086 (delete-char 1))
2087 (if (looking-at "-")
2088 (delete-char 1)
2089 (insert "-")))
2090 (goto-char (point-max)))
2091 ((eq last-command-char ?p)
2092 (if (or (calc-minibuffer-contains ".*\\+/-.*")
2093 (calc-minibuffer-contains ".*mod.*")
2094 (calc-minibuffer-contains ".*#.*")
2095 (calc-minibuffer-contains ".*[-+e:]\\'"))
2096 (beep)
2097 (if (not (calc-minibuffer-contains ".* \\'"))
2098 (insert " "))
2099 (insert "+/- ")))
2100 ((and (eq last-command-char ?M)
2101 (not (calc-minibuffer-contains
2102 "[-+]?\\(2[3-9]\\|[3-9][0-9]\\)#.*")))
2103 (if (or (calc-minibuffer-contains ".*\\+/-.*")
2104 (calc-minibuffer-contains ".*mod *[^ ]+")
2105 (calc-minibuffer-contains ".*[-+e:]\\'"))
2106 (beep)
2107 (if (calc-minibuffer-contains ".*mod \\'")
2108 (if calc-previous-modulo
2109 (insert (math-format-flat-expr calc-previous-modulo 0))
2110 (beep))
2111 (if (not (calc-minibuffer-contains ".* \\'"))
2112 (insert " "))
2113 (insert "mod "))))
2114 (t
2115 (insert (char-to-string last-command-char))
2116 (if (or (and (calc-minibuffer-contains "[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9][0-9]?\\)#[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\)?\\|.[0-9a-zA-Z]*\\(e[-+]?[0-9]*\\)?\\)?\\'")
2117 (let ((radix (string-to-int
2118 (buffer-substring
2119 (match-beginning 2) (match-end 2)))))
2120 (and (>= radix 2)
2121 (<= radix 36)
2122 (or (memq last-command-char '(?# ?: ?. ?e ?+ ?-))
2123 (let ((dig (math-read-radix-digit
2124 (upcase last-command-char))))
2125 (and dig
2126 (< dig radix)))))))
2127 (calc-minibuffer-contains
2128 "[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9]+\\.?0*[@oh] *\\)?\\([0-9]+\\.?0*['m] *\\)?[0-9]*\\(\\.?[0-9]*\\(e[-+]?[0-3]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?\\)?\\|[0-9]:\\([0-9]+:\\)?[0-9]*\\)?[\"s]?\\'"))
2129 (if (and (memq last-command-char '(?@ ?o ?h ?\' ?m))
2130 (string-match " " calc-hms-format))
2131 (insert " "))
2132 (if (and (eq this-command last-command)
2133 (eq last-command-char ?.))
2134 (progn
2135 (calc-extensions)
2136 (calc-digit-dots))
2137 (delete-backward-char 1)
2138 (beep)
2139 (calc-temp-minibuffer-message " [Bad format]"))))))
2140 (setq calc-prev-prev-char calc-prev-char
2141 calc-prev-char last-command-char))
2142
2143
2144 (defun calcDigit-backspace ()
2145 (interactive)
2146 (goto-char (point-max))
2147 (cond ((calc-minibuffer-contains ".* \\+/- \\'")
2148 (backward-delete-char 5))
2149 ((calc-minibuffer-contains ".* mod \\'")
2150 (backward-delete-char 5))
2151 ((calc-minibuffer-contains ".* \\'")
2152 (backward-delete-char 2))
2153 ((eq last-command 'calcDigit-start)
2154 (erase-buffer))
2155 (t (backward-delete-char 1)))
2156 (if (= (calc-minibuffer-size) 0)
2157 (progn
2158 (setq last-command-char 13)
2159 (calcDigit-nondigit))))
2160
2161
2162
2163
2164
2165
2166
2167 ;;;; Arithmetic routines.
2168 ;;;
2169 ;;; An object as manipulated by one of these routines may take any of the
2170 ;;; following forms:
2171 ;;;
2172 ;;; integer An integer. For normalized numbers, this format
2173 ;;; is used only for -999999 ... 999999.
2174 ;;;
2175 ;;; (bigpos N0 N1 N2 ...) A big positive integer, N0 + N1*1000 + N2*10^6 ...
2176 ;;; (bigneg N0 N1 N2 ...) A big negative integer, - N0 - N1*1000 ...
2177 ;;; Each digit N is in the range 0 ... 999.
2178 ;;; Normalized, always at least three N present,
2179 ;;; and the most significant N is nonzero.
2180 ;;;
2181 ;;; (frac NUM DEN) A fraction. NUM and DEN are small or big integers.
2182 ;;; Normalized, DEN > 1.
2183 ;;;
2184 ;;; (float NUM EXP) A floating-point number, NUM * 10^EXP;
2185 ;;; NUM is a small or big integer, EXP is a small int.
2186 ;;; Normalized, NUM is not a multiple of 10, and
2187 ;;; abs(NUM) < 10^calc-internal-prec.
2188 ;;; Normalized zero is stored as (float 0 0).
2189 ;;;
2190 ;;; (cplx REAL IMAG) A complex number; REAL and IMAG are any of above.
2191 ;;; Normalized, IMAG is nonzero.
2192 ;;;
2193 ;;; (polar R THETA) Polar complex number. Normalized, R > 0 and THETA
2194 ;;; is neither zero nor 180 degrees (pi radians).
2195 ;;;
2196 ;;; (vec A B C ...) Vector of objects A, B, C, ... A matrix is a
2197 ;;; vector of vectors.
2198 ;;;
2199 ;;; (hms H M S) Angle in hours-minutes-seconds form. All three
2200 ;;; components have the same sign; H and M must be
2201 ;;; numerically integers; M and S are expected to
2202 ;;; lie in the range [0,60).
2203 ;;;
2204 ;;; (date N) A date or date/time object. N is an integer to
2205 ;;; store a date only, or a fraction or float to
2206 ;;; store a date and time.
2207 ;;;
2208 ;;; (sdev X SIGMA) Error form, X +/- SIGMA. When normalized,
2209 ;;; SIGMA > 0. X is any complex number and SIGMA
2210 ;;; is real numbers; or these may be symbolic
2211 ;;; expressions where SIGMA is assumed real.
2212 ;;;
2213 ;;; (intv MASK LO HI) Interval form. MASK is 0=(), 1=(], 2=[), or 3=[].
2214 ;;; LO and HI are any real numbers, or symbolic
2215 ;;; expressions which are assumed real, and LO < HI.
2216 ;;; For [LO..HI], if LO = HI normalization produces LO,
2217 ;;; and if LO > HI normalization produces [LO..LO).
2218 ;;; For other intervals, if LO > HI normalization
2219 ;;; sets HI equal to LO.
2220 ;;;
2221 ;;; (mod N M) Number modulo M. When normalized, 0 <= N < M.
2222 ;;; N and M are real numbers.
2223 ;;;
2224 ;;; (var V S) Symbolic variable. V is a Lisp symbol which
2225 ;;; represents the variable's visible name. S is
2226 ;;; the symbol which actually stores the variable's
2227 ;;; value: (var pi var-pi).
2228 ;;;
2229 ;;; In general, combining rational numbers in a calculation always produces
2230 ;;; a rational result, but if either argument is a float, result is a float.
2231
2232 ;;; In the following comments, [x y z] means result is x, args must be y, z,
2233 ;;; respectively, where the code letters are:
2234 ;;;
2235 ;;; O Normalized object (vector or number)
2236 ;;; V Normalized vector
2237 ;;; N Normalized number of any type
2238 ;;; N Normalized complex number
2239 ;;; R Normalized real number (float or rational)
2240 ;;; F Normalized floating-point number
2241 ;;; T Normalized rational number
2242 ;;; I Normalized integer
2243 ;;; B Normalized big integer
2244 ;;; S Normalized small integer
2245 ;;; D Digit (small integer, 0..999)
2246 ;;; L Normalized bignum digit list (without "bigpos" or "bigneg" symbol)
2247 ;;; or normalized vector element list (without "vec")
2248 ;;; P Predicate (truth value)
2249 ;;; X Any Lisp object
2250 ;;; Z "nil"
2251 ;;;
2252 ;;; Lower-case letters signify possibly un-normalized values.
2253 ;;; "L.D" means a cons of an L and a D.
2254 ;;; [N N; n n] means result will be normalized if argument is.
2255 ;;; Also, [Public] marks routines intended to be called from outside.
2256 ;;; [This notation has been neglected in many recent routines.]
2257
2258 ;;; Reduce an object to canonical (normalized) form. [O o; Z Z] [Public]
2259 (defun math-normalize (a)
2260 (cond
2261 ((not (consp a))
2262 (if (integerp a)
2263 (if (or (>= a 1000000) (<= a -1000000))
2264 (math-bignum a)
2265 a)
2266 a))
2267 ((eq (car a) 'bigpos)
2268 (if (eq (nth (1- (length a)) a) 0)
2269 (let* ((last (setq a (copy-sequence a))) (digs a))
2270 (while (setq digs (cdr digs))
2271 (or (eq (car digs) 0) (setq last digs)))
2272 (setcdr last nil)))
2273 (if (cdr (cdr (cdr a)))
2274 a
2275 (cond
2276 ((cdr (cdr a)) (+ (nth 1 a) (* (nth 2 a) 1000)))
2277 ((cdr a) (nth 1 a))
2278 (t 0))))
2279 ((eq (car a) 'bigneg)
2280 (if (eq (nth (1- (length a)) a) 0)
2281 (let* ((last (setq a (copy-sequence a))) (digs a))
2282 (while (setq digs (cdr digs))
2283 (or (eq (car digs) 0) (setq last digs)))
2284 (setcdr last nil)))
2285 (if (cdr (cdr (cdr a)))
2286 a
2287 (cond
2288 ((cdr (cdr a)) (- (+ (nth 1 a) (* (nth 2 a) 1000))))
2289 ((cdr a) (- (nth 1 a)))
2290 (t 0))))
2291 ((eq (car a) 'float)
2292 (math-make-float (math-normalize (nth 1 a)) (nth 2 a)))
2293 ((or (memq (car a) '(frac cplx polar hms date mod sdev intv vec var quote
2294 special-const calcFunc-if calcFunc-lambda
2295 calcFunc-quote calcFunc-condition
2296 calcFunc-evalto))
2297 (integerp (car a))
2298 (and (consp (car a)) (not (eq (car (car a)) 'lambda))))
2299 (calc-extensions)
2300 (math-normalize-fancy a))
2301 (t
2302 (or (and calc-simplify-mode
2303 (calc-extensions)
2304 (math-normalize-nonstandard))
2305 (let ((args (mapcar 'math-normalize (cdr a))))
2306 (or (condition-case err
2307 (let ((func (assq (car a) '( ( + . math-add )
2308 ( - . math-sub )
2309 ( * . math-mul )
2310 ( / . math-div )
2311 ( % . math-mod )
2312 ( ^ . math-pow )
2313 ( neg . math-neg )
2314 ( | . math-concat ) ))))
2315 (or (and var-EvalRules
2316 (progn
2317 (or (eq var-EvalRules math-eval-rules-cache-tag)
2318 (progn
2319 (calc-extensions)
2320 (math-recompile-eval-rules)))
2321 (and (or math-eval-rules-cache-other
2322 (assq (car a) math-eval-rules-cache))
2323 (math-apply-rewrites
2324 (cons (car a) args)
2325 (cdr math-eval-rules-cache)
2326 nil math-eval-rules-cache))))
2327 (if func
2328 (apply (cdr func) args)
2329 (and (or (consp (car a))
2330 (fboundp (car a))
2331 (and (not calc-extensions-loaded)
2332 (calc-extensions)
2333 (fboundp (car a))))
2334 (apply (car a) args)))))
2335 (wrong-number-of-arguments
2336 (calc-record-why "*Wrong number of arguments"
2337 (cons (car a) args))
2338 nil)
2339 (wrong-type-argument
2340 (or calc-next-why (calc-record-why "Wrong type of argument"
2341 (cons (car a) args)))
2342 nil)
2343 (args-out-of-range
2344 (calc-record-why "*Argument out of range" (cons (car a) args))
2345 nil)
2346 (inexact-result
2347 (calc-record-why "No exact representation for result"
2348 (cons (car a) args))
2349 nil)
2350 (math-overflow
2351 (calc-record-why "*Floating-point overflow occurred"
2352 (cons (car a) args))
2353 nil)
2354 (math-underflow
2355 (calc-record-why "*Floating-point underflow occurred"
2356 (cons (car a) args))
2357 nil)
2358 (void-variable
2359 (if (eq (nth 1 err) 'var-EvalRules)
2360 (progn
2361 (setq var-EvalRules nil)
2362 (math-normalize (cons (car a) args)))
2363 (calc-record-why "*Variable is void" (nth 1 err)))))
2364 (if (consp (car a))
2365 (math-dimension-error)
2366 (cons (car a) args))))))))
2367
2368
2369
2370 ;;; True if A is a floating-point real or complex number. [P x] [Public]
2371 (defun math-floatp (a)
2372 (cond ((eq (car-safe a) 'float) t)
2373 ((memq (car-safe a) '(cplx polar mod sdev intv))
2374 (or (math-floatp (nth 1 a))
2375 (math-floatp (nth 2 a))
2376 (and (eq (car a) 'intv) (math-floatp (nth 3 a)))))
2377 ((eq (car-safe a) 'date)
2378 (math-floatp (nth 1 a)))))
2379
2380
2381
2382 ;;; Verify that A is a complete object and return A. [x x] [Public]
2383 (defun math-check-complete (a)
2384 (cond ((integerp a) a)
2385 ((eq (car-safe a) 'incomplete)
2386 (calc-incomplete-error a))
2387 ((consp a) a)
2388 (t (error "Invalid data object encountered"))))
2389
2390
2391
2392 ;;; Coerce integer A to be a bignum. [B S]
2393 (defun math-bignum (a)
2394 (if (>= a 0)
2395 (cons 'bigpos (math-bignum-big a))
2396 (cons 'bigneg (math-bignum-big (- a)))))
2397
2398 (defun math-bignum-big (a) ; [L s]
2399 (if (= a 0)
2400 nil
2401 (cons (% a 1000) (math-bignum-big (/ a 1000)))))
2402
2403
2404 ;;; Build a normalized floating-point number. [F I S]
2405 (defun math-make-float (mant exp)
2406 (if (eq mant 0)
2407 '(float 0 0)
2408 (let* ((ldiff (- calc-internal-prec (math-numdigs mant))))
2409 (if (< ldiff 0)
2410 (setq mant (math-scale-rounding mant ldiff)
2411 exp (- exp ldiff))))
2412 (if (consp mant)
2413 (let ((digs (cdr mant)))
2414 (if (= (% (car digs) 10) 0)
2415 (progn
2416 (while (= (car digs) 0)
2417 (setq digs (cdr digs)
2418 exp (+ exp 3)))
2419 (while (= (% (car digs) 10) 0)
2420 (setq digs (math-div10-bignum digs)
2421 exp (1+ exp)))
2422 (setq mant (math-normalize (cons (car mant) digs))))))
2423 (while (= (% mant 10) 0)
2424 (setq mant (/ mant 10)
2425 exp (1+ exp))))
2426 (if (and (<= exp -4000000)
2427 (<= (+ exp (math-numdigs mant) -1) -4000000))
2428 (signal 'math-underflow nil)
2429 (if (and (>= exp 3000000)
2430 (>= (+ exp (math-numdigs mant) -1) 4000000))
2431 (signal 'math-overflow nil)
2432 (list 'float mant exp)))))
2433
2434 (defun math-div10-bignum (a) ; [l l]
2435 (if (cdr a)
2436 (cons (+ (/ (car a) 10) (* (% (nth 1 a) 10) 100))
2437 (math-div10-bignum (cdr a)))
2438 (list (/ (car a) 10))))
2439
2440 ;;; Coerce A to be a float. [F N; V V] [Public]
2441 (defun math-float (a)
2442 (cond ((Math-integerp a) (math-make-float a 0))
2443 ((eq (car a) 'frac) (math-div (math-float (nth 1 a)) (nth 2 a)))
2444 ((eq (car a) 'float) a)
2445 ((memq (car a) '(cplx polar vec hms date sdev mod))
2446 (cons (car a) (mapcar 'math-float (cdr a))))
2447 (t (math-float-fancy a))))
2448
2449
2450 (defun math-neg (a)
2451 (cond ((not (consp a)) (- a))
2452 ((eq (car a) 'bigpos) (cons 'bigneg (cdr a)))
2453 ((eq (car a) 'bigneg) (cons 'bigpos (cdr a)))
2454 ((memq (car a) '(frac float))
2455 (list (car a) (Math-integer-neg (nth 1 a)) (nth 2 a)))
2456 ((memq (car a) '(cplx vec hms date calcFunc-idn))
2457 (cons (car a) (mapcar 'math-neg (cdr a))))
2458 (t (math-neg-fancy a))))
2459
2460
2461 ;;; Compute the number of decimal digits in integer A. [S I]
2462 (defun math-numdigs (a)
2463 (if (consp a)
2464 (if (cdr a)
2465 (let* ((len (1- (length a)))
2466 (top (nth len a)))
2467 (+ (* len 3) (cond ((>= top 100) 0) ((>= top 10) -1) (t -2))))
2468 0)
2469 (cond ((>= a 100) (+ (math-numdigs (/ a 1000)) 3))
2470 ((>= a 10) 2)
2471 ((>= a 1) 1)
2472 ((= a 0) 0)
2473 ((> a -10) 1)
2474 ((> a -100) 2)
2475 (t (math-numdigs (- a))))))
2476
2477 ;;; Multiply (with truncation toward 0) the integer A by 10^N. [I i S]
2478 (defun math-scale-int (a n)
2479 (cond ((= n 0) a)
2480 ((> n 0) (math-scale-left a n))
2481 (t (math-normalize (math-scale-right a (- n))))))
2482
2483 (defun math-scale-left (a n) ; [I I S]
2484 (if (= n 0)
2485 a
2486 (if (consp a)
2487 (cons (car a) (math-scale-left-bignum (cdr a) n))
2488 (if (>= n 3)
2489 (if (or (>= a 1000) (<= a -1000))
2490 (math-scale-left (math-bignum a) n)
2491 (math-scale-left (* a 1000) (- n 3)))
2492 (if (= n 2)
2493 (if (or (>= a 10000) (<= a -10000))
2494 (math-scale-left (math-bignum a) 2)
2495 (* a 100))
2496 (if (or (>= a 100000) (<= a -100000))
2497 (math-scale-left (math-bignum a) 1)
2498 (* a 10)))))))
2499
2500 (defun math-scale-left-bignum (a n)
2501 (if (>= n 3)
2502 (while (>= (setq a (cons 0 a)
2503 n (- n 3)) 3)))
2504 (if (> n 0)
2505 (math-mul-bignum-digit a (if (= n 2) 100 10) 0)
2506 a))
2507
2508 (defun math-scale-right (a n) ; [i i S]
2509 (if (= n 0)
2510 a
2511 (if (consp a)
2512 (cons (car a) (math-scale-right-bignum (cdr a) n))
2513 (if (<= a 0)
2514 (if (= a 0)
2515 0
2516 (- (math-scale-right (- a) n)))
2517 (if (>= n 3)
2518 (while (and (> (setq a (/ a 1000)) 0)
2519 (>= (setq n (- n 3)) 3))))
2520 (if (= n 2)
2521 (/ a 100)
2522 (if (= n 1)
2523 (/ a 10)
2524 a))))))
2525
2526 (defun math-scale-right-bignum (a n) ; [L L S; l l S]
2527 (if (>= n 3)
2528 (setq a (nthcdr (/ n 3) a)
2529 n (% n 3)))
2530 (if (> n 0)
2531 (cdr (math-mul-bignum-digit a (if (= n 2) 10 100) 0))
2532 a))
2533
2534 ;;; Multiply (with rounding) the integer A by 10^N. [I i S]
2535 (defun math-scale-rounding (a n)
2536 (cond ((>= n 0)
2537 (math-scale-left a n))
2538 ((consp a)
2539 (math-normalize
2540 (cons (car a)
2541 (let ((val (if (< n -3)
2542 (math-scale-right-bignum (cdr a) (- -3 n))
2543 (if (= n -2)
2544 (math-mul-bignum-digit (cdr a) 10 0)
2545 (if (= n -1)
2546 (math-mul-bignum-digit (cdr a) 100 0)
2547 (cdr a)))))) ; n = -3
2548 (if (and val (>= (car val) 500))
2549 (if (cdr val)
2550 (if (eq (car (cdr val)) 999)
2551 (math-add-bignum (cdr val) '(1))
2552 (cons (1+ (car (cdr val))) (cdr (cdr val))))
2553 '(1))
2554 (cdr val))))))
2555 (t
2556 (if (< a 0)
2557 (- (math-scale-rounding (- a) n))
2558 (if (= n -1)
2559 (/ (+ a 5) 10)
2560 (/ (+ (math-scale-right a (- -1 n)) 5) 10))))))
2561
2562
2563 ;;; Compute the sum of A and B. [O O O] [Public]
2564 (defun math-add (a b)
2565 (or
2566 (and (not (or (consp a) (consp b)))
2567 (progn
2568 (setq a (+ a b))
2569 (if (or (<= a -1000000) (>= a 1000000))
2570 (math-bignum a)
2571 a)))
2572 (and (Math-zerop a) (not (eq (car-safe a) 'mod))
2573 (if (and (math-floatp a) (Math-ratp b)) (math-float b) b))
2574 (and (Math-zerop b) (not (eq (car-safe b) 'mod))
2575 (if (and (math-floatp b) (Math-ratp a)) (math-float a) a))
2576 (and (Math-objvecp a) (Math-objvecp b)
2577 (or
2578 (and (Math-integerp a) (Math-integerp b)
2579 (progn
2580 (or (consp a) (setq a (math-bignum a)))
2581 (or (consp b) (setq b (math-bignum b)))
2582 (if (eq (car a) 'bigneg)
2583 (if (eq (car b) 'bigneg)
2584 (cons 'bigneg (math-add-bignum (cdr a) (cdr b)))
2585 (math-normalize
2586 (let ((diff (math-sub-bignum (cdr b) (cdr a))))
2587 (if (eq diff 'neg)
2588 (cons 'bigneg (math-sub-bignum (cdr a) (cdr b)))
2589 (cons 'bigpos diff)))))
2590 (if (eq (car b) 'bigneg)
2591 (math-normalize
2592 (let ((diff (math-sub-bignum (cdr a) (cdr b))))
2593 (if (eq diff 'neg)
2594 (cons 'bigneg (math-sub-bignum (cdr b) (cdr a)))
2595 (cons 'bigpos diff))))
2596 (cons 'bigpos (math-add-bignum (cdr a) (cdr b)))))))
2597 (and (Math-ratp a) (Math-ratp b)
2598 (calc-extensions)
2599 (calc-add-fractions a b))
2600 (and (Math-realp a) (Math-realp b)
2601 (progn
2602 (or (and (consp a) (eq (car a) 'float))
2603 (setq a (math-float a)))
2604 (or (and (consp b) (eq (car b) 'float))
2605 (setq b (math-float b)))
2606 (math-add-float a b)))
2607 (and (calc-extensions)
2608 (math-add-objects-fancy a b))))
2609 (and (calc-extensions)
2610 (math-add-symb-fancy a b))))
2611
2612 (defun math-add-bignum (a b) ; [L L L; l l l]
2613 (if a
2614 (if b
2615 (let* ((a (copy-sequence a)) (aa a) (carry nil) sum)
2616 (while (and aa b)
2617 (if carry
2618 (if (< (setq sum (+ (car aa) (car b))) 999)
2619 (progn
2620 (setcar aa (1+ sum))
2621 (setq carry nil))
2622 (setcar aa (+ sum -999)))
2623 (if (< (setq sum (+ (car aa) (car b))) 1000)
2624 (setcar aa sum)
2625 (setcar aa (+ sum -1000))
2626 (setq carry t)))
2627 (setq aa (cdr aa)
2628 b (cdr b)))
2629 (if carry
2630 (if b
2631 (nconc a (math-add-bignum b '(1)))
2632 (while (eq (car aa) 999)
2633 (setcar aa 0)
2634 (setq aa (cdr aa)))
2635 (if aa
2636 (progn
2637 (setcar aa (1+ (car aa)))
2638 a)
2639 (nconc a '(1))))
2640 (if b
2641 (nconc a b)
2642 a)))
2643 a)
2644 b))
2645
2646 (defun math-sub-bignum (a b) ; [l l l]
2647 (if b
2648 (if a
2649 (let* ((a (copy-sequence a)) (aa a) (borrow nil) sum)
2650 (while (and aa b)
2651 (if borrow
2652 (if (>= (setq diff (- (car aa) (car b))) 1)
2653 (progn
2654 (setcar aa (1- diff))
2655 (setq borrow nil))
2656 (setcar aa (+ diff 999)))
2657 (if (>= (setq diff (- (car aa) (car b))) 0)
2658 (setcar aa diff)
2659 (setcar aa (+ diff 1000))
2660 (setq borrow t)))
2661 (setq aa (cdr aa)
2662 b (cdr b)))
2663 (if borrow
2664 (progn
2665 (while (eq (car aa) 0)
2666 (setcar aa 999)
2667 (setq aa (cdr aa)))
2668 (if aa
2669 (progn
2670 (setcar aa (1- (car aa)))
2671 a)
2672 'neg))
2673 (while (eq (car b) 0)
2674 (setq b (cdr b)))
2675 (if b
2676 'neg
2677 a)))
2678 (while (eq (car b) 0)
2679 (setq b (cdr b)))
2680 (and b
2681 'neg))
2682 a))
2683
2684 (defun math-add-float (a b) ; [F F F]
2685 (let ((ediff (- (nth 2 a) (nth 2 b))))
2686 (if (>= ediff 0)
2687 (if (>= ediff (+ calc-internal-prec calc-internal-prec))
2688 a
2689 (math-make-float (math-add (nth 1 b)
2690 (if (eq ediff 0)
2691 (nth 1 a)
2692 (math-scale-left (nth 1 a) ediff)))
2693 (nth 2 b)))
2694 (if (>= (setq ediff (- ediff))
2695 (+ calc-internal-prec calc-internal-prec))
2696 b
2697 (math-make-float (math-add (nth 1 a)
2698 (math-scale-left (nth 1 b) ediff))
2699 (nth 2 a))))))
2700
2701 ;;; Compute the difference of A and B. [O O O] [Public]
2702 (defun math-sub (a b)
2703 (if (or (consp a) (consp b))
2704 (math-add a (math-neg b))
2705 (setq a (- a b))
2706 (if (or (<= a -1000000) (>= a 1000000))
2707 (math-bignum a)
2708 a)))
2709
2710 (defun math-sub-float (a b) ; [F F F]
2711 (let ((ediff (- (nth 2 a) (nth 2 b))))
2712 (if (>= ediff 0)
2713 (if (>= ediff (+ calc-internal-prec calc-internal-prec))
2714 a
2715 (math-make-float (math-add (Math-integer-neg (nth 1 b))
2716 (if (eq ediff 0)
2717 (nth 1 a)
2718 (math-scale-left (nth 1 a) ediff)))
2719 (nth 2 b)))
2720 (if (>= (setq ediff (- ediff))
2721 (+ calc-internal-prec calc-internal-prec))
2722 b
2723 (math-make-float (math-add (nth 1 a)
2724 (Math-integer-neg
2725 (math-scale-left (nth 1 b) ediff)))
2726 (nth 2 a))))))
2727
2728
2729 ;;; Compute the product of A and B. [O O O] [Public]
2730 (defun math-mul (a b)
2731 (or
2732 (and (not (consp a)) (not (consp b))
2733 (< a 1000) (> a -1000) (< b 1000) (> b -1000)
2734 (* a b))
2735 (and (Math-zerop a) (not (eq (car-safe b) 'mod))
2736 (if (Math-scalarp b)
2737 (if (and (math-floatp b) (Math-ratp a)) (math-float a) a)
2738 (calc-extensions)
2739 (math-mul-zero a b)))
2740 (and (Math-zerop b) (not (eq (car-safe a) 'mod))
2741 (if (Math-scalarp a)
2742 (if (and (math-floatp a) (Math-ratp b)) (math-float b) b)
2743 (calc-extensions)
2744 (math-mul-zero b a)))
2745 (and (Math-objvecp a) (Math-objvecp b)
2746 (or
2747 (and (Math-integerp a) (Math-integerp b)
2748 (progn
2749 (or (consp a) (setq a (math-bignum a)))
2750 (or (consp b) (setq b (math-bignum b)))
2751 (math-normalize
2752 (cons (if (eq (car a) (car b)) 'bigpos 'bigneg)
2753 (if (cdr (cdr a))
2754 (if (cdr (cdr b))
2755 (math-mul-bignum (cdr a) (cdr b))
2756 (math-mul-bignum-digit (cdr a) (nth 1 b) 0))
2757 (math-mul-bignum-digit (cdr b) (nth 1 a) 0))))))
2758 (and (Math-ratp a) (Math-ratp b)
2759 (calc-extensions)
2760 (calc-mul-fractions a b))
2761 (and (Math-realp a) (Math-realp b)
2762 (progn
2763 (or (and (consp a) (eq (car a) 'float))
2764 (setq a (math-float a)))
2765 (or (and (consp b) (eq (car b) 'float))
2766 (setq b (math-float b)))
2767 (math-make-float (math-mul (nth 1 a) (nth 1 b))
2768 (+ (nth 2 a) (nth 2 b)))))
2769 (and (calc-extensions)
2770 (math-mul-objects-fancy a b))))
2771 (and (calc-extensions)
2772 (math-mul-symb-fancy a b))))
2773
2774 (defun math-infinitep (a &optional undir)
2775 (while (and (consp a) (memq (car a) '(* / neg)))
2776 (if (or (not (eq (car a) '*)) (math-infinitep (nth 1 a)))
2777 (setq a (nth 1 a))
2778 (setq a (nth 2 a))))
2779 (and (consp a)
2780 (eq (car a) 'var)
2781 (memq (nth 2 a) '(var-inf var-uinf var-nan))
2782 (if (and undir (eq (nth 2 a) 'var-inf))
2783 '(var uinf var-uinf)
2784 a)))
2785
2786 ;;; Multiply digit lists A and B. [L L L; l l l]
2787 (defun math-mul-bignum (a b)
2788 (and a b
2789 (let* ((sum (if (<= (car b) 1)
2790 (if (= (car b) 0)
2791 (list 0)
2792 (copy-sequence a))
2793 (math-mul-bignum-digit a (car b) 0)))
2794 (sump sum) c d aa ss prod)
2795 (while (setq b (cdr b))
2796 (setq ss (setq sump (or (cdr sump) (setcdr sump (list 0))))
2797 d (car b)
2798 c 0
2799 aa a)
2800 (while (progn
2801 (setcar ss (% (setq prod (+ (+ (car ss) (* (car aa) d))
2802 c)) 1000))
2803 (setq aa (cdr aa)))
2804 (setq c (/ prod 1000)
2805 ss (or (cdr ss) (setcdr ss (list 0)))))
2806 (if (>= prod 1000)
2807 (if (cdr ss)
2808 (setcar (cdr ss) (+ (/ prod 1000) (car (cdr ss))))
2809 (setcdr ss (list (/ prod 1000))))))
2810 sum)))
2811
2812 ;;; Multiply digit list A by digit D. [L L D D; l l D D]
2813 (defun math-mul-bignum-digit (a d c)
2814 (if a
2815 (if (<= d 1)
2816 (and (= d 1) a)
2817 (let* ((a (copy-sequence a)) (aa a) prod)
2818 (while (progn
2819 (setcar aa (% (setq prod (+ (* (car aa) d) c)) 1000))
2820 (cdr aa))
2821 (setq aa (cdr aa)
2822 c (/ prod 1000)))
2823 (if (>= prod 1000)
2824 (setcdr aa (list (/ prod 1000))))
2825 a))
2826 (and (> c 0)
2827 (list c))))
2828
2829
2830 ;;; Compute the integer (quotient . remainder) of A and B, which may be
2831 ;;; small or big integers. Type and consistency of truncation is undefined
2832 ;;; if A or B is negative. B must be nonzero. [I.I I I] [Public]
2833 (defun math-idivmod (a b)
2834 (if (eq b 0)
2835 (math-reject-arg a "*Division by zero"))
2836 (if (or (consp a) (consp b))
2837 (if (and (natnump b) (< b 1000))
2838 (let ((res (math-div-bignum-digit (cdr a) b)))
2839 (cons
2840 (math-normalize (cons (car a) (car res)))
2841 (cdr res)))
2842 (or (consp a) (setq a (math-bignum a)))
2843 (or (consp b) (setq b (math-bignum b)))
2844 (let ((res (math-div-bignum (cdr a) (cdr b))))
2845 (cons
2846 (math-normalize (cons (if (eq (car a) (car b)) 'bigpos 'bigneg)
2847 (car res)))
2848 (math-normalize (cons (car a) (cdr res))))))
2849 (cons (/ a b) (% a b))))
2850
2851 (defun math-quotient (a b) ; [I I I] [Public]
2852 (if (and (not (consp a)) (not (consp b)))
2853 (if (= b 0)
2854 (math-reject-arg a "*Division by zero")
2855 (/ a b))
2856 (if (and (natnump b) (< b 1000))
2857 (if (= b 0)
2858 (math-reject-arg a "*Division by zero")
2859 (math-normalize (cons (car a)
2860 (car (math-div-bignum-digit (cdr a) b)))))
2861 (or (consp a) (setq a (math-bignum a)))
2862 (or (consp b) (setq b (math-bignum b)))
2863 (let* ((alen (1- (length a)))
2864 (blen (1- (length b)))
2865 (d (/ 1000 (1+ (nth (1- blen) (cdr b)))))
2866 (res (math-div-bignum-big (math-mul-bignum-digit (cdr a) d 0)
2867 (math-mul-bignum-digit (cdr b) d 0)
2868 alen blen)))
2869 (math-normalize (cons (if (eq (car a) (car b)) 'bigpos 'bigneg)
2870 (car res)))))))
2871
2872
2873 ;;; Divide a bignum digit list by another. [l.l l L]
2874 ;;; The following division algorithm is borrowed from Knuth vol. II, sec. 4.3.1
2875 (defun math-div-bignum (a b)
2876 (if (cdr b)
2877 (let* ((alen (length a))
2878 (blen (length b))
2879 (d (/ 1000 (1+ (nth (1- blen) b))))
2880 (res (math-div-bignum-big (math-mul-bignum-digit a d 0)
2881 (math-mul-bignum-digit b d 0)
2882 alen blen)))
2883 (if (= d 1)
2884 res
2885 (cons (car res)
2886 (car (math-div-bignum-digit (cdr res) d)))))
2887 (let ((res (math-div-bignum-digit a (car b))))
2888 (cons (car res) (list (cdr res))))))
2889
2890 ;;; Divide a bignum digit list by a digit. [l.D l D]
2891 (defun math-div-bignum-digit (a b)
2892 (if a
2893 (let* ((res (math-div-bignum-digit (cdr a) b))
2894 (num (+ (* (cdr res) 1000) (car a))))
2895 (cons
2896 (cons (/ num b) (car res))
2897 (% num b)))
2898 '(nil . 0)))
2899
2900 (defun math-div-bignum-big (a b alen blen) ; [l.l l L]
2901 (if (< alen blen)
2902 (cons nil a)
2903 (let* ((res (math-div-bignum-big (cdr a) b (1- alen) blen))
2904 (num (cons (car a) (cdr res)))
2905 (res2 (math-div-bignum-part num b blen)))
2906 (cons
2907 (cons (car res2) (car res))
2908 (cdr res2)))))
2909
2910 (defun math-div-bignum-part (a b blen) ; a < b*1000 [D.l l L]
2911 (let* ((num (+ (* (or (nth blen a) 0) 1000) (or (nth (1- blen) a) 0)))
2912 (den (nth (1- blen) b))
2913 (guess (min (/ num den) 999)))
2914 (math-div-bignum-try a b (math-mul-bignum-digit b guess 0) guess)))
2915
2916 (defun math-div-bignum-try (a b c guess) ; [D.l l l D]
2917 (let ((rem (math-sub-bignum a c)))
2918 (if (eq rem 'neg)
2919 (math-div-bignum-try a b (math-sub-bignum c b) (1- guess))
2920 (cons guess rem))))
2921
2922
2923 ;;; Compute the quotient of A and B. [O O N] [Public]
2924 (defun math-div (a b)
2925 (or
2926 (and (Math-zerop b)
2927 (calc-extensions)
2928 (math-div-by-zero a b))
2929 (and (Math-zerop a) (not (eq (car-safe b) 'mod))
2930 (if (Math-scalarp b)
2931 (if (and (math-floatp b) (Math-ratp a)) (math-float a) a)
2932 (calc-extensions)
2933 (math-div-zero a b)))
2934 (and (Math-objvecp a) (Math-objvecp b)
2935 (or
2936 (and (Math-integerp a) (Math-integerp b)
2937 (let ((q (math-idivmod a b)))
2938 (if (eq (cdr q) 0)
2939 (car q)
2940 (if calc-prefer-frac
2941 (progn
2942 (calc-extensions)
2943 (math-make-frac a b))
2944 (math-div-float (math-make-float a 0)
2945 (math-make-float b 0))))))
2946 (and (Math-ratp a) (Math-ratp b)
2947 (calc-extensions)
2948 (calc-div-fractions a b))
2949 (and (Math-realp a) (Math-realp b)
2950 (progn
2951 (or (and (consp a) (eq (car a) 'float))
2952 (setq a (math-float a)))
2953 (or (and (consp b) (eq (car b) 'float))
2954 (setq b (math-float b)))
2955 (math-div-float a b)))
2956 (and (calc-extensions)
2957 (math-div-objects-fancy a b))))
2958 (and (calc-extensions)
2959 (math-div-symb-fancy a b))))
2960
2961 (defun math-div-float (a b) ; [F F F]
2962 (let ((ldiff (max (- (1+ calc-internal-prec)
2963 (- (math-numdigs (nth 1 a)) (math-numdigs (nth 1 b))))
2964 0)))
2965 (math-make-float (math-quotient (math-scale-int (nth 1 a) ldiff) (nth 1 b))
2966 (- (- (nth 2 a) (nth 2 b)) ldiff))))
2967
2968
2969
2970
2971
2972 ;;; Format the number A as a string. [X N; X Z] [Public]
2973 (defun math-format-stack-value (entry)
2974 (setq calc-selection-cache-entry calc-selection-cache-default-entry)
2975 (let* ((a (car entry))
2976 (math-comp-selected (nth 2 entry))
2977 (c (cond ((null a) "<nil>")
2978 ((eq calc-display-raw t) (format "%s" a))
2979 ((stringp a) a)
2980 ((eq a 'top-of-stack) ".")
2981 (calc-prepared-composition
2982 calc-prepared-composition)
2983 ((and (Math-scalarp a)
2984 (memq calc-language '(nil flat unform))
2985 (null math-comp-selected))
2986 (math-format-number a))
2987 (t (calc-extensions)
2988 (math-compose-expr a 0))))
2989 (off (math-stack-value-offset c))
2990 s w)
2991 (and math-comp-selected (setq calc-any-selections t))
2992 (setq w (cdr off)
2993 off (car off))
2994 (if (> off 0)
2995 (setq c (math-comp-concat (make-string off ? ) c)))
2996 (or (equal calc-left-label "")
2997 (setq c (math-comp-concat (if (eq a 'top-of-stack)
2998 (make-string (length calc-left-label) ? )
2999 calc-left-label)
3000 c)))
3001 (if calc-line-numbering
3002 (setq c (math-comp-concat (if (eq calc-language 'big)
3003 (if math-comp-selected
3004 '(tag t "1: ") "1: ")
3005 " ")
3006 c)))
3007 (or (equal calc-right-label "")
3008 (eq a 'top-of-stack)
3009 (progn
3010 (calc-extensions)
3011 (setq c (list 'horiz c
3012 (make-string (max (- w (math-comp-width c)
3013 (length calc-right-label)) 0) ? )
3014 '(break -1)
3015 calc-right-label))))
3016 (setq s (if (stringp c)
3017 (if calc-display-raw
3018 (prin1-to-string c)
3019 c)
3020 (math-composition-to-string c w)))
3021 (if calc-language-output-filter
3022 (setq s (funcall calc-language-output-filter s)))
3023 (if (eq calc-language 'big)
3024 (setq s (concat s "\n"))
3025 (if calc-line-numbering
3026 (progn
3027 (aset s 0 ?1)
3028 (aset s 1 ?:))))
3029 (setcar (cdr entry) (calc-count-lines s))
3030 s))
3031
3032 (defun math-stack-value-offset (c)
3033 (let* ((num (if calc-line-numbering 4 0))
3034 (wid (calc-window-width))
3035 off)
3036 (if calc-display-just
3037 (progn
3038 (calc-extensions)
3039 (math-stack-value-offset-fancy))
3040 (setq off (or calc-display-origin 0))
3041 (if (integerp calc-line-breaking)
3042 (setq wid calc-line-breaking)))
3043 (cons (max (- off (length calc-left-label)) 0)
3044 (+ wid num))))
3045
3046 (defun calc-count-lines (s)
3047 (let ((pos 0)
3048 (num 1))
3049 (while (setq newpos (string-match "\n" s pos))
3050 (setq pos (1+ newpos)
3051 num (1+ num)))
3052 num))
3053
3054 (defun math-format-value (a &optional w)
3055 (if (and (Math-scalarp a)
3056 (memq calc-language '(nil flat unform)))
3057 (math-format-number a)
3058 (calc-extensions)
3059 (let ((calc-line-breaking nil))
3060 (math-composition-to-string (math-compose-expr a 0) w))))
3061
3062 (defun calc-window-width ()
3063 (if calc-embedded-info
3064 (let ((win (get-buffer-window (aref calc-embedded-info 0))))
3065 (1- (if win (window-width win) (frame-width))))
3066 (- (window-width (get-buffer-window (current-buffer)))
3067 (if calc-line-numbering 5 1))))
3068
3069 (defun math-comp-concat (c1 c2)
3070 (if (and (stringp c1) (stringp c2))
3071 (concat c1 c2)
3072 (list 'horiz c1 c2)))
3073
3074
3075
3076 ;;; Format an expression as a one-line string suitable for re-reading.
3077
3078 (defun math-format-flat-expr (a prec)
3079 (cond
3080 ((or (not (or (consp a) (integerp a)))
3081 (eq calc-display-raw t))
3082 (let ((print-escape-newlines t))
3083 (concat "'" (prin1-to-string a))))
3084 ((Math-scalarp a)
3085 (let ((calc-group-digits nil)
3086 (calc-point-char ".")
3087 (calc-frac-format (if (> (length (car calc-frac-format)) 1)
3088 '("::" nil) '(":" nil)))
3089 (calc-complex-format nil)
3090 (calc-hms-format "%s@ %s' %s\"")
3091 (calc-language nil))
3092 (math-format-number a)))
3093 (t
3094 (calc-extensions)
3095 (math-format-flat-expr-fancy a prec))))
3096
3097
3098
3099 ;;; Format a number as a string.
3100 (defun math-format-number (a &optional prec) ; [X N] [Public]
3101 (cond
3102 ((eq calc-display-raw t) (format "%s" a))
3103 ((and (nth 1 calc-frac-format) (Math-integerp a))
3104 (calc-extensions)
3105 (math-format-number (math-adjust-fraction a)))
3106 ((integerp a)
3107 (if (not (or calc-group-digits calc-leading-zeros))
3108 (if (= calc-number-radix 10)
3109 (int-to-string a)
3110 (if (< a 0)
3111 (concat "-" (math-format-number (- a)))
3112 (calc-extensions)
3113 (if math-radix-explicit-format
3114 (if calc-radix-formatter
3115 (funcall calc-radix-formatter
3116 calc-number-radix
3117 (if (= calc-number-radix 2)
3118 (math-format-binary a)
3119 (math-format-radix a)))
3120 (format "%d#%s" calc-number-radix
3121 (if (= calc-number-radix 2)
3122 (math-format-binary a)
3123 (math-format-radix a))))
3124 (math-format-radix a))))
3125 (math-format-number (math-bignum a))))
3126 ((stringp a) a)
3127 ((not (consp a)) (prin1-to-string a))
3128 ((eq (car a) 'bigpos) (math-format-bignum (cdr a)))
3129 ((eq (car a) 'bigneg) (concat "-" (math-format-bignum (cdr a))))
3130 ((and (eq (car a) 'float) (= calc-number-radix 10))
3131 (if (Math-integer-negp (nth 1 a))
3132 (concat "-" (math-format-number (math-neg a)))
3133 (let ((mant (nth 1 a))
3134 (exp (nth 2 a))
3135 (fmt (car calc-float-format))
3136 (figs (nth 1 calc-float-format))
3137 (point calc-point-char)
3138 str)
3139 (if (and (eq fmt 'fix)
3140 (or (and (< figs 0) (setq figs (- figs)))
3141 (> (+ exp (math-numdigs mant)) (- figs))))
3142 (progn
3143 (setq mant (math-scale-rounding mant (+ exp figs))
3144 str (if (integerp mant)
3145 (int-to-string mant)
3146 (math-format-bignum-decimal (cdr mant))))
3147 (if (<= (length str) figs)
3148 (setq str (concat (make-string (1+ (- figs (length str))) ?0)
3149 str)))
3150 (if (> figs 0)
3151 (setq str (concat (substring str 0 (- figs)) point
3152 (substring str (- figs))))
3153 (setq str (concat str point)))
3154 (if calc-group-digits
3155 (setq str (math-group-float str))))
3156 (if (< figs 0)
3157 (setq figs (+ calc-internal-prec figs)))
3158 (if (> figs 0)
3159 (let ((adj (- figs (math-numdigs mant))))
3160 (if (< adj 0)
3161 (setq mant (math-scale-rounding mant adj)
3162 exp (- exp adj)))))
3163 (setq str (if (integerp mant)
3164 (int-to-string mant)
3165 (math-format-bignum-decimal (cdr mant))))
3166 (let* ((len (length str))
3167 (dpos (+ exp len)))
3168 (if (and (eq fmt 'float)
3169 (<= dpos (+ calc-internal-prec calc-display-sci-high))
3170 (>= dpos (+ calc-display-sci-low 2)))
3171 (progn
3172 (cond
3173 ((= dpos 0)
3174 (setq str (concat "0" point str)))
3175 ((and (<= exp 0) (> dpos 0))
3176 (setq str (concat (substring str 0 dpos) point
3177 (substring str dpos))))
3178 ((> exp 0)
3179 (setq str (concat str (make-string exp ?0) point)))
3180 (t ; (< dpos 0)
3181 (setq str (concat "0" point
3182 (make-string (- dpos) ?0) str))))
3183 (if calc-group-digits
3184 (setq str (math-group-float str))))
3185 (let* ((eadj (+ exp len))
3186 (scale (if (eq fmt 'eng)
3187 (1+ (math-mod (+ eadj 300002) 3))
3188 1)))
3189 (if (> scale (length str))
3190 (setq str (concat str (make-string (- scale (length str))
3191 ?0))))
3192 (if (< scale (length str))
3193 (setq str (concat (substring str 0 scale) point
3194 (substring str scale))))
3195 (if calc-group-digits
3196 (setq str (math-group-float str)))
3197 (setq str (format (if (memq calc-language '(math maple))
3198 (if (and prec (> prec 191))
3199 "(%s*10.^%d)" "%s*10.^%d")
3200 "%se%d")
3201 str (- eadj scale)))))))
3202 str)))
3203 (t
3204 (calc-extensions)
3205 (math-format-number-fancy a prec))))
3206
3207 (defun math-format-bignum (a) ; [X L]
3208 (if (and (= calc-number-radix 10)
3209 (not calc-leading-zeros)
3210 (not calc-group-digits))
3211 (math-format-bignum-decimal a)
3212 (calc-extensions)
3213 (math-format-bignum-fancy a)))
3214
3215 (defun math-format-bignum-decimal (a) ; [X L]
3216 (if a
3217 (let ((s ""))
3218 (while (cdr (cdr a))
3219 (setq s (concat (format "%06d" (+ (* (nth 1 a) 1000) (car a))) s)
3220 a (cdr (cdr a))))
3221 (concat (int-to-string (+ (* (or (nth 1 a) 0) 1000) (car a))) s))
3222 "0"))
3223
3224
3225
3226 ;;; Parse a simple number in string form. [N X] [Public]
3227 (defun math-read-number (s)
3228 (math-normalize
3229 (cond
3230
3231 ;; Integers (most common case)
3232 ((string-match "\\` *\\([0-9]+\\) *\\'" s)
3233 (let ((digs (math-match-substring s 1)))
3234 (if (and (eq calc-language 'c)
3235 (> (length digs) 1)
3236 (eq (aref digs 0) ?0))
3237 (math-read-number (concat "8#" digs))
3238 (if (<= (length digs) 6)
3239 (string-to-int digs)
3240 (cons 'bigpos (math-read-bignum digs))))))
3241
3242 ;; Clean up the string if necessary
3243 ((string-match "\\`\\(.*\\)[ \t\n]+\\([^\001]*\\)\\'" s)
3244 (math-read-number (concat (math-match-substring s 1)
3245 (math-match-substring s 2))))
3246
3247 ;; Plus and minus signs
3248 ((string-match "^[-_+]\\(.*\\)$" s)
3249 (let ((val (math-read-number (math-match-substring s 1))))
3250 (and val (if (eq (aref s 0) ?+) val (math-neg val)))))
3251
3252 ;; Forms that require extensions module
3253 ((string-match "[^-+0-9eE.]" s)
3254 (calc-extensions)
3255 (math-read-number-fancy s))
3256
3257 ;; Decimal point
3258 ((string-match "^\\([0-9]*\\)\\.\\([0-9]*\\)$" s)
3259 (let ((int (math-match-substring s 1))
3260 (frac (math-match-substring s 2)))
3261 (let ((ilen (length int))
3262 (flen (length frac)))
3263 (let ((int (if (> ilen 0) (math-read-number int) 0))
3264 (frac (if (> flen 0) (math-read-number frac) 0)))
3265 (and int frac (or (> ilen 0) (> flen 0))
3266 (list 'float
3267 (math-add (math-scale-int int flen) frac)
3268 (- flen)))))))
3269
3270 ;; "e" notation
3271 ((string-match "^\\(.*\\)[eE]\\([-+]?[0-9]+\\)$" s)
3272 (let ((mant (math-match-substring s 1))
3273 (exp (math-match-substring s 2)))
3274 (let ((mant (if (> (length mant) 0) (math-read-number mant) 1))
3275 (exp (if (<= (length exp) (if (memq (aref exp 0) '(?+ ?-)) 8 7))
3276 (string-to-int exp))))
3277 (and mant exp (Math-realp mant) (> exp -4000000) (< exp 4000000)
3278 (let ((mant (math-float mant)))
3279 (list 'float (nth 1 mant) (+ (nth 2 mant) exp)))))))
3280
3281 ;; Syntax error!
3282 (t nil))))
3283
3284 (defun math-match-substring (s n)
3285 (if (match-beginning n)
3286 (substring s (match-beginning n) (match-end n))
3287 ""))
3288
3289 (defun math-read-bignum (s) ; [l X]
3290 (if (> (length s) 3)
3291 (cons (string-to-int (substring s -3))
3292 (math-read-bignum (substring s 0 -3)))
3293 (list (string-to-int s))))
3294
3295
3296 (defconst math-tex-ignore-words
3297 '( ("\\hbox") ("\\mbox") ("\\text") ("\\left") ("\\right")
3298 ("\\,") ("\\>") ("\\:") ("\\;") ("\\!") ("\\ ")
3299 ("\\quad") ("\\qquad") ("\\hfil") ("\\hfill")
3300 ("\\displaystyle") ("\\textstyle") ("\\dsize") ("\\tsize")
3301 ("\\scriptstyle") ("\\scriptscriptstyle") ("\\ssize") ("\\sssize")
3302 ("\\rm") ("\\bf") ("\\it") ("\\sl")
3303 ("\\roman") ("\\bold") ("\\italic") ("\\slanted")
3304 ("\\cal") ("\\mit") ("\\Cal") ("\\Bbb") ("\\frak") ("\\goth")
3305 ("\\evalto")
3306 ("\\matrix" mat) ("\\bmatrix" mat) ("\\pmatrix" mat)
3307 ("\\cr" punc ";") ("\\\\" punc ";") ("\\*" punc "*")
3308 ("\\{" punc "[") ("\\}" punc "]")
3309 ))
3310
3311 (defconst math-eqn-ignore-words
3312 '( ("roman") ("bold") ("italic") ("mark") ("lineup") ("evalto")
3313 ("left" ("floor") ("ceil"))
3314 ("right" ("floor") ("ceil"))
3315 ("arc" ("sin") ("cos") ("tan") ("sinh") ("cosh") ("tanh"))
3316 ("size" n) ("font" n) ("fwd" n) ("back" n) ("up" n) ("down" n)
3317 ("above" punc ",")
3318 ))
3319
3320 (defconst math-standard-opers
3321 '( ( "_" calcFunc-subscr 1200 1201 )
3322 ( "%" calcFunc-percent 1100 -1 )
3323 ( "u+" ident -1 1000 )
3324 ( "u-" neg -1 1000 197 )
3325 ( "u!" calcFunc-lnot -1 1000 )
3326 ( "mod" mod 400 400 185 )
3327 ( "+/-" sdev 300 300 185 )
3328 ( "!!" calcFunc-dfact 210 -1 )
3329 ( "!" calcFunc-fact 210 -1 )
3330 ( "^" ^ 201 200 )
3331 ( "**" ^ 201 200 )
3332 ( "*" * 196 195 )
3333 ( "2x" * 196 195 )
3334 ( "/" / 190 191 )
3335 ( "%" % 190 191 )
3336 ( "\\" calcFunc-idiv 190 191 )
3337 ( "+" + 180 181 )
3338 ( "-" - 180 181 )
3339 ( "|" | 170 171 )
3340 ( "<" calcFunc-lt 160 161 )
3341 ( ">" calcFunc-gt 160 161 )
3342 ( "<=" calcFunc-leq 160 161 )
3343 ( ">=" calcFunc-geq 160 161 )
3344 ( "=" calcFunc-eq 160 161 )
3345 ( "==" calcFunc-eq 160 161 )
3346 ( "!=" calcFunc-neq 160 161 )
3347 ( "&&" calcFunc-land 110 111 )
3348 ( "||" calcFunc-lor 100 101 )
3349 ( "?" (math-read-if) 91 90 )
3350 ( "!!!" calcFunc-pnot -1 85 )
3351 ( "&&&" calcFunc-pand 80 81 )
3352 ( "|||" calcFunc-por 75 76 )
3353 ( ":=" calcFunc-assign 51 50 )
3354 ( "::" calcFunc-condition 45 46 )
3355 ( "=>" calcFunc-evalto 40 41 )
3356 ( "=>" calcFunc-evalto 40 -1 )))
3357 (defvar math-expr-opers math-standard-opers)
3358
3359 ;;;###autoload
3360 (defun calc-grab-region (top bot arg)
3361 "Parse the region as a vector of numbers and push it on the Calculator stack."
3362 (interactive "r\nP")
3363 (calc-extensions)
3364 (calc-do-grab-region top bot arg))
3365
3366 ;;;###autoload
3367 (defun calc-grab-rectangle (top bot arg)
3368 "Parse a rectangle as a matrix of numbers and push it on the Calculator stack."
3369 (interactive "r\nP")
3370 (calc-extensions)
3371 (calc-do-grab-rectangle top bot arg))
3372
3373 (defun calc-grab-sum-down (top bot arg)
3374 "Parse a rectangle as a matrix of numbers and sum its columns."
3375 (interactive "r\nP")
3376 (calc-extensions)
3377 (calc-do-grab-rectangle top bot arg 'calcFunc-reduced))
3378
3379 (defun calc-grab-sum-across (top bot arg)
3380 "Parse a rectangle as a matrix of numbers and sum its rows."
3381 (interactive "r\nP")
3382 (calc-extensions)
3383 (calc-do-grab-rectangle top bot arg 'calcFunc-reducea))
3384
3385
3386 ;;;###autoload
3387 (defun calc-embedded (arg &optional end obeg oend)
3388 "Start Calc Embedded mode on the formula surrounding point."
3389 (interactive "P")
3390 (calc-extensions)
3391 (calc-do-embedded arg end obeg oend))
3392
3393 ;;;###autoload
3394 (defun calc-embedded-activate (&optional arg cbuf)
3395 "Scan the current editing buffer for all embedded := and => formulas.
3396 Also looks for the equivalent TeX words, \\gets and \\evalto."
3397 (interactive "P")
3398 (calc-do-embedded-activate arg cbuf))
3399
3400
3401 (defun calc-user-invocation ()
3402 (interactive)
3403 (or (stringp calc-invocation-macro)
3404 (error "Use `Z I' inside Calc to define a `M-# Z' keyboard macro"))
3405 (execute-kbd-macro calc-invocation-macro nil))
3406
3407
3408
3409
3410 ;;; User-programmability.
3411
3412 ;;;###autoload
3413 (defmacro defmath (func args &rest body) ; [Public]
3414 (calc-extensions)
3415 (math-do-defmath func args body))
3416
3417
3418 ;;; Functions needed for Lucid Emacs support.
3419
3420 (defun calc-read-key (&optional optkey)
3421 (cond (calc-emacs-type-lucid
3422 (let ((event (next-command-event)))
3423 (let ((key (event-to-character event t t)))
3424 (or key optkey (error "Expected a plain keystroke"))
3425 (cons key event))))
3426 (calc-emacs-type-gnu19
3427 (let ((key (read-event)))
3428 (cons key key)))
3429 (t
3430 (let ((key (read-char)))
3431 (cons key key)))))
3432
3433 (defun calc-unread-command (&optional input)
3434 (if (featurep 'xemacs)
3435 (setq unread-command-event
3436 (if (integerp input) (character-to-event input)
3437 (or input last-command-event)))
3438 (push (or input last-command-event) unread-command-events)))
3439
3440 (defun calc-clear-unread-commands ()
3441 (if (featurep 'xemacs)
3442 (calc-emacs-type-lucid (setq unread-command-event nil))
3443 (setq unread-command-events nil)))
3444
3445 (if calc-always-load-extensions
3446 (progn
3447 (calc-extensions)
3448 (calc-load-everything)))
3449
3450
3451 (run-hooks 'calc-load-hook)
3452
3453 ;;; calc.el ends here