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