1 ;;; calc.el --- the GNU Emacs calculator
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
7 ;; Keywords: convenience, extensions
10 ;; This file is part of GNU Emacs.
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.
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.
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.
36 ;; Original author's address:
37 ;; Dave Gillespie, daveg@synaptics.com, uunet!synaptx!daveg.
38 ;; Synaptics, Inc., 2698 Orchard Parkway, San Jose, CA 95134.
40 ;; The old address daveg@csvax.cs.caltech.edu will continue to
41 ;; work for the foreseeable future.
43 ;; Bug reports and suggestions are always welcome! (Type M-x
44 ;; report-calc-bug to send them).
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
54 ;; Fix rewrite mechanism to do less gratuitous rearrangement of terms.
55 ;; Implement a pattern-based "refers" predicate.
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.
62 ;; Implement line-breaking in non-flat compositions (is this desirable?).
63 ;; Implement matrix formatting with multi-line components.
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.
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.
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.
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.
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.
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.
91 ;; Support polar plotting with GNUPLOT.
92 ;; Make a calc-graph-histogram function.
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.
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).
111 ;; Tim suggests adding spreadsheet-like features.
112 ;; Implement language modes for Gnuplot, Lisp, Ada, APL, ...?
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.
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.
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)
149 ;; Could you add logistic curve fitting to the current list?
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.)
155 ;; Here is the current list for curve fitting;
158 ;; Linear or multilinear. a + b x + c y + d z.
161 ;; Polynomials. a + b x + c x^2 + d x^3.
164 ;; Exponential. a exp(b x) exp(c y).
167 ;; Base-10 exponential. a 10^(b x) 10^(c y).
170 ;; Exponential (alternate notation). exp(a + b x + c y).
173 ;; Base-10 exponential (alternate). 10^(a + b x + c y).
176 ;; Logarithmic. a + b ln(x) + c ln(y).
179 ;; Base-10 logarithmic. a + b log10(x) + c log10(y).
182 ;; General exponential. a b^x c^y.
185 ;; Power law. a x^b y^c.
188 ;; Quadratic. a + b (x-c)^2 + d (x-e)^2.
191 ;; Gaussian. (a / b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2).
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.
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.)
209 ;;; The "###autoload" comment will be used by Emacs version 19 for
210 ;;; maintaining the loaddefs.el file automatically.
213 (defvar calc-info-filename
"calc.info"
214 "*File name in which to look for the Calculator's Info documentation.")
217 (defvar calc-settings-file user-init-file
218 "*File in which to record permanent settings; default is `user-init-file'.")
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.")
228 (defvar calc-gnuplot-name
"gnuplot"
229 "*Name of GNUPLOT program, for calc-graph features.")
232 (defvar calc-gnuplot-plot-command nil
233 "*Name of command for displaying GNUPLOT output; %s = file name to print.")
236 (defvar calc-gnuplot-print-command
"lp %s"
237 "*Name of command for printing GNUPLOT output; %s = file name to print.")
239 ;; Address of the author of Calc, for use by report-calc-bug.
240 (defvar calc-bug-address
"walters@debian.org")
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
)
246 (defvar calc-extensions-loaded nil
)
249 ;; Entries are 3-lists: Formula, Height (in lines), Selection (or nil).
250 (defvar calc-stack
'((top-of-stack 1 nil
)))
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)
256 ;; If non-nil, load the calc-ext module automatically when calc is loaded.
257 ;;(defvar calc-always-load-extensions nil)
259 ;; If non-nil, display line numbers in Calculator stack.
260 ;;(defvar calc-line-numbering t)
262 ;; If non-nil, break long values across multiple lines in Calculator stack.
263 ;;(defvar calc-line-breaking t)
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)
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)
276 ;; Radix for entry and display of numbers in calc-mode, 2-36.
277 ;;(defvar calc-number-radix 10)
279 ;; If non-nil, leading zeros are provided to pad integers to calc-word-size.
280 ;;(defvar calc-leading-zeros nil)
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)
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 ",")
291 ;; The character (in the form of a string) to be used as a decimal point.
292 ;;(defvar calc-point-char ".")
294 ;; Format of displayed fractions; a string of one or two of ":" or "/".
295 ;;(defvar calc-frac-format '(":" nil))
297 ;; If non-nil, prefer fractional over floating-point results.
298 ;;(defvar calc-prefer-frac nil)
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\"")
304 ;; Format of displayed date forms.
305 ;;(defvar calc-date-format '((H ":" mm ":" SS pp " ") Www " " Mmm " " D ", " YYYY))
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))
321 ;; Format to use when full precision must be displayed.
322 ;;(defvar calc-full-float-format '(float 0))
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)
330 ;; Preferred form, either 'cplx or 'polar, for complex numbers.
331 ;;(defvar calc-complex-mode 'cplx)
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)
338 ;; If non-nil, display vectors of byte-sized integers as strings.
339 ;;(defvar calc-display-strings nil)
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)
346 ;; If non-nil, display vectors one element per line.
347 ;;(defvar calc-break-vectors nil)
349 ;; If non-nil, display long vectors in full. If nil, use abbreviated form.
350 ;;(defvar calc-full-vectors t)
352 ;; If non-nil, display long vectors in full in the trail.
353 ;;(defvar calc-full-trail-vectors t)
355 ;; If non-nil, separate elements of displayed vectors with this string.
356 ;;(defvar calc-vector-commas ",")
358 ;; If non-nil, surround displayed vectors with these characters.
359 ;;(defvar calc-vector-brackets "[]")
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))
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)
381 ;; Numeric prefix argument for the command that set calc-language.
382 ;;(defvar calc-language-option nil)
384 ;; Open-parenthesis string for function call notation.
385 ;;(defvar calc-function-open "(")
387 ;; Close-parenthesis string for function call notation.
388 ;;(defvar calc-function-close ")")
390 ;; Function through which to pass strings after formatting.
391 ;;(defvar calc-language-output-filter nil)
393 ;; Function through which to pass strings before parsing.
394 ;;(defvar calc-language-input-filter nil)
396 ;; Formatting function used for non-decimal numbers.
397 ;;(defvar calc-radix-formatter nil)
399 ;; Label to display at left of formula.
400 ;;(defvar calc-left-label "")
402 ;; Label to display at right of formula.
403 ;;(defvar calc-right-label "")
405 ;; Minimum number of bits per word, if any, for binary operations in calc-mode.
406 ;;(defvar calc-word-size 32)
408 ;; Most recently used value of M in a modulo form.
409 ;;(defvar calc-previous-modulo nil)
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)
421 ;; If non-nil, recompute evalto's automatically when necessary.
422 ;;(defvar calc-auto-recompute t)
424 ;; If non-nil, display shows unformatted Lisp exprs. (For debugging)
425 ;;(defvar calc-display-raw nil)
427 ;; Number of digits of internal precision for calc-mode calculations.
428 ;;(defvar calc-internal-prec 12)
430 ;; If non-nil, next operation is Inverse.
431 ;;(defvar calc-inverse-flag nil)
433 ;; If non-nil, next operation is Hyperbolic.
434 ;;(defvar calc-hyperbolic-flag nil)
436 ;; If non-nil, next operation should not remove its arguments from stack.
437 ;;(defvar calc-keep-args-flag nil)
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)
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)
447 ;; Like calc-algebraic-mode except only affects ( and [ keys.
448 ;;(defvar calc-incomplete-algebraic-mode nil)
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)
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)
460 ;; If non-nil, shifted letter keys are prefix keys rather than normal meanings.
461 ;;(defvar calc-shift-prefix nil)
463 ;; Initial height of Calculator window.
464 ;;(defvar calc-window-height 7)
466 ;; If non-nil, M-x calc creates a window to display Calculator trail.
467 ;;(defvar calc-display-trail t)
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)
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)
477 ;; If non-nil, selection hides deep structure of associative formulas.
478 ;;(defvar calc-assoc-selections t)
480 ;; If non-nil, display "Working..." for potentially slow Calculator commands.
481 ;;(defvar calc-display-working-message 'lots)
483 ;; If non-nil, automatically execute a "why" command to explain odd results.
484 ;;(defvar calc-auto-why nil)
486 ;; If non-nil, display timing information on each slow command.
487 ;;(defvar calc-timing nil)
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)
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)
498 ;; List of used-defined strings to append to Calculator mode line.
499 (defvar calc-other-modes nil
)
501 ;; List of strings for Y prefix help.
502 (defvar calc-Y-help-msgs nil
)
504 ;; t if calc-settings-file has been loaded yet.
505 (defvar calc-loaded-settings-file nil
)
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
)
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
529 "<H:mm:SSpp >Www Mmm D, YYYY"
530 "D Mmm YYYY<, h:mm:SS>"
531 "Www Mmm BD< hh:mm:ss> YYYY"
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
)
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
)
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
)
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
)))
586 (defconst calc-local-var-list
'(calc-stack
590 calc-always-load-extensions
601 calc-incomplete-algebraic-mode
617 calc-standard-date-formats
619 calc-full-float-format
623 calc-full-trail-vectors
637 calc-assoc-selections
642 (defun calc-init-base ()
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
)))
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
))
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
))))
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
)))))
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
)
674 (name2 (concat (file-name-directory name
)
676 (while (and p2
(not (file-exists-p
677 (expand-file-name name2
(car p2
)))))
680 (setq load-path
(nconc load-path
685 name
(car p2
))))))))))
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
)))))))
694 ;; The following modes use specially-formatted data.
695 (put 'calc-mode
'mode-class
'special
)
696 (put 'calc-trail-mode
'mode-class
'special
)
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)")
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")
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.
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
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)))
760 (mapcar (function (lambda (v) (or (boundp (car v
)) (set (car v
) (nth 1 v
)))))
762 (mapcar (function (lambda (v) (or (boundp v
) (set v nil
))))
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
)
795 (define-key calc-mode-map
(char-to-string x
) 'undefined
)))
799 (define-key calc-mode-map
(char-to-string x
)
801 (concat "ABCDEFGHIJKLMNPQRSTUVXZabcdfghjkmoprstuvwxyz"
802 ":\\|!()[]<>{},;=~`\C-k\M-k\C-w\M-w\C-y\C-_"))
805 (define-key calc-mode-map
(char-to-string x
) 'calcDigit-start
)))
808 (setq calc-digit-map
(make-keymap))
809 (if calc-emacs-type-lucid
810 (map-keymap (function
812 (define-key calc-digit-map keys
813 (if (eq bind
'undefined
)
814 'undefined
'calcDigit-nondigit
))))
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
)
822 (if (eq (aref cmap i
) 'undefined
)
823 'undefined
'calcDigit-nondigit
))
827 (define-key calc-digit-map
(char-to-string x
)
829 "_0123456789.e+-:n#@oh'\"mspM")
832 (define-key calc-digit-map
(char-to-string x
)
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
)
843 (define-key calc-digit-map x
'calcDigit-backspace
)
844 (define-key calc-mode-map x
'calc-pop
)
845 (define-key calc-mode-map
847 (if calc-emacs-type-lucid
849 (vector (if (consp (aref x
0))
850 (cons 'meta
(aref x
0))
851 (list 'meta
(aref x
0))))
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
)
863 (setq calc-dispatch-map
(make-keymap))
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
)
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
)
881 ( ?j . calc-embedded-select
)
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
)
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
)
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
)
903 ( ?
# . calc-same-interface
)
904 ( ?? . calc-dispatch-help
) ))
907 (autoload 'calc-extensions
"calc-ext")
908 (autoload 'calc-need-macros
"calc-macs")
910 ;;;; (Autoloads here)
911 (mapcar (function (lambda (x)
912 (mapcar (function (lambda (func)
913 (autoload func
(car x
)))) (cdr x
))))
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
)
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
)
934 (mapcar (function (lambda (x)
935 (mapcar (function (lambda (cmd)
936 (autoload cmd
(car x
) nil t
))) (cdr x
))))
939 ("calc-aent" calc-algebraic-entry calc-auto-algebraic-entry
940 calcDigit-algebraic calcDigit-edit
)
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
952 ;;;###autoload (global-set-key "\e#" 'calc-dispatch)
955 (defun calc-dispatch (&optional arg
)
956 "Invoke the GNU Emacs Calculator. See `calc-dispatch-help' for details."
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
)
965 (and (string-match "\\`[\C-@-\C-_]" keys
)
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
))))
971 (calc-do-dispatch arg
))
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))))
981 (setq key
(lookup-key calc-dispatch-map key
))
985 (or (commandp key
) (calc-extensions))
986 (call-interactively key
))
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
))
1001 (if (commandp (key-binding (if calc-emacs-type-19
1003 (char-to-string (cdr key
)))))
1005 (use-global-map glob
)
1006 (use-local-map loc
)))))
1011 "Calculator major mode.
1013 This is an RPN calculator featuring arbitrary-precision integer, rational,
1014 floating-point, complex, matrix, and symbolic arithmetic.
1016 RPN calculation: 2 RET 3 + produces 5.
1017 Algebraic style: ' 2+3 RET produces 5.
1019 Basic operators are +, -, *, /, ^, & (reciprocal), % (modulo), n (change-sign).
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.
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)
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
)
1056 (setq calc-stack
(list (list 'top-of-stack
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
)
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
1068 (let ((p command-line-args
))
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
))
1075 (run-hooks 'calc-mode-hook
)
1077 (calc-set-mode-line)
1078 ;; The calc-defs variable is a relic. Use calc-define properties instead.
1079 (if (and (boundp 'calc-defs
)
1082 (message "Evaluating calc-defs...")
1084 (eval (cons 'progn calc-defs
))
1085 (setq calc-defs nil
)
1086 (calc-set-mode-line)))
1087 (calc-check-defines))
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
))))
1099 (set-buffer "*Calculator*")
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
)))))
1108 (defun calc-trail-mode (&optional buf
)
1110 This mode is used by the *Calc Trail* buffer, which records all results
1111 obtained by the GNU Emacs Calculator.
1113 Calculator commands beginning with the `t' key are used to manipulate
1116 This buffer uses the same key map as the *Calculator* buffer; calculator
1117 commands given here will actually operate on the *Calculator* stack."
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
)
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
))
1136 (defun calc-create-buffer ()
1137 (set-buffer (get-buffer-create "*Calculator*"))
1138 (or (eq major-mode
'calc-mode
)
1140 (setq max-lisp-eval-depth
(max max-lisp-eval-depth
1000))
1141 (if calc-always-load-extensions
1146 (calc-set-language calc-language calc-language-option t
))))
1149 (defun calc (&optional arg full-display interactive
)
1150 "The Emacs Calculator. Full documentation is listed under \"calc-mode\"."
1156 (if (= (prefix-numeric-value arg
) -
1)
1157 (calc-grab-region (region-beginning) (region-end) nil
)
1158 (if (= (prefix-numeric-value arg
) -
2)
1160 (if (get-buffer-window "*Calc Keypad*")
1163 (set-buffer (window-buffer (selected-window)))))
1164 (if (eq major-mode
'calc-mode
)
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)))
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)
1189 (set-window-buffer w
(current-buffer))
1191 (pop-to-buffer (current-buffer)))))))
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
)
1208 (setq calc-said-hello t
)))))
1212 "Invoke the Calculator and give it a full-sized window."
1214 (calc nil t
(interactive-p)))
1216 (defun calc-same-interface (arg)
1217 "Invoke the Calculator using the most recent interface (calc or calc-keypad)."
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
1228 (calc arg calc-full-mode t
))))))
1231 (defun calc-quit (&optional non-fatal
)
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
)
1240 (if (eq major-mode
'MacEdit-mode
)
1241 (MacEdit-cancel-edit)
1242 (if (and (interactive-p)
1244 (eq (current-buffer) (aref calc-embedded-info
0)))
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))
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)))
1262 (calc-delete-windows-keep buf kbuf
)
1263 (delete-windows-on buf
)
1264 (delete-windows-on kbuf
))
1266 (bury-buffer calc-trail-buffer
)
1267 (and kbuf
(bury-buffer kbuf
)))))))
1270 (defun quick-calc ()
1271 "Do a quick calculation in the minibuffer without invoking full Calculator."
1273 (calc-do-quick-calc))
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
))
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."
1290 (calc-do-keypad calc-full-mode
(interactive-p)))
1293 (defun full-calc-keypad ()
1294 "Invoke the Calculator in full-screen \"visual keypad\" mode.
1295 See calc-keypad for details."
1298 (calc-do-keypad t
(interactive-p)))
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
)
1309 (current-time-string)))
1310 (gc-cons-threshold (max gc-cons-threshold
1311 (if calc-timing
2000000 100000))))
1312 (setq calc-aborted-prefix
"")
1316 (if calc-embedded-info
1317 (calc-embedded-select-buffer)
1318 (calc-select-buffer))
1319 (and (eq calc-algebraic-mode
'total
)
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
))
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
)
1332 (if (and (eq (car err
) 'error
)
1333 (stringp (nth 1 err
))
1334 (string-match "max-specpdl-size\\|max-lisp-eval-depth"
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
)
1349 (calcFunc-unixtime (math-parse-date end-time
) 0)
1350 (calcFunc-unixtime (math-parse-date calc-start-time
)
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
)
1360 (goto-line calc-final-point-line
)
1361 (move-to-column calc-final-point-column
))
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
)
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
1380 (defun calc-set-command-flag (f)
1381 (unless (memq f calc-command-flags
)
1382 (setq calc-command-flags
(cons f calc-command-flags
))))
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*")))
1391 (error "Calculator buffer not available"))))))
1393 (defun calc-cursor-stack-index (&optional index
)
1394 (goto-char (point-max))
1395 (forward-line (- (calc-substack-height (or index
1)))))
1397 (defun calc-stack-size ()
1398 (- (length calc-stack
) calc-stack-top
))
1400 (defun calc-substack-height (n)
1403 (setq n
(+ n calc-stack-top
))
1404 (while (and (> n
0) stack
)
1405 (setq sum
(+ sum
(nth 1 (car stack
)))
1410 (defun calc-set-mode-line ()
1412 (calc-select-buffer)
1413 (let* ((fmt (car calc-float-format
))
1414 (figs (nth 1 calc-float-format
))
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)
1421 (substring (buffer-name) 12)
1424 (capitalize (symbol-name calc-angle-mode
))
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[( " "")))
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 ")
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 ")
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 ")
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 ")
1463 (capitalize (symbol-name calc-language
))
1465 (cond ((eq fmt
'float
)
1466 (if (zerop figs
) "" (format "Norm%d " figs
)))
1467 ((eq fmt
'fix
) (format "Fix%d " figs
))
1469 (if (zerop figs
) "Sci " (format "Sci%d " figs
)))
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
)
1480 (if calc-display-origin
1481 (format "Center%d " calc-display-origin
)
1483 (cond ((integerp calc-line-breaking
)
1484 (format "Wid%d " calc-line-breaking
))
1485 (calc-line-breaking "")
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 ")
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
)
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))))))
1515 (defun calc-align-stack-window ()
1516 (if (eq major-mode
'calc-mode
)
1518 (let ((win (get-buffer-window (current-buffer))))
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)))))
1528 (calc-select-buffer)
1529 (calc-align-stack-window))))
1531 (defun calc-check-stack (n)
1532 (if (> n
(calc-stack-size))
1533 (error "Too few elements on stack"))
1535 (error "Invalid argument")))
1537 (defun calc-push-list (vals &optional m sels
)
1539 (if calc-executing-macro
1540 (calc-push-list-in-macro vals m sels
)
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)))
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
)
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
)))
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))
1573 (calc-record-list vals prefix
)
1574 (calc-record (car vals
) prefix
)))
1575 (calc-pop-push-list n vals m sels
))
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)))
1590 (calc-pop-push-record-list n prefix vals m
)
1593 (defun calc-normalize (val)
1594 (if (memq calc-simplify-mode
'(nil none num
))
1595 (math-normalize val
)
1597 (calc-normalize-fancy val
)))
1599 (defun calc-handle-whys ()
1601 (calc-do-handle-whys)))
1604 (defun calc-pop-stack (&optional n m sel-ok
) ; pop N objs at level M of stack.
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
))
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
)))
1616 (calc-select-buffer)
1617 (let ((buffer-read-only nil
))
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
))))))
1631 (defun calc-get-stack-element (x)
1632 (cond ((eq sel-mode
'entry
)
1636 ((or (null (nth 2 x
))
1638 (not calc-use-selections
))
1644 ;; Get the Nth element of the stack (N=1 is the top element).
1645 (defun calc-top (&optional n sel-mode
)
1647 (calc-check-stack n
)
1648 (calc-get-stack-element (nth (+ n calc-stack-top -
1) calc-stack
)))
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
))))
1653 (defun calc-top-list (&optional n m sel-mode
)
1656 (calc-check-stack (+ n m -
1))
1658 (let ((top (copy-sequence (nthcdr (+ m calc-stack-top -
1)
1660 (setcdr (nthcdr (1- n
) top
) nil
)
1661 (nreverse (mapcar 'calc-get-stack-element top
)))))
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
))))
1668 (defun calc-renumber-stack ()
1669 (if calc-line-numbering
1671 (calc-cursor-stack-index 0)
1673 (buffer-read-only nil
)
1674 (stack (nthcdr calc-stack-top calc-stack
)))
1675 (if (re-search-forward "^[0-9]+[:*]" nil t
)
1678 (while (re-search-forward "^[0-9]+[:*]" nil t
)
1679 (let ((buffer-read-only nil
))
1683 (calc-cursor-stack-index 0)))
1684 (while (re-search-backward "^[0-9]+[:*]" nil t
)
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))))
1695 (setq lnum
(1+ lnum
)
1696 stack
(cdr stack
))))))
1697 (and calc-embedded-info
(calc-embedded-stack-change)))
1699 (defun calc-refresh (&optional align
)
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 "\\.$"))
1708 (setq calc-any-selections nil
1709 calc-any-evaltos nil
)
1711 (insert "--- Emacs Calculator Mode ---\n")
1713 (goto-char (point-min))
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
))
1728 (set-buffer (aref calc-embedded-info
1))
1729 (calc-refresh align
)))
1730 (setq calc-refresh-count
(1+ calc-refresh-count
)))
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
))
1741 (let* ((buf (x-get-cut-buffer))
1742 (val (math-read-exprs (calc-clean-newlines buf
))))
1743 (if (eq (car-safe val
) 'error
)
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
)))
1753 ;;;; The Calc Trail buffer.
1755 (defun calc-check-trail-aligned ()
1757 (let ((win (get-buffer-window (current-buffer))))
1759 (pos-visible-in-window-p (1- (point-max)) win
)))))
1761 (defun calc-trail-buffer ()
1762 (and (or (null calc-trail-buffer
)
1763 (null (buffer-name calc-trail-buffer
)))
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*"))
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
))
1775 (set-buffer calc-trail-buffer
)
1777 (setq calc-trail-pointer
(point-marker))))
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
)
1790 (math-showing-full-precision
1791 (math-format-flat-expr val
0)))
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
))))
1804 (let ((win (get-buffer-window buf
)))
1805 (if (and aligned win
(not (memq 'hold-trail calc-command-flags
)))
1807 (goto-char (1- (point-max))))))))
1811 (defun calc-trail-display (flag &optional no-refresh
)
1813 (let ((win (get-buffer-window (calc-trail-buffer))))
1814 (if (setq calc-display-trail
1815 (not (if flag
(memq flag
'(nil 0)) win
)))
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
)))
1823 (setq overlay-arrow-string calc-trail-overlay
1824 overlay-arrow-position calc-trail-pointer
)
1836 (calc-refresh))))))))
1839 (defun calc-trail-here ()
1841 (if (eq major-mode
'calc-trail-mode
)
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)
1854 overlay-arrow-string calc-trail-overlay
1855 overlay-arrow-position calc-trail-pointer
)
1857 (let ((win (get-buffer-window (current-buffer))))
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")))
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
)
1881 (calc-set-command-flag 'undo
))))
1886 ;;; Arithmetic commands.
1888 (defun calc-binary-op (name func arg
&optional ident unary func2
)
1889 (setq calc-aborted-prefix name
)
1891 (calc-enter-result 2 name
(cons (or func2 func
)
1892 (mapcar 'math-check-complete
1893 (calc-top-list 2))))
1895 (calc-binary-op-fancy name func arg ident unary
)))
1897 (defun calc-unary-op (name func arg
&optional func2
)
1898 (setq calc-aborted-prefix name
)
1900 (calc-enter-result 1 name
(list (or func2 func
)
1901 (math-check-complete (calc-top 1))))
1903 (calc-unary-op-fancy name func arg
)))
1906 (defun calc-plus (arg)
1909 (calc-binary-op "+" 'calcFunc-add arg
0 nil
'+)))
1911 (defun calc-minus (arg)
1914 (calc-binary-op "-" 'calcFunc-sub arg
0 'neg
'-
)))
1916 (defun calc-times (arg)
1919 (calc-binary-op "*" 'calcFunc-mul arg
1 nil
'*)))
1921 (defun calc-divide (arg)
1924 (calc-binary-op "/" 'calcFunc-div arg
0 'calcFunc-inv
'/)))
1927 (defun calc-change-sign (arg)
1930 (calc-unary-op "chs" 'neg arg
)))
1934 ;;; Stack management commands.
1936 (defun calc-enter (n)
1940 (calc-push-list (calc-top-list 1 (- n
))))
1942 (calc-push-list (calc-top-list (calc-stack-size))))
1944 (calc-push-list (calc-top-list n
))))))
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
)
1959 (if (and calc-any-selections
1960 (calc-top-selected 1 (- nn
)))
1961 (calc-delete-selection (- nn
))
1962 (calc-pop-stack 1 (- nn
) t
)))
1964 (calc-pop-stack (calc-stack-size) 1 t
))
1966 (if (and calc-any-selections
1968 (calc-top-selected 1 1))
1969 (calc-delete-selection 1)
1970 (calc-pop-stack nn
)))))))
1975 ;;;; Reading a number using the minibuffer.
1977 (defun calcDigit-start ()
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
1991 (catch 'execute-kbd-macro
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")))
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
)
2014 (defsubst calc-minibuffer-size
()
2015 (- (point-max) (minibuffer-prompt-end)))
2017 (defun calcDigit-nondigit ()
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))
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))
2039 (defun calc-minibuffer-contains (rex)
2041 (goto-char (minibuffer-prompt-end))
2044 (defun calcDigit-key ()
2046 (goto-char (point-max))
2047 (if (or (and (memq last-command-char
'(?
+ ?-
))
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 ?
:))
2064 (if (and (calc-minibuffer-contains "[-+]?[0-9]+#\\'")
2065 (eq last-command-char ?.
))
2067 (if (and (calc-minibuffer-contains "[-+]?0*\\([2-9]\\|1[0-4]\\)#\\'")
2068 (eq last-command-char ?e
))
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
)))
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
)))
2083 (and (not (calc-minibuffer-contains "[-+]?\\(1[5-9]\\|[2-9][0-9]\\)#.*"))
2084 (search-forward "e" nil t
))
2085 (if (looking-at "+")
2087 (if (looking-at "-")
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:]\\'"))
2097 (if (not (calc-minibuffer-contains ".* \\'"))
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:]\\'"))
2107 (if (calc-minibuffer-contains ".*mod \\'")
2108 (if calc-previous-modulo
2109 (insert (math-format-flat-expr calc-previous-modulo
0))
2111 (if (not (calc-minibuffer-contains ".* \\'"))
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
2119 (match-beginning 2) (match-end 2)))))
2122 (or (memq last-command-char
'(?
# ?
: ?. ?e ?
+ ?-
))
2123 (let ((dig (math-read-radix-digit
2124 (upcase last-command-char
))))
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
))
2132 (if (and (eq this-command last-command
)
2133 (eq last-command-char ?.
))
2137 (delete-backward-char 1)
2139 (calc-temp-minibuffer-message " [Bad format]"))))))
2140 (setq calc-prev-prev-char calc-prev-char
2141 calc-prev-char last-command-char
))
2144 (defun calcDigit-backspace ()
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
)
2155 (t (backward-delete-char 1)))
2156 (if (= (calc-minibuffer-size) 0)
2158 (setq last-command-char
13)
2159 (calcDigit-nondigit))))
2167 ;;;; Arithmetic routines.
2169 ;;; An object as manipulated by one of these routines may take any of the
2170 ;;; following forms:
2172 ;;; integer An integer. For normalized numbers, this format
2173 ;;; is used only for -999999 ... 999999.
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.
2181 ;;; (frac NUM DEN) A fraction. NUM and DEN are small or big integers.
2182 ;;; Normalized, DEN > 1.
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).
2190 ;;; (cplx REAL IMAG) A complex number; REAL and IMAG are any of above.
2191 ;;; Normalized, IMAG is nonzero.
2193 ;;; (polar R THETA) Polar complex number. Normalized, R > 0 and THETA
2194 ;;; is neither zero nor 180 degrees (pi radians).
2196 ;;; (vec A B C ...) Vector of objects A, B, C, ... A matrix is a
2197 ;;; vector of vectors.
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).
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.
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.
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.
2221 ;;; (mod N M) Number modulo M. When normalized, 0 <= N < M.
2222 ;;; N and M are real numbers.
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).
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.
2232 ;;; In the following comments, [x y z] means result is x, args must be y, z,
2233 ;;; respectively, where the code letters are:
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
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.]
2258 ;;; Reduce an object to canonical (normalized) form. [O o; Z Z] [Public]
2259 (defun math-normalize (a)
2263 (if (or (>= a
1000000) (<= a -
1000000))
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
)))
2273 (if (cdr (cdr (cdr a
)))
2276 ((cdr (cdr a
)) (+ (nth 1 a
) (* (nth 2 a
) 1000)))
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
)))
2285 (if (cdr (cdr (cdr a
)))
2288 ((cdr (cdr a
)) (- (+ (nth 1 a
) (* (nth 2 a
) 1000))))
2289 ((cdr a
) (- (nth 1 a
)))
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
2298 (and (consp (car a
)) (not (eq (car (car a
)) 'lambda
))))
2300 (math-normalize-fancy a
))
2302 (or (and calc-simplify-mode
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
)
2314 ( | . math-concat
) ))))
2315 (or (and var-EvalRules
2317 (or (eq var-EvalRules math-eval-rules-cache-tag
)
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
2325 (cdr math-eval-rules-cache
)
2326 nil math-eval-rules-cache
))))
2328 (apply (cdr func
) args
)
2329 (and (or (consp (car a
))
2331 (and (not calc-extensions-loaded
)
2334 (apply (car a
) args
)))))
2335 (wrong-number-of-arguments
2336 (calc-record-why "*Wrong number of arguments"
2337 (cons (car a
) args
))
2339 (wrong-type-argument
2340 (or calc-next-why
(calc-record-why "Wrong type of argument"
2341 (cons (car a
) args
)))
2344 (calc-record-why "*Argument out of range" (cons (car a
) args
))
2347 (calc-record-why "No exact representation for result"
2348 (cons (car a
) args
))
2351 (calc-record-why "*Floating-point overflow occurred"
2352 (cons (car a
) args
))
2355 (calc-record-why "*Floating-point underflow occurred"
2356 (cons (car a
) args
))
2359 (if (eq (nth 1 err
) 'var-EvalRules
)
2361 (setq var-EvalRules nil
)
2362 (math-normalize (cons (car a
) args
)))
2363 (calc-record-why "*Variable is void" (nth 1 err
)))))
2365 (math-dimension-error)
2366 (cons (car a
) args
))))))))
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
)))))
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
))
2388 (t (error "Invalid data object encountered"))))
2392 ;;; Coerce integer A to be a bignum. [B S]
2393 (defun math-bignum (a)
2395 (cons 'bigpos
(math-bignum-big a
))
2396 (cons 'bigneg
(math-bignum-big (- a
)))))
2398 (defun math-bignum-big (a) ; [L s]
2401 (cons (% a
1000) (math-bignum-big (/ a
1000)))))
2404 ;;; Build a normalized floating-point number. [F I S]
2405 (defun math-make-float (mant exp
)
2408 (let* ((ldiff (- calc-internal-prec
(math-numdigs mant
))))
2410 (setq mant
(math-scale-rounding mant ldiff
)
2411 exp
(- exp ldiff
))))
2413 (let ((digs (cdr mant
)))
2414 (if (= (%
(car digs
) 10) 0)
2416 (while (= (car digs
) 0)
2417 (setq digs
(cdr digs
)
2419 (while (= (%
(car digs
) 10) 0)
2420 (setq digs
(math-div10-bignum digs
)
2422 (setq mant
(math-normalize (cons (car mant
) digs
))))))
2423 (while (= (% mant
10) 0)
2424 (setq mant
(/ mant
10)
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
)))))
2434 (defun math-div10-bignum (a) ; [l l]
2436 (cons (+ (/ (car a
) 10) (* (%
(nth 1 a
) 10) 100))
2437 (math-div10-bignum (cdr a
)))
2438 (list (/ (car a
) 10))))
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
))))
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
))))
2461 ;;; Compute the number of decimal digits in integer A. [S I]
2462 (defun math-numdigs (a)
2465 (let* ((len (1- (length a
)))
2467 (+ (* len
3) (cond ((>= top
100) 0) ((>= top
10) -
1) (t -
2))))
2469 (cond ((>= a
100) (+ (math-numdigs (/ a
1000)) 3))
2475 (t (math-numdigs (- a
))))))
2477 ;;; Multiply (with truncation toward 0) the integer A by 10^N. [I i S]
2478 (defun math-scale-int (a n
)
2480 ((> n
0) (math-scale-left a n
))
2481 (t (math-normalize (math-scale-right a
(- n
))))))
2483 (defun math-scale-left (a n
) ; [I I S]
2487 (cons (car a
) (math-scale-left-bignum (cdr a
) n
))
2489 (if (or (>= a
1000) (<= a -
1000))
2490 (math-scale-left (math-bignum a
) n
)
2491 (math-scale-left (* a
1000) (- n
3)))
2493 (if (or (>= a
10000) (<= a -
10000))
2494 (math-scale-left (math-bignum a
) 2)
2496 (if (or (>= a
100000) (<= a -
100000))
2497 (math-scale-left (math-bignum a
) 1)
2500 (defun math-scale-left-bignum (a n
)
2502 (while (>= (setq a
(cons 0 a
)
2505 (math-mul-bignum-digit a
(if (= n
2) 100 10) 0)
2508 (defun math-scale-right (a n
) ; [i i S]
2512 (cons (car a
) (math-scale-right-bignum (cdr a
) n
))
2516 (- (math-scale-right (- a
) n
)))
2518 (while (and (> (setq a
(/ a
1000)) 0)
2519 (>= (setq n
(- n
3)) 3))))
2526 (defun math-scale-right-bignum (a n
) ; [L L S; l l S]
2528 (setq a
(nthcdr (/ n
3) a
)
2531 (cdr (math-mul-bignum-digit a
(if (= n
2) 10 100) 0))
2534 ;;; Multiply (with rounding) the integer A by 10^N. [I i S]
2535 (defun math-scale-rounding (a n
)
2537 (math-scale-left a n
))
2541 (let ((val (if (< n -
3)
2542 (math-scale-right-bignum (cdr a
) (- -
3 n
))
2544 (math-mul-bignum-digit (cdr a
) 10 0)
2546 (math-mul-bignum-digit (cdr a
) 100 0)
2547 (cdr a
)))))) ; n = -3
2548 (if (and val
(>= (car val
) 500))
2550 (if (eq (car (cdr val
)) 999)
2551 (math-add-bignum (cdr val
) '(1))
2552 (cons (1+ (car (cdr val
))) (cdr (cdr val
))))
2557 (- (math-scale-rounding (- a
) n
))
2560 (/ (+ (math-scale-right a
(- -
1 n
)) 5) 10))))))
2563 ;;; Compute the sum of A and B. [O O O] [Public]
2564 (defun math-add (a b
)
2566 (and (not (or (consp a
) (consp b
)))
2569 (if (or (<= a -
1000000) (>= a
1000000))
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
)
2578 (and (Math-integerp a
) (Math-integerp b
)
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
)))
2586 (let ((diff (math-sub-bignum (cdr b
) (cdr a
))))
2588 (cons 'bigneg
(math-sub-bignum (cdr a
) (cdr b
)))
2589 (cons 'bigpos diff
)))))
2590 (if (eq (car b
) 'bigneg
)
2592 (let ((diff (math-sub-bignum (cdr a
) (cdr b
))))
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
)
2599 (calc-add-fractions a b
))
2600 (and (Math-realp a
) (Math-realp b
)
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
))))
2612 (defun math-add-bignum (a b
) ; [L L L; l l l]
2615 (let* ((a (copy-sequence a
)) (aa a
) (carry nil
) sum
)
2618 (if (< (setq sum
(+ (car aa
) (car b
))) 999)
2620 (setcar aa
(1+ sum
))
2622 (setcar aa
(+ sum -
999)))
2623 (if (< (setq sum
(+ (car aa
) (car b
))) 1000)
2625 (setcar aa
(+ sum -
1000))
2631 (nconc a
(math-add-bignum b
'(1)))
2632 (while (eq (car aa
) 999)
2637 (setcar aa
(1+ (car aa
)))
2646 (defun math-sub-bignum (a b
) ; [l l l]
2649 (let* ((a (copy-sequence a
)) (aa a
) (borrow nil
) sum
)
2652 (if (>= (setq diff
(- (car aa
) (car b
))) 1)
2654 (setcar aa
(1- diff
))
2656 (setcar aa
(+ diff
999)))
2657 (if (>= (setq diff
(- (car aa
) (car b
))) 0)
2659 (setcar aa
(+ diff
1000))
2665 (while (eq (car aa
) 0)
2670 (setcar aa
(1- (car aa
)))
2673 (while (eq (car b
) 0)
2678 (while (eq (car b
) 0)
2684 (defun math-add-float (a b
) ; [F F F]
2685 (let ((ediff (- (nth 2 a
) (nth 2 b
))))
2687 (if (>= ediff
(+ calc-internal-prec calc-internal-prec
))
2689 (math-make-float (math-add (nth 1 b
)
2692 (math-scale-left (nth 1 a
) ediff
)))
2694 (if (>= (setq ediff
(- ediff
))
2695 (+ calc-internal-prec calc-internal-prec
))
2697 (math-make-float (math-add (nth 1 a
)
2698 (math-scale-left (nth 1 b
) ediff
))
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
))
2706 (if (or (<= a -
1000000) (>= a
1000000))
2710 (defun math-sub-float (a b
) ; [F F F]
2711 (let ((ediff (- (nth 2 a
) (nth 2 b
))))
2713 (if (>= ediff
(+ calc-internal-prec calc-internal-prec
))
2715 (math-make-float (math-add (Math-integer-neg (nth 1 b
))
2718 (math-scale-left (nth 1 a
) ediff
)))
2720 (if (>= (setq ediff
(- ediff
))
2721 (+ calc-internal-prec calc-internal-prec
))
2723 (math-make-float (math-add (nth 1 a
)
2725 (math-scale-left (nth 1 b
) ediff
)))
2729 ;;; Compute the product of A and B. [O O O] [Public]
2730 (defun math-mul (a b
)
2732 (and (not (consp a
)) (not (consp b
))
2733 (< a
1000) (> a -
1000) (< b
1000) (> b -
1000)
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
)
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
)
2744 (math-mul-zero b a
)))
2745 (and (Math-objvecp a
) (Math-objvecp b
)
2747 (and (Math-integerp a
) (Math-integerp b
)
2749 (or (consp a
) (setq a
(math-bignum a
)))
2750 (or (consp b
) (setq b
(math-bignum b
)))
2752 (cons (if (eq (car a
) (car b
)) 'bigpos
'bigneg
)
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
)
2760 (calc-mul-fractions a b
))
2761 (and (Math-realp a
) (Math-realp b
)
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
))))
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
)))
2778 (setq a
(nth 2 a
))))
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
)
2786 ;;; Multiply digit lists A and B. [L L L; l l l]
2787 (defun math-mul-bignum (a b
)
2789 (let* ((sum (if (<= (car b
) 1)
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))))
2801 (setcar ss
(%
(setq prod
(+ (+ (car ss
) (* (car aa
) d
))
2804 (setq c
(/ prod
1000)
2805 ss
(or (cdr ss
) (setcdr ss
(list 0)))))
2808 (setcar (cdr ss
) (+ (/ prod
1000) (car (cdr ss
))))
2809 (setcdr ss
(list (/ prod
1000))))))
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
)
2817 (let* ((a (copy-sequence a
)) (aa a
) prod
)
2819 (setcar aa
(%
(setq prod
(+ (* (car aa
) d
) c
)) 1000))
2824 (setcdr aa
(list (/ prod
1000))))
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
)
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
)))
2840 (math-normalize (cons (car a
) (car 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
))))
2846 (math-normalize (cons (if (eq (car a
) (car b
)) 'bigpos
'bigneg
)
2848 (math-normalize (cons (car a
) (cdr res
))))))
2849 (cons (/ a b
) (% a b
))))
2851 (defun math-quotient (a b
) ; [I I I] [Public]
2852 (if (and (not (consp a
)) (not (consp b
)))
2854 (math-reject-arg a
"*Division by zero")
2856 (if (and (natnump b
) (< b
1000))
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)
2869 (math-normalize (cons (if (eq (car a
) (car b
)) 'bigpos
'bigneg
)
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
)
2877 (let* ((alen (length a
))
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)
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
))))))
2890 ;;; Divide a bignum digit list by a digit. [l.D l D]
2891 (defun math-div-bignum-digit (a b
)
2893 (let* ((res (math-div-bignum-digit (cdr a
) b
))
2894 (num (+ (* (cdr res
) 1000) (car a
))))
2896 (cons (/ num b
) (car res
))
2900 (defun math-div-bignum-big (a b alen blen
) ; [l.l l L]
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
)))
2907 (cons (car res2
) (car res
))
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
)))
2916 (defun math-div-bignum-try (a b c guess
) ; [D.l l l D]
2917 (let ((rem (math-sub-bignum a c
)))
2919 (math-div-bignum-try a b
(math-sub-bignum c b
) (1- guess
))
2923 ;;; Compute the quotient of A and B. [O O N] [Public]
2924 (defun math-div (a b
)
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
)
2933 (math-div-zero a b
)))
2934 (and (Math-objvecp a
) (Math-objvecp b
)
2936 (and (Math-integerp a
) (Math-integerp b
)
2937 (let ((q (math-idivmod a b
)))
2940 (if calc-prefer-frac
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
)
2948 (calc-div-fractions a b
))
2949 (and (Math-realp a
) (Math-realp b
)
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
))))
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
))))
2965 (math-make-float (math-quotient (math-scale-int (nth 1 a
) ldiff
) (nth 1 b
))
2966 (- (- (nth 2 a
) (nth 2 b
)) ldiff
))))
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
))
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
))
2991 (and math-comp-selected
(setq calc-any-selections t
))
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
) ?
)
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: ")
3007 (or (equal calc-right-label
"")
3008 (eq a
'top-of-stack
)
3011 (setq c
(list 'horiz c
3012 (make-string (max (- w
(math-comp-width c
)
3013 (length calc-right-label
)) 0) ?
)
3015 calc-right-label
))))
3016 (setq s
(if (stringp c
)
3017 (if calc-display-raw
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
3029 (setcar (cdr entry
) (calc-count-lines s
))
3032 (defun math-stack-value-offset (c)
3033 (let* ((num (if calc-line-numbering
4 0))
3034 (wid (calc-window-width))
3036 (if calc-display-just
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)
3046 (defun calc-count-lines (s)
3049 (while (setq newpos
(string-match "\n" s pos
))
3050 (setq pos
(1+ newpos
)
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
)
3059 (let ((calc-line-breaking nil
))
3060 (math-composition-to-string (math-compose-expr a
0) w
))))
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))))
3069 (defun math-comp-concat (c1 c2
)
3070 (if (and (stringp c1
) (stringp c2
))
3072 (list 'horiz c1 c2
)))
3076 ;;; Format an expression as a one-line string suitable for re-reading.
3078 (defun math-format-flat-expr (a prec
)
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
))))
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
)))
3095 (math-format-flat-expr-fancy a prec
))))
3099 ;;; Format a number as a string.
3100 (defun math-format-number (a &optional prec
) ; [X N] [Public]
3102 ((eq calc-display-raw t
) (format "%s" a
))
3103 ((and (nth 1 calc-frac-format
) (Math-integerp a
))
3105 (math-format-number (math-adjust-fraction a
)))
3107 (if (not (or calc-group-digits calc-leading-zeros
))
3108 (if (= calc-number-radix
10)
3111 (concat "-" (math-format-number (- a
)))
3113 (if math-radix-explicit-format
3114 (if calc-radix-formatter
3115 (funcall calc-radix-formatter
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
))))
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
))
3135 (fmt (car calc-float-format
))
3136 (figs (nth 1 calc-float-format
))
3137 (point calc-point-char
)
3139 (if (and (eq fmt
'fix
)
3140 (or (and (< figs
0) (setq figs
(- figs
)))
3141 (> (+ exp
(math-numdigs mant
)) (- figs
))))
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)
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
))))
3157 (setq figs
(+ calc-internal-prec figs
)))
3159 (let ((adj (- figs
(math-numdigs mant
))))
3161 (setq mant
(math-scale-rounding mant adj
)
3163 (setq str
(if (integerp mant
)
3164 (int-to-string mant
)
3165 (math-format-bignum-decimal (cdr mant
))))
3166 (let* ((len (length str
))
3168 (if (and (eq fmt
'float
)
3169 (<= dpos
(+ calc-internal-prec calc-display-sci-high
))
3170 (>= dpos
(+ calc-display-sci-low
2)))
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
))))
3179 (setq str
(concat str
(make-string exp ?
0) point
)))
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))
3189 (if (> scale
(length str
))
3190 (setq str
(concat str
(make-string (- scale
(length str
))
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")
3201 str
(- eadj scale
)))))))
3205 (math-format-number-fancy a prec
))))
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
)
3213 (math-format-bignum-fancy a
)))
3215 (defun math-format-bignum-decimal (a) ; [X L]
3218 (while (cdr (cdr a
))
3219 (setq s
(concat (format "%06d" (+ (* (nth 1 a
) 1000) (car a
))) s
)
3221 (concat (int-to-string (+ (* (or (nth 1 a
) 0) 1000) (car a
))) s
))
3226 ;;; Parse a simple number in string form. [N X] [Public]
3227 (defun math-read-number (s)
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
)
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
))))))
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))))
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
)))))
3252 ;; Forms that require extensions module
3253 ((string-match "[^-+0-9eE.]" s
)
3255 (math-read-number-fancy s
))
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))
3267 (math-add (math-scale-int int flen
) frac
)
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
)))))))
3284 (defun math-match-substring (s n
)
3285 (if (match-beginning n
)
3286 (substring s
(match-beginning n
) (match-end n
))
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
))))
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")
3306 ("\\matrix" mat
) ("\\bmatrix" mat
) ("\\pmatrix" mat
)
3307 ("\\cr" punc
";") ("\\\\" punc
";") ("\\*" punc
"*")
3308 ("\\{" punc
"[") ("\\}" punc
"]")
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
)
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 )
3336 ( "\\" calcFunc-idiv
190 191 )
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
)
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")
3364 (calc-do-grab-region top bot arg
))
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")
3371 (calc-do-grab-rectangle top bot arg
))
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")
3377 (calc-do-grab-rectangle top bot arg
'calcFunc-reduced
))
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")
3383 (calc-do-grab-rectangle top bot arg
'calcFunc-reducea
))
3387 (defun calc-embedded (arg &optional end obeg oend
)
3388 "Start Calc Embedded mode on the formula surrounding point."
3391 (calc-do-embedded arg end obeg oend
))
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."
3398 (calc-do-embedded-activate arg cbuf
))
3401 (defun calc-user-invocation ()
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
))
3410 ;;; User-programmability.
3413 (defmacro defmath
(func args
&rest body
) ; [Public]
3415 (math-do-defmath func args body
))
3418 ;;; Functions needed for Lucid Emacs support.
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"))
3426 (calc-emacs-type-gnu19
3427 (let ((key (read-event)))
3430 (let ((key (read-char)))
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
)))
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
)))
3445 (if calc-always-load-extensions
3448 (calc-load-everything)))
3451 (run-hooks 'calc-load-hook
)
3453 ;;; calc.el ends here