1 ;;; calc.el --- the GNU Emacs calculator
3 ;; Copyright (C) 1990-1993, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
7 ;; Keywords: convenience, extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Calc is split into many files. This file is the main entry point.
27 ;; This file includes autoload commands for various other basic Calc
28 ;; facilities. The more advanced features are based in calc-ext, which
29 ;; in turn contains autoloads for the rest of the Calc files. This
30 ;; odd set of interactions is designed to make Calc's loading time
31 ;; be as short as possible when only simple calculations are needed.
33 ;; Original author's address:
34 ;; Dave Gillespie, daveg@synaptics.com, uunet!synaptx!daveg.
35 ;; Synaptics, Inc., 2698 Orchard Parkway, San Jose, CA 95134.
37 ;; The old address daveg@csvax.cs.caltech.edu will continue to
38 ;; work for the foreseeable future.
40 ;; Bug reports and suggestions are always welcome! (Type M-x
41 ;; report-calc-bug to send them).
43 ;; All functions, macros, and Lisp variables defined here begin with one
44 ;; of the prefixes "math", "Math", or "calc", with the exceptions of
45 ;; "full-calc", "full-calc-keypad", "another-calc", "quick-calc",
46 ;; "report-calc-bug", and "defmath". User-accessible variables begin
51 ;; Fix rewrite mechanism to do less gratuitous rearrangement of terms.
52 ;; Implement a pattern-based "refers" predicate.
54 ;; Make it possible to Undo a selection command.
55 ;; Figure out how to allow selecting rows of matrices.
56 ;; If cursor was in selection before, move it after j n, j p, j L, etc.
57 ;; Consider reimplementing calc-delete-selection using rewrites.
59 ;; Implement line-breaking in non-flat compositions (is this desirable?).
60 ;; Implement matrix formatting with multi-line components.
62 ;; Have "Z R" define a user command based on a set of rewrite rules.
63 ;; Support "incf" and "decf" in defmath definitions.
64 ;; Have defmath generate calls to calc-binary-op or calc-unary-op.
65 ;; Make some way to define algebraic functions using keyboard macros.
67 ;; Allow calc-word-size=0 => Common Lisp-style signed bitwise arithmetic.
68 ;; Consider digamma function (and thus arb. prec. Euler's gamma constant).
69 ;; May as well make continued-fractions stuff available to the user.
71 ;; How about matrix eigenvalues, SVD, pseudo-inverse, etc.?
72 ;; Should cache matrix inverses as well as decompositions.
73 ;; If dividing by a non-square matrix, use least-squares automatically.
74 ;; Consider supporting matrix exponentials.
76 ;; Have ninteg detect and work around singularities at the endpoints.
77 ;; Use an adaptive subdivision algorithm for ninteg.
78 ;; Provide nsum and nprod to go along with ninteg.
80 ;; Handle TeX-mode parsing of \matrix{ ... } where ... contains braces.
81 ;; Support AmS-TeX's \{d,t,}frac, \{d,t,}binom notations.
82 ;; Format and parse sums and products in Eqn and Math modes.
84 ;; Get math-read-big-expr to read sums, products, etc.
85 ;; Change calc-grab-region to use math-read-big-expr.
86 ;; Have a way to define functions using := in Embedded Mode.
88 ;; Support polar plotting with GNUPLOT.
89 ;; Make a calc-graph-histogram function.
91 ;; Replace hokey formulas for complex functions with formulas designed
92 ;; to minimize roundoff while maintaining the proper branch cuts.
93 ;; Test accuracy of advanced math functions over whole complex plane.
94 ;; Extend Bessel functions to provide arbitrary precision.
95 ;; Extend advanced math functions to handle error forms and intervals.
96 ;; Provide a better implementation for math-sin-cos-raw.
97 ;; Provide a better implementation for math-hypot.
98 ;; Provide a better implementation for math-make-frac.
99 ;; Provide a better implementation for calcFunc-prfac.
100 ;; Provide a better implementation for calcFunc-factor.
102 ;; Provide more examples in the tutorial section of the manual.
103 ;; Cover in the tutorial: simplification modes, declarations,
104 ;; bitwise stuff, selections, matrix mapping, financial functions.
105 ;; Provide more Lisp programming examples in the manual.
106 ;; Finish the Internals section of the manual (and bring it up to date).
108 ;; Tim suggests adding spreadsheet-like features.
109 ;; Implement language modes for Gnuplot, Lisp, Ada, APL, ...?
111 ;; For atan series, if x > tan(pi/12) (about 0.268) reduce using the identity
112 ;; atan(x) = atan((x * sqrt(3) - 1) / (sqrt(3) + x)) + pi/6.
114 ;; A better integration algorithm:
115 ;; Use breadth-first instead of depth-first search, as follows:
116 ;; The integral cache allows unfinished integrals in symbolic notation
117 ;; on the righthand side. An entry with no unfinished integrals on the
118 ;; RHS is "complete"; references to it elsewhere are replaced by the
119 ;; integrated value. More than one cache entry for the same integral
120 ;; may exist, though if one becomes complete, the others may be deleted.
121 ;; The integrator works by using every applicable rule (such as
122 ;; substitution, parts, linearity, etc.) to generate possible righthand
123 ;; sides, all of which are entered into the cache. Now, as long as the
124 ;; target integral is not complete (and the time limit has not run out)
125 ;; choose an incomplete integral from the cache and, for every integral
126 ;; appearing in its RHS's, add those integrals to the cache using the
127 ;; same substitution, parts, etc. rules. The cache should be organized
128 ;; as a priority queue, choosing the "simplest" incomplete integral at
129 ;; each step, or choosing randomly among equally simple integrals.
130 ;; Simplicity equals small size, and few steps removed from the original
131 ;; target integral. Note that when the integrator finishes, incomplete
132 ;; integrals can be left in the cache, so the algorithm can start where
133 ;; it left off if another similar integral is later requested.
134 ;; Breadth-first search would avoid the nagging problem of, e.g., whether
135 ;; to use parts or substitution first, and which decomposition is best.
136 ;; All are tried, and any path that diverges will quickly be put on the
137 ;; back burner by the priority queue.
138 ;; Note: Probably a good idea to call math-simplify-extended before
139 ;; measuring a formula's simplicity.
145 ;; Declare functions which are defined elsewhere.
146 (declare-function calc-set-language
"calc-lang" (lang &optional option no-refresh
))
147 (declare-function calc-edit-finish
"calc-yank" (&optional keep
))
148 (declare-function calc-edit-cancel
"calc-yank" ())
149 (declare-function calc-do-quick-calc
"calc-aent" ())
150 (declare-function calc-do-calc-eval
"calc-aent" (str separator args
))
151 (declare-function calc-do-keypad
"calc-keypd" (&optional full-display interactive
))
152 (declare-function calcFunc-unixtime
"calc-forms" (date &optional zone
))
153 (declare-function math-parse-date
"calc-forms" (math-pd-str))
154 (declare-function math-lessp
"calc-ext" (a b
))
155 (declare-function math-compare
"calc-ext" (a b
))
156 (declare-function calc-embedded-finish-command
"calc-embed" ())
157 (declare-function calc-embedded-select-buffer
"calc-embed" ())
158 (declare-function calc-embedded-mode-line-change
"calc-embed" ())
159 (declare-function calc-push-list-in-macro
"calc-prog" (vals m sels
))
160 (declare-function calc-replace-selections
"calc-sel" (n vals m
))
161 (declare-function calc-record-list
"calc-misc" (vals &optional prefix
))
162 (declare-function calc-normalize-fancy
"calc-ext" (val))
163 (declare-function calc-do-handle-whys
"calc-misc" ())
164 (declare-function calc-top-selected
"calc-sel" (&optional n m
))
165 (declare-function calc-sel-error
"calc-sel" ())
166 (declare-function calc-pop-stack-in-macro
"calc-prog" (n mm
))
167 (declare-function calc-embedded-stack-change
"calc-embed" ())
168 (declare-function calc-refresh-evaltos
"calc-ext" (&optional which-var
))
169 (declare-function calc-do-refresh
"calc-misc" ())
170 (declare-function calc-binary-op-fancy
"calc-ext" (name func arg ident unary
))
171 (declare-function calc-unary-op-fancy
"calc-ext" (name func arg
))
172 (declare-function calc-delete-selection
"calc-sel" (n))
173 (declare-function calc-alg-digit-entry
"calc-aent" ())
174 (declare-function calc-alg-entry
"calc-aent" (&optional initial prompt
))
175 (declare-function calc-dots
"calc-incom" ())
176 (declare-function calc-temp-minibuffer-message
"calc-misc" (m))
177 (declare-function math-read-radix-digit
"calc-misc" (dig))
178 (declare-function calc-digit-dots
"calc-incom" ())
179 (declare-function math-normalize-fancy
"calc-ext" (a))
180 (declare-function math-normalize-nonstandard
"calc-ext" ())
181 (declare-function math-recompile-eval-rules
"calc-alg" ())
182 (declare-function math-apply-rewrites
"calc-rewr" (expr rules
&optional heads math-apply-rw-ruleset
))
183 (declare-function calc-record-why
"calc-misc" (&rest stuff
))
184 (declare-function math-dimension-error
"calc-vec" ())
185 (declare-function calc-incomplete-error
"calc-incom" (a))
186 (declare-function math-float-fancy
"calc-arith" (a))
187 (declare-function math-neg-fancy
"calc-arith" (a))
188 (declare-function calc-add-fractions
"calc-frac" (a b
))
189 (declare-function math-add-objects-fancy
"calc-arith" (a b
))
190 (declare-function math-add-symb-fancy
"calc-arith" (a b
))
191 (declare-function math-mul-zero
"calc-arith" (a b
))
192 (declare-function calc-mul-fractions
"calc-frac" (a b
))
193 (declare-function math-mul-objects-fancy
"calc-arith" (a b
))
194 (declare-function math-mul-symb-fancy
"calc-arith" (a b
))
195 (declare-function math-reject-arg
"calc-misc" (&optional a p option
))
196 (declare-function math-div-by-zero
"calc-arith" (a b
))
197 (declare-function math-div-zero
"calc-arith" (a b
))
198 (declare-function math-make-frac
"calc-frac" (num den
))
199 (declare-function calc-div-fractions
"calc-frac" (a b
))
200 (declare-function math-div-objects-fancy
"calc-arith" (a b
))
201 (declare-function math-div-symb-fancy
"calc-arith" (a b
))
202 (declare-function math-compose-expr
"calccomp" (a prec
&optional div
))
203 (declare-function math-comp-width
"calccomp" (c))
204 (declare-function math-composition-to-string
"calccomp" (c &optional width
))
205 (declare-function math-stack-value-offset-fancy
"calccomp" ())
206 (declare-function math-format-flat-expr-fancy
"calc-ext" (a prec
))
207 (declare-function math-adjust-fraction
"calc-ext" (a))
208 (declare-function math-format-binary
"calc-bin" (a))
209 (declare-function math-format-radix
"calc-bin" (a))
210 (declare-function math-format-twos-complement
"calc-bin" (a))
211 (declare-function math-group-float
"calc-ext" (str))
212 (declare-function math-mod
"calc-misc" (a b
))
213 (declare-function math-format-number-fancy
"calc-ext" (a prec
))
214 (declare-function math-format-bignum-fancy
"calc-ext" (a))
215 (declare-function math-read-number-fancy
"calc-ext" (s))
216 (declare-function calc-do-grab-region
"calc-yank" (top bot arg
))
217 (declare-function calc-do-grab-rectangle
"calc-yank" (top bot arg
&optional reduce
))
218 (declare-function calc-do-embedded
"calc-embed" (calc-embed-arg end obeg oend
))
219 (declare-function calc-do-embedded-activate
"calc-embed" (calc-embed-arg cbuf
))
220 (declare-function math-do-defmath
"calc-prog" (func args body
))
221 (declare-function calc-load-everything
"calc-ext" ())
225 "Advanced desk calculator and mathematical tool."
228 :group
'applications
)
230 ;; Do not autoload, so it is evaluated at run-time rather than at dump time.
232 (defcustom calc-settings-file
233 (locate-user-emacs-file "calc.el" ".calc.el")
234 "File in which to record permanent settings."
238 (defcustom calc-language-alist
239 '((latex-mode . latex
)
241 (plain-tex-mode . tex
)
244 (pascal-mode . pascal
)
247 (fortran-mode . fortran
)
249 (texinfo-mode . calc-normal-language
))
250 "Alist of major modes with appropriate Calc languages."
252 :type
'(alist :key-type
(symbol :tag
"Major mode")
253 :value-type
(symbol :tag
"Calc language")))
255 (defcustom calc-embedded-announce-formula
256 "%Embed\n\\(% .*\n\\)*"
257 "A regular expression which is sure to be followed by a calc-embedded formula."
261 (defcustom calc-embedded-announce-formula-alist
262 '((c++-mode .
"//Embed\n\\(// .*\n\\)*")
263 (c-mode .
"/\\*Embed\\*/\n\\(/\\* .*\\*/\n\\)*")
264 (f90-mode .
"!Embed\n\\(! .*\n\\)*")
265 (fortran-mode .
"C Embed\n\\(C .*\n\\)*")
266 (html-helper-mode .
"<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
267 (html-mode .
"<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
268 (nroff-mode .
"\\\\\"Embed\n\\(\\\\\" .*\n\\)*")
269 (pascal-mode .
"{Embed}\n\\({.*}\n\\)*")
270 (sgml-mode .
"<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
271 (xml-mode .
"<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
272 (texinfo-mode .
"@c Embed\n\\(@c .*\n\\)*"))
273 "Alist of major modes with appropriate values for `calc-embedded-announce-formula'."
275 :type
'(alist :key-type
(symbol :tag
"Major mode")
276 :value-type
(regexp :tag
"Regexp to announce formula")))
278 (defcustom calc-embedded-open-formula
279 "\\`\\|^\n\\|\\$\\$?\\|\\\\\\[\\|^\\\\begin[^{].*\n\\|^\\\\begin{.*[^x]}.*\n\\|^@.*\n\\|^\\.EQ.*\n\\|\\\\(\\|^%\n\\|^\\.\\\\\"\n"
280 "A regular expression for the opening delimiter of a formula used by calc-embedded."
284 (defcustom calc-embedded-close-formula
285 "\\'\\|\n$\\|\\$\\$?\\|\\\\]\\|^\\\\end[^{].*\n\\|^\\\\end{.*[^x]}.*\n\\|^@.*\n\\|^\\.EN.*\n\\|\\\\)\\|\n%\n\\|^\\.\\\\\"\n"
286 "A regular expression for the closing delimiter of a formula used by calc-embedded."
290 (defcustom calc-embedded-open-close-formula-alist
292 "Alist of major modes with pairs of formula delimiters used by calc-embedded."
294 :type
'(alist :key-type
(symbol :tag
"Major mode")
295 :value-type
(list (regexp :tag
"Opening formula delimiter")
296 (regexp :tag
"Closing formula delimiter"))))
298 (defcustom calc-embedded-word-regexp
299 "[-+]?[0-9]+\\(\\.[0-9]+\\)?\\([eE][-+]?[0-9]+\\)?"
300 "A regular expression determining a word for calc-embedded-word."
304 (defcustom calc-embedded-word-regexp-alist
306 "Alist of major modes with word regexps used by calc-embedded-word."
308 :type
'(alist :key-type
(symbol :tag
"Major mode")
309 :value-type
(regexp :tag
"Regexp for word")))
311 (defcustom calc-embedded-open-plain
313 "A string which is the opening delimiter for a \"plain\" formula.
314 If calc-show-plain mode is enabled, this is inserted at the front of
319 (defcustom calc-embedded-close-plain
321 "A string which is the closing delimiter for a \"plain\" formula.
322 See calc-embedded-open-plain."
326 (defcustom calc-embedded-open-close-plain-alist
327 '((c++-mode
"// %% " " %%\n")
328 (c-mode "/* %% " " %% */\n")
329 (f90-mode "! %% " " %%\n")
330 (fortran-mode "C %% " " %%\n")
331 (html-helper-mode "<!-- %% " " %% -->\n")
332 (html-mode "<!-- %% " " %% -->\n")
333 (nroff-mode "\\\" %% " " %%\n")
334 (pascal-mode "{%% " " %%}\n")
335 (sgml-mode "<!-- %% " " %% -->\n")
336 (xml-mode "<!-- %% " " %% -->\n")
337 (texinfo-mode "@c %% " " %%\n"))
338 "Alist of major modes with pairs of delimiters for \"plain\" formulas."
340 :type
'(alist :key-type
(symbol :tag
"Major mode")
341 :value-type
(list (string :tag
"Opening \"plain\" delimiter")
342 (string :tag
"Closing \"plain\" delimiter"))))
344 (defcustom calc-embedded-open-new-formula
346 "A string which is inserted at front of formula by calc-embedded-new-formula."
350 (defcustom calc-embedded-close-new-formula
352 "A string which is inserted at end of formula by calc-embedded-new-formula."
356 (defcustom calc-embedded-open-close-new-formula-alist
358 "Alist of major modes with pairs of new formula delimiters used by calc-embedded."
360 :type
'(alist :key-type
(symbol :tag
"Major mode")
361 :value-type
(list (string :tag
"Opening new formula delimiter")
362 (string :tag
"Closing new formula delimiter"))))
364 (defcustom calc-embedded-open-mode
366 "A string which should precede calc-embedded mode annotations.
367 This is not required to be present for user-written mode annotations."
371 (defcustom calc-embedded-close-mode
373 "A string which should follow calc-embedded mode annotations.
374 This is not required to be present for user-written mode annotations."
378 (defcustom calc-embedded-open-close-mode-alist
379 '((c++-mode
"// " "\n")
380 (c-mode "/* " " */\n")
382 (fortran-mode "C " "\n")
383 (html-helper-mode "<!-- " " -->\n")
384 (html-mode "<!-- " " -->\n")
385 (nroff-mode "\\\" " "\n")
386 (pascal-mode "{ " " }\n")
387 (sgml-mode "<!-- " " -->\n")
388 (xml-mode "<!-- " " -->\n")
389 (texinfo-mode "@c " "\n"))
390 "Alist of major modes with pairs of strings to delimit annotations."
392 :type
'(alist :key-type
(symbol :tag
"Major mode")
393 :value-type
(list (string :tag
"Opening annotation delimiter")
394 (string :tag
"Closing annotation delimiter"))))
396 (defcustom calc-gnuplot-name
397 (if (eq system-type
'windows-nt
) "pgnuplot" "gnuplot")
398 "Name of GNUPLOT program, for calc-graph features."
402 (defcustom calc-gnuplot-plot-command
404 "Name of command for displaying GNUPLOT output; %s = file name to print."
406 :type
'(choice (string) (sexp)))
408 (defcustom calc-gnuplot-print-command
410 "Name of command for printing GNUPLOT output; %s = file name to print."
412 :type
'(choice (string) (sexp)))
414 (defcustom calc-multiplication-has-precedence
416 "If non-nil, multiplication has precedence over division
421 (defcustom calc-ensure-consistent-units
423 "If non-nil, make sure new units are consistent with current units
424 when converting units."
429 (defcustom calc-undo-length
431 "The number of undo steps that will be preserved when Calc is quit."
435 (defcustom calc-highlight-selections-with-faces
437 "If non-nil, use a separate face to indicate selected sub-formulas.
438 If option `calc-show-selections' is non-nil, then selected sub-formulas are
439 shown by displaying the rest of the formula in `calc-nonselected-face'.
440 If option `calc-show-selections' is nil, then selected sub-formulas are shown
441 by displaying the sub-formula in `calc-selected-face'."
446 (defcustom calc-lu-field-reference
448 "The default reference level for logarithmic units (field)."
453 (defcustom calc-lu-power-reference
455 "The default reference level for logarithmic units (power)."
460 (defcustom calc-note-threshold
"1"
461 "The number of cents that a frequency should be near a note
462 to be identified as that note."
467 (defvar math-format-date-cache
) ; calc-forms.el
469 (defface calc-nonselected-face
472 "Face used to show the non-selected portion of a formula."
475 (defface calc-selected-face
477 "Face used to show the selected portion of a formula."
480 (defvar calc-bug-address
"jay.p.belanger@gmail.com"
481 "Address of the maintainer of Calc, for use by `report-calc-bug'.")
483 (defvar calc-scan-for-dels t
484 "If t, scan keymaps to find all DEL-like keys.
485 if nil, only DEL itself is mapped to calc-pop.")
487 (defvar calc-stack
'((top-of-stack 1 nil
))
489 Entries are 3-lists: Formula, Height (in lines), Selection (or nil).")
491 (defvar calc-stack-top
1
492 "Index into `calc-stack' of \"top\" of stack.
493 This is 1 unless `calc-truncate-stack' has been used.")
495 (defvar calc-display-sci-high
0
496 "Floating-point numbers with this positive exponent or higher above the
497 current precision are displayed in scientific notation in calc-mode.")
499 (defvar calc-display-sci-low -
3
500 "Floating-point numbers with this negative exponent or lower are displayed
501 scientific notation in calc-mode.")
503 (defvar calc-other-modes nil
504 "List of used-defined strings to append to Calculator mode line.")
506 (defvar calc-Y-help-msgs nil
507 "List of strings for Y prefix help.")
509 (defvar calc-loaded-settings-file nil
510 "t if `calc-settings-file' has been loaded yet.")
513 (defvar calc-mode-var-list
'()
514 "List of variables used in customizing GNU Calc.")
516 (defmacro defcalcmodevar
(var defval
&optional doc
)
517 "Declare VAR as a Calc variable, with default value DEFVAL
519 The variable VAR will be added to `calc-mode-var-list'."
521 (defvar ,var
,defval
,doc
)
522 (add-to-list 'calc-mode-var-list
(list (quote ,var
) ,defval
))))
524 (defun calc-mode-var-list-restore-default-values ()
525 "Restore the default values of the variables in `calc-mode-var-list'."
526 (mapcar (function (lambda (v) (set (car v
) (nth 1 v
))))
529 (defun calc-mode-var-list-restore-saved-values ()
530 "Restore the user-saved values of the variables in `calc-mode-var-list'."
531 (let ((newvarlist '()))
534 (file (substitute-in-file-name calc-settings-file
)))
536 (file-regular-p file
)
537 (set-buffer (find-file-noselect file
))
538 (goto-char (point-min))
539 (search-forward ";;; Mode settings stored by Calc" nil t
)
543 (search-forward "\n;;; End of mode settings" nil t
)))
545 (calc-mode-var-list-restore-default-values)
546 (eval-region pos
(point))
547 (let ((varlist calc-mode-var-list
))
549 (let ((var (car varlist
)))
551 (cons (list (car var
) (symbol-value (car var
)))
553 (setq varlist
(cdr varlist
)))))))
555 (mapcar (function (lambda (v) (set (car v
) (nth 1 v
))))
557 (calc-mode-var-list-restore-default-values))))
559 (defcalcmodevar calc-always-load-extensions nil
560 "If non-nil, load the calc-ext module automatically when Calc is loaded.")
562 (defcalcmodevar calc-line-numbering t
563 "If non-nil, display line numbers in Calculator stack.")
565 (defcalcmodevar calc-line-breaking t
566 "If non-nil, break long values across multiple lines in Calculator stack.")
568 (defcalcmodevar calc-display-just nil
569 "If nil, stack display is left-justified.
570 If `right', stack display is right-justified.
571 If `center', stack display is centered.")
573 (defcalcmodevar calc-display-origin nil
574 "Horizontal origin of displayed stack entries.
575 In left-justified mode, this is effectively indentation. (Default 0).
576 In right-justified mode, this is effectively window width.
577 In centered mode, center of stack entry is placed here.")
579 (defcalcmodevar calc-number-radix
10
580 "Radix for entry and display of numbers in calc-mode, 2-36.")
582 (defcalcmodevar calc-leading-zeros nil
583 "If non-nil, leading zeros are provided to pad integers to calc-word-size.")
585 (defcalcmodevar calc-group-digits nil
586 "If non-nil, group digits in large displayed integers by inserting spaces.
587 If an integer, group that many digits at a time.
588 If t, use 4 for binary and hex, 3 otherwise.")
590 (defcalcmodevar calc-group-char
","
591 "The character (in the form of a string) to be used for grouping digits.
592 This is used only when calc-group-digits mode is on.")
594 (defcalcmodevar calc-point-char
"."
595 "The character (in the form of a string) to be used as a decimal point.")
597 (defcalcmodevar calc-frac-format
'(":" nil
)
598 "Format of displayed fractions; a string of one or two of \":\" or \"/\".")
600 (defcalcmodevar calc-prefer-frac nil
601 "If non-nil, prefer fractional over floating-point results.")
603 (defcalcmodevar calc-hms-format
"%s@ %s' %s\""
604 "Format of displayed hours-minutes-seconds angles, a format string.
605 String must contain three %s marks for hours, minutes, seconds respectively.")
607 (defcalcmodevar calc-date-format
'((H ":" mm C SS pp
" ")
608 Www
" " Mmm
" " D
", " YYYY
)
609 "Format of displayed date forms.")
611 (defcalcmodevar calc-float-format
'(float 0)
612 "Format to use for display of floating-point numbers in calc-mode.
613 Must be a list of one of the following forms:
614 (float 0) Floating point format, display full precision.
615 (float N) N > 0: Floating point format, at most N significant figures.
616 (float -N) -N < 0: Floating point format, calc-internal-prec - N figs.
617 (fix N) N >= 0: Fixed point format, N places after decimal point.
618 (sci 0) Scientific notation, full precision.
619 (sci N) N > 0: Scientific notation, N significant figures.
620 (sci -N) -N < 0: Scientific notation, calc-internal-prec - N figs.
621 (eng 0) Engineering notation, full precision.
622 (eng N) N > 0: Engineering notation, N significant figures.
623 (eng -N) -N < 0: Engineering notation, calc-internal-prec - N figs.")
625 (defcalcmodevar calc-full-float-format
'(float 0)
626 "Format to use when full precision must be displayed.")
628 (defcalcmodevar calc-complex-format nil
629 "Format to use for display of complex numbers in calc-mode. Must be one of:
634 (defcalcmodevar calc-complex-mode
'cplx
635 "Preferred form, either `cplx' or `polar', for complex numbers.")
637 (defcalcmodevar calc-infinite-mode nil
638 "If nil, 1 / 0 is left unsimplified.
639 If 0, 1 / 0 is changed to inf (zeros are considered positive).
640 Otherwise, 1 / 0 is changed to uinf (undirected infinity).")
642 (defcalcmodevar calc-display-strings nil
643 "If non-nil, display vectors of byte-sized integers as strings.")
645 (defcalcmodevar calc-matrix-just
'center
646 "If nil, vector elements are left-justified.
647 If `right', vector elements are right-justified.
648 If `center', vector elements are centered.")
650 (defcalcmodevar calc-break-vectors nil
651 "If non-nil, display vectors one element per line.")
653 (defcalcmodevar calc-full-vectors t
654 "If non-nil, display long vectors in full. If nil, use abbreviated form.")
656 (defcalcmodevar calc-full-trail-vectors t
657 "If non-nil, display long vectors in full in the trail.")
659 (defcalcmodevar calc-vector-commas
","
660 "If non-nil, separate elements of displayed vectors with this string.")
662 (defcalcmodevar calc-vector-brackets
"[]"
663 "If non-nil, surround displayed vectors with these characters.")
665 (defcalcmodevar calc-matrix-brackets
'(R O
)
666 "A list of code-letter symbols that control \"big\" matrix display.
667 If `R' is present, display inner brackets for matrices.
668 If `O' is present, display outer brackets for matrices (above/below).
669 If `C' is present, display outer brackets for matrices (centered).")
671 (defcalcmodevar calc-language nil
672 "Language or format for entry and display of stack values. Must be one of:
673 nil Use standard Calc notation.
674 flat Use standard Calc notation, one-line format.
675 big Display formulas in 2-d notation (enter w/std notation).
676 unform Use unformatted display: add(a, mul(b,c)).
677 c Use C language notation.
678 pascal Use Pascal language notation.
679 fortran Use Fortran language notation.
680 tex Use TeX notation.
681 latex Use LaTeX notation.
682 eqn Use eqn notation.
683 yacas Use Yacas notation.
684 maxima Use Maxima notation.
685 giac Use Giac notation.
686 math Use Mathematica(tm) notation.
687 maple Use Maple notation.")
689 (defcalcmodevar calc-language-option nil
690 "Numeric prefix argument for the command that set `calc-language'.")
692 (defcalcmodevar calc-left-label
""
693 "Label to display at left of formula.")
695 (defcalcmodevar calc-right-label
""
696 "Label to display at right of formula.")
698 (defcalcmodevar calc-word-size
32
699 "Minimum number of bits per word, if any, for binary operations in calc-mode.")
701 (defcalcmodevar calc-previous-modulo nil
702 "Most recently used value of M in a modulo form.")
704 (defcalcmodevar calc-simplify-mode
'alg
705 "Type of simplification applied to results.
706 If `none', results are not simplified when pushed on the stack.
707 If `num', functions are simplified only when args are constant.
708 If nil, only limited simplifications are applied.
709 If `binary', `math-clip' is applied if appropriate.
710 If `alg', `math-simplify' is applied.
711 If `ext', `math-simplify-extended' is applied.
712 If `units', `math-simplify-units' is applied.")
714 (defcalcmodevar calc-auto-recompute t
715 "If non-nil, recompute evalto's automatically when necessary.")
717 (defcalcmodevar calc-display-raw nil
718 "If non-nil, display shows unformatted Lisp exprs. (For debugging)")
720 (defcalcmodevar calc-internal-prec
12
721 "Number of digits of internal precision for calc-mode calculations.")
723 (defcalcmodevar calc-angle-mode
'deg
724 "If deg, angles are in degrees; if rad, angles are in radians.
725 If hms, angles are in degrees-minutes-seconds.")
727 (defcalcmodevar calc-algebraic-mode nil
728 "If non-nil, numeric entry accepts whole algebraic expressions.
729 If nil, algebraic expressions must be preceded by \"'\".")
731 (defcalcmodevar calc-incomplete-algebraic-mode nil
732 "Like calc-algebraic-mode except only affects ( and [ keys.")
734 (defcalcmodevar calc-symbolic-mode nil
735 "If non-nil, inexact numeric computations like sqrt(2) are postponed.
736 If nil, computations on numbers always yield numbers where possible.")
738 (defcalcmodevar calc-matrix-mode nil
739 "If `matrix', variables are assumed to be matrix-valued.
740 If a number, variables are assumed to be NxN matrices.
741 If `sqmatrix', variables are assumed to be square matrices of an unspecified size.
742 If `scalar', variables are assumed to be scalar-valued.
743 If nil, symbolic math routines make no assumptions about variables.")
745 (defcalcmodevar calc-twos-complement-mode nil
746 "If non-nil, display integers in two's complement mode.")
749 (defcalcmodevar calc-shift-prefix nil
750 "If non-nil, shifted letter keys are prefix keys rather than normal meanings.")
752 (defcalcmodevar calc-window-height
7
753 "Initial height of Calculator window.")
755 (defcalcmodevar calc-display-trail t
756 "If non-nil, M-x calc creates a window to display Calculator trail.")
758 (defcalcmodevar calc-show-selections t
759 "If non-nil, selected sub-formulas are shown by obscuring rest of formula.
760 If nil, selected sub-formulas are highlighted by obscuring the sub-formulas.")
762 (defcalcmodevar calc-use-selections t
763 "If non-nil, commands operate only on selected portions of formulas.
764 If nil, selections displayed but ignored.")
766 (defcalcmodevar calc-assoc-selections t
767 "If non-nil, selection hides deep structure of associative formulas.")
769 (defcalcmodevar calc-display-working-message
'lots
770 "If non-nil, display \"Working...\" for potentially slow Calculator commands.")
772 (defcalcmodevar calc-auto-why
'maybe
773 "If non-nil, automatically execute a \"why\" command to explain odd results.")
775 (defcalcmodevar calc-timing nil
776 "If non-nil, display timing information on each slow command.")
778 (defcalcmodevar calc-mode-save-mode
'local
)
780 (defcalcmodevar calc-standard-date-formats
782 "<H:mm:SSpp >Www Mmm D, YYYY"
783 "D Mmm YYYY<, h:mm:SS>"
784 "Www Mmm BD< hh:mm:ss> YYYY"
791 "ZYYY-MM-DD Www< hh:mm>"
792 "IYYY-Iww-w<Thh:mm:ss>"))
794 (defcalcmodevar calc-autorange-units nil
795 "If non-nil, automatically set unit prefixes to keep units in a reasonable range.")
797 (defcalcmodevar calc-was-keypad-mode nil
798 "Non-nil if Calc was last invoked in keypad mode.")
800 (defcalcmodevar calc-full-mode nil
801 "Non-nil if Calc was last invoked in full-screen mode.")
803 (defcalcmodevar calc-user-parse-tables nil
804 "Alist of languages with user-defined parse rules.")
806 (defcalcmodevar calc-gnuplot-default-device
"default"
807 "The default device name for GNUPLOT plotting.")
809 (defcalcmodevar calc-gnuplot-default-output
"STDOUT"
810 "The default output file for GNUPLOT plotting.")
812 (defcalcmodevar calc-gnuplot-print-device
"postscript"
813 "The default device name for GNUPLOT printing.")
815 (defcalcmodevar calc-gnuplot-print-output
"auto"
816 "The default output for GNUPLOT printing.")
818 (defcalcmodevar calc-gnuplot-geometry nil
819 "The default geometry for the GNUPLOT window.")
821 (defcalcmodevar calc-graph-default-resolution
15
822 "The default number of data points when plotting curves.")
824 (defcalcmodevar calc-graph-default-resolution-3d
5
825 "The default number of x- and y- data points when plotting surfaces.")
827 (defcalcmodevar calc-invocation-macro nil
828 "A user defined macro for starting Calc.
829 Used by `calc-user-invocation'.")
831 (defcalcmodevar calc-show-banner t
832 "If non-nil, show a friendly greeting above the stack.")
834 (defconst calc-local-var-list
'(calc-stack
838 calc-always-load-extensions
849 calc-incomplete-algebraic-mode
866 calc-standard-date-formats
868 calc-full-float-format
872 calc-full-trail-vectors
886 calc-assoc-selections
890 (defvar calc-mode-hook nil
891 "Hook run when entering calc-mode.")
893 (defvar calc-trail-mode-hook nil
894 "Hook run when entering calc-trail-mode.")
896 (defvar calc-start-hook nil
897 "Hook run when calc is started.")
899 (defvar calc-end-hook nil
900 "Hook run when calc is quit.")
902 (defvar calc-load-hook nil
903 "Hook run when calc.el is loaded.")
905 (defvar calc-window-hook nil
906 "Hook called to create the Calc window.")
908 (defvar calc-trail-window-hook nil
909 "Hook called to create the Calc trail window.")
911 (defvar calc-embedded-new-buffer-hook nil
912 "Hook run when starting embedded mode in a new buffer.")
914 (defvar calc-embedded-new-formula-hook nil
915 "Hook run when starting embedded mode in a new formula.")
917 (defvar calc-embedded-mode-hook nil
918 "Hook run when starting embedded mode.")
920 ;; The following modes use specially-formatted data.
921 (put 'calc-mode
'mode-class
'special
)
922 (put 'calc-trail-mode
'mode-class
'special
)
924 (define-error 'calc-error
"Calc internal error")
925 (define-error 'inexact-result
926 "Calc internal error (inexact-result)" 'calc-error
)
928 (define-error 'math-overflow
"Floating-point overflow occurred" 'calc-error
)
929 (define-error 'math-underflow
"Floating-point underflow occurred" 'calc-error
)
931 (defvar calc-trail-pointer nil
932 "The \"current\" entry in trail buffer.")
933 (defvar calc-trail-overlay nil
934 "The value of overlay-arrow-string.")
935 (defvar calc-undo-list nil
936 "The list of previous operations for undo.")
937 (defvar calc-redo-list nil
938 "The list of recent undo operations.")
939 (defvar calc-main-buffer nil
940 "A pointer to Calculator buffer.")
941 (defvar calc-buffer-list nil
942 "A list of all Calc buffers.")
943 (defvar calc-trail-buffer nil
944 "A pointer to Calc Trail buffer.")
946 "Explanations of most recent errors.")
947 (defvar calc-next-why nil
)
948 (defvar calc-inverse-flag nil
949 "If non-nil, next operation is Inverse.")
950 (defvar calc-hyperbolic-flag nil
951 "If non-nil, next operation is Hyperbolic.")
952 (defvar calc-option-flag nil
953 "If non-nil, next operation has Optional behavior.")
954 (defvar calc-keep-args-flag nil
955 "If non-nil, next operation should not remove its arguments from stack.")
956 (defvar calc-function-open
"("
957 "Open-parenthesis string for function call notation.")
958 (defvar calc-function-close
")"
959 "Close-parenthesis string for function call notation.")
960 (defvar calc-language-output-filter nil
961 "Function through which to pass strings after formatting.")
962 (defvar calc-language-input-filter nil
963 "Function through which to pass strings before parsing.")
964 (defvar calc-radix-formatter nil
965 "Formatting function used for non-decimal numbers.")
966 (defvar calc-lang-slash-idiv nil
967 "A list of languages in which / might represent integer division.")
968 (defvar calc-lang-allow-underscores nil
969 "A list of languages which allow underscores in variable names.")
970 (defvar calc-lang-allow-percentsigns nil
971 "A list of languages which allow percent signs in variable names.")
972 (defvar calc-lang-c-type-hex nil
973 "Languages in which octal and hex numbers are written with leading 0 and 0x,")
974 (defvar calc-lang-brackets-are-subscripts nil
975 "Languages in which subscripts are indicated by brackets.")
976 (defvar calc-lang-parens-are-subscripts nil
977 "Languages in which subscripts are indicated by parentheses.")
979 (defvar calc-last-kill nil
980 "The last number killed in calc-mode.")
981 (defvar calc-dollar-values nil
982 "Values to be used for '$'.")
983 (defvar calc-dollar-used nil
984 "The highest order of '$' that occurred.")
985 (defvar calc-hashes-used nil
986 "The highest order of '#' that occurred.")
987 (defvar calc-quick-prev-results nil
988 "Previous results from Quick Calc.")
989 (defvar calc-said-hello nil
990 "Non-nil if the welcome message has been displayed.")
991 (defvar calc-executing-macro nil
992 "Non-nil if a keyboard macro is executing from the \"K\" key.")
993 (defvar calc-any-selections nil
994 "Non-nil if there are selections present.")
995 (defvar calc-help-phase
0
996 "The number of consecutive \"?\" keystrokes.")
997 (defvar calc-full-help-flag nil
998 "Non-nil if `calc-full-help' is being executed.")
999 (defvar calc-refresh-count
0
1000 "The number of `calc-refresh' calls.")
1001 (defvar calc-display-dirty nil
1002 "Non-nil if the stack display might not reflect the latest mode settings.")
1003 (defvar calc-prepared-composition nil
)
1004 (defvar calc-selection-cache-default-entry nil
)
1005 (defvar calc-embedded-info nil
1006 "If non-nil, a vector consisting of information for embedded mode.")
1007 (defvar calc-embedded-active nil
1008 "Alist of buffers with sorted lists of calc-embedded-infos.")
1009 (defvar calc-standalone-flag nil
1010 "Non-nil if Emacs started with standalone Calc.")
1011 (defvar var-EvalRules nil
1012 "User defined rules that Calc will apply automatically.")
1013 (defvar math-eval-rules-cache-tag t
)
1014 (defvar math-radix-explicit-format t
)
1015 (defvar math-expr-function-mapping nil
1016 "Alist of language specific functions with Calc functions.")
1017 (defvar math-expr-variable-mapping nil
1018 "Alist of language specific variables with Calc variables.")
1019 (defvar math-read-expr-quotes nil
)
1020 (defvar math-working-step nil
)
1021 (defvar math-working-step-2 nil
)
1022 (defvar var-i
'(special-const (math-imaginary 1)))
1023 (defvar var-pi
'(special-const (math-pi)))
1024 (defvar var-Ï€
'(special-const (math-pi)))
1025 (defvar var-e
'(special-const (math-e)))
1026 (defvar var-phi
'(special-const (math-phi)))
1027 (defvar var-φ
'(special-const (math-phi)))
1028 (defvar var-gamma
'(special-const (math-gamma-const)))
1029 (defvar var-γ
'(special-const (math-gamma-const)))
1030 (defvar var-Modes
'(special-const (math-get-modes-vec)))
1032 (mapc (lambda (v) (or (boundp v
) (set v nil
)))
1033 calc-local-var-list
)
1035 (defvar calc-mode-map
1036 (let ((map (make-keymap)))
1037 (suppress-keymap map t
)
1038 (define-key map
"+" 'calc-plus
)
1039 (define-key map
"-" 'calc-minus
)
1040 (define-key map
"*" 'calc-times
)
1041 (define-key map
"/" 'calc-divide
)
1042 (define-key map
"%" 'calc-mod
)
1043 (define-key map
"&" 'calc-inv
)
1044 (define-key map
"^" 'calc-power
)
1045 (define-key map
"\M-%" 'calc-percent
)
1046 (define-key map
"e" 'calcDigit-start
)
1047 (define-key map
"i" 'calc-info
)
1048 (define-key map
"n" 'calc-change-sign
)
1049 (define-key map
"q" 'calc-quit
)
1050 (define-key map
"Y" 'nil
)
1051 (define-key map
"Y?" 'calc-shift-Y-prefix-help
)
1052 (define-key map
"?" 'calc-help
)
1053 (define-key map
" " 'calc-enter
)
1054 (define-key map
"'" 'calc-algebraic-entry
)
1055 (define-key map
"$" 'calc-auto-algebraic-entry
)
1056 (define-key map
"\"" 'calc-auto-algebraic-entry
)
1057 (define-key map
"\t" 'calc-roll-down
)
1058 (define-key map
"\M-\t" 'calc-roll-up
)
1059 (define-key map
"\C-x\C-t" 'calc-transpose-lines
)
1060 (define-key map
"\C-m" 'calc-enter
)
1061 (define-key map
"\M-\C-m" 'calc-last-args-stub
)
1062 (define-key map
"\C-j" 'calc-over
)
1063 (define-key map
"\C-y" 'calc-yank
)
1064 (define-key map
[mouse-2
] 'calc-yank
)
1065 (define-key map
[remap undo
] 'calc-undo
)
1067 (mapc (lambda (x) (define-key map
(char-to-string x
) 'undefined
))
1069 (mapc (lambda (x) (define-key map
(char-to-string x
) 'calc-missing-key
))
1070 (concat "ABCDEFGHIJKLMNOPQRSTUVXZabcdfghjkmoprstuvwxyz"
1071 ":\\|!()[]<>{},;=~`\C-k\C-w"))
1072 (define-key map
"\M-w" 'calc-missing-key
)
1073 (define-key map
"\M-k" 'calc-missing-key
)
1074 (define-key map
"\M-\C-w" 'calc-missing-key
)
1075 (mapc (lambda (x) (define-key map
(char-to-string x
) 'calcDigit-start
))
1078 "The key map for Calc.")
1080 (defvar calc-digit-map
1081 (let ((map (make-keymap)))
1082 (map-keymap (lambda (key bind
)
1083 (define-key map
(vector key
)
1084 (if (eq bind
'undefined
)
1085 'undefined
'calcDigit-nondigit
)))
1087 (mapc (lambda (x) (define-key map
(char-to-string x
) 'calcDigit-key
))
1088 "_0123456789.e+-:n#@oh'\"mspM")
1089 (mapc (lambda (x) (define-key map
(char-to-string x
) 'calcDigit-letter
))
1090 "abcdfgijklqrtuvwxyzABCDEFGHIJKLNOPQRSTUVWXYZ")
1091 (define-key map
"'" 'calcDigit-algebraic
)
1092 (define-key map
"`" 'calcDigit-edit
)
1093 (define-key map
"\C-g" 'abort-recursive-edit
)
1095 "The key map for entering Calc digits.")
1100 (define-key calc-digit-map x
'calcDigit-backspace
)
1101 (define-key calc-mode-map x
'calc-pop
)
1102 (define-key calc-mode-map
1103 (if (and (vectorp x
) (featurep 'xemacs
))
1104 (if (= (length x
) 1)
1105 (vector (if (consp (aref x
0))
1106 (cons 'meta
(aref x
0))
1107 (list 'meta
(aref x
0))))
1112 (if calc-scan-for-dels
1113 (append (where-is-internal 'delete-backward-char global-map
)
1114 (where-is-internal 'backward-delete-char global-map
)
1115 (where-is-internal 'backward-delete-char-untabify global-map
)
1119 (defvar calc-dispatch-map
1120 (let ((map (make-keymap)))
1122 (let* ((x-chr (car x
))
1123 (x-str (char-to-string x-chr
))
1125 (define-key map x-str x-def
)
1126 (when (string-match "[a-z]" x-str
)
1127 ;; Map upper case char to same definition.
1128 (define-key map
(upcase x-str
) x-def
)
1129 (unless (string-match "[gmv]" x-str
)
1130 ;; Map control prefixed char to same definition.
1131 (define-key map
(vector (list 'control x-chr
)) x-def
)))
1132 (define-key map
(format "\e%c" x-chr
) x-def
)))
1133 '( ( ?a . calc-embedded-activate
)
1134 ( ?b . calc-big-or-small
)
1136 ( ?d . calc-embedded-duplicate
)
1137 ( ?e . calc-embedded
)
1138 ( ?f . calc-embedded-new-formula
)
1139 ( ?g . calc-grab-region
)
1140 ( ?h . calc-dispatch-help
)
1142 ( ?j . calc-embedded-select
)
1143 ( ?k . calc-keypad
)
1144 ( ?l . calc-load-everything
)
1145 ( ?m . read-kbd-macro
)
1146 ( ?n . calc-embedded-next
)
1147 ( ?o . calc-other-window
)
1148 ( ?p . calc-embedded-previous
)
1150 ( ?r . calc-grab-rectangle
)
1151 ( ?s . calc-info-summary
)
1152 ( ?t . calc-tutorial
)
1153 ( ?u . calc-embedded-update-formula
)
1154 ( ?w . calc-embedded-word
)
1156 ( ?y . calc-copy-to-buffer
)
1157 ( ?z . calc-user-invocation
)
1158 ( ?
\' . calc-embedded-new-formula
)
1159 ( ?\
` . calc-embedded-edit
)
1160 ( ?
: . calc-grab-sum-down
)
1161 ( ?_ . calc-grab-sum-across
)
1163 ( ?? . calc-dispatch-help
)
1164 ( ?
# . calc-same-interface
)
1165 ( ?
& . calc-same-interface
)
1166 ( ?
\\ . calc-same-interface
)
1167 ( ?
= . calc-same-interface
)
1168 ( ?
* . calc-same-interface
)
1169 ( ?
/ . calc-same-interface
)
1170 ( ?
+ . calc-same-interface
)
1171 ( ?- . calc-same-interface
) ))
1173 "The key map for starting Calc.")
1176 ;;;; (Autoloads here)
1177 (load "calc-loaddefs.el" nil t
)
1179 ;;;###autoload (define-key ctl-x-map "*" 'calc-dispatch)
1182 (defun calc-dispatch (&optional arg
)
1183 "Invoke the GNU Emacs Calculator. See \\[calc-dispatch-help] for details."
1185 ; (sit-for echo-keystrokes)
1186 (condition-case err
; look for other keys bound to calc-dispatch
1187 (let ((keys (this-command-keys)))
1188 (unless (or (not (stringp keys
))
1189 (string-match "\\`\C-u\\|\\`\e[-0-9#]\\|`[\M--\M-0-\M-9]" keys
)
1190 (eq (lookup-key calc-dispatch-map keys
) 'calc-same-interface
))
1191 (when (and (string-match "\\`[\C-@-\C-_]" keys
)
1193 (lookup-key calc-dispatch-map
(substring keys
0 1))))
1194 (define-key calc-dispatch-map
(substring keys
0 1) nil
))
1195 (define-key calc-dispatch-map keys
'calc-same-interface
)))
1197 (calc-do-dispatch arg
))
1199 (defvar calc-dispatch-help nil
)
1200 (defun calc-do-dispatch (arg)
1201 "Start the Calculator."
1202 (let ((key (calc-read-key-sequence
1203 (if calc-dispatch-help
1204 "Calc options: Calc, Keypad, Quick, Embed; eXit; Info, Tutorial; Grab; ?=more"
1205 (format "%s (Type ? for a list of Calc options)"
1206 (key-description (this-command-keys))))
1207 calc-dispatch-map
)))
1208 (setq key
(lookup-key calc-dispatch-map key
))
1212 (or (commandp key
) (require 'calc-ext
))
1213 (call-interactively key
))
1216 (defun calc-read-key-sequence (prompt map
)
1217 "Read keys, with prompt PROMPT and keymap MAP."
1218 (let ((prompt2 (format "%s " (key-description (this-command-keys))))
1219 (glob (current-global-map))
1220 (loc (current-local-map)))
1221 (or (input-pending-p) (message "%s" prompt
))
1222 (let ((key (calc-read-key t
))
1223 (input-method-function nil
))
1224 (calc-unread-command (cdr key
))
1227 (use-global-map map
)
1229 (read-key-sequence nil
))
1230 (use-global-map glob
)
1231 (use-local-map loc
)))))
1233 (defvar calc-alg-map
) ; Defined in calc-ext.el
1236 (defvar calc-embedded-modes
) ; Defined in calc-embed.el
1237 (defvar calc-override-minor-modes
) ; Defined in calc-embed.el
1238 (defun calc-kill-stack-buffer ()
1239 "Check to see if user wants to kill the Calc stack buffer.
1240 This will look for buffers using the Calc buffer for embedded mode,
1241 and inform the user if there are any.
1242 If the user wants to kill the Calc buffer, this will remove
1243 embedded information from the appropriate buffers and tidy up
1245 (let ((cb (current-buffer))
1249 (cea calc-embedded-active
))
1250 ;; Get a list of all buffers using this buffer for
1253 (when (and (eq cb
(aref (nth 1 (car cea
)) 1))
1254 (buffer-name (car (car cea
))))
1255 (setq info-list
(cons (car cea
) info-list
)))
1256 (setq cea
(cdr cea
)))
1257 ;; Eventually, prompt user with a list of buffers using embedded mode.
1261 (concat "This Calc stack is being used for embedded mode. Kill anyway?")))
1263 (with-current-buffer (car (car info-list
))
1264 (when calc-embedded-info
1265 (setq calc-embedded-info nil
1266 mode-line-buffer-identification
(car calc-embedded-modes
)
1267 truncate-lines
(nth 2 calc-embedded-modes
)
1268 buffer-read-only nil
)
1269 (use-local-map (nth 1 calc-embedded-modes
))
1270 (setq minor-mode-overriding-map-alist
1271 (remq calc-override-minor-modes minor-mode-overriding-map-alist
))
1272 (let ((str mode-line-buffer-identification
))
1273 (setq mode-line-buffer-identification str
))
1274 (set-buffer-modified-p (buffer-modified-p))))
1275 (setq calc-embedded-active
1276 (delete (car info-list
) calc-embedded-active
))
1277 (setq info-list
(cdr info-list
))))
1280 (setq calc-buffer-list
(delete cb calc-buffer-list
))
1281 (if (buffer-live-p calc-trail-buffer
)
1282 (with-current-buffer calc-trail-buffer
1283 (if (eq cb calc-main-buffer
)
1284 ;; If there are other Calc stacks, make another one
1285 ;; the calc-main-buffer ...
1286 (if calc-buffer-list
1287 (setq calc-main-buffer
(car calc-buffer-list
))
1288 ;; ... otherwise kill the trail and its windows.
1289 (let ((wl (get-buffer-window-list calc-trail-buffer
)))
1291 (delete-window (car wl
))
1292 (setq wl
(cdr wl
))))
1293 (kill-buffer calc-trail-buffer
)))))
1294 (setq calc-trail-buffer nil
)
1298 "Calculator major mode.
1300 This is an RPN calculator featuring arbitrary-precision integer, rational,
1301 floating-point, complex, matrix, and symbolic arithmetic.
1303 RPN calculation: 2 RET 3 + produces 5.
1304 Algebraic style: ' 2+3 RET produces 5.
1306 Basic operators are +, -, *, /, ^, & (reciprocal), % (modulo), n (change-sign).
1308 Press ? repeatedly for more complete help. Press `h i' to read the
1309 Calc manual on-line, `h s' to read the summary, or `h t' for the tutorial.
1311 Notations: 3.14e6 3.14 * 10^6
1312 _23 negative number -23 (or type `23 n')
1313 17:3 the fraction 17/3
1314 5:2:3 the fraction 5 and 2/3
1315 16#12C the integer 12C base 16 = 300 base 10
1316 8#177:100 the fraction 177:100 base 8 = 127:64 base 10
1317 (2, 4) complex number 2 + 4i
1318 (2; 4) polar complex number (r; theta)
1319 [1, 2, 3] vector ([[1, 2], [3, 4]] is a matrix)
1320 [1 .. 4) semi-open interval, 1 <= x < 4
1321 2 +/- 3 (p key) number with mean 2, standard deviation 3
1322 2 mod 3 (M key) number 2 computed modulo 3
1323 <1 jan 91> Date form (enter using ' key)
1329 (mapc (function ;FIXME: Why (set-default v (symbol-value v)) ?!?!?
1330 (lambda (v) (set-default v
(symbol-value v
)))) calc-local-var-list
)
1331 (kill-all-local-variables)
1332 (use-local-map (if (eq calc-algebraic-mode
'total
)
1333 (progn (require 'calc-ext
) calc-alg-map
) calc-mode-map
))
1334 (mapc #'make-local-variable calc-local-var-list
)
1335 (make-local-variable 'overlay-arrow-position
)
1336 (make-local-variable 'overlay-arrow-string
)
1337 (add-hook 'change-major-mode-hook
'font-lock-defontify nil t
)
1338 (add-hook 'kill-buffer-query-functions
1339 'calc-kill-stack-buffer
1341 (setq truncate-lines t
)
1342 (setq buffer-read-only t
)
1343 (setq major-mode
'calc-mode
)
1344 (setq mode-name
"Calculator")
1345 (setq calc-stack-top
(length (or (memq (assq 'top-of-stack calc-stack
)
1347 (setq calc-stack
(list (list 'top-of-stack
1349 (setq calc-stack-top
(- (length calc-stack
) calc-stack-top -
1))
1350 (or calc-loaded-settings-file
1351 (null calc-settings-file
)
1352 (equal calc-settings-file user-init-file
)
1354 (setq calc-loaded-settings-file t
)
1355 (load (file-name-sans-extension calc-settings-file
) t
))) ; t = missing-ok
1356 (let ((p command-line-args
))
1358 (and (equal (car p
) "-f")
1359 (string-match "calc" (nth 1 p
))
1360 (string-match "full" (nth 1 p
))
1361 (setq calc-standalone-flag t
))
1363 (require 'calc-menu
)
1364 (run-mode-hooks 'calc-mode-hook
)
1366 (calc-set-mode-line)
1367 (calc-check-defines)
1368 (if calc-buffer-list
(setq calc-stack
(copy-sequence calc-stack
)))
1369 (add-to-list 'calc-buffer-list
(current-buffer) t
))
1371 (defvar calc-check-defines
'calc-check-defines
) ; Suitable for run-hooks.
1372 (defun calc-check-defines ()
1373 (if (symbol-plist 'calc-define
)
1374 (let ((plist (copy-sequence (symbol-plist 'calc-define
))))
1375 (while (and plist
(null (nth 1 plist
)))
1376 (setq plist
(cdr (cdr plist
))))
1380 (require 'calc-macs
)
1381 (set-buffer "*Calculator*")
1383 (put 'calc-define
(car plist
) nil
)
1384 (eval (nth 1 plist
))
1385 (setq plist
(cdr (cdr plist
))))
1386 ;; See if this has added any more calc-define properties.
1387 (calc-check-defines))
1388 (setplist 'calc-define nil
)))))
1390 (defvar calc-trail-mode-map
1391 (let ((map (make-sparse-keymap)))
1392 (set-keymap-parent map calc-mode-map
)
1395 (define-derived-mode calc-trail-mode fundamental-mode
"Calc Trail"
1397 This mode is used by the *Calc Trail* buffer, which records all results
1398 obtained by the GNU Emacs Calculator.
1400 Calculator commands beginning with the `t' key are used to manipulate
1403 This buffer uses the same key map as the *Calculator* buffer; calculator
1404 commands given here will actually operate on the *Calculator* stack."
1405 (setq truncate-lines t
)
1406 (setq buffer-read-only t
)
1407 (make-local-variable 'overlay-arrow-position
)
1408 (make-local-variable 'overlay-arrow-string
)
1409 (when (= (buffer-size) 0)
1410 (let ((buffer-read-only nil
))
1411 (insert (propertize "Emacs Calculator Trail\n" 'face
'italic
)))))
1413 (defun calc-create-buffer ()
1414 "Create and initialize a buffer for the Calculator."
1415 (set-buffer (get-buffer-create "*Calculator*"))
1416 (or (derived-mode-p 'calc-mode
)
1418 (setq max-lisp-eval-depth
(max max-lisp-eval-depth
1000))
1419 (when calc-always-load-extensions
1420 (require 'calc-ext
))
1423 (calc-set-language calc-language calc-language-option t
)))
1426 (defun calc (&optional arg full-display interactive
)
1427 "The Emacs Calculator. Full documentation is listed under \"calc-mode\"."
1428 (interactive "P\ni\np")
1432 (if (= (prefix-numeric-value arg
) -
1)
1433 (calc-grab-region (region-beginning) (region-end) nil
)
1434 (when (= (prefix-numeric-value arg
) -
2)
1436 (when (get-buffer-window "*Calc Keypad*")
1438 (set-buffer (window-buffer)))
1439 (if (derived-mode-p 'calc-mode
)
1441 (let ((oldbuf (current-buffer)))
1442 (calc-create-buffer)
1443 (setq calc-was-keypad-mode nil
)
1444 (if (or (eq full-display t
)
1445 (and (null full-display
) calc-full-mode
))
1446 (switch-to-buffer (current-buffer) t
)
1447 (if (get-buffer-window (current-buffer))
1448 (select-window (get-buffer-window (current-buffer)))
1449 (if calc-window-hook
1450 (run-hooks 'calc-window-hook
)
1451 (let ((w (get-largest-window)))
1452 (if (and pop-up-windows
1453 (> (window-height w
)
1454 (+ window-min-height calc-window-height
2)))
1456 (setq w
(split-window w
1457 (- (window-height w
)
1458 calc-window-height
2)
1460 (set-window-buffer w
(current-buffer))
1462 (pop-to-buffer (current-buffer)))))))
1463 (with-current-buffer (calc-trail-buffer)
1464 (and calc-display-trail
1465 (= (window-width) (frame-width))
1466 (calc-trail-display 1 t
)))
1467 (message "Welcome to the GNU Emacs Calculator! Press `?' or `h' for help, `q' to quit")
1468 (run-hooks 'calc-start-hook
)
1469 (and (windowp full-display
)
1470 (window-point full-display
)
1471 (select-window full-display
))
1472 (calc-check-defines)
1473 (when (and calc-said-hello interactive
)
1476 (setq calc-said-hello t
)))))
1479 (defun full-calc (&optional interactive
)
1480 "Invoke the Calculator and give it a full-sized window."
1482 (calc nil t interactive
))
1484 (defun calc-same-interface (arg)
1485 "Invoke the Calculator using the most recent interface (calc or calc-keypad)."
1487 (if (and (equal (buffer-name) "*Gnuplot Trail*")
1488 (> (recursion-depth) 0))
1489 (exit-recursive-edit)
1490 (if (derived-mode-p 'calc-edit-mode
)
1491 (calc-edit-finish arg
)
1492 (if calc-was-keypad-mode
1494 (calc arg calc-full-mode t
)))))
1496 (defun calc-quit (&optional non-fatal interactive
)
1497 "Quit the Calculator in an appropriate manner."
1498 (interactive "i\np")
1499 (and calc-standalone-flag
(not non-fatal
)
1500 (save-buffers-kill-emacs nil
))
1501 (if (and (equal (buffer-name) "*Gnuplot Trail*")
1502 (> (recursion-depth) 0))
1503 (exit-recursive-edit))
1504 (if (derived-mode-p 'calc-edit-mode
)
1506 (if (and interactive
1508 (eq (current-buffer) (aref calc-embedded-info
0)))
1510 (unless (derived-mode-p 'calc-mode
)
1511 (calc-create-buffer))
1512 (run-hooks 'calc-end-hook
)
1513 (if (integerp calc-undo-length
)
1515 ((= calc-undo-length
0)
1516 (setq calc-undo-list nil calc-redo-list nil
))
1517 ((> calc-undo-length
0)
1518 (let ((tail (nthcdr (1- calc-undo-length
) calc-undo-list
)))
1519 (if tail
(setcdr tail nil
)))
1520 (setq calc-redo-list nil
))))
1521 (mapc (function (lambda (v) (set-default v
(symbol-value v
))))
1522 calc-local-var-list
)
1523 (let ((buf (current-buffer))
1524 (win (get-buffer-window (current-buffer)))
1525 (kbuf (get-buffer "*Calc Keypad*")))
1526 (delete-windows-on (calc-trail-buffer))
1527 ;; The next few lines will set `calc-window-height' so that the
1528 ;; next time Calc is called, the window will be the same size
1529 ;; as the current window.
1531 (not (window-full-height-p win
))
1532 (window-full-width-p win
) ; avoid calc-keypad
1533 (not (get-buffer-window "*Calc Keypad*")))
1534 (setq calc-window-height
(- (window-height win
) 2)))
1536 (delete-windows-on buf
)
1537 (and kbuf
(delete-windows-on kbuf
)))
1539 (bury-buffer calc-trail-buffer
)
1540 (and kbuf
(bury-buffer kbuf
))))))
1543 (defun quick-calc ()
1544 "Do a quick calculation in the minibuffer without invoking full Calculator."
1546 (calc-do-quick-calc))
1549 (defun calc-eval (str &optional separator
&rest args
)
1550 "Do a quick calculation and return the result as a string.
1551 Return value will either be the formatted result in string form,
1552 or a list containing a character position and an error message in string form."
1553 (calc-do-calc-eval str separator args
))
1556 (defun calc-keypad (&optional interactive
)
1557 "Invoke the Calculator in \"visual keypad\" mode.
1558 This is most useful in the X window system.
1559 In this mode, click on the Calc \"buttons\" using the left mouse button.
1560 Or, position the cursor manually and do M-x calc-keypad-press."
1563 (calc-do-keypad calc-full-mode interactive
))
1566 (defun full-calc-keypad (&optional interactive
)
1567 "Invoke the Calculator in full-screen \"visual keypad\" mode.
1568 See calc-keypad for details."
1571 (calc-do-keypad t interactive
))
1574 (defvar calc-aborted-prefix nil
)
1575 (defvar calc-start-time nil
)
1576 (defvar calc-command-flags nil
)
1577 (defvar calc-final-point-line
)
1578 (defvar calc-final-point-column
)
1579 ;;; Note that modifications to this function may break calc-pass-errors.
1580 (defun calc-do (do-body &optional do-slow
)
1581 (calc-check-defines)
1582 (let* ((calc-command-flags nil
)
1583 (calc-start-time (and calc-timing
(not calc-start-time
)
1585 (current-time-string)))
1586 (gc-cons-threshold (max gc-cons-threshold
1587 (if calc-timing
2000000 100000)))
1588 calc-final-point-line calc-final-point-column
)
1589 (setq calc-aborted-prefix
"")
1593 (if calc-embedded-info
1594 (calc-embedded-select-buffer)
1595 (calc-select-buffer))
1596 (and (eq calc-algebraic-mode
'total
)
1598 (use-local-map calc-alg-map
))
1599 (when (and do-slow calc-display-working-message
)
1600 (message "Working...")
1601 (calc-set-command-flag 'clear-message
))
1603 (setq calc-aborted-prefix nil
)
1604 (when (memq 'renum-stack calc-command-flags
)
1605 (calc-renumber-stack))
1606 (when (memq 'clear-message calc-command-flags
)
1609 (if (and (eq (car err
) 'error
)
1610 (stringp (nth 1 err
))
1611 (string-match "max-specpdl-size\\|max-lisp-eval-depth"
1613 (error "Computation got stuck or ran too long. Type `M' to increase the limit")
1614 (setq calc-aborted-prefix nil
)
1615 (signal (car err
) (cdr err
)))))
1616 (when calc-aborted-prefix
1617 (calc-record "<Aborted>" calc-aborted-prefix
))
1618 (and calc-start-time
1619 (let* ((calc-internal-prec 12)
1620 (calc-date-format nil
)
1621 (end-time (current-time-string))
1622 (time (if (equal calc-start-time end-time
)
1625 (calcFunc-unixtime (math-parse-date end-time
) 0)
1626 (calcFunc-unixtime (math-parse-date calc-start-time
)
1628 (if (math-lessp 1 time
)
1629 (calc-record time
"(t)"))))
1630 (or (memq 'no-align calc-command-flags
)
1631 (derived-mode-p 'calc-trail-mode
)
1632 (calc-align-stack-window))
1633 (and (memq 'position-point calc-command-flags
)
1634 (if (derived-mode-p 'calc-mode
)
1636 (goto-char (point-min))
1637 (forward-line (1- calc-final-point-line
))
1638 (move-to-column calc-final-point-column
))
1639 (save-current-buffer
1640 (calc-select-buffer)
1641 (goto-char (point-min))
1642 (forward-line (1- calc-final-point-line
))
1643 (move-to-column calc-final-point-column
))))
1644 (unless (memq 'keep-flags calc-command-flags
)
1646 (calc-select-buffer)
1647 (setq calc-inverse-flag nil
1648 calc-hyperbolic-flag nil
1649 calc-option-flag nil
1650 calc-keep-args-flag nil
)))
1651 (when (memq 'do-edit calc-command-flags
)
1652 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
1653 (calc-set-mode-line)
1654 (when calc-embedded-info
1655 (calc-embedded-finish-command))))
1656 (identity nil
)) ; allow a GC after timing is done
1659 (defun calc-set-command-flag (f)
1660 (unless (memq f calc-command-flags
)
1661 (setq calc-command-flags
(cons f calc-command-flags
))))
1663 (defun calc-select-buffer ()
1664 (or (derived-mode-p 'calc-mode
)
1665 (if calc-main-buffer
1666 (set-buffer calc-main-buffer
)
1667 (let ((buf (get-buffer "*Calculator*")))
1670 (error "Calculator buffer not available"))))))
1672 (defun calc-cursor-stack-index (&optional index
)
1673 (goto-char (point-max))
1674 (forward-line (- (calc-substack-height (or index
1)))))
1676 (defun calc-stack-size ()
1677 (- (length calc-stack
) calc-stack-top
))
1679 (defun calc-substack-height (n)
1682 (setq n
(+ n calc-stack-top
))
1683 (while (and (> n
0) stack
)
1684 (setq sum
(+ sum
(nth 1 (car stack
)))
1689 (defun calc-set-mode-line ()
1691 (calc-select-buffer)
1692 (let* ((fmt (car calc-float-format
))
1693 (figs (nth 1 calc-float-format
))
1695 (format "Calc%s%s: %d %s %-14s"
1696 (if (and calc-embedded-info
1697 (eq (aref calc-embedded-info
1) (current-buffer)))
1699 (if (and (> (length (buffer-name)) 12)
1700 (equal (substring (buffer-name) 0 12)
1702 (substring (buffer-name) 12)
1705 (capitalize (symbol-name calc-angle-mode
))
1708 ;; Input-related modes
1709 (if (eq calc-algebraic-mode
'total
) "Alg* "
1710 (if calc-algebraic-mode
"Alg "
1711 (if calc-incomplete-algebraic-mode
"Alg[( " "")))
1713 ;; Computational modes
1714 (if calc-symbolic-mode
"Symb " "")
1715 (cond ((eq calc-matrix-mode
'matrix
) "Matrix ")
1716 ((integerp calc-matrix-mode
)
1717 (format "Matrix%d " calc-matrix-mode
))
1718 ((eq calc-matrix-mode
'sqmatrix
) "SqMatrix ")
1719 ((eq calc-matrix-mode
'scalar
) "Scalar ")
1721 (if (eq calc-complex-mode
'polar
) "Polar " "")
1722 (if calc-prefer-frac
"Frac " "")
1723 (cond ((null calc-infinite-mode
) "")
1724 ((eq calc-infinite-mode
1) "+Inf ")
1726 (cond ((eq calc-simplify-mode
'none
) "NoSimp ")
1727 ((eq calc-simplify-mode
'num
) "NumSimp ")
1728 ((eq calc-simplify-mode
'binary
)
1729 (format "BinSimp%d " calc-word-size
))
1730 ((eq calc-simplify-mode
'alg
) "")
1731 ((eq calc-simplify-mode
'ext
) "ExtSimp ")
1732 ((eq calc-simplify-mode
'units
) "UnitSimp ")
1736 (cond ((= calc-number-radix
10) "")
1737 ((= calc-number-radix
2) "Bin ")
1738 ((= calc-number-radix
8) "Oct ")
1739 ((= calc-number-radix
16) "Hex ")
1740 (t (format "Radix%d " calc-number-radix
)))
1741 (if calc-twos-complement-mode
"TwosComp " "")
1742 (if calc-leading-zeros
"Zero " "")
1743 (cond ((null calc-language
) "")
1744 ((get calc-language
'math-lang-name
)
1745 (concat (get calc-language
'math-lang-name
) " "))
1747 (capitalize (symbol-name calc-language
))
1749 (cond ((eq fmt
'float
)
1750 (if (zerop figs
) "" (format "Norm%d " figs
)))
1751 ((eq fmt
'fix
) (format "Fix%d " figs
))
1753 (if (zerop figs
) "Sci " (format "Sci%d " figs
)))
1755 (if (zerop figs
) "Eng " (format "Eng%d " figs
))))
1756 (cond ((not calc-display-just
)
1757 (if calc-display-origin
1758 (format "Left%d " calc-display-origin
) ""))
1759 ((eq calc-display-just
'right
)
1760 (if calc-display-origin
1761 (format "Right%d " calc-display-origin
)
1764 (if calc-display-origin
1765 (format "Center%d " calc-display-origin
)
1767 (cond ((integerp calc-line-breaking
)
1768 (format "Wid%d " calc-line-breaking
))
1769 (calc-line-breaking "")
1772 ;; Miscellaneous other modes/indicators
1773 (if calc-assoc-selections
"" "Break ")
1774 (cond ((eq calc-mode-save-mode
'save
) "Save ")
1775 ((not calc-embedded-info
) "")
1776 ((eq calc-mode-save-mode
'local
) "Local ")
1777 ((eq calc-mode-save-mode
'edit
) "LocEdit ")
1778 ((eq calc-mode-save-mode
'perm
) "LocPerm ")
1779 ((eq calc-mode-save-mode
'global
) "Global ")
1781 (if calc-auto-recompute
"" "Manual ")
1782 (if (and (fboundp 'calc-gnuplot-alive
)
1783 (calc-gnuplot-alive)) "Graph " "")
1784 (if (and calc-embedded-info
1785 (> (calc-stack-size) 0)
1786 (calc-top 1 'sel
)) "Sel " "")
1787 (if calc-display-dirty
"Dirty " "")
1788 (if calc-option-flag
"Opt " "")
1789 (if calc-inverse-flag
"Inv " "")
1790 (if calc-hyperbolic-flag
"Hyp " "")
1791 (if calc-keep-args-flag
"Keep " "")
1792 (if (/= calc-stack-top
1) "Narrow " "")
1793 (apply 'concat calc-other-modes
)))))
1794 (if (equal new-mode-string mode-line-buffer-identification
)
1796 (setq mode-line-buffer-identification new-mode-string
)
1797 (set-buffer-modified-p (buffer-modified-p))
1798 (and calc-embedded-info
(calc-embedded-mode-line-change))))))
1800 (defun calc-align-stack-window ()
1801 (if (derived-mode-p 'calc-mode
)
1803 (let ((win (get-buffer-window (current-buffer))))
1806 (calc-cursor-stack-index 0)
1807 (vertical-motion (- 2 (window-height win
)))
1808 (set-window-start win
(point)))))
1809 (calc-cursor-stack-index 0)
1810 (if (looking-at " *\\.$")
1811 (goto-char (1- (match-end 0)))))
1813 (calc-select-buffer)
1814 (calc-align-stack-window))))
1816 (defun calc-check-stack (n)
1817 (if (> n
(calc-stack-size))
1818 (error "Too few elements on stack"))
1820 (error "Invalid argument")))
1822 (defun calc-push-list (vals &optional m sels
)
1824 (if calc-executing-macro
1825 (calc-push-list-in-macro vals m sels
)
1827 (calc-select-buffer)
1828 (let* ((val (car vals
))
1829 (entry (list val
1 (car sels
)))
1830 (mm (+ (or m
1) calc-stack-top
)))
1831 (calc-cursor-stack-index (1- (or m
1)))
1833 (setcdr (nthcdr (- mm
2) calc-stack
)
1834 (cons entry
(nthcdr (1- mm
) calc-stack
)))
1835 (setq calc-stack
(cons entry calc-stack
)))
1836 (let ((buffer-read-only nil
))
1837 (insert (math-format-stack-value entry
) "\n"))
1838 (calc-record-undo (list 'push mm
))
1839 (calc-set-command-flag 'renum-stack
))))
1840 (setq vals
(cdr vals
)
1843 (defun calc-pop-push-list (n vals
&optional m sels
)
1844 (if (and calc-any-selections
(null sels
))
1845 (calc-replace-selections n vals m
)
1846 (calc-pop-stack n m sels
)
1847 (calc-push-list vals m sels
)))
1849 (defun calc-pop-push-record-list (n prefix vals
&optional m sels
)
1850 (or (and (consp vals
)
1851 (or (integerp (car vals
))
1852 (consp (car vals
))))
1853 (and vals
(setq vals
(list vals
)
1854 sels
(and sels
(list sels
)))))
1855 (calc-check-stack (+ n
(or m
1) -
1))
1858 (calc-record-list vals prefix
)
1859 (calc-record (car vals
) prefix
)))
1860 (calc-pop-push-list n vals m sels
))
1862 (defun calc-enter-result (n prefix vals
&optional m
)
1863 (setq calc-aborted-prefix prefix
)
1864 (if (and (consp vals
)
1865 (or (integerp (car vals
))
1866 (consp (car vals
))))
1867 (setq vals
(mapcar 'calc-normalize vals
))
1868 (setq vals
(calc-normalize vals
)))
1869 (or (and (consp vals
)
1870 (or (integerp (car vals
))
1871 (consp (car vals
))))
1872 (setq vals
(list vals
)))
1873 (if (equal vals
'((nil)))
1875 (calc-pop-push-record-list n prefix vals m
)
1878 (defun calc-normalize (val)
1879 (if (memq calc-simplify-mode
'(nil none num
))
1880 (math-normalize val
)
1882 (calc-normalize-fancy val
)))
1884 (defun calc-handle-whys ()
1886 (calc-do-handle-whys)))
1889 (defun calc-pop-stack (&optional n m sel-ok
) ; pop N objs at level M of stack.
1892 (or calc-keep-args-flag
1893 (let ((mm (+ m calc-stack-top
)))
1894 (if (and calc-any-selections
(not sel-ok
)
1895 (calc-top-selected n m
))
1897 (if calc-executing-macro
1898 (calc-pop-stack-in-macro n mm
)
1899 (calc-record-undo (list 'pop mm
(calc-top-list n m
'full
)))
1901 (calc-select-buffer)
1902 (let ((buffer-read-only nil
))
1905 (calc-cursor-stack-index (1- m
))
1906 (let ((bot (point)))
1907 (calc-cursor-stack-index (+ n m -
1))
1908 (delete-region (point) bot
))
1909 (setcdr (nthcdr (- mm
2) calc-stack
)
1910 (nthcdr (+ n mm -
1) calc-stack
)))
1911 (calc-cursor-stack-index n
)
1912 (setq calc-stack
(nthcdr n calc-stack
))
1913 (delete-region (point) (point-max))))
1914 (calc-set-command-flag 'renum-stack
))))))
1916 (defun calc-get-stack-element (x &optional sel-mode
)
1917 (cond ((eq sel-mode
'entry
)
1921 ((or (null (nth 2 x
))
1923 (not calc-use-selections
))
1929 ;; Get the Nth element of the stack (N=1 is the top element).
1930 (defun calc-top (&optional n sel-mode
)
1932 (calc-check-stack n
)
1933 (calc-get-stack-element (nth (+ n calc-stack-top -
1) calc-stack
) sel-mode
))
1935 (defun calc-top-n (&optional n sel-mode
) ; In case precision has changed.
1936 (math-check-complete (calc-normalize (calc-top n sel-mode
))))
1938 (defun calc-top-list (&optional n m sel-mode
)
1941 (calc-check-stack (+ n m -
1))
1943 (let ((top (copy-sequence (nthcdr (+ m calc-stack-top -
1)
1945 (setcdr (nthcdr (1- n
) top
) nil
)
1947 (mapcar (lambda (x) (calc-get-stack-element x sel-mode
)) top
)))))
1949 (defun calc-top-list-n (&optional n m sel-mode
)
1950 (mapcar 'math-check-complete
1951 (mapcar 'calc-normalize
(calc-top-list n m sel-mode
))))
1954 (defun calc-renumber-stack ()
1955 (if calc-line-numbering
1957 (calc-cursor-stack-index 0)
1959 (buffer-read-only nil
)
1960 (stack (nthcdr calc-stack-top calc-stack
)))
1961 (if (re-search-forward "^[0-9]+[:*]" nil t
)
1964 (while (re-search-forward "^[0-9]+[:*]" nil t
)
1965 (let ((buffer-read-only nil
))
1969 (calc-cursor-stack-index 0)))
1970 (while (re-search-backward "^[0-9]+[:*]" nil t
)
1973 (insert (format "%03d%s" (% lnum
1000)
1974 (if (and (nth 2 (car stack
))
1975 calc-use-selections
) "*" ":")))
1976 (let ((prefix (int-to-string lnum
)))
1977 (insert prefix
(if (and (nth 2 (car stack
))
1978 calc-use-selections
) "*" ":")
1979 (make-string (- 3 (length prefix
)) 32))))
1981 (setq lnum
(1+ lnum
)
1982 stack
(cdr stack
))))))
1983 (and calc-embedded-info
(calc-embedded-stack-change)))
1985 (defvar calc-any-evaltos nil
)
1986 (defun calc-refresh (&optional align
)
1988 (and (derived-mode-p 'calc-mode
)
1989 (not calc-executing-macro
)
1990 (let* ((buffer-read-only nil
)
1991 (save-point (point))
1992 (save-mark (condition-case err
(mark) (error nil
)))
1993 (save-aligned (looking-at "\\.$"))
1995 (calc-any-evaltos nil
))
1996 (setq calc-any-selections nil
)
1998 (when calc-show-banner
1999 (insert (propertize "--- Emacs Calculator Mode ---\n"
2002 (goto-char (point-min))
2003 (when calc-show-banner
2005 (insert (math-format-stack-value (car thing
)) "\n")
2006 (setq thing
(cdr thing
)))
2007 (calc-renumber-stack)
2008 (if calc-display-dirty
2009 (calc-wrapper (setq calc-display-dirty nil
)))
2010 (and calc-any-evaltos calc-auto-recompute
2011 (calc-wrapper (calc-refresh-evaltos)))
2012 (if (or align save-aligned
)
2013 (calc-align-stack-window)
2014 (goto-char save-point
))
2015 (if save-mark
(set-mark save-mark
))))
2016 (and calc-embedded-info
(not (derived-mode-p 'calc-mode
))
2017 (with-current-buffer (aref calc-embedded-info
1)
2018 (calc-refresh align
)))
2019 (setq calc-refresh-count
(1+ calc-refresh-count
)))
2021 ;; Dates that are built-in options for `calc-gregorian-switch' should be
2022 ;; (YEAR MONTH DAY math-date-from-gregorian-dt(YEAR MONTH DAY)) for speed.
2023 (defcustom calc-gregorian-switch nil
2024 "The first day the Gregorian calendar is used by Calc's date forms.
2025 This is `nil' (the default) if the Gregorian calendar is the only one used.
2026 Otherwise, it should be a list `(YEAR MONTH DAY)' when Calc begins to use
2027 the Gregorian calendar; Calc will use the Julian calendar for earlier dates.
2028 The dates in which different regions of the world began to use the
2029 Gregorian calendar vary quite a bit, even within a single country.
2030 If you want Calc's date forms to switch between the Julian and
2031 Gregorian calendar, you can specify the date or choose from several
2032 common choices. Some of these choices should be taken with a grain
2033 of salt; for example different parts of France changed calendars at
2034 different times, and Sweden's change to the Gregorian calendar was
2035 complicated. Also, the boundaries of the countries were different at
2036 the times of the calendar changes than they are now.
2037 The Vatican decided that the Gregorian calendar should take effect
2038 on 15 October 1582 (Gregorian), and many Catholic countries made
2039 the change then. Great Britain and its colonies had the Gregorian
2040 calendar take effect on 14 September 1752 (Gregorian); this includes
2044 :type
'(choice (const :tag
"Always use the Gregorian calendar" nil
)
2045 (const :tag
"1582-10-15 - Italy, Poland, Portugal, Spain" (1582 10 15 577736))
2046 (const :tag
"1582-12-20 - France" (1582 12 20 577802))
2047 (const :tag
"1582-12-25 - Luxemburg" (1582 12 25 577807))
2048 (const :tag
"1584-01-17 - Bohemia and Moravia" (1584 1 17 578195))
2049 (const :tag
"1587-11-01 - Hungary" (1587 11 1 579579))
2050 (const :tag
"1700-03-01 - Denmark" (1700 3 1 620607))
2051 (const :tag
"1701-01-12 - Protestant Switzerland" (1701 1 12 620924))
2052 (const :tag
"1752-09-14 - Great Britain and dominions" (1752 9 14 639797))
2053 (const :tag
"1753-03-01 - Sweden" (1753 3 1 639965))
2054 (const :tag
"1918-02-14 - Russia" (1918 2 14 700214))
2055 (const :tag
"1919-04-14 - Romania" (1919 4 14 700638))
2056 (list :tag
"(YEAR MONTH DAY)"
2057 (integer :tag
"Year")
2058 (integer :tag
"Month (integer)")
2059 (integer :tag
"Day")))
2060 :set
(lambda (symbol value
)
2061 (set-default symbol value
)
2062 (setq math-format-date-cache nil
)
2065 ;;;; The Calc Trail buffer.
2067 (defun calc-check-trail-aligned ()
2069 (let ((win (get-buffer-window (current-buffer))))
2071 (pos-visible-in-window-p (1- (point-max)) win
)))))
2073 (defun calc-trail-buffer ()
2074 (and (or (null calc-trail-buffer
)
2075 (null (buffer-name calc-trail-buffer
)))
2077 (setq calc-trail-buffer
(get-buffer-create "*Calc Trail*"))
2078 (let ((buf (or (and (not (derived-mode-p 'calc-mode
))
2079 (get-buffer "*Calculator*"))
2081 (set-buffer calc-trail-buffer
)
2082 (unless (derived-mode-p 'calc-trail-mode
)
2084 (set (make-local-variable 'calc-main-buffer
) buf
)))))
2085 (or (and calc-trail-pointer
2086 (eq (marker-buffer calc-trail-pointer
) calc-trail-buffer
))
2087 (with-current-buffer calc-trail-buffer
2088 (goto-char (point-min))
2090 (setq calc-trail-pointer
(point-marker))))
2093 (defun calc-record (val &optional prefix
)
2094 (setq calc-aborted-prefix nil
)
2095 (or calc-executing-macro
2096 (let* ((mainbuf (current-buffer))
2097 (buf (calc-trail-buffer))
2098 (calc-display-raw nil
)
2099 (calc-can-abbrev-vectors t
)
2103 (math-showing-full-precision
2104 (math-format-flat-expr val
0)))
2106 (with-current-buffer buf
2107 (let ((aligned (calc-check-trail-aligned))
2108 (buffer-read-only nil
))
2109 (goto-char (point-max))
2110 (cond ((null prefix
) (insert " "))
2111 ((and (> (length prefix
) 4)
2112 (string-match " " prefix
4))
2113 (insert (substring prefix
0 4) " "))
2114 (t (insert (format "%4s " prefix
))))
2116 (let ((win (get-buffer-window buf
)))
2117 (if (and aligned win
(not (memq 'hold-trail calc-command-flags
)))
2119 (goto-char (1- (point-max))))))))
2123 (defun calc-trail-display (flag &optional no-refresh interactive
)
2124 (interactive "P\ni\np")
2125 (let ((win (get-buffer-window (calc-trail-buffer))))
2126 (if (setq calc-display-trail
2127 (not (if flag
(memq flag
'(nil 0)) win
)))
2130 (if calc-trail-window-hook
2131 (run-hooks 'calc-trail-window-hook
)
2132 (let ((w (split-window nil
(/ (* (window-width) 2) 3) t
)))
2133 (set-window-buffer w calc-trail-buffer
)))
2135 (setq overlay-arrow-string calc-trail-overlay
2136 overlay-arrow-position calc-trail-pointer
)
2148 (calc-refresh))))))))
2151 (defun calc-trail-here ()
2153 (if (derived-mode-p 'calc-trail-mode
)
2160 (if (or (bobp) (eobp))
2161 (setq overlay-arrow-position nil
) ; trail is empty
2162 (set-marker calc-trail-pointer
(point) (current-buffer))
2163 (setq calc-trail-overlay
(concat (buffer-substring (point)
2166 overlay-arrow-string calc-trail-overlay
2167 overlay-arrow-position calc-trail-pointer
)
2169 (let ((win (get-buffer-window (current-buffer))))
2172 (forward-line (/ (window-height win
) 2))
2173 (forward-line (- 1 (window-height win
)))
2174 (set-window-start win
(point))
2175 (set-window-point win
(+ calc-trail-pointer
4))
2176 (set-buffer calc-main-buffer
)
2177 (setq overlay-arrow-string calc-trail-overlay
2178 overlay-arrow-position calc-trail-pointer
))))))
2179 (error "Not in Calc Trail buffer")))
2186 (defun calc-record-undo (rec)
2187 (or calc-executing-macro
2188 (if (memq 'undo calc-command-flags
)
2189 (setq calc-undo-list
(cons (cons rec
(car calc-undo-list
))
2190 (cdr calc-undo-list
)))
2191 (setq calc-undo-list
(cons (list rec
) calc-undo-list
)
2193 (calc-set-command-flag 'undo
))))
2198 ;;; Arithmetic commands.
2200 (defun calc-binary-op (name func arg
&optional ident unary func2
)
2201 (setq calc-aborted-prefix name
)
2203 (calc-enter-result 2 name
(cons (or func2 func
)
2204 (mapcar 'math-check-complete
2205 (calc-top-list 2))))
2207 (calc-binary-op-fancy name func arg ident unary
)))
2209 (defun calc-unary-op (name func arg
&optional func2
)
2210 (setq calc-aborted-prefix name
)
2212 (calc-enter-result 1 name
(list (or func2 func
)
2213 (math-check-complete (calc-top 1))))
2215 (calc-unary-op-fancy name func arg
)))
2218 (defun calc-plus (arg)
2221 (calc-binary-op "+" 'calcFunc-add arg
0 nil
'+)))
2223 (defun calc-minus (arg)
2226 (calc-binary-op "-" 'calcFunc-sub arg
0 'neg
'-
)))
2228 (defun calc-times (arg)
2231 (calc-binary-op "*" 'calcFunc-mul arg
1 nil
'*)))
2233 (defun calc-divide (arg)
2236 (calc-binary-op "/" 'calcFunc-div arg
0 'calcFunc-inv
'/)))
2238 (defun calc-left-divide (arg)
2241 (calc-binary-op "ldiv" 'calcFunc-ldiv arg
0 nil nil
)))
2243 (defun calc-change-sign (arg)
2246 (calc-unary-op "chs" 'neg arg
)))
2250 ;;; Stack management commands.
2252 (defun calc-enter (n)
2256 (calc-push-list (calc-top-list 1 (- n
))))
2258 (calc-push-list (calc-top-list (calc-stack-size))))
2260 (calc-push-list (calc-top-list n
))))))
2266 (let* ((nn (prefix-numeric-value n
))
2267 (top (and (null n
) (calc-top 1))))
2268 (cond ((and (null n
)
2269 (eq (car-safe top
) 'incomplete
)
2270 (> (length top
) (if (eq (nth 1 top
) 'intv
) 3 2)))
2271 (calc-pop-push-list 1 (let ((tt (copy-sequence top
)))
2272 (setcdr (nthcdr (- (length tt
) 2) tt
) nil
)
2275 (if (and calc-any-selections
2276 (calc-top-selected 1 (- nn
)))
2277 (calc-delete-selection (- nn
))
2278 (calc-pop-stack 1 (- nn
) t
)))
2280 (calc-pop-stack (calc-stack-size) 1 t
))
2282 (if (and calc-any-selections
2284 (calc-top-selected 1 1))
2285 (calc-delete-selection 1)
2286 (calc-pop-stack nn
)))))))
2291 ;;;; Reading a number using the minibuffer.
2292 (defvar calc-buffer
)
2293 (defvar calc-prev-char
)
2294 (defvar calc-prev-prev-char
)
2295 (defvar calc-digit-value
)
2296 (defun calcDigit-start ()
2299 (if (or calc-algebraic-mode
2300 (and (> calc-number-radix
14) (eq last-command-event ?e
)))
2301 (calc-alg-digit-entry)
2302 (calc-unread-command)
2303 (setq calc-aborted-prefix nil
)
2304 (let* ((calc-digit-value nil
)
2305 (calc-prev-char nil
)
2306 (calc-prev-prev-char nil
)
2307 (calc-buffer (current-buffer))
2308 (buf (if (featurep 'xemacs
)
2310 (catch 'execute-kbd-macro
2312 (read-from-minibuffer
2313 "Calc: " "" calc-digit-map
)))
2314 (error "XEmacs requires RET after %s"
2315 "digit entry in kbd macro"))
2316 (let ((old-esc (lookup-key global-map
"\e")))
2319 (define-key global-map
"\e" nil
)
2320 (read-from-minibuffer "Calc: " "" calc-digit-map
))
2321 (define-key global-map
"\e" old-esc
))))))
2322 (or calc-digit-value
(setq calc-digit-value
(math-read-number buf
)))
2323 (if (stringp calc-digit-value
)
2324 (calc-alg-entry calc-digit-value
)
2325 (if calc-digit-value
2326 (calc-push-list (list (calc-record (calc-normalize
2327 calc-digit-value
))))))
2328 (if (eq calc-prev-char
'dots
)
2333 (defsubst calc-minibuffer-size
()
2334 (- (point-max) (minibuffer-prompt-end)))
2336 (defun calcDigit-nondigit ()
2338 ;; Exercise for the reader: Figure out why this is a good precaution!
2339 (or (boundp 'calc-buffer
)
2340 (use-local-map minibuffer-local-map
))
2341 (let ((str (minibuffer-contents)))
2342 (setq calc-digit-value
(with-current-buffer calc-buffer
2343 (math-read-number str
))))
2344 (if (and (null calc-digit-value
) (> (calc-minibuffer-size) 0))
2347 (calc-temp-minibuffer-message " [Bad format]"))
2348 (or (memq last-command-event
'(32 13))
2349 (progn (setq prefix-arg current-prefix-arg
)
2350 (calc-unread-command (if (and (eq last-command-event
27)
2351 (>= last-input-event
128))
2357 (defun calc-minibuffer-contains (rex)
2359 (goto-char (minibuffer-prompt-end))
2362 (defun calcDigit-key ()
2364 (goto-char (point-max))
2365 (if (or (and (memq last-command-event
'(?
+ ?-
))
2367 (/= (preceding-char) ?e
))
2368 (and (memq last-command-event
'(?m ?s
))
2369 (not (calc-minibuffer-contains "[-+]?[0-9]+\\.?0*[@oh].*"))
2370 (not (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*"))))
2371 (calcDigit-nondigit)
2372 (if (calc-minibuffer-contains "\\([-+]?\\|.* \\)\\'")
2373 (cond ((memq last-command-event
'(?. ?
@)) (insert "0"))
2374 ((and (memq last-command-event
'(?o ?h ?m
))
2375 (not (calc-minibuffer-contains ".*#.*"))) (insert "0"))
2376 ((memq last-command-event
'(?
: ?e
)) (insert "1"))
2377 ((eq last-command-event ?
#)
2378 (insert (int-to-string calc-number-radix
)))))
2379 (if (and (calc-minibuffer-contains "\\([-+]?[0-9]+#\\|[^:]*:\\)\\'")
2380 (eq last-command-event ?
:))
2382 (if (and (calc-minibuffer-contains "[-+]?[0-9]+#\\'")
2383 (eq last-command-event ?.
))
2385 (if (and (calc-minibuffer-contains "[-+]?0*\\([2-9]\\|1[0-4]\\)#\\'")
2386 (eq last-command-event ?e
))
2388 (if (or (and (memq last-command-event
'(?h ?o ?m ?s ?p
))
2389 (calc-minibuffer-contains ".*#.*"))
2390 (and (eq last-command-event ?e
)
2391 (calc-minibuffer-contains "[-+]?\\(1[5-9]\\|[2-9][0-9]\\)#.*"))
2392 (and (eq last-command-event ?n
)
2393 (calc-minibuffer-contains "[-+]?\\(2[4-9]\\|[3-9][0-9]\\)#.*")))
2394 (setq last-command-event
(upcase last-command-event
)))
2396 ((memq last-command-event
'(?_ ?n
))
2397 (goto-char (minibuffer-prompt-end))
2398 (if (and (search-forward " +/- " nil t
)
2399 (not (search-forward "e" nil t
)))
2401 (and (not (calc-minibuffer-contains "[-+]?\\(1[5-9]\\|[2-9][0-9]\\)#.*"))
2402 (search-forward "e" nil t
))
2403 (if (looking-at "+")
2405 (if (looking-at "-")
2408 (goto-char (point-max)))
2409 ((eq last-command-event ?p
)
2410 (if (or (calc-minibuffer-contains ".*\\+/-.*")
2411 (calc-minibuffer-contains ".*mod.*")
2412 (calc-minibuffer-contains ".*#.*")
2413 (calc-minibuffer-contains ".*[-+e:]\\'"))
2415 (if (not (calc-minibuffer-contains ".* \\'"))
2418 ((and (eq last-command-event ?M
)
2419 (not (calc-minibuffer-contains
2420 "[-+]?\\(2[3-9]\\|[3-9][0-9]\\)#.*")))
2421 (if (or (calc-minibuffer-contains ".*\\+/-.*")
2422 (calc-minibuffer-contains ".*mod *[^ ]+")
2423 (calc-minibuffer-contains ".*[-+e:]\\'"))
2425 (if (calc-minibuffer-contains ".*mod \\'")
2426 (if calc-previous-modulo
2427 (insert (math-format-flat-expr calc-previous-modulo
0))
2429 (if (not (calc-minibuffer-contains ".* \\'"))
2433 (insert (char-to-string last-command-event
))
2434 (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]*\\)?\\)?\\'")
2435 (let ((radix (string-to-number
2437 (match-beginning 2) (match-end 2)))))
2440 (or (memq last-command-event
'(?
# ?
: ?. ?e ?
+ ?-
))
2441 (let ((dig (math-read-radix-digit
2442 (upcase last-command-event
))))
2445 (calc-minibuffer-contains
2446 "[-+]?\\(.*\\+/- *\\|.*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]?\\'"))
2447 (if (and (memq last-command-event
'(?
@ ?o ?h ?
\' ?m
))
2448 (string-match " " calc-hms-format
))
2450 (if (and (eq this-command last-command
)
2451 (eq last-command-event ?.
))
2457 (calc-temp-minibuffer-message " [Bad format]"))))))
2458 (setq calc-prev-prev-char calc-prev-char
2459 calc-prev-char last-command-event
))
2462 (defun calcDigit-backspace ()
2464 (goto-char (point-max))
2465 (cond ((calc-minibuffer-contains ".* \\+/- \\'")
2466 (backward-delete-char 5))
2467 ((calc-minibuffer-contains ".* mod \\'")
2468 (backward-delete-char 5))
2469 ((calc-minibuffer-contains ".* \\'")
2470 (backward-delete-char 2))
2471 ((eq last-command
'calcDigit-start
)
2473 (t (backward-delete-char 1)))
2474 (if (= (calc-minibuffer-size) 0)
2476 (setq last-command-event
13)
2477 (calcDigit-nondigit))))
2482 (defconst math-bignum-digit-length
2483 (truncate (/ (log (/ most-positive-fixnum
2) 10) 2))
2484 "The length of a \"digit\" in Calc bignums.
2485 If a big integer is of the form (bigpos N0 N1 ...), this is the
2486 length of the allowable Emacs integers N0, N1,...
2487 The value of 2*10^(2*MATH-BIGNUM-DIGIT-LENGTH) must be less than the
2488 largest Emacs integer.")
2490 (defconst math-bignum-digit-size
2491 (expt 10 math-bignum-digit-length
)
2492 "An upper bound for the size of the \"digit\"s in Calc bignums.")
2494 (defconst math-small-integer-size
2495 (expt math-bignum-digit-size
2)
2496 "An upper bound for the size of \"small integer\"s in Calc.")
2499 ;;;; Arithmetic routines.
2501 ;; An object as manipulated by one of these routines may take any of the
2504 ;; integer An integer. For normalized numbers, this format
2506 ;; negative math-small-integer-size + 1 to
2507 ;; math-small-integer-size - 1
2509 ;; (bigpos N0 N1 N2 ...) A big positive integer,
2510 ;; N0 + N1*math-bignum-digit-size
2511 ;; + N2*(math-bignum-digit-size)^2 ...
2512 ;; (bigneg N0 N1 N2 ...) A big negative integer,
2513 ;; - N0 - N1*math-bignum-digit-size ...
2514 ;; Each digit N is in the range
2515 ;; 0 ... math-bignum-digit-size -1.
2516 ;; Normalized, always at least three N present,
2517 ;; and the most significant N is nonzero.
2519 ;; (frac NUM DEN) A fraction. NUM and DEN are small or big integers.
2520 ;; Normalized, DEN > 1.
2522 ;; (float NUM EXP) A floating-point number, NUM * 10^EXP;
2523 ;; NUM is a small or big integer, EXP is a small int.
2524 ;; Normalized, NUM is not a multiple of 10, and
2525 ;; abs(NUM) < 10^calc-internal-prec.
2526 ;; Normalized zero is stored as (float 0 0).
2528 ;; (cplx REAL IMAG) A complex number; REAL and IMAG are any of above.
2529 ;; Normalized, IMAG is nonzero.
2531 ;; (polar R THETA) Polar complex number. Normalized, R > 0 and THETA
2532 ;; is neither zero nor 180 degrees (pi radians).
2534 ;; (vec A B C ...) Vector of objects A, B, C, ... A matrix is a
2535 ;; vector of vectors.
2537 ;; (hms H M S) Angle in hours-minutes-seconds form. All three
2538 ;; components have the same sign; H and M must be
2539 ;; numerically integers; M and S are expected to
2540 ;; lie in the range [0,60).
2542 ;; (date N) A date or date/time object. N is an integer to
2543 ;; store a date only, or a fraction or float to
2544 ;; store a date and time.
2546 ;; (sdev X SIGMA) Error form, X +/- SIGMA. When normalized,
2547 ;; SIGMA > 0. X is any complex number and SIGMA
2548 ;; is real numbers; or these may be symbolic
2549 ;; expressions where SIGMA is assumed real.
2551 ;; (intv MASK LO HI) Interval form. MASK is 0=(), 1=(], 2=[), or 3=[].
2552 ;; LO and HI are any real numbers, or symbolic
2553 ;; expressions which are assumed real, and LO < HI.
2554 ;; For [LO..HI], if LO = HI normalization produces LO,
2555 ;; and if LO > HI normalization produces [LO..LO).
2556 ;; For other intervals, if LO > HI normalization
2557 ;; sets HI equal to LO.
2559 ;; (mod N M) Number modulo M. When normalized, 0 <= N < M.
2560 ;; N and M are real numbers.
2562 ;; (var V S) Symbolic variable. V is a Lisp symbol which
2563 ;; represents the variable's visible name. S is
2564 ;; the symbol which actually stores the variable's
2565 ;; value: (var pi var-pi).
2567 ;; In general, combining rational numbers in a calculation always produces
2568 ;; a rational result, but if either argument is a float, result is a float.
2570 ;; In the following comments, [x y z] means result is x, args must be y, z,
2571 ;; respectively, where the code letters are:
2573 ;; O Normalized object (vector or number)
2574 ;; V Normalized vector
2575 ;; N Normalized number of any type
2576 ;; N Normalized complex number
2577 ;; R Normalized real number (float or rational)
2578 ;; F Normalized floating-point number
2579 ;; T Normalized rational number
2580 ;; I Normalized integer
2581 ;; B Normalized big integer
2582 ;; S Normalized small integer
2583 ;; D Digit (small integer, 0..999)
2584 ;; L Normalized bignum digit list (without "bigpos" or "bigneg" symbol)
2585 ;; or normalized vector element list (without "vec")
2586 ;; P Predicate (truth value)
2587 ;; X Any Lisp object
2590 ;; Lower-case letters signify possibly un-normalized values.
2591 ;; "L.D" means a cons of an L and a D.
2592 ;; [N N; n n] means result will be normalized if argument is.
2593 ;; Also, [Public] marks routines intended to be called from outside.
2594 ;; [This notation has been neglected in many recent routines.]
2596 (defvar math-eval-rules-cache
)
2597 (defvar math-eval-rules-cache-other
)
2598 ;;; Reduce an object to canonical (normalized) form. [O o; Z Z] [Public]
2600 (defvar math-normalize-a
)
2601 (defvar math-normalize-error nil
2602 "Non-nil if the last call the `math-normalize' returned an error.")
2604 (defun math-normalize (math-normalize-a)
2605 (setq math-normalize-error nil
)
2607 ((not (consp math-normalize-a
))
2608 (if (integerp math-normalize-a
)
2609 (if (or (>= math-normalize-a math-small-integer-size
)
2610 (<= math-normalize-a
(- math-small-integer-size
)))
2611 (math-bignum math-normalize-a
)
2614 ((eq (car math-normalize-a
) 'bigpos
)
2615 (if (eq (nth (1- (length math-normalize-a
)) math-normalize-a
) 0)
2616 (let* ((last (setq math-normalize-a
2617 (copy-sequence math-normalize-a
))) (digs math-normalize-a
))
2618 (while (setq digs
(cdr digs
))
2619 (or (eq (car digs
) 0) (setq last digs
)))
2621 (if (cdr (cdr (cdr math-normalize-a
)))
2624 ((cdr (cdr math-normalize-a
)) (+ (nth 1 math-normalize-a
)
2625 (* (nth 2 math-normalize-a
)
2626 math-bignum-digit-size
)))
2627 ((cdr math-normalize-a
) (nth 1 math-normalize-a
))
2629 ((eq (car math-normalize-a
) 'bigneg
)
2630 (if (eq (nth (1- (length math-normalize-a
)) math-normalize-a
) 0)
2631 (let* ((last (setq math-normalize-a
(copy-sequence math-normalize-a
)))
2632 (digs math-normalize-a
))
2633 (while (setq digs
(cdr digs
))
2634 (or (eq (car digs
) 0) (setq last digs
)))
2636 (if (cdr (cdr (cdr math-normalize-a
)))
2639 ((cdr (cdr math-normalize-a
)) (- (+ (nth 1 math-normalize-a
)
2640 (* (nth 2 math-normalize-a
)
2641 math-bignum-digit-size
))))
2642 ((cdr math-normalize-a
) (- (nth 1 math-normalize-a
)))
2644 ((eq (car math-normalize-a
) 'float
)
2645 (math-make-float (math-normalize (nth 1 math-normalize-a
))
2646 (nth 2 math-normalize-a
)))
2647 ((or (memq (car math-normalize-a
)
2648 '(frac cplx polar hms date mod sdev intv vec var quote
2649 special-const calcFunc-if calcFunc-lambda
2650 calcFunc-quote calcFunc-condition
2652 (integerp (car math-normalize-a
))
2653 (and (consp (car math-normalize-a
))
2654 (not (eq (car (car math-normalize-a
)) 'lambda
))))
2656 (math-normalize-fancy math-normalize-a
))
2658 (or (and calc-simplify-mode
2660 (math-normalize-nonstandard))
2661 (let ((args (mapcar 'math-normalize
(cdr math-normalize-a
))))
2662 (or (condition-case err
2664 (assq (car math-normalize-a
) '( ( + . math-add
)
2671 ( | . math-concat
) ))))
2672 (or (and var-EvalRules
2674 (or (eq var-EvalRules math-eval-rules-cache-tag
)
2677 (math-recompile-eval-rules)))
2678 (and (or math-eval-rules-cache-other
2679 (assq (car math-normalize-a
)
2680 math-eval-rules-cache
))
2681 (math-apply-rewrites
2682 (cons (car math-normalize-a
) args
)
2683 (cdr math-eval-rules-cache
)
2684 nil math-eval-rules-cache
))))
2686 (apply (cdr func
) args
)
2687 (and (or (consp (car math-normalize-a
))
2688 (fboundp (car math-normalize-a
))
2689 (and (not (featurep 'calc-ext
))
2691 (fboundp (car math-normalize-a
))))
2692 (apply (car math-normalize-a
) args
)))))
2693 (wrong-number-of-arguments
2694 (setq math-normalize-error t
)
2695 (calc-record-why "*Wrong number of arguments"
2696 (cons (car math-normalize-a
) args
))
2698 (wrong-type-argument
2700 (calc-record-why "Wrong type of argument"
2701 (cons (car math-normalize-a
) args
)))
2704 (setq math-normalize-error t
)
2705 (calc-record-why "*Argument out of range"
2706 (cons (car math-normalize-a
) args
))
2709 (calc-record-why "No exact representation for result"
2710 (cons (car math-normalize-a
) args
))
2713 (setq math-normalize-error t
)
2714 (calc-record-why "*Floating-point overflow occurred"
2715 (cons (car math-normalize-a
) args
))
2718 (setq math-normalize-error t
)
2719 (calc-record-why "*Floating-point underflow occurred"
2720 (cons (car math-normalize-a
) args
))
2723 (setq math-normalize-error t
)
2724 (if (eq (nth 1 err
) 'var-EvalRules
)
2726 (setq var-EvalRules nil
)
2727 (math-normalize (cons (car math-normalize-a
) args
)))
2728 (calc-record-why "*Variable is void" (nth 1 err
)))))
2729 (if (consp (car math-normalize-a
))
2730 (math-dimension-error)
2731 (cons (car math-normalize-a
) args
))))))))
2735 ;; True if A is a floating-point real or complex number. [P x] [Public]
2736 (defun math-floatp (a)
2737 (cond ((eq (car-safe a
) 'float
) t
)
2738 ((memq (car-safe a
) '(cplx polar mod sdev intv
))
2739 (or (math-floatp (nth 1 a
))
2740 (math-floatp (nth 2 a
))
2741 (and (eq (car a
) 'intv
) (math-floatp (nth 3 a
)))))
2742 ((eq (car-safe a
) 'date
)
2743 (math-floatp (nth 1 a
)))))
2747 ;; Verify that A is a complete object and return A. [x x] [Public]
2748 (defun math-check-complete (a)
2749 (cond ((integerp a
) a
)
2750 ((eq (car-safe a
) 'incomplete
)
2751 (calc-incomplete-error a
))
2753 (t (error "Invalid data object encountered"))))
2757 ;; Coerce integer A to be a bignum. [B S]
2758 (defun math-bignum (a)
2760 (cons 'bigpos
(math-bignum-big a
))
2761 (cons 'bigneg
(math-bignum-big (- a
)))))
2763 (defun math-bignum-big (a) ; [L s]
2766 (cons (% a math-bignum-digit-size
)
2767 (math-bignum-big (/ a math-bignum-digit-size
)))))
2770 ;; Build a normalized floating-point number. [F I S]
2771 (defun math-make-float (mant exp
)
2774 (let* ((ldiff (- calc-internal-prec
(math-numdigs mant
))))
2776 (setq mant
(math-scale-rounding mant ldiff
)
2777 exp
(- exp ldiff
))))
2779 (let ((digs (cdr mant
)))
2780 (if (= (%
(car digs
) 10) 0)
2782 (while (= (car digs
) 0)
2783 (setq digs
(cdr digs
)
2784 exp
(+ exp math-bignum-digit-length
)))
2785 (while (= (%
(car digs
) 10) 0)
2786 (setq digs
(math-div10-bignum digs
)
2788 (setq mant
(math-normalize (cons (car mant
) digs
))))))
2789 (while (= (% mant
10) 0)
2790 (setq mant
(/ mant
10)
2792 (if (and (<= exp -
4000000)
2793 (<= (+ exp
(math-numdigs mant
) -
1) -
4000000))
2794 (signal 'math-underflow nil
)
2795 (if (and (>= exp
3000000)
2796 (>= (+ exp
(math-numdigs mant
) -
1) 4000000))
2797 (signal 'math-overflow nil
)
2798 (list 'float mant exp
)))))
2800 (defun math-div10-bignum (a) ; [l l]
2802 (cons (+ (/ (car a
) 10) (* (%
(nth 1 a
) 10)
2803 (expt 10 (1- math-bignum-digit-length
))))
2804 (math-div10-bignum (cdr a
)))
2805 (list (/ (car a
) 10))))
2807 ;;; Coerce A to be a float. [F N; V V] [Public]
2808 (defun math-float (a)
2809 (cond ((Math-integerp a
) (math-make-float a
0))
2810 ((eq (car a
) 'frac
) (math-div (math-float (nth 1 a
)) (nth 2 a
)))
2811 ((eq (car a
) 'float
) a
)
2812 ((memq (car a
) '(cplx polar vec hms date sdev mod
))
2813 (cons (car a
) (mapcar 'math-float
(cdr a
))))
2814 (t (math-float-fancy a
))))
2818 (cond ((not (consp a
)) (- a
))
2819 ((eq (car a
) 'bigpos
) (cons 'bigneg
(cdr a
)))
2820 ((eq (car a
) 'bigneg
) (cons 'bigpos
(cdr a
)))
2821 ((memq (car a
) '(frac float
))
2822 (list (car a
) (Math-integer-neg (nth 1 a
)) (nth 2 a
)))
2823 ((memq (car a
) '(cplx vec hms date calcFunc-idn
))
2824 (cons (car a
) (mapcar 'math-neg
(cdr a
))))
2825 (t (math-neg-fancy a
))))
2828 ;;; Compute the number of decimal digits in integer A. [S I]
2829 (defun math-numdigs (a)
2832 (let* ((len (1- (length a
)))
2834 (+ (* (1- len
) math-bignum-digit-length
) (math-numdigs top
)))
2836 (cond ((>= a
100) (+ (math-numdigs (/ a
1000)) 3))
2842 (t (math-numdigs (- a
))))))
2844 ;;; Multiply (with truncation toward 0) the integer A by 10^N. [I i S]
2845 (defun math-scale-int (a n
)
2847 ((> n
0) (math-scale-left a n
))
2848 (t (math-normalize (math-scale-right a
(- n
))))))
2850 (defun math-scale-left (a n
) ; [I I S]
2854 (cons (car a
) (math-scale-left-bignum (cdr a
) n
))
2855 (if (>= n math-bignum-digit-length
)
2856 (if (or (>= a math-bignum-digit-size
)
2857 (<= a
(- math-bignum-digit-size
)))
2858 (math-scale-left (math-bignum a
) n
)
2859 (math-scale-left (* a math-bignum-digit-size
)
2860 (- n math-bignum-digit-length
)))
2861 (let ((sz (expt 10 (- (* 2 math-bignum-digit-length
) n
))))
2862 (if (or (>= a sz
) (<= a
(- sz
)))
2863 (math-scale-left (math-bignum a
) n
)
2864 (* a
(expt 10 n
))))))))
2866 (defun math-scale-left-bignum (a n
)
2867 (if (>= n math-bignum-digit-length
)
2868 (while (>= (setq a
(cons 0 a
)
2869 n
(- n math-bignum-digit-length
))
2870 math-bignum-digit-length
)))
2872 (math-mul-bignum-digit a
(expt 10 n
) 0)
2875 (defun math-scale-right (a n
) ; [i i S]
2879 (cons (car a
) (math-scale-right-bignum (cdr a
) n
))
2883 (- (math-scale-right (- a
) n
)))
2884 (if (>= n math-bignum-digit-length
)
2885 (while (and (> (setq a
(/ a math-bignum-digit-size
)) 0)
2886 (>= (setq n
(- n math-bignum-digit-length
))
2887 math-bignum-digit-length
))))
2892 (defun math-scale-right-bignum (a n
) ; [L L S; l l S]
2893 (if (>= n math-bignum-digit-length
)
2894 (setq a
(nthcdr (/ n math-bignum-digit-length
) a
)
2895 n
(% n math-bignum-digit-length
)))
2897 (cdr (math-mul-bignum-digit a
(expt 10 (- math-bignum-digit-length n
)) 0))
2900 ;;; Multiply (with rounding) the integer A by 10^N. [I i S]
2901 (defun math-scale-rounding (a n
)
2903 (math-scale-left a n
))
2907 (let ((val (if (< n
(- math-bignum-digit-length
))
2908 (math-scale-right-bignum
2910 (- (- math-bignum-digit-length
) n
))
2912 (math-mul-bignum-digit
2914 (expt 10 (+ math-bignum-digit-length n
)) 0)
2915 (cdr a
))))) ; n = -math-bignum-digit-length
2916 (if (and val
(>= (car val
) (/ math-bignum-digit-size
2)))
2918 (if (eq (car (cdr val
)) (1- math-bignum-digit-size
))
2919 (math-add-bignum (cdr val
) '(1))
2920 (cons (1+ (car (cdr val
))) (cdr (cdr val
))))
2925 (- (math-scale-rounding (- a
) n
))
2928 (/ (+ (math-scale-right a
(- -
1 n
)) 5) 10))))))
2931 ;;; Compute the sum of A and B. [O O O] [Public]
2932 (defun math-add (a b
)
2934 (and (not (or (consp a
) (consp b
)))
2937 (if (or (<= a
(- math-small-integer-size
)) (>= a math-small-integer-size
))
2940 (and (Math-zerop a
) (not (eq (car-safe a
) 'mod
))
2941 (if (and (math-floatp a
) (Math-ratp b
)) (math-float b
) b
))
2942 (and (Math-zerop b
) (not (eq (car-safe b
) 'mod
))
2943 (if (and (math-floatp b
) (Math-ratp a
)) (math-float a
) a
))
2944 (and (Math-objvecp a
) (Math-objvecp b
)
2946 (and (Math-integerp a
) (Math-integerp b
)
2948 (or (consp a
) (setq a
(math-bignum a
)))
2949 (or (consp b
) (setq b
(math-bignum b
)))
2950 (if (eq (car a
) 'bigneg
)
2951 (if (eq (car b
) 'bigneg
)
2952 (cons 'bigneg
(math-add-bignum (cdr a
) (cdr b
)))
2954 (let ((diff (math-sub-bignum (cdr b
) (cdr a
))))
2956 (cons 'bigneg
(math-sub-bignum (cdr a
) (cdr b
)))
2957 (cons 'bigpos diff
)))))
2958 (if (eq (car b
) 'bigneg
)
2960 (let ((diff (math-sub-bignum (cdr a
) (cdr b
))))
2962 (cons 'bigneg
(math-sub-bignum (cdr b
) (cdr a
)))
2963 (cons 'bigpos diff
))))
2964 (cons 'bigpos
(math-add-bignum (cdr a
) (cdr b
)))))))
2965 (and (Math-ratp a
) (Math-ratp b
)
2967 (calc-add-fractions a b
))
2968 (and (Math-realp a
) (Math-realp b
)
2970 (or (and (consp a
) (eq (car a
) 'float
))
2971 (setq a
(math-float a
)))
2972 (or (and (consp b
) (eq (car b
) 'float
))
2973 (setq b
(math-float b
)))
2974 (math-add-float a b
)))
2975 (and (require 'calc-ext
)
2976 (math-add-objects-fancy a b
))))
2977 (and (require 'calc-ext
)
2978 (math-add-symb-fancy a b
))))
2980 (defun math-add-bignum (a b
) ; [L L L; l l l]
2983 (let* ((a (copy-sequence a
)) (aa a
) (carry nil
) sum
)
2986 (if (< (setq sum
(+ (car aa
) (car b
)))
2987 (1- math-bignum-digit-size
))
2989 (setcar aa
(1+ sum
))
2991 (setcar aa
(- sum
(1- math-bignum-digit-size
))))
2992 (if (< (setq sum
(+ (car aa
) (car b
))) math-bignum-digit-size
)
2994 (setcar aa
(- sum math-bignum-digit-size
))
3000 (nconc a
(math-add-bignum b
'(1)))
3001 (while (eq (car aa
) (1- math-bignum-digit-size
))
3006 (setcar aa
(1+ (car aa
)))
3015 (defun math-sub-bignum (a b
) ; [l l l]
3018 (let* ((a (copy-sequence a
)) (aa a
) (borrow nil
) sum diff
)
3021 (if (>= (setq diff
(- (car aa
) (car b
))) 1)
3023 (setcar aa
(1- diff
))
3025 (setcar aa
(+ diff
(1- math-bignum-digit-size
))))
3026 (if (>= (setq diff
(- (car aa
) (car b
))) 0)
3028 (setcar aa
(+ diff math-bignum-digit-size
))
3034 (while (eq (car aa
) 0)
3035 (setcar aa
(1- math-bignum-digit-size
))
3039 (setcar aa
(1- (car aa
)))
3042 (while (eq (car b
) 0)
3047 (while (eq (car b
) 0)
3053 (defun math-add-float (a b
) ; [F F F]
3054 (let ((ediff (- (nth 2 a
) (nth 2 b
))))
3056 (if (>= ediff
(+ calc-internal-prec calc-internal-prec
))
3058 (math-make-float (math-add (nth 1 b
)
3061 (math-scale-left (nth 1 a
) ediff
)))
3063 (if (>= (setq ediff
(- ediff
))
3064 (+ calc-internal-prec calc-internal-prec
))
3066 (math-make-float (math-add (nth 1 a
)
3067 (math-scale-left (nth 1 b
) ediff
))
3070 ;;; Compute the difference of A and B. [O O O] [Public]
3071 (defun math-sub (a b
)
3072 (if (or (consp a
) (consp b
))
3073 (math-add a
(math-neg b
))
3075 (if (or (<= a
(- math-small-integer-size
)) (>= a math-small-integer-size
))
3079 (defun math-sub-float (a b
) ; [F F F]
3080 (let ((ediff (- (nth 2 a
) (nth 2 b
))))
3082 (if (>= ediff
(+ calc-internal-prec calc-internal-prec
))
3084 (math-make-float (math-add (Math-integer-neg (nth 1 b
))
3087 (math-scale-left (nth 1 a
) ediff
)))
3089 (if (>= (setq ediff
(- ediff
))
3090 (+ calc-internal-prec calc-internal-prec
))
3092 (math-make-float (math-add (nth 1 a
)
3094 (math-scale-left (nth 1 b
) ediff
)))
3098 ;;; Compute the product of A and B. [O O O] [Public]
3099 (defun math-mul (a b
)
3101 (and (not (consp a
)) (not (consp b
))
3102 (< a math-bignum-digit-size
) (> a
(- math-bignum-digit-size
))
3103 (< b math-bignum-digit-size
) (> b
(- math-bignum-digit-size
))
3105 (and (Math-zerop a
) (not (eq (car-safe b
) 'mod
))
3106 (if (Math-scalarp b
)
3107 (if (and (math-floatp b
) (Math-ratp a
)) (math-float a
) a
)
3109 (math-mul-zero a b
)))
3110 (and (Math-zerop b
) (not (eq (car-safe a
) 'mod
))
3111 (if (Math-scalarp a
)
3112 (if (and (math-floatp a
) (Math-ratp b
)) (math-float b
) b
)
3114 (math-mul-zero b a
)))
3115 (and (Math-objvecp a
) (Math-objvecp b
)
3117 (and (Math-integerp a
) (Math-integerp b
)
3119 (or (consp a
) (setq a
(math-bignum a
)))
3120 (or (consp b
) (setq b
(math-bignum b
)))
3122 (cons (if (eq (car a
) (car b
)) 'bigpos
'bigneg
)
3125 (math-mul-bignum (cdr a
) (cdr b
))
3126 (math-mul-bignum-digit (cdr a
) (nth 1 b
) 0))
3127 (math-mul-bignum-digit (cdr b
) (nth 1 a
) 0))))))
3128 (and (Math-ratp a
) (Math-ratp b
)
3130 (calc-mul-fractions a b
))
3131 (and (Math-realp a
) (Math-realp b
)
3133 (or (and (consp a
) (eq (car a
) 'float
))
3134 (setq a
(math-float a
)))
3135 (or (and (consp b
) (eq (car b
) 'float
))
3136 (setq b
(math-float b
)))
3137 (math-make-float (math-mul (nth 1 a
) (nth 1 b
))
3138 (+ (nth 2 a
) (nth 2 b
)))))
3139 (and (require 'calc-ext
)
3140 (math-mul-objects-fancy a b
))))
3141 (and (require 'calc-ext
)
3142 (math-mul-symb-fancy a b
))))
3144 (defun math-infinitep (a &optional undir
)
3145 (while (and (consp a
) (memq (car a
) '(* / neg
)))
3146 (if (or (not (eq (car a
) '*)) (math-infinitep (nth 1 a
)))
3148 (setq a
(nth 2 a
))))
3151 (memq (nth 2 a
) '(var-inf var-uinf var-nan
))
3152 (if (and undir
(eq (nth 2 a
) 'var-inf
))
3153 '(var uinf var-uinf
)
3156 ;;; Multiply digit lists A and B. [L L L; l l l]
3157 (defun math-mul-bignum (a b
)
3159 (let* ((sum (if (<= (car b
) 1)
3163 (math-mul-bignum-digit a
(car b
) 0)))
3164 (sump sum
) c d aa ss prod
)
3165 (while (setq b
(cdr b
))
3166 (setq ss
(setq sump
(or (cdr sump
) (setcdr sump
(list 0))))
3171 (setcar ss
(%
(setq prod
(+ (+ (car ss
) (* (car aa
) d
))
3172 c
)) math-bignum-digit-size
))
3174 (setq c
(/ prod math-bignum-digit-size
)
3175 ss
(or (cdr ss
) (setcdr ss
(list 0)))))
3176 (if (>= prod math-bignum-digit-size
)
3178 (setcar (cdr ss
) (+ (/ prod math-bignum-digit-size
) (car (cdr ss
))))
3179 (setcdr ss
(list (/ prod math-bignum-digit-size
))))))
3182 ;;; Multiply digit list A by digit D. [L L D D; l l D D]
3183 (defun math-mul-bignum-digit (a d c
)
3187 (let* ((a (copy-sequence a
)) (aa a
) prod
)
3190 (%
(setq prod
(+ (* (car aa
) d
) c
))
3191 math-bignum-digit-size
))
3194 c
(/ prod math-bignum-digit-size
)))
3195 (if (>= prod math-bignum-digit-size
)
3196 (setcdr aa
(list (/ prod math-bignum-digit-size
))))
3202 ;;; Compute the integer (quotient . remainder) of A and B, which may be
3203 ;;; small or big integers. Type and consistency of truncation is undefined
3204 ;;; if A or B is negative. B must be nonzero. [I.I I I] [Public]
3205 (defun math-idivmod (a b
)
3207 (math-reject-arg a
"*Division by zero"))
3208 (if (or (consp a
) (consp b
))
3209 (if (and (natnump b
) (< b math-bignum-digit-size
))
3210 (let ((res (math-div-bignum-digit (cdr a
) b
)))
3212 (math-normalize (cons (car a
) (car res
)))
3214 (or (consp a
) (setq a
(math-bignum a
)))
3215 (or (consp b
) (setq b
(math-bignum b
)))
3216 (let ((res (math-div-bignum (cdr a
) (cdr b
))))
3218 (math-normalize (cons (if (eq (car a
) (car b
)) 'bigpos
'bigneg
)
3220 (math-normalize (cons (car a
) (cdr res
))))))
3221 (cons (/ a b
) (% a b
))))
3223 (defun math-quotient (a b
) ; [I I I] [Public]
3224 (if (and (not (consp a
)) (not (consp b
)))
3226 (math-reject-arg a
"*Division by zero")
3228 (if (and (natnump b
) (< b math-bignum-digit-size
))
3230 (math-reject-arg a
"*Division by zero")
3231 (math-normalize (cons (car a
)
3232 (car (math-div-bignum-digit (cdr a
) b
)))))
3233 (or (consp a
) (setq a
(math-bignum a
)))
3234 (or (consp b
) (setq b
(math-bignum b
)))
3235 (let* ((alen (1- (length a
)))
3236 (blen (1- (length b
)))
3237 (d (/ math-bignum-digit-size
(1+ (nth (1- blen
) (cdr b
)))))
3238 (res (math-div-bignum-big (math-mul-bignum-digit (cdr a
) d
0)
3239 (math-mul-bignum-digit (cdr b
) d
0)
3241 (math-normalize (cons (if (eq (car a
) (car b
)) 'bigpos
'bigneg
)
3245 ;;; Divide a bignum digit list by another. [l.l l L]
3246 ;;; The following division algorithm is borrowed from Knuth vol. II, sec. 4.3.1
3247 (defun math-div-bignum (a b
)
3249 (let* ((alen (length a
))
3251 (d (/ math-bignum-digit-size
(1+ (nth (1- blen
) b
))))
3252 (res (math-div-bignum-big (math-mul-bignum-digit a d
0)
3253 (math-mul-bignum-digit b d
0)
3258 (car (math-div-bignum-digit (cdr res
) d
)))))
3259 (let ((res (math-div-bignum-digit a
(car b
))))
3260 (cons (car res
) (list (cdr res
))))))
3262 ;;; Divide a bignum digit list by a digit. [l.D l D]
3263 (defun math-div-bignum-digit (a b
)
3265 (let* ((res (math-div-bignum-digit (cdr a
) b
))
3266 (num (+ (* (cdr res
) math-bignum-digit-size
) (car a
))))
3268 (cons (/ num b
) (car res
))
3272 (defun math-div-bignum-big (a b alen blen
) ; [l.l l L]
3275 (let* ((res (math-div-bignum-big (cdr a
) b
(1- alen
) blen
))
3276 (num (cons (car a
) (cdr res
)))
3277 (res2 (math-div-bignum-part num b blen
)))
3279 (cons (car res2
) (car res
))
3282 (defun math-div-bignum-part (a b blen
) ; a < b*math-bignum-digit-size [D.l l L]
3283 (let* ((num (+ (* (or (nth blen a
) 0) math-bignum-digit-size
)
3284 (or (nth (1- blen
) a
) 0)))
3285 (den (nth (1- blen
) b
))
3286 (guess (min (/ num den
) (1- math-bignum-digit-size
))))
3287 (math-div-bignum-try a b
(math-mul-bignum-digit b guess
0) guess
)))
3289 (defun math-div-bignum-try (a b c guess
) ; [D.l l l D]
3290 (let ((rem (math-sub-bignum a c
)))
3292 (math-div-bignum-try a b
(math-sub-bignum c b
) (1- guess
))
3296 ;;; Compute the quotient of A and B. [O O N] [Public]
3297 (defun math-div (a b
)
3301 (math-div-by-zero a b
))
3302 (and (Math-zerop a
) (not (eq (car-safe b
) 'mod
))
3303 (if (Math-scalarp b
)
3304 (if (and (math-floatp b
) (Math-ratp a
)) (math-float a
) a
)
3306 (math-div-zero a b
)))
3307 (and (Math-objvecp a
) (Math-objvecp b
)
3309 (and (Math-integerp a
) (Math-integerp b
)
3310 (let ((q (math-idivmod a b
)))
3313 (if calc-prefer-frac
3316 (math-make-frac a b
))
3317 (math-div-float (math-make-float a
0)
3318 (math-make-float b
0))))))
3319 (and (Math-ratp a
) (Math-ratp b
)
3321 (calc-div-fractions a b
))
3322 (and (Math-realp a
) (Math-realp b
)
3324 (or (and (consp a
) (eq (car a
) 'float
))
3325 (setq a
(math-float a
)))
3326 (or (and (consp b
) (eq (car b
) 'float
))
3327 (setq b
(math-float b
)))
3328 (math-div-float a b
)))
3329 (and (require 'calc-ext
)
3330 (math-div-objects-fancy a b
))))
3331 (and (require 'calc-ext
)
3332 (math-div-symb-fancy a b
))))
3334 (defun math-div-float (a b
) ; [F F F]
3335 (let ((ldiff (max (- (1+ calc-internal-prec
)
3336 (- (math-numdigs (nth 1 a
)) (math-numdigs (nth 1 b
))))
3338 (math-make-float (math-quotient (math-scale-int (nth 1 a
) ldiff
) (nth 1 b
))
3339 (- (- (nth 2 a
) (nth 2 b
)) ldiff
))))
3344 (defvar calc-selection-cache-entry
)
3345 ;;; Format the number A as a string. [X N; X Z] [Public]
3346 (defun math-format-stack-value (entry)
3347 (setq calc-selection-cache-entry calc-selection-cache-default-entry
)
3348 (let* ((a (car entry
))
3349 (math-comp-selected (nth 2 entry
))
3350 (c (cond ((null a
) "<nil>")
3351 ((eq calc-display-raw t
) (format "%s" a
))
3353 ((eq a
'top-of-stack
) (propertize "." 'font-lock-face
'bold
))
3354 (calc-prepared-composition
3355 calc-prepared-composition
)
3356 ((and (Math-scalarp a
)
3357 (memq calc-language
'(nil flat unform
))
3358 (null math-comp-selected
))
3359 (math-format-number a
))
3360 (t (require 'calc-ext
)
3361 (math-compose-expr a
0))))
3362 (off (math-stack-value-offset c
))
3364 (and math-comp-selected
(setq calc-any-selections t
))
3368 (setq c
(math-comp-concat (make-string off ?\s
) c
)))
3369 (or (equal calc-left-label
"")
3370 (setq c
(math-comp-concat (if (eq a
'top-of-stack
)
3371 (make-string (length calc-left-label
) ?\s
)
3374 (when calc-line-numbering
3375 (setq c
(math-comp-concat (if (eq calc-language
'big
)
3376 (if math-comp-selected
3381 (unless (or (equal calc-right-label
"")
3382 (eq a
'top-of-stack
))
3384 (setq c
(list 'horiz c
3385 (make-string (max (- w
(math-comp-width c
)
3386 (length calc-right-label
)) 0) ?\s
)
3389 (setq s
(if (stringp c
)
3390 (if calc-display-raw
3393 (math-composition-to-string c w
)))
3394 (when calc-language-output-filter
3395 (setq s
(funcall calc-language-output-filter s
)))
3396 (if (eq calc-language
'big
)
3397 (setq s
(concat s
"\n"))
3398 (when calc-line-numbering
3399 (setq s
(concat "1:" (substring s
2)))))
3400 (setcar (cdr entry
) (calc-count-lines s
))
3403 ;; The variables math-svo-c, math-svo-wid and math-svo-off are local
3404 ;; to math-stack-value-offset, but are used by math-stack-value-offset-fancy
3407 (defun math-stack-value-offset (math-svo-c)
3408 (let* ((num (if calc-line-numbering
4 0))
3409 (math-svo-wid (calc-window-width))
3411 (if calc-display-just
3414 (math-stack-value-offset-fancy))
3415 (setq math-svo-off
(or calc-display-origin
0))
3416 (when (integerp calc-line-breaking
)
3417 (setq math-svo-wid calc-line-breaking
)))
3418 (cons (max (- math-svo-off
(length calc-left-label
)) 0)
3419 (+ math-svo-wid num
))))
3421 (defun calc-count-lines (s)
3424 (while (setq pos
(string-match "\n" s pos
))
3429 (defun math-format-value (a &optional w
)
3430 (if (and (Math-scalarp a
)
3431 (memq calc-language
'(nil flat unform
)))
3432 (math-format-number a
)
3434 (let ((calc-line-breaking nil
))
3435 (math-composition-to-string (math-compose-expr a
0) w
))))
3437 (defun calc-window-width ()
3438 (if calc-embedded-info
3439 (let ((win (get-buffer-window (aref calc-embedded-info
0))))
3440 (1- (if win
(window-width win
) (frame-width))))
3441 (- (window-width (get-buffer-window (current-buffer)))
3442 (if calc-line-numbering
5 1))))
3444 (defun math-comp-concat (c1 c2
)
3445 (if (and (stringp c1
) (stringp c2
))
3447 (list 'horiz c1 c2
)))
3451 ;;; Format an expression as a one-line string suitable for re-reading.
3453 (defun math-format-flat-expr (a prec
)
3455 ((or (not (or (consp a
) (integerp a
)))
3456 (eq calc-display-raw t
))
3457 (let ((print-escape-newlines t
))
3458 (concat "'" (prin1-to-string a
))))
3460 (let ((calc-group-digits nil
)
3461 (calc-point-char ".")
3462 (calc-frac-format (if (> (length (car calc-frac-format
)) 1)
3463 '("::" nil
) '(":" nil
)))
3464 (calc-complex-format nil
)
3465 (calc-hms-format "%s@ %s' %s\"")
3466 (calc-language nil
))
3467 (math-format-number a
)))
3470 (math-format-flat-expr-fancy a prec
))))
3474 ;;; Format a number as a string.
3475 (defvar math-half-2-word-size
)
3476 (defun math-format-number (a &optional prec
) ; [X N] [Public]
3478 ((eq calc-display-raw t
) (format "%s" a
))
3479 ((and calc-twos-complement-mode
3480 math-radix-explicit-format
3483 (and (Math-integer-posp a
)
3484 (Math-lessp a math-half-2-word-size
))
3485 (and (Math-integer-negp a
)
3488 (math-compare (Math-integer-neg a
) math-half-2-word-size
)))
3489 (or (= comparison
0)
3490 (= comparison -
1))))))
3492 (math-format-twos-complement a
))
3493 ((and (nth 1 calc-frac-format
) (Math-integerp a
))
3495 (math-format-number (math-adjust-fraction a
)))
3497 (if (not (or calc-group-digits calc-leading-zeros
))
3498 (if (= calc-number-radix
10)
3501 (concat "-" (math-format-number (- a
)))
3503 (if math-radix-explicit-format
3504 (if calc-radix-formatter
3505 (funcall calc-radix-formatter
3507 (if (= calc-number-radix
2)
3508 (math-format-binary a
)
3509 (math-format-radix a
)))
3510 (format "%d#%s" calc-number-radix
3511 (if (= calc-number-radix
2)
3512 (math-format-binary a
)
3513 (math-format-radix a
))))
3514 (math-format-radix a
))))
3515 (math-format-number (math-bignum a
))))
3517 ((not (consp a
)) (prin1-to-string a
))
3518 ((eq (car a
) 'bigpos
) (math-format-bignum (cdr a
)))
3519 ((eq (car a
) 'bigneg
) (concat "-" (math-format-bignum (cdr a
))))
3520 ((and (eq (car a
) 'float
) (= calc-number-radix
10))
3521 (if (Math-integer-negp (nth 1 a
))
3522 (concat "-" (math-format-number (math-neg a
)))
3523 (let ((mant (nth 1 a
))
3525 (fmt (car calc-float-format
))
3526 (figs (nth 1 calc-float-format
))
3527 (point calc-point-char
)
3529 (if (and (eq fmt
'fix
)
3530 (or (and (< figs
0) (setq figs
(- figs
)))
3531 (> (+ exp
(math-numdigs mant
)) (- figs
))))
3533 (setq mant
(math-scale-rounding mant
(+ exp figs
))
3534 str
(if (integerp mant
)
3535 (int-to-string mant
)
3536 (math-format-bignum-decimal (cdr mant
))))
3537 (if (<= (length str
) figs
)
3538 (setq str
(concat (make-string (1+ (- figs
(length str
))) ?
0)
3541 (setq str
(concat (substring str
0 (- figs
)) point
3542 (substring str
(- figs
))))
3543 (setq str
(concat str point
)))
3544 (when calc-group-digits
3546 (setq str
(math-group-float str
))))
3548 (setq figs
(+ calc-internal-prec figs
)))
3550 (let ((adj (- figs
(math-numdigs mant
))))
3552 (setq mant
(math-scale-rounding mant adj
)
3554 (setq str
(if (integerp mant
)
3555 (int-to-string mant
)
3556 (math-format-bignum-decimal (cdr mant
))))
3557 (let* ((len (length str
))
3559 (if (and (eq fmt
'float
)
3560 (<= dpos
(+ calc-internal-prec calc-display-sci-high
))
3561 (>= dpos
(+ calc-display-sci-low
2)))
3565 (setq str
(concat "0" point str
)))
3566 ((and (<= exp
0) (> dpos
0))
3567 (setq str
(concat (substring str
0 dpos
) point
3568 (substring str dpos
))))
3570 (setq str
(concat str
(make-string exp ?
0) point
)))
3572 (setq str
(concat "0" point
3573 (make-string (- dpos
) ?
0) str
))))
3574 (when calc-group-digits
3576 (setq str
(math-group-float str
))))
3577 (let* ((eadj (+ exp len
))
3578 (scale (if (eq fmt
'eng
)
3579 (1+ (math-mod (+ eadj
300002) 3))
3581 (if (> scale
(length str
))
3582 (setq str
(concat str
(make-string (- scale
(length str
))
3584 (if (< scale
(length str
))
3585 (setq str
(concat (substring str
0 scale
) point
3586 (substring str scale
))))
3587 (when calc-group-digits
3589 (setq str
(math-group-float str
)))
3590 (setq str
(format (if (memq calc-language
'(math maple
))
3591 (if (and prec
(> prec
191))
3592 "(%s*10.^%d)" "%s*10.^%d")
3594 str
(- eadj scale
)))))))
3598 (math-format-number-fancy a prec
))))
3600 (defun math-format-bignum (a) ; [X L]
3601 (if (and (= calc-number-radix
10)
3602 (not calc-leading-zeros
)
3603 (not calc-group-digits
))
3604 (math-format-bignum-decimal a
)
3606 (math-format-bignum-fancy a
)))
3608 (defun math-format-bignum-decimal (a) ; [X L]
3611 (while (cdr (cdr a
))
3615 (number-to-string (* 2 math-bignum-digit-length
))
3617 (+ (* (nth 1 a
) math-bignum-digit-size
) (car a
))) s
)
3619 (concat (int-to-string
3620 (+ (* (or (nth 1 a
) 0) math-bignum-digit-size
) (car a
))) s
))
3625 ;;; Parse a simple number in string form. [N X] [Public]
3626 (defun math-read-number (s &optional decimal
)
3627 "Convert the string S into a Calc number."
3632 ;; Integers (most common case)
3633 ((string-match "\\` *\\([0-9]+\\) *\\'" s
)
3634 (let ((digs (math-match-substring s
1)))
3635 (if (and (memq calc-language calc-lang-c-type-hex
)
3637 (eq (aref digs
0) ?
0)
3639 (math-read-number (concat "8#" digs
))
3640 (if (<= (length digs
) (* 2 math-bignum-digit-length
))
3641 (string-to-number digs
)
3642 (cons 'bigpos
(math-read-bignum digs
))))))
3644 ;; Clean up the string if necessary
3645 ((string-match "\\`\\(.*\\)[ \t\n]+\\([^\001]*\\)\\'" s
)
3646 (math-read-number (concat (math-match-substring s
1)
3647 (math-match-substring s
2))))
3649 ;; Plus and minus signs
3650 ((string-match "^[-_+]\\(.*\\)$" s
)
3651 (let ((val (math-read-number (math-match-substring s
1))))
3652 (and val
(if (eq (aref s
0) ?
+) val
(math-neg val
)))))
3654 ;; Forms that require extensions module
3655 ((string-match "[^-+0-9eE.]" s
)
3657 (math-read-number-fancy s
))
3660 ((string-match "^\\([0-9]*\\)\\.\\([0-9]*\\)$" s
)
3661 (let ((int (math-match-substring s
1))
3662 (frac (math-match-substring s
2)))
3663 (let ((ilen (length int
))
3664 (flen (length frac
)))
3665 (let ((int (if (> ilen
0) (math-read-number int t
) 0))
3666 (frac (if (> flen
0) (math-read-number frac t
) 0)))
3667 (and int frac
(or (> ilen
0) (> flen
0))
3669 (math-add (math-scale-int int flen
) frac
)
3673 ((string-match "^\\(.*\\)[eE]\\([-+]?[0-9]+\\)$" s
)
3674 (let ((mant (math-match-substring s
1))
3675 (exp (math-match-substring s
2)))
3676 (let ((mant (if (> (length mant
) 0) (math-read-number mant t
) 1))
3677 (exp (if (<= (length exp
) (if (memq (aref exp
0) '(?
+ ?-
)) 8 7))
3678 (string-to-number exp
))))
3679 (and mant exp
(Math-realp mant
) (> exp -
4000000) (< exp
4000000)
3680 (let ((mant (math-float mant
)))
3681 (list 'float
(nth 1 mant
) (+ (nth 2 mant
) exp
)))))))
3686 ;;; Parse a very simple number, keeping all digits.
3687 (defun math-read-number-simple (s)
3688 "Convert the string S into a Calc number.
3689 S is assumed to be a simple number (integer or float without an exponent)
3690 and all digits are kept, regardless of Calc's current precision."
3694 ((string-match "^[0-9]+$" s
)
3695 (if (string-match "^\\(0+\\)" s
)
3696 (setq s
(substring s
(match-end 0))))
3697 (if (<= (length s
) (* 2 math-bignum-digit-length
))
3698 (string-to-number s
)
3699 (cons 'bigpos
(math-read-bignum s
))))
3701 ((string-match "^-[0-9]+$" s
)
3702 (if (<= (length s
) (1+ (* 2 math-bignum-digit-length
)))
3703 (string-to-number s
)
3704 (cons 'bigneg
(math-read-bignum (substring s
1)))))
3706 ((string-match "^\\(-?[0-9]*\\)\\.\\([0-9]*\\)$" s
)
3707 (let ((int (math-match-substring s
1))
3708 (frac (math-match-substring s
2)))
3709 (list 'float
(math-read-number-simple (concat int frac
))
3710 (- (length frac
)))))
3714 (defun math-match-substring (s n
)
3715 (if (match-beginning n
)
3716 (substring s
(match-beginning n
) (match-end n
))
3719 (defun math-read-bignum (s) ; [l X]
3720 (if (> (length s
) math-bignum-digit-length
)
3721 (cons (string-to-number (substring s
(- math-bignum-digit-length
)))
3722 (math-read-bignum (substring s
0 (- math-bignum-digit-length
))))
3723 (list (string-to-number s
))))
3725 (defconst math-standard-opers
3726 '( ( "_" calcFunc-subscr
1200 1201 )
3727 ( "%" calcFunc-percent
1100 -
1 )
3728 ( "u!" calcFunc-lnot -
1 1000 )
3729 ( "mod" mod
400 400 185 )
3730 ( "+/-" sdev
300 300 185 )
3731 ( "!!" calcFunc-dfact
210 -
1 )
3732 ( "!" calcFunc-fact
210 -
1 )
3735 ( "u+" ident -
1 197 )
3739 ( "\\" calcFunc-idiv
190 191 )
3743 ( "<" calcFunc-lt
160 161 )
3744 ( ">" calcFunc-gt
160 161 )
3745 ( "<=" calcFunc-leq
160 161 )
3746 ( ">=" calcFunc-geq
160 161 )
3747 ( "=" calcFunc-eq
160 161 )
3748 ( "==" calcFunc-eq
160 161 )
3749 ( "!=" calcFunc-neq
160 161 )
3750 ( "&&" calcFunc-land
110 111 )
3751 ( "||" calcFunc-lor
100 101 )
3752 ( "?" (math-read-if) 91 90 )
3753 ( "!!!" calcFunc-pnot -
1 85 )
3754 ( "&&&" calcFunc-pand
80 81 )
3755 ( "|||" calcFunc-por
75 76 )
3756 ( ":=" calcFunc-assign
51 50 )
3757 ( "::" calcFunc-condition
45 46 )
3758 ( "=>" calcFunc-evalto
40 41 )
3759 ( "=>" calcFunc-evalto
40 -
1 )))
3761 (defun math-standard-ops ()
3762 (if calc-multiplication-has-precedence
3767 math-standard-opers
))
3772 math-standard-opers
))))
3774 (defvar math-expr-opers
(math-standard-ops))
3776 (defun math-standard-ops-p ()
3777 (let ((meo (caar math-expr-opers
)))
3779 (string= meo
"*"))))
3781 (defun math-expr-ops ()
3782 (if (math-standard-ops-p)
3787 (defun calc-grab-region (top bot arg
)
3788 "Parse the region as a vector of numbers and push it on the Calculator stack."
3789 (interactive "r\nP")
3791 (calc-do-grab-region top bot arg
))
3794 (defun calc-grab-rectangle (top bot arg
)
3795 "Parse a rectangle as a matrix of numbers and push it on the Calculator stack."
3796 (interactive "r\nP")
3798 (calc-do-grab-rectangle top bot arg
))
3800 (defun calc-grab-sum-down (top bot arg
)
3801 "Parse a rectangle as a matrix of numbers and sum its columns."
3802 (interactive "r\nP")
3804 (calc-do-grab-rectangle top bot arg
'calcFunc-reduced
))
3806 (defun calc-grab-sum-across (top bot arg
)
3807 "Parse a rectangle as a matrix of numbers and sum its rows."
3808 (interactive "r\nP")
3810 (calc-do-grab-rectangle top bot arg
'calcFunc-reducea
))
3814 (defun calc-embedded (arg &optional end obeg oend
)
3815 "Start Calc Embedded mode on the formula surrounding point."
3818 (calc-do-embedded arg end obeg oend
))
3821 (defun calc-embedded-activate (&optional arg cbuf
)
3822 "Scan the current editing buffer for all embedded := and => formulas.
3823 Also looks for the equivalent TeX words, \\gets and \\evalto."
3825 (calc-do-embedded-activate arg cbuf
))
3827 (defun calc-user-invocation ()
3829 (unless calc-invocation-macro
3830 (error "Use `Z I' inside Calc to define a `C-x * Z' keyboard macro"))
3831 (execute-kbd-macro calc-invocation-macro nil
))
3833 ;;; User-programmability.
3836 (defmacro defmath
(func args
&rest body
) ; [Public]
3837 "Define Calc function.
3839 Like `defun' except that code in the body of the definition can
3840 make use of the full range of Calc data types and the usual
3841 arithmetic operations are converted to their Calc equivalents.
3843 The prefix `calcFunc-' is added to the specified name to get the
3844 actual Lisp function name.
3846 See Info node `(calc)Defining Functions'."
3847 (declare (doc-string 3))
3849 (math-do-defmath func args body
))
3851 ;;; Functions needed for Lucid Emacs support.
3853 (defun calc-read-key (&optional optkey
)
3854 (cond ((featurep 'xemacs
)
3855 (let ((event (next-command-event)))
3856 (let ((key (event-to-character event t t
)))
3857 (or key optkey
(error "Expected a plain keystroke"))
3860 (let ((key (read-event)))
3863 (defun calc-unread-command (&optional input
)
3864 (if (featurep 'xemacs
)
3865 (setq unread-command-event
3866 (if (integerp input
) (character-to-event input
)
3867 (or input last-command-event
)))
3868 (push (or input last-command-event
) unread-command-events
)))
3870 (defun calc-clear-unread-commands ()
3871 (if (featurep 'xemacs
)
3872 (setq unread-command-event nil
)
3873 (setq unread-command-events nil
)))
3875 (defcalcmodevar math-2-word-size
3876 (math-read-number-simple "4294967296")
3877 "Two to the power of `calc-word-size'.")
3879 (defcalcmodevar math-half-2-word-size
3880 (math-read-number-simple "2147483648")
3881 "One-half of two to the power of `calc-word-size'.")
3883 (when calc-always-load-extensions
3885 (calc-load-everything))
3888 (run-hooks 'calc-load-hook
)
3896 ;;; calc.el ends here