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