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