Mercurial > emacs
annotate lisp/calc/calc-prog.el @ 58786:bb0ccdf15685
(maintainer-clean): Remove the info files in $(infodir) where they are created.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 06 Dec 2004 00:55:10 +0000 |
parents | 827d00badeb5 |
children | f27d7a9c6f27 f2ebccfa87d4 |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calc-prog.el --- user programmability functions for Calc |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
6 ;; Maintainer: Jay Belanger <belanger@truman.edu> |
40785 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Commentary: |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
26 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
27 ;;; Code: |
40785 | 28 |
29 ;; This file is autoloaded from calc-ext.el. | |
30 | |
58668
827d00badeb5
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58614
diff
changeset
|
31 (require 'calc-ext) |
40785 | 32 (require 'calc-macs) |
33 | |
34 | |
35 (defun calc-equal-to (arg) | |
36 (interactive "P") | |
37 (calc-wrapper | |
38 (if (and (integerp arg) (> arg 2)) | |
39 (calc-enter-result arg "eq" (cons 'calcFunc-eq (calc-top-list-n arg))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
40 (calc-binary-op "eq" 'calcFunc-eq arg)))) |
40785 | 41 |
42 (defun calc-remove-equal (arg) | |
43 (interactive "P") | |
44 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
45 (calc-unary-op "rmeq" 'calcFunc-rmeq arg))) |
40785 | 46 |
47 (defun calc-not-equal-to (arg) | |
48 (interactive "P") | |
49 (calc-wrapper | |
50 (if (and (integerp arg) (> arg 2)) | |
51 (calc-enter-result arg "neq" (cons 'calcFunc-neq (calc-top-list-n arg))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
52 (calc-binary-op "neq" 'calcFunc-neq arg)))) |
40785 | 53 |
54 (defun calc-less-than (arg) | |
55 (interactive "P") | |
56 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
57 (calc-binary-op "lt" 'calcFunc-lt arg))) |
40785 | 58 |
59 (defun calc-greater-than (arg) | |
60 (interactive "P") | |
61 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
62 (calc-binary-op "gt" 'calcFunc-gt arg))) |
40785 | 63 |
64 (defun calc-less-equal (arg) | |
65 (interactive "P") | |
66 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
67 (calc-binary-op "leq" 'calcFunc-leq arg))) |
40785 | 68 |
69 (defun calc-greater-equal (arg) | |
70 (interactive "P") | |
71 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
72 (calc-binary-op "geq" 'calcFunc-geq arg))) |
40785 | 73 |
74 (defun calc-in-set (arg) | |
75 (interactive "P") | |
76 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
77 (calc-binary-op "in" 'calcFunc-in arg))) |
40785 | 78 |
79 (defun calc-logical-and (arg) | |
80 (interactive "P") | |
81 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
82 (calc-binary-op "land" 'calcFunc-land arg 1))) |
40785 | 83 |
84 (defun calc-logical-or (arg) | |
85 (interactive "P") | |
86 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
87 (calc-binary-op "lor" 'calcFunc-lor arg 0))) |
40785 | 88 |
89 (defun calc-logical-not (arg) | |
90 (interactive "P") | |
91 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
92 (calc-unary-op "lnot" 'calcFunc-lnot arg))) |
40785 | 93 |
94 (defun calc-logical-if () | |
95 (interactive) | |
96 (calc-wrapper | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
97 (calc-enter-result 3 "if" (cons 'calcFunc-if (calc-top-list-n 3))))) |
40785 | 98 |
99 | |
100 | |
101 | |
102 | |
103 (defun calc-timing (n) | |
104 (interactive "P") | |
105 (calc-wrapper | |
106 (calc-change-mode 'calc-timing n nil t) | |
107 (message (if calc-timing | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
108 "Reporting timing of slow commands in Trail" |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
109 "Not reporting timing of commands")))) |
40785 | 110 |
111 (defun calc-pass-errors () | |
112 (interactive) | |
113 ;; The following two cases are for the new, optimizing byte compiler | |
114 ;; or the standard 18.57 byte compiler, respectively. | |
115 (condition-case err | |
116 (let ((place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 15))) | |
117 (or (memq (car-safe (car-safe place)) '(error xxxerror)) | |
118 (setq place (aref (nth 2 (nth 2 (symbol-function 'calc-do))) 27))) | |
119 (or (memq (car (car place)) '(error xxxerror)) | |
120 (error "foo")) | |
121 (setcar (car place) 'xxxerror)) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
122 (error (error "The calc-do function has been modified; unable to patch")))) |
40785 | 123 |
124 (defun calc-user-define () | |
125 (interactive) | |
126 (message "Define user key: z-") | |
127 (let ((key (read-char))) | |
128 (if (= (calc-user-function-classify key) 0) | |
129 (error "Can't redefine \"?\" key")) | |
130 (let ((func (intern (completing-read (concat "Set key z " | |
131 (char-to-string key) | |
132 " to command: ") | |
133 obarray | |
134 'commandp | |
135 t | |
136 "calc-")))) | |
137 (let* ((kmap (calc-user-key-map)) | |
138 (old (assq key kmap))) | |
139 (if old | |
140 (setcdr old func) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
141 (setcdr kmap (cons (cons key func) (cdr kmap)))))))) |
40785 | 142 |
143 (defun calc-user-undefine () | |
144 (interactive) | |
145 (message "Undefine user key: z-") | |
146 (let ((key (read-char))) | |
147 (if (= (calc-user-function-classify key) 0) | |
148 (error "Can't undefine \"?\" key")) | |
149 (let* ((kmap (calc-user-key-map))) | |
150 (delq (or (assq key kmap) | |
151 (assq (upcase key) kmap) | |
152 (assq (downcase key) kmap) | |
153 (error "No such user key is defined")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
154 kmap)))) |
40785 | 155 |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
156 |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
157 ;; math-integral-cache-state is originally declared in calcalg2.el, |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
158 ;; it is used in calc-user-define-variable. |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
159 (defvar math-integral-cache-state) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
160 |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
161 ;; calc-user-formula-alist is local to calc-user-define-formula, |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
162 ;; calc-user-define-compostion and calc-finish-formula-edit, |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
163 ;; but is used by calc-fix-user-formula. |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
164 (defvar calc-user-formula-alist) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
165 |
40785 | 166 (defun calc-user-define-formula () |
167 (interactive) | |
168 (calc-wrapper | |
169 (let* ((form (calc-top 1)) | |
170 (arglist nil) | |
171 (is-lambda (and (eq (car-safe form) 'calcFunc-lambda) | |
172 (>= (length form) 2))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
173 odef key keyname cmd cmd-base func calc-user-formula-alist is-symb) |
40785 | 174 (if is-lambda |
175 (setq arglist (mapcar (function (lambda (x) (nth 1 x))) | |
176 (nreverse (cdr (reverse (cdr form))))) | |
177 form (nth (1- (length form)) form)) | |
178 (calc-default-formula-arglist form) | |
179 (setq arglist (sort arglist 'string-lessp))) | |
180 (message "Define user key: z-") | |
181 (setq key (read-char)) | |
182 (if (= (calc-user-function-classify key) 0) | |
183 (error "Can't redefine \"?\" key")) | |
184 (setq key (and (not (memq key '(13 32))) key) | |
185 keyname (and key | |
186 (if (or (and (<= ?0 key) (<= key ?9)) | |
187 (and (<= ?a key) (<= key ?z)) | |
188 (and (<= ?A key) (<= key ?Z))) | |
189 (char-to-string key) | |
190 (format "%03d" key))) | |
191 odef (assq key (calc-user-key-map))) | |
192 (while | |
193 (progn | |
194 (setq cmd (completing-read "Define M-x command name: " | |
195 obarray 'commandp nil | |
196 (if (and odef (symbolp (cdr odef))) | |
197 (symbol-name (cdr odef)) | |
198 "calc-")) | |
199 cmd-base (and (string-match "\\`calc-\\(.+\\)\\'" cmd) | |
200 (math-match-substring cmd 1)) | |
201 cmd (and (not (or (string-equal cmd "") | |
202 (string-equal cmd "calc-"))) | |
203 (intern cmd))) | |
204 (and cmd | |
205 (fboundp cmd) | |
206 odef | |
207 (not | |
208 (y-or-n-p | |
209 (if (get cmd 'calc-user-defn) | |
210 (concat "Replace previous definition for " | |
211 (symbol-name cmd) "? ") | |
212 "That name conflicts with a built-in Emacs function. Replace this function? ")))))) | |
213 (if (and key (not cmd)) | |
214 (setq cmd (intern (concat "calc-User-" keyname)))) | |
215 (while | |
216 (progn | |
217 (setq func (completing-read "Define algebraic function name: " | |
218 obarray 'fboundp nil | |
219 (concat "calcFunc-" | |
220 (if cmd-base | |
221 (if (string-match | |
222 "\\`User-.+" cmd-base) | |
223 (concat | |
224 "User" | |
225 (substring cmd-base 5)) | |
226 cmd-base) | |
227 ""))) | |
228 func (and (not (or (string-equal func "") | |
229 (string-equal func "calcFunc-"))) | |
230 (intern func))) | |
231 (and func | |
232 (fboundp func) | |
233 (not (fboundp cmd)) | |
234 odef | |
235 (not | |
236 (y-or-n-p | |
237 (if (get func 'calc-user-defn) | |
238 (concat "Replace previous definition for " | |
239 (symbol-name func) "? ") | |
240 "That name conflicts with a built-in Emacs function. Replace this function? ")))))) | |
241 (if (not func) | |
242 (setq func (intern (concat "calcFunc-User" | |
243 (or keyname | |
244 (and cmd (symbol-name cmd)) | |
245 (format "%05d" (% (random) 10000))))))) | |
246 (if is-lambda | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
247 (setq calc-user-formula-alist arglist) |
40785 | 248 (while |
249 (progn | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
250 (setq calc-user-formula-alist |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
251 (read-from-minibuffer "Function argument list: " |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
252 (if arglist |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
253 (prin1-to-string arglist) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
254 "()") |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
255 minibuffer-local-map |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
256 t)) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
257 (and (not (calc-subsetp calc-user-formula-alist arglist)) |
40785 | 258 (not (y-or-n-p |
259 "Okay for arguments that don't appear in formula to be ignored? ")))))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
260 (setq is-symb (and calc-user-formula-alist |
40785 | 261 func |
262 (y-or-n-p | |
263 "Leave it symbolic for non-constant arguments? "))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
264 (setq calc-user-formula-alist |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
265 (mapcar (function (lambda (x) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
266 (or (cdr (assq x '((nil . arg-nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
267 (t . arg-t)))) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
268 x))) calc-user-formula-alist)) |
40785 | 269 (if cmd |
270 (progn | |
58614
eba1cd703531
(calc-user-define-formula, calc-do-defmath): Replace calc-need-macros by
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58390
diff
changeset
|
271 (require 'calc-macs) |
40785 | 272 (fset cmd |
273 (list 'lambda | |
274 '() | |
275 '(interactive) | |
276 (list 'calc-wrapper | |
277 (list 'calc-enter-result | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
278 (length calc-user-formula-alist) |
40785 | 279 (let ((name (symbol-name (or func cmd)))) |
280 (and (string-match | |
281 "\\([^-][^-]?[^-]?[^-]?\\)[^-]*\\'" | |
282 name) | |
283 (math-match-substring name 1))) | |
284 (list 'cons | |
285 (list 'quote func) | |
286 (list 'calc-top-list-n | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
287 (length calc-user-formula-alist))))))) |
40785 | 288 (put cmd 'calc-user-defn t))) |
289 (let ((body (list 'math-normalize (calc-fix-user-formula form)))) | |
290 (fset func | |
291 (append | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
292 (list 'lambda calc-user-formula-alist) |
40785 | 293 (and is-symb |
294 (mapcar (function (lambda (v) | |
295 (list 'math-check-const v t))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
296 calc-user-formula-alist)) |
40785 | 297 (list body)))) |
298 (put func 'calc-user-defn form) | |
299 (setq math-integral-cache-state nil) | |
300 (if key | |
301 (let* ((kmap (calc-user-key-map)) | |
302 (old (assq key kmap))) | |
303 (if old | |
304 (setcdr old cmd) | |
305 (setcdr kmap (cons (cons key cmd) (cdr kmap))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
306 (message ""))) |
40785 | 307 |
308 (defun calc-default-formula-arglist (form) | |
309 (if (consp form) | |
310 (if (eq (car form) 'var) | |
311 (if (or (memq (nth 1 form) arglist) | |
312 (math-const-var form)) | |
313 () | |
314 (setq arglist (cons (nth 1 form) arglist))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
315 (calc-default-formula-arglist-step (cdr form))))) |
40785 | 316 |
317 (defun calc-default-formula-arglist-step (l) | |
318 (and l | |
319 (progn | |
320 (calc-default-formula-arglist (car l)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
321 (calc-default-formula-arglist-step (cdr l))))) |
40785 | 322 |
323 (defun calc-subsetp (a b) | |
324 (or (null a) | |
325 (and (memq (car a) b) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
326 (calc-subsetp (cdr a) b)))) |
40785 | 327 |
328 (defun calc-fix-user-formula (f) | |
329 (if (consp f) | |
330 (let (temp) | |
331 (cond ((and (eq (car f) 'var) | |
332 (memq (setq temp (or (cdr (assq (nth 1 f) '((nil . arg-nil) | |
333 (t . arg-t)))) | |
334 (nth 1 f))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
335 calc-user-formula-alist)) |
40785 | 336 temp) |
337 ((or (math-constp f) (eq (car f) 'var)) | |
338 (list 'quote f)) | |
339 ((and (eq (car f) 'calcFunc-eval) | |
340 (= (length f) 2)) | |
341 (list 'let '((calc-simplify-mode nil)) | |
342 (list 'math-normalize (calc-fix-user-formula (nth 1 f))))) | |
343 ((and (eq (car f) 'calcFunc-evalsimp) | |
344 (= (length f) 2)) | |
345 (list 'math-simplify (calc-fix-user-formula (nth 1 f)))) | |
346 ((and (eq (car f) 'calcFunc-evalextsimp) | |
347 (= (length f) 2)) | |
348 (list 'math-simplify-extended | |
349 (calc-fix-user-formula (nth 1 f)))) | |
350 (t | |
351 (cons 'list | |
352 (cons (list 'quote (car f)) | |
353 (mapcar 'calc-fix-user-formula (cdr f))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
354 f)) |
40785 | 355 |
356 (defun calc-user-define-composition () | |
357 (interactive) | |
358 (calc-wrapper | |
359 (if (eq calc-language 'unform) | |
360 (error "Can't define formats for unformatted mode")) | |
361 (let* ((comp (calc-top 1)) | |
362 (func (intern (completing-read "Define format for which function: " | |
363 obarray 'fboundp nil "calcFunc-"))) | |
364 (comps (get func 'math-compose-forms)) | |
365 entry entry2 | |
366 (arglist nil) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
367 (calc-user-formula-alist nil)) |
40785 | 368 (if (math-zerop comp) |
369 (if (setq entry (assq calc-language comps)) | |
370 (put func 'math-compose-forms (delq entry comps))) | |
371 (calc-default-formula-arglist comp) | |
372 (setq arglist (sort arglist 'string-lessp)) | |
373 (while | |
374 (progn | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
375 (setq calc-user-formula-alist |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
376 (read-from-minibuffer "Composition argument list: " |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
377 (if arglist |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
378 (prin1-to-string arglist) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
379 "()") |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
380 minibuffer-local-map |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
381 t)) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
382 (and (not (calc-subsetp calc-user-formula-alist arglist)) |
40785 | 383 (y-or-n-p |
384 "Okay for arguments that don't appear in formula to be invisible? ")))) | |
385 (or (setq entry (assq calc-language comps)) | |
386 (put func 'math-compose-forms | |
387 (cons (setq entry (list calc-language)) comps))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
388 (or (setq entry2 (assq (length calc-user-formula-alist) (cdr entry))) |
40785 | 389 (setcdr entry |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
390 (cons (setq entry2 |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
391 (list (length calc-user-formula-alist))) (cdr entry)))) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
392 (setcdr entry2 |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
393 (list 'lambda calc-user-formula-alist (calc-fix-user-formula comp)))) |
40785 | 394 (calc-pop-stack 1) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
395 (calc-do-refresh)))) |
40785 | 396 |
397 | |
398 (defun calc-user-define-kbd-macro (arg) | |
399 (interactive "P") | |
400 (or last-kbd-macro | |
401 (error "No keyboard macro defined")) | |
402 (message "Define last kbd macro on user key: z-") | |
403 (let ((key (read-char))) | |
404 (if (= (calc-user-function-classify key) 0) | |
405 (error "Can't redefine \"?\" key")) | |
406 (let ((cmd (intern (completing-read "Full name for new command: " | |
407 obarray | |
408 'commandp | |
409 nil | |
410 (concat "calc-User-" | |
411 (if (or (and (>= key ?a) | |
412 (<= key ?z)) | |
413 (and (>= key ?A) | |
414 (<= key ?Z)) | |
415 (and (>= key ?0) | |
416 (<= key ?9))) | |
417 (char-to-string key) | |
418 (format "%03d" key))))))) | |
419 (and (fboundp cmd) | |
420 (not (let ((f (symbol-function cmd))) | |
421 (or (stringp f) | |
422 (and (consp f) | |
423 (eq (car-safe (nth 3 f)) | |
424 'calc-execute-kbd-macro))))) | |
425 (error "Function %s is already defined and not a keyboard macro" | |
426 cmd)) | |
427 (put cmd 'calc-user-defn t) | |
428 (fset cmd (if (< (prefix-numeric-value arg) 0) | |
429 last-kbd-macro | |
430 (list 'lambda | |
431 '(arg) | |
432 '(interactive "P") | |
433 (list 'calc-execute-kbd-macro | |
434 (vector (key-description last-kbd-macro) | |
435 last-kbd-macro) | |
436 'arg | |
437 (format "z%c" key))))) | |
438 (let* ((kmap (calc-user-key-map)) | |
439 (old (assq key kmap))) | |
440 (if old | |
441 (setcdr old cmd) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
442 (setcdr kmap (cons (cons key cmd) (cdr kmap)))))))) |
40785 | 443 |
444 | |
445 (defun calc-edit-user-syntax () | |
446 (interactive) | |
447 (calc-wrapper | |
448 (let ((lang calc-language)) | |
449 (calc-edit-mode (list 'calc-finish-user-syntax-edit (list 'quote lang)) | |
450 t | |
451 (format "Editing %s-Mode Syntax Table" | |
452 (cond ((null lang) "Normal") | |
453 ((eq lang 'tex) "TeX") | |
454 (t (capitalize (symbol-name lang)))))) | |
455 (calc-write-parse-table (cdr (assq lang calc-user-parse-tables)) | |
456 lang))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
457 (calc-show-edit-buffer)) |
40785 | 458 |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
459 (defvar calc-original-buffer) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
460 |
40785 | 461 (defun calc-finish-user-syntax-edit (lang) |
462 (let ((tab (calc-read-parse-table calc-original-buffer lang)) | |
463 (entry (assq lang calc-user-parse-tables))) | |
464 (if tab | |
465 (setcdr (or entry | |
466 (car (setq calc-user-parse-tables | |
467 (cons (list lang) calc-user-parse-tables)))) | |
468 tab) | |
469 (if entry | |
470 (setq calc-user-parse-tables | |
471 (delq entry calc-user-parse-tables))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
472 (switch-to-buffer calc-original-buffer)) |
40785 | 473 |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
474 ;; The variable calc-lang is local to calc-write-parse-table, but is |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
475 ;; used by calc-write-parse-table-part which is called by |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
476 ;; calc-write-parse-table. The variable is also local to |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
477 ;; calc-read-parse-table, but is used by calc-fix-token-name which |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
478 ;; is called (indirectly) by calc-read-parse-table. |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
479 (defvar calc-lang) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
480 |
40785 | 481 (defun calc-write-parse-table (tab calc-lang) |
482 (let ((p tab)) | |
483 (while p | |
484 (calc-write-parse-table-part (car (car p))) | |
485 (insert ":= " | |
486 (let ((math-format-hash-args t)) | |
487 (math-format-flat-expr (cdr (car p)) 0)) | |
488 "\n") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
489 (setq p (cdr p))))) |
40785 | 490 |
491 (defun calc-write-parse-table-part (p) | |
492 (while p | |
493 (cond ((stringp (car p)) | |
494 (let ((s (car p))) | |
495 (if (and (string-match "\\`\\\\dots\\>" s) | |
496 (not (eq calc-lang 'tex))) | |
497 (setq s (concat ".." (substring s 5)))) | |
498 (if (or (and (string-match | |
499 "[a-zA-Z0-9\"{}]\\|\\`:=\\'\\|\\`#\\|\\`%%" s) | |
500 (string-match "[^a-zA-Z0-9\\]" s)) | |
501 (and (assoc s '((")") ("]") (">"))) | |
502 (not (cdr p)))) | |
503 (insert (prin1-to-string s) " ") | |
504 (insert s " ")))) | |
505 ((integerp (car p)) | |
506 (insert "#") | |
507 (or (= (car p) 0) | |
508 (insert "/" (int-to-string (car p)))) | |
509 (insert " ")) | |
510 ((and (eq (car (car p)) '\?) (equal (car (nth 2 (car p))) "$$")) | |
511 (insert (car (nth 1 (car p))) " ")) | |
512 (t | |
513 (insert "{ ") | |
514 (calc-write-parse-table-part (nth 1 (car p))) | |
515 (insert "}" (symbol-name (car (car p)))) | |
516 (if (nth 2 (car p)) | |
517 (calc-write-parse-table-part (list (car (nth 2 (car p))))) | |
518 (insert " ")))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
519 (setq p (cdr p)))) |
40785 | 520 |
521 (defun calc-read-parse-table (calc-buf calc-lang) | |
522 (let ((tab nil)) | |
523 (while (progn | |
524 (skip-chars-forward "\n\t ") | |
525 (not (eobp))) | |
526 (if (looking-at "%%") | |
527 (end-of-line) | |
528 (let ((pt (point)) | |
529 (p (calc-read-parse-table-part ":=[\n\t ]+" ":="))) | |
530 (or (stringp (car p)) | |
531 (and (integerp (car p)) | |
532 (stringp (nth 1 p))) | |
533 (progn | |
534 (goto-char pt) | |
535 (error "Malformed syntax rule"))) | |
536 (let ((pos (point))) | |
537 (end-of-line) | |
538 (let* ((str (buffer-substring pos (point))) | |
539 (exp (save-excursion | |
540 (set-buffer calc-buf) | |
541 (let ((calc-user-parse-tables nil) | |
542 (calc-language nil) | |
543 (math-expr-opers math-standard-opers) | |
544 (calc-hashes-used 0)) | |
545 (math-read-expr | |
546 (if (string-match ",[ \t]*\\'" str) | |
547 (substring str 0 (match-beginning 0)) | |
548 str)))))) | |
549 (if (eq (car-safe exp) 'error) | |
550 (progn | |
551 (goto-char (+ pos (nth 1 exp))) | |
552 (error (nth 2 exp)))) | |
553 (setq tab (nconc tab (list (cons p exp))))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
554 tab)) |
40785 | 555 |
556 (defun calc-fix-token-name (name &optional unquoted) | |
557 (cond ((string-match "\\`\\.\\." name) | |
558 (concat "\\dots" (substring name 2))) | |
559 ((and (equal name "{") (memq calc-lang '(tex eqn))) | |
560 "(") | |
561 ((and (equal name "}") (memq calc-lang '(tex eqn))) | |
562 ")") | |
563 ((and (equal name "&") (eq calc-lang 'tex)) | |
564 ",") | |
565 ((equal name "#") | |
566 (search-backward "#") | |
567 (error "Token '#' is reserved")) | |
568 ((and unquoted (string-match "#" name)) | |
569 (error "Tokens containing '#' must be quoted")) | |
570 ((not (string-match "[^ ]" name)) | |
571 (search-backward "\"" nil t) | |
572 (error "Blank tokens are not allowed")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
573 (t name))) |
40785 | 574 |
575 (defun calc-read-parse-table-part (term eterm) | |
576 (let ((part nil) | |
577 (quoted nil)) | |
578 (while (progn | |
579 (skip-chars-forward "\n\t ") | |
580 (if (eobp) (error "Expected '%s'" eterm)) | |
581 (not (looking-at term))) | |
582 (cond ((looking-at "%%") | |
583 (end-of-line)) | |
584 ((looking-at "{[\n\t ]") | |
585 (forward-char 2) | |
586 (let ((p (calc-read-parse-table-part "}" "}"))) | |
587 (or (looking-at "[+*?]") | |
588 (error "Expected '+', '*', or '?'")) | |
589 (let ((sym (intern (buffer-substring (point) (1+ (point)))))) | |
590 (forward-char 1) | |
591 (looking-at "[^\n\t ]*") | |
592 (let ((sep (buffer-substring (point) (match-end 0)))) | |
593 (goto-char (match-end 0)) | |
594 (and (eq sym '\?) (> (length sep) 0) | |
595 (not (equal sep "$")) (not (equal sep ".")) | |
596 (error "Separator not allowed with { ... }?")) | |
597 (if (string-match "\\`\"" sep) | |
598 (setq sep (read-from-string sep))) | |
599 (setq sep (calc-fix-token-name sep)) | |
600 (setq part (nconc part | |
601 (list (list sym p | |
602 (and (> (length sep) 0) | |
603 (cons sep p)))))))))) | |
604 ((looking-at "}") | |
605 (error "Too many }'s")) | |
606 ((looking-at "\"") | |
607 (setq quoted (calc-fix-token-name (read (current-buffer))) | |
608 part (nconc part (list quoted)))) | |
609 ((looking-at "#\\(\\(/[0-9]+\\)?\\)[\n\t ]") | |
610 (setq part (nconc part (list (if (= (match-beginning 1) | |
611 (match-end 1)) | |
612 0 | |
613 (string-to-int | |
614 (buffer-substring | |
615 (1+ (match-beginning 1)) | |
616 (match-end 1))))))) | |
617 (goto-char (match-end 0))) | |
618 ((looking-at ":=[\n\t ]") | |
619 (error "Misplaced ':='")) | |
620 (t | |
621 (looking-at "[^\n\t ]*") | |
622 (let ((end (match-end 0))) | |
623 (setq part (nconc part (list (calc-fix-token-name | |
624 (buffer-substring | |
625 (point) end) t)))) | |
626 (goto-char end))))) | |
627 (goto-char (match-end 0)) | |
628 (let ((len (length part))) | |
629 (while (and (> len 1) | |
630 (let ((last (nthcdr (setq len (1- len)) part))) | |
631 (and (assoc (car last) '((")") ("]") (">"))) | |
632 (not (eq (car last) quoted)) | |
633 (setcar last | |
634 (list '\? (list (car last)) '("$$")))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
635 part)) |
40785 | 636 |
637 | |
638 (defun calc-user-define-invocation () | |
639 (interactive) | |
640 (or last-kbd-macro | |
641 (error "No keyboard macro defined")) | |
642 (setq calc-invocation-macro last-kbd-macro) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
643 (message "Use `M-# Z' to invoke this macro")) |
40785 | 644 |
645 | |
646 (defun calc-user-define-edit (prefix) | |
647 (interactive "P") ; but no calc-wrapper! | |
648 (message "Edit definition of command: z-") | |
649 (let* ((key (read-char)) | |
650 (def (or (assq key (calc-user-key-map)) | |
651 (assq (upcase key) (calc-user-key-map)) | |
652 (assq (downcase key) (calc-user-key-map)) | |
653 (error "No command defined for that key"))) | |
654 (cmd (cdr def))) | |
655 (if (symbolp cmd) | |
656 (setq cmd (symbol-function cmd))) | |
657 (cond ((or (stringp cmd) | |
658 (and (consp cmd) | |
659 (eq (car-safe (nth 3 cmd)) 'calc-execute-kbd-macro))) | |
660 (if (and (>= (prefix-numeric-value prefix) 0) | |
661 (fboundp 'edit-kbd-macro) | |
662 (symbolp (cdr def)) | |
663 (eq major-mode 'calc-mode)) | |
664 (progn | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
665 (if (and (< (window-width) (frame-width)) |
40785 | 666 calc-display-trail) |
667 (let ((win (get-buffer-window (calc-trail-buffer)))) | |
668 (if win | |
669 (delete-window win)))) | |
670 (edit-kbd-macro (cdr def) prefix nil | |
671 (function | |
672 (lambda (x) | |
673 (and calc-display-trail | |
674 (calc-wrapper | |
675 (calc-trail-display 1 t))))) | |
676 (function | |
677 (lambda (cmd) | |
678 (if (stringp (symbol-function cmd)) | |
679 (symbol-function cmd) | |
680 (let ((mac (nth 1 (nth 3 (symbol-function | |
681 cmd))))) | |
682 (if (vectorp mac) | |
683 (aref mac 1) | |
684 mac))))) | |
685 (function | |
686 (lambda (new cmd) | |
687 (if (stringp (symbol-function cmd)) | |
688 (fset cmd new) | |
689 (let ((mac (cdr (nth 3 (symbol-function | |
690 cmd))))) | |
691 (if (vectorp (car mac)) | |
692 (progn | |
693 (aset (car mac) 0 | |
694 (key-description new)) | |
695 (aset (car mac) 1 new)) | |
696 (setcar mac new)))))))) | |
697 (let ((keys (progn (and (fboundp 'edit-kbd-macro) | |
698 (edit-kbd-macro nil)) | |
699 (fboundp 'MacEdit-parse-keys)))) | |
700 (calc-wrapper | |
701 (calc-edit-mode (list 'calc-finish-macro-edit | |
702 (list 'quote def) | |
703 keys) | |
704 t) | |
705 (if keys | |
706 (let (top | |
707 (fill-column 70) | |
708 (fill-prefix nil)) | |
709 (insert "Notations: RET, SPC, TAB, DEL, LFD, NUL" | |
710 ", C-xxx, M-xxx.\n\n") | |
711 (setq top (point)) | |
712 (insert (if (stringp cmd) | |
713 (key-description cmd) | |
714 (if (vectorp (nth 1 (nth 3 cmd))) | |
715 (aref (nth 1 (nth 3 cmd)) 0) | |
716 (key-description (nth 1 (nth 3 cmd))))) | |
717 "\n") | |
718 (if (>= (prog2 (forward-char -1) | |
719 (current-column) | |
720 (forward-char 1)) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
721 (frame-width)) |
40785 | 722 (fill-region top (point)))) |
723 (insert "Press C-q to quote control characters like RET" | |
724 " and TAB.\n" | |
725 (if (stringp cmd) | |
726 cmd | |
727 (if (vectorp (nth 1 (nth 3 cmd))) | |
728 (aref (nth 1 (nth 3 cmd)) 1) | |
729 (nth 1 (nth 3 cmd))))))) | |
730 (calc-show-edit-buffer) | |
731 (forward-line (if keys 2 1))))) | |
732 (t (let* ((func (calc-stack-command-p cmd)) | |
733 (defn (and func | |
734 (symbolp func) | |
735 (get func 'calc-user-defn)))) | |
736 (if (and defn (calc-valid-formula-func func)) | |
737 (progn | |
738 (calc-wrapper | |
739 (calc-edit-mode (list 'calc-finish-formula-edit | |
740 (list 'quote func))) | |
741 (insert (math-showing-full-precision | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
742 (math-format-nice-expr defn (frame-width))) |
40785 | 743 "\n")) |
744 (calc-show-edit-buffer)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
745 (error "That command's definition cannot be edited"))))))) |
40785 | 746 |
747 (defun calc-finish-macro-edit (def keys) | |
748 (forward-line 1) | |
749 (if (and keys (looking-at "\n")) (forward-line 1)) | |
750 (let* ((true-str (buffer-substring (point) (point-max))) | |
751 (str true-str)) | |
752 (if keys (setq str (MacEdit-parse-keys str))) | |
753 (if (symbolp (cdr def)) | |
754 (if (stringp (symbol-function (cdr def))) | |
755 (fset (cdr def) str) | |
756 (let ((mac (cdr (nth 3 (symbol-function (cdr def)))))) | |
757 (if (vectorp (car mac)) | |
758 (progn | |
759 (aset (car mac) 0 (if keys true-str (key-description str))) | |
760 (aset (car mac) 1 str)) | |
761 (setcar mac str)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
762 (setcdr def str)))) |
40785 | 763 |
764 ;;; The following are hooks into the MacEdit package from macedit.el. | |
765 (put 'calc-execute-extended-command 'MacEdit-print | |
766 (function (lambda () | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
767 (setq macro-str (concat "\excalc-" macro-str))))) |
40785 | 768 |
769 (put 'calcDigit-start 'MacEdit-print | |
770 (function (lambda () | |
771 (if calc-algebraic-mode | |
772 (calc-macro-edit-algebraic) | |
773 (MacEdit-unread-chars key-last) | |
774 (let ((str "") | |
775 (min-bsp 0) | |
776 ch last) | |
777 (while (and (setq ch (MacEdit-read-char)) | |
778 (or (and (>= ch ?0) (<= ch ?9)) | |
779 (memq ch '(?\. ?e ?\_ ?n ?\: ?\# ?M | |
780 ?o ?h ?\@ ?\")) | |
781 (and (memq ch '(?\' ?m ?s)) | |
782 (string-match "[@oh]" str)) | |
783 (and (or (and (>= ch ?a) (<= ch ?z)) | |
784 (and (>= ch ?A) (<= ch ?Z))) | |
785 (string-match | |
786 "^[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#" | |
787 str)) | |
788 (and (memq ch '(?\177 ?\C-h)) | |
789 (> (length str) 0)) | |
790 (and (memq ch '(?+ ?-)) | |
791 (> (length str) 0) | |
792 (eq (aref str (1- (length str))) | |
793 ?e)))) | |
794 (if (or (and (>= ch ?0) (<= ch ?9)) | |
795 (and (or (not (memq ch '(?\177 ?\C-h))) | |
796 (<= (length str) min-bsp)) | |
797 (setq min-bsp (1+ (length str))))) | |
798 (setq str (concat str (char-to-string ch))) | |
799 (setq str (substring str 0 -1)))) | |
800 (if (memq ch '(32 10 13)) | |
801 (setq str (concat str (char-to-string ch))) | |
802 (MacEdit-unread-chars ch)) | |
803 (insert "type \"") | |
804 (MacEdit-insert-string str) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
805 (insert "\"\n")))))) |
40785 | 806 |
807 (defun calc-macro-edit-algebraic () | |
808 (MacEdit-unread-chars key-last) | |
809 (let ((str "") | |
810 (min-bsp 0)) | |
811 (while (progn | |
812 (MacEdit-lookup-key calc-alg-ent-map) | |
813 (or (and (memq key-symbol '(self-insert-command | |
814 calcAlg-previous)) | |
815 (< (length str) 60)) | |
816 (memq key-symbol | |
817 '(backward-delete-char | |
818 delete-backward-char | |
819 backward-delete-char-untabify)) | |
820 (eq key-last 9))) | |
821 (setq macro-str (substring macro-str (length key-str))) | |
822 (if (or (eq key-symbol 'self-insert-command) | |
823 (and (or (not (memq key-symbol '(backward-delete-char | |
824 delete-backward-char | |
825 backward-delete-char-untabify))) | |
826 (<= (length str) min-bsp)) | |
827 (setq min-bsp (+ (length str) (length key-str))))) | |
828 (setq str (concat str key-str)) | |
829 (setq str (substring str 0 -1)))) | |
830 (if (memq key-last '(10 13)) | |
831 (setq str (concat str key-str) | |
832 macro-str (substring macro-str (length key-str)))) | |
833 (if (> (length str) 0) | |
834 (progn | |
835 (insert "type \"") | |
836 (MacEdit-insert-string str) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
837 (insert "\"\n"))))) |
40785 | 838 (put 'calc-algebraic-entry 'MacEdit-print 'calc-macro-edit-algebraic) |
839 (put 'calc-auto-algebraic-entry 'MacEdit-print 'calc-macro-edit-algebraic) | |
840 | |
841 (defun calc-macro-edit-variable (&optional no-cmd) | |
842 (let ((str "") ch) | |
843 (or no-cmd (insert (symbol-name key-symbol) "\n")) | |
49841
babed35678ca
(calc-macro-edit-variable): Fix character constant.
Juanma Barranquero <lekktu@gmail.com>
parents:
49598
diff
changeset
|
844 (if (memq (MacEdit-peek-char) '(?\+ ?\- ?\* ?\/ ?^ ?\|)) |
40785 | 845 (setq str (char-to-string (MacEdit-read-char)))) |
846 (if (and (setq ch (MacEdit-peek-char)) | |
847 (>= ch ?0) (<= ch ?9)) | |
848 (insert "type \"" str | |
849 (char-to-string (MacEdit-read-char)) "\"\n") | |
850 (if (> (length str) 0) | |
851 (insert "type \"" str "\"\n")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
852 (MacEdit-read-argument)))) |
40785 | 853 (put 'calc-store 'MacEdit-print 'calc-macro-edit-variable) |
854 (put 'calc-store-into 'MacEdit-print 'calc-macro-edit-variable) | |
855 (put 'calc-store-neg 'MacEdit-print 'calc-macro-edit-variable) | |
856 (put 'calc-store-plus 'MacEdit-print 'calc-macro-edit-variable) | |
857 (put 'calc-store-minus 'MacEdit-print 'calc-macro-edit-variable) | |
858 (put 'calc-store-times 'MacEdit-print 'calc-macro-edit-variable) | |
859 (put 'calc-store-div 'MacEdit-print 'calc-macro-edit-variable) | |
860 (put 'calc-store-power 'MacEdit-print 'calc-macro-edit-variable) | |
861 (put 'calc-store-concat 'MacEdit-print 'calc-macro-edit-variable) | |
862 (put 'calc-store-inv 'MacEdit-print 'calc-macro-edit-variable) | |
863 (put 'calc-store-decr 'MacEdit-print 'calc-macro-edit-variable) | |
864 (put 'calc-store-incr 'MacEdit-print 'calc-macro-edit-variable) | |
865 (put 'calc-store-exchange 'MacEdit-print 'calc-macro-edit-variable) | |
866 (put 'calc-unstore 'MacEdit-print 'calc-macro-edit-variable) | |
867 (put 'calc-recall 'MacEdit-print 'calc-macro-edit-variable) | |
868 (put 'calc-let 'MacEdit-print 'calc-macro-edit-variable) | |
869 (put 'calc-permanent-variable 'MacEdit-print 'calc-macro-edit-variable) | |
870 | |
871 (defun calc-macro-edit-variable-2 () | |
872 (calc-macro-edit-variable) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
873 (calc-macro-edit-variable t)) |
40785 | 874 (put 'calc-copy-variable 'MacEdit-print 'calc-macro-edit-variable-2) |
875 (put 'calc-declare-variable 'MacEdit-print 'calc-macro-edit-variable-2) | |
876 | |
877 (defun calc-macro-edit-quick-digit () | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
878 (insert "type \"" key-str "\" # " (symbol-name key-symbol) "\n")) |
40785 | 879 (put 'calc-store-quick 'MacEdit-print 'calc-macro-edit-quick-digit) |
880 (put 'calc-store-into-quick 'MacEdit-print 'calc-macro-edit-quick-digit) | |
881 (put 'calc-recall-quick 'MacEdit-print 'calc-macro-edit-quick-digit) | |
882 (put 'calc-select-part 'MacEdit-print 'calc-macro-edit-quick-digit) | |
883 (put 'calc-clean-num 'MacEdit-print 'calc-macro-edit-quick-digit) | |
884 | |
885 | |
886 (defun calc-finish-formula-edit (func) | |
887 (let ((buf (current-buffer)) | |
888 (str (buffer-substring (point) (point-max))) | |
889 (start (point)) | |
890 (body (calc-valid-formula-func func))) | |
891 (set-buffer calc-original-buffer) | |
892 (let ((val (math-read-expr str))) | |
893 (if (eq (car-safe val) 'error) | |
894 (progn | |
895 (set-buffer buf) | |
896 (goto-char (+ start (nth 1 val))) | |
897 (error (nth 2 val)))) | |
898 (setcar (cdr body) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
899 (let ((calc-user-formula-alist (nth 1 (symbol-function func)))) |
40785 | 900 (calc-fix-user-formula val))) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
901 (put func 'calc-user-defn val)))) |
40785 | 902 |
903 (defun calc-valid-formula-func (func) | |
904 (let ((def (symbol-function func))) | |
905 (and (consp def) | |
906 (eq (car def) 'lambda) | |
907 (progn | |
908 (setq def (cdr (cdr def))) | |
909 (while (and def | |
910 (not (eq (car (car def)) 'math-normalize))) | |
911 (setq def (cdr def))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
912 (car def))))) |
40785 | 913 |
914 | |
915 (defun calc-get-user-defn () | |
916 (interactive) | |
917 (calc-wrapper | |
918 (message "Get definition of command: z-") | |
919 (let* ((key (read-char)) | |
920 (def (or (assq key (calc-user-key-map)) | |
921 (assq (upcase key) (calc-user-key-map)) | |
922 (assq (downcase key) (calc-user-key-map)) | |
923 (error "No command defined for that key"))) | |
924 (cmd (cdr def))) | |
925 (if (symbolp cmd) | |
926 (setq cmd (symbol-function cmd))) | |
927 (cond ((stringp cmd) | |
928 (message "Keyboard macro: %s" cmd)) | |
929 (t (let* ((func (calc-stack-command-p cmd)) | |
930 (defn (and func | |
931 (symbolp func) | |
932 (get func 'calc-user-defn)))) | |
933 (if defn | |
934 (progn | |
935 (and (calc-valid-formula-func func) | |
936 (setq defn (append '(calcFunc-lambda) | |
937 (mapcar 'math-build-var-name | |
938 (nth 1 (symbol-function | |
939 func))) | |
940 (list defn)))) | |
941 (calc-enter-result 0 "gdef" defn)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
942 (error "That command is not defined by a formula")))))))) |
40785 | 943 |
944 | |
945 (defun calc-user-define-permanent () | |
946 (interactive) | |
947 (calc-wrapper | |
948 (message "Record in %s the command: z-" calc-settings-file) | |
949 (let* ((key (read-char)) | |
950 (def (or (assq key (calc-user-key-map)) | |
951 (assq (upcase key) (calc-user-key-map)) | |
952 (assq (downcase key) (calc-user-key-map)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
953 (and (eq key ?\') |
40785 | 954 (cons nil |
955 (intern (completing-read | |
956 (format "Record in %s the function: " | |
957 calc-settings-file) | |
958 obarray 'fboundp nil "calcFunc-")))) | |
959 (error "No command defined for that key")))) | |
960 (set-buffer (find-file-noselect (substitute-in-file-name | |
961 calc-settings-file))) | |
962 (goto-char (point-max)) | |
963 (let* ((cmd (cdr def)) | |
964 (fcmd (and cmd (symbolp cmd) (symbol-function cmd))) | |
965 (func nil) | |
966 (pt (point)) | |
967 (fill-column 70) | |
968 (fill-prefix nil) | |
969 str q-ok) | |
970 (insert "\n;;; Definition stored by Calc on " (current-time-string) | |
971 "\n(put 'calc-define '" | |
972 (if (symbolp cmd) (symbol-name cmd) (format "key%d" key)) | |
973 " '(progn\n") | |
974 (if (and fcmd | |
975 (eq (car-safe fcmd) 'lambda) | |
976 (get cmd 'calc-user-defn)) | |
977 (let ((pt (point))) | |
978 (and (eq (car-safe (nth 3 fcmd)) 'calc-execute-kbd-macro) | |
979 (vectorp (nth 1 (nth 3 fcmd))) | |
980 (progn (and (fboundp 'edit-kbd-macro) | |
981 (edit-kbd-macro nil)) | |
982 (fboundp 'MacEdit-parse-keys)) | |
983 (setq q-ok t) | |
984 (aset (nth 1 (nth 3 fcmd)) 1 nil)) | |
985 (insert (setq str (prin1-to-string | |
986 (cons 'defun (cons cmd (cdr fcmd))))) | |
987 "\n") | |
988 (or (and (string-match "\"" str) (not q-ok)) | |
989 (fill-region pt (point))) | |
990 (indent-rigidly pt (point) 2) | |
991 (delete-region pt (1+ pt)) | |
992 (insert " (put '" (symbol-name cmd) | |
993 " 'calc-user-defn '" | |
994 (prin1-to-string (get cmd 'calc-user-defn)) | |
995 ")\n") | |
996 (setq func (calc-stack-command-p cmd)) | |
997 (let ((ffunc (and func (symbolp func) (symbol-function func))) | |
998 (pt (point))) | |
999 (and ffunc | |
1000 (eq (car-safe ffunc) 'lambda) | |
1001 (get func 'calc-user-defn) | |
1002 (progn | |
1003 (insert (setq str (prin1-to-string | |
1004 (cons 'defun (cons func | |
1005 (cdr ffunc))))) | |
1006 "\n") | |
1007 (or (and (string-match "\"" str) (not q-ok)) | |
1008 (fill-region pt (point))) | |
1009 (indent-rigidly pt (point) 2) | |
1010 (delete-region pt (1+ pt)) | |
1011 (setq pt (point)) | |
1012 (insert "(put '" (symbol-name func) | |
1013 " 'calc-user-defn '" | |
1014 (prin1-to-string (get func 'calc-user-defn)) | |
1015 ")\n") | |
1016 (fill-region pt (point)) | |
1017 (indent-rigidly pt (point) 2) | |
1018 (delete-region pt (1+ pt)))))) | |
1019 (and (stringp fcmd) | |
1020 (insert " (fset '" (prin1-to-string cmd) | |
1021 " " (prin1-to-string fcmd) ")\n"))) | |
1022 (or func (setq func (and cmd (symbolp cmd) (fboundp cmd) cmd))) | |
1023 (if (get func 'math-compose-forms) | |
1024 (let ((pt (point))) | |
1025 (insert "(put '" (symbol-name cmd) | |
1026 " 'math-compose-forms '" | |
1027 (prin1-to-string (get func 'math-compose-forms)) | |
1028 ")\n") | |
1029 (fill-region pt (point)) | |
1030 (indent-rigidly pt (point) 2) | |
1031 (delete-region pt (1+ pt)))) | |
1032 (if (car def) | |
1033 (insert " (define-key calc-mode-map " | |
1034 (prin1-to-string (concat "z" (char-to-string key))) | |
1035 " '" | |
1036 (prin1-to-string cmd) | |
1037 ")\n"))) | |
1038 (insert "))\n") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1039 (save-buffer)))) |
40785 | 1040 |
1041 (defun calc-stack-command-p (cmd) | |
1042 (if (and cmd (symbolp cmd)) | |
1043 (and (fboundp cmd) | |
1044 (calc-stack-command-p (symbol-function cmd))) | |
1045 (and (consp cmd) | |
1046 (eq (car cmd) 'lambda) | |
1047 (setq cmd (or (assq 'calc-wrapper cmd) | |
1048 (assq 'calc-slow-wrapper cmd))) | |
1049 (setq cmd (assq 'calc-enter-result cmd)) | |
1050 (memq (car (nth 3 cmd)) '(cons list)) | |
1051 (eq (car (nth 1 (nth 3 cmd))) 'quote) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1052 (nth 1 (nth 1 (nth 3 cmd)))))) |
40785 | 1053 |
1054 | |
1055 (defun calc-call-last-kbd-macro (arg) | |
1056 (interactive "P") | |
1057 (and defining-kbd-macro | |
1058 (error "Can't execute anonymous macro while defining one")) | |
1059 (or last-kbd-macro | |
1060 (error "No kbd macro has been defined")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1061 (calc-execute-kbd-macro last-kbd-macro arg)) |
40785 | 1062 |
1063 (defun calc-execute-kbd-macro (mac arg &rest prefix) | |
1064 (if (and (vectorp mac) (> (length mac) 0) (stringp (aref mac 0))) | |
1065 (setq mac (or (aref mac 1) | |
1066 (aset mac 1 (progn (and (fboundp 'edit-kbd-macro) | |
1067 (edit-kbd-macro nil)) | |
1068 (MacEdit-parse-keys (aref mac 0))))))) | |
1069 (if (< (prefix-numeric-value arg) 0) | |
1070 (execute-kbd-macro mac (- (prefix-numeric-value arg))) | |
1071 (if calc-executing-macro | |
1072 (execute-kbd-macro mac arg) | |
1073 (calc-slow-wrapper | |
1074 (let ((old-stack-whole (copy-sequence calc-stack)) | |
1075 (old-stack-top calc-stack-top) | |
1076 (old-buffer-size (buffer-size)) | |
1077 (old-refresh-count calc-refresh-count)) | |
1078 (unwind-protect | |
1079 (let ((calc-executing-macro mac)) | |
1080 (execute-kbd-macro mac arg)) | |
1081 (calc-select-buffer) | |
1082 (let ((new-stack (reverse calc-stack)) | |
1083 (old-stack (reverse old-stack-whole))) | |
1084 (while (and new-stack old-stack | |
1085 (equal (car new-stack) (car old-stack))) | |
1086 (setq new-stack (cdr new-stack) | |
1087 old-stack (cdr old-stack))) | |
1088 (or (equal prefix '(nil)) | |
1089 (calc-record-list (if (> (length new-stack) 1) | |
1090 (mapcar 'car new-stack) | |
1091 '("")) | |
1092 (or (car prefix) "kmac"))) | |
1093 (calc-record-undo (list 'set 'saved-stack-top old-stack-top)) | |
1094 (and old-stack | |
1095 (calc-record-undo (list 'pop 1 (mapcar 'car old-stack)))) | |
1096 (let ((calc-stack old-stack-whole) | |
1097 (calc-stack-top 0)) | |
1098 (calc-cursor-stack-index (length old-stack))) | |
1099 (if (and (= old-buffer-size (buffer-size)) | |
1100 (= old-refresh-count calc-refresh-count)) | |
1101 (let ((buffer-read-only nil)) | |
1102 (delete-region (point) (point-max)) | |
1103 (while new-stack | |
1104 (calc-record-undo (list 'push 1)) | |
1105 (insert (math-format-stack-value (car new-stack)) "\n") | |
1106 (setq new-stack (cdr new-stack))) | |
1107 (calc-renumber-stack)) | |
1108 (while new-stack | |
1109 (calc-record-undo (list 'push 1)) | |
1110 (setq new-stack (cdr new-stack))) | |
1111 (calc-refresh)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1112 (calc-record-undo (list 'set 'saved-stack-top 0))))))))) |
40785 | 1113 |
1114 (defun calc-push-list-in-macro (vals m sels) | |
1115 (let ((entry (list (car vals) 1 (car sels))) | |
1116 (mm (+ (or m 1) calc-stack-top))) | |
1117 (if (> mm 1) | |
1118 (setcdr (nthcdr (- mm 2) calc-stack) | |
1119 (cons entry (nthcdr (1- mm) calc-stack))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1120 (setq calc-stack (cons entry calc-stack))))) |
40785 | 1121 |
1122 (defun calc-pop-stack-in-macro (n mm) | |
1123 (if (> mm 1) | |
1124 (setcdr (nthcdr (- mm 2) calc-stack) | |
1125 (nthcdr (+ n mm -1) calc-stack)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1126 (setq calc-stack (nthcdr n calc-stack)))) |
40785 | 1127 |
1128 | |
1129 (defun calc-kbd-if () | |
1130 (interactive) | |
1131 (calc-wrapper | |
1132 (let ((cond (calc-top-n 1))) | |
1133 (calc-pop-stack 1) | |
1134 (if (math-is-true cond) | |
1135 (if defining-kbd-macro | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1136 (message "If true..")) |
40785 | 1137 (if defining-kbd-macro |
1138 (message "Condition is false; skipping to Z: or Z] ...")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1139 (calc-kbd-skip-to-else-if t))))) |
40785 | 1140 |
1141 (defun calc-kbd-else-if () | |
1142 (interactive) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1143 (calc-kbd-if)) |
40785 | 1144 |
1145 (defun calc-kbd-skip-to-else-if (else-okay) | |
1146 (let ((count 0) | |
1147 ch) | |
1148 (while (>= count 0) | |
1149 (setq ch (read-char)) | |
1150 (if (= ch -1) | |
1151 (error "Unterminated Z[ in keyboard macro")) | |
1152 (if (= ch ?Z) | |
1153 (progn | |
1154 (setq ch (read-char)) | |
1155 (cond ((= ch ?\[) | |
1156 (setq count (1+ count))) | |
1157 ((= ch ?\]) | |
1158 (setq count (1- count))) | |
1159 ((= ch ?\:) | |
1160 (and (= count 0) | |
1161 else-okay | |
1162 (setq count -1))) | |
1163 ((eq ch 7) | |
1164 (keyboard-quit)))))) | |
1165 (and defining-kbd-macro | |
1166 (if (= ch ?\:) | |
1167 (message "Else...") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1168 (message "End-if..."))))) |
40785 | 1169 |
1170 (defun calc-kbd-end-if () | |
1171 (interactive) | |
1172 (if defining-kbd-macro | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1173 (message "End-if..."))) |
40785 | 1174 |
1175 (defun calc-kbd-else () | |
1176 (interactive) | |
1177 (if defining-kbd-macro | |
1178 (message "Else; skipping to Z] ...")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1179 (calc-kbd-skip-to-else-if nil)) |
40785 | 1180 |
1181 | |
1182 (defun calc-kbd-repeat () | |
1183 (interactive) | |
1184 (let (count) | |
1185 (calc-wrapper | |
1186 (setq count (math-trunc (calc-top-n 1))) | |
1187 (or (Math-integerp count) | |
1188 (error "Count must be an integer")) | |
1189 (if (Math-integer-negp count) | |
1190 (setq count 0)) | |
1191 (or (integerp count) | |
1192 (setq count 1000000)) | |
1193 (calc-pop-stack 1)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1194 (calc-kbd-loop count))) |
40785 | 1195 |
1196 (defun calc-kbd-for (dir) | |
1197 (interactive "P") | |
1198 (let (init final) | |
1199 (calc-wrapper | |
1200 (setq init (calc-top-n 2) | |
1201 final (calc-top-n 1)) | |
1202 (or (and (math-anglep init) (math-anglep final)) | |
1203 (error "Initial and final values must be real numbers")) | |
1204 (calc-pop-stack 2)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1205 (calc-kbd-loop nil init final (and dir (prefix-numeric-value dir))))) |
40785 | 1206 |
1207 (defun calc-kbd-loop (rpt-count &optional initial final dir) | |
1208 (interactive "P") | |
1209 (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000)) | |
1210 (let* ((count 0) | |
1211 (parts nil) | |
1212 (body "") | |
1213 (open last-command-char) | |
1214 (counter initial) | |
1215 ch) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
1216 (or executing-kbd-macro |
40785 | 1217 (message "Reading loop body...")) |
1218 (while (>= count 0) | |
1219 (setq ch (read-char)) | |
1220 (if (= ch -1) | |
1221 (error "Unterminated Z%c in keyboard macro" open)) | |
1222 (if (= ch ?Z) | |
1223 (progn | |
1224 (setq ch (read-char) | |
1225 body (concat body "Z" (char-to-string ch))) | |
1226 (cond ((memq ch '(?\< ?\( ?\{)) | |
1227 (setq count (1+ count))) | |
1228 ((memq ch '(?\> ?\) ?\})) | |
1229 (setq count (1- count))) | |
1230 ((and (= ch ?/) | |
1231 (= count 0)) | |
1232 (setq parts (nconc parts (list (concat (substring body 0 -2) | |
1233 "Z]"))) | |
1234 body "")) | |
1235 ((eq ch 7) | |
1236 (keyboard-quit)))) | |
1237 (setq body (concat body (char-to-string ch))))) | |
1238 (if (/= ch (cdr (assq open '( (?\< . ?\>) (?\( . ?\)) (?\{ . ?\}) )))) | |
1239 (error "Mismatched Z%c and Z%c in keyboard macro" open ch)) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
1240 (or executing-kbd-macro |
40785 | 1241 (message "Looping...")) |
1242 (setq body (concat (substring body 0 -2) "Z]")) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
1243 (and (not executing-kbd-macro) |
40785 | 1244 (= rpt-count 1000000) |
1245 (null parts) | |
1246 (null counter) | |
1247 (progn | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1248 (message "Warning: Infinite loop! Not executing") |
40785 | 1249 (setq rpt-count 0))) |
1250 (or (not initial) dir | |
1251 (setq dir (math-compare final initial))) | |
1252 (calc-wrapper | |
1253 (while (> rpt-count 0) | |
1254 (let ((part parts)) | |
1255 (if counter | |
1256 (if (cond ((eq dir 0) (Math-equal final counter)) | |
1257 ((eq dir 1) (Math-lessp final counter)) | |
1258 ((eq dir -1) (Math-lessp counter final))) | |
1259 (setq rpt-count 0) | |
1260 (calc-push counter))) | |
1261 (while (and part (> rpt-count 0)) | |
1262 (execute-kbd-macro (car part)) | |
1263 (if (math-is-true (calc-top-n 1)) | |
1264 (setq rpt-count 0) | |
1265 (setq part (cdr part))) | |
1266 (calc-pop-stack 1)) | |
1267 (if (> rpt-count 0) | |
1268 (progn | |
1269 (execute-kbd-macro body) | |
1270 (if counter | |
1271 (let ((step (calc-top-n 1))) | |
1272 (calc-pop-stack 1) | |
1273 (setq counter (calcFunc-add counter step))) | |
1274 (setq rpt-count (1- rpt-count)))))))) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
1275 (or executing-kbd-macro |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1276 (message "Looping...done")))) |
40785 | 1277 |
1278 (defun calc-kbd-end-repeat () | |
1279 (interactive) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1280 (error "Unbalanced Z> in keyboard macro")) |
40785 | 1281 |
1282 (defun calc-kbd-end-for () | |
1283 (interactive) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1284 (error "Unbalanced Z) in keyboard macro")) |
40785 | 1285 |
1286 (defun calc-kbd-end-loop () | |
1287 (interactive) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1288 (error "Unbalanced Z} in keyboard macro")) |
40785 | 1289 |
1290 (defun calc-kbd-break () | |
1291 (interactive) | |
1292 (calc-wrapper | |
1293 (let ((cond (calc-top-n 1))) | |
1294 (calc-pop-stack 1) | |
1295 (if (math-is-true cond) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1296 (error "Keyboard macro aborted"))))) |
40785 | 1297 |
1298 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1299 (defvar calc-kbd-push-level 0) |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1300 |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1301 ;; The variables var-q0 through var-q9 are the "quick" variables. |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1302 (defvar var-q0 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1303 (defvar var-q1 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1304 (defvar var-q2 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1305 (defvar var-q3 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1306 (defvar var-q4 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1307 (defvar var-q5 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1308 (defvar var-q6 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1309 (defvar var-q7 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1310 (defvar var-q8 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1311 (defvar var-q9 nil) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1312 |
40785 | 1313 (defun calc-kbd-push (arg) |
1314 (interactive "P") | |
1315 (calc-wrapper | |
1316 (let* ((defs (and arg (> (prefix-numeric-value arg) 0))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1317 (var-q0 var-q0) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1318 (var-q1 var-q1) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1319 (var-q2 var-q2) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1320 (var-q3 var-q3) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1321 (var-q4 var-q4) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1322 (var-q5 var-q5) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1323 (var-q6 var-q6) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1324 (var-q7 var-q7) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1325 (var-q8 var-q8) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1326 (var-q9 var-q9) |
40785 | 1327 (calc-internal-prec (if defs 12 calc-internal-prec)) |
1328 (calc-word-size (if defs 32 calc-word-size)) | |
1329 (calc-angle-mode (if defs 'deg calc-angle-mode)) | |
1330 (calc-simplify-mode (if defs nil calc-simplify-mode)) | |
1331 (calc-algebraic-mode (if arg nil calc-algebraic-mode)) | |
1332 (calc-incomplete-algebraic-mode (if arg nil | |
1333 calc-incomplete-algebraic-mode)) | |
1334 (calc-symbolic-mode (if defs nil calc-symbolic-mode)) | |
1335 (calc-matrix-mode (if defs nil calc-matrix-mode)) | |
1336 (calc-prefer-frac (if defs nil calc-prefer-frac)) | |
1337 (calc-complex-mode (if defs nil calc-complex-mode)) | |
1338 (calc-infinite-mode (if defs nil calc-infinite-mode)) | |
1339 (count 0) | |
1340 (body "") | |
1341 ch) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
1342 (if (or executing-kbd-macro defining-kbd-macro) |
40785 | 1343 (progn |
1344 (if defining-kbd-macro | |
1345 (message "Reading body...")) | |
1346 (while (>= count 0) | |
1347 (setq ch (read-char)) | |
1348 (if (= ch -1) | |
1349 (error "Unterminated Z` in keyboard macro")) | |
1350 (if (= ch ?Z) | |
1351 (progn | |
1352 (setq ch (read-char) | |
1353 body (concat body "Z" (char-to-string ch))) | |
1354 (cond ((eq ch ?\`) | |
1355 (setq count (1+ count))) | |
1356 ((eq ch ?\') | |
1357 (setq count (1- count))) | |
1358 ((eq ch 7) | |
1359 (keyboard-quit)))) | |
1360 (setq body (concat body (char-to-string ch))))) | |
1361 (if defining-kbd-macro | |
1362 (message "Reading body...done")) | |
1363 (let ((calc-kbd-push-level 0)) | |
1364 (execute-kbd-macro (substring body 0 -2)))) | |
1365 (let ((calc-kbd-push-level (1+ calc-kbd-push-level))) | |
1366 (message "Saving modes; type Z' to restore") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1367 (recursive-edit)))))) |
40785 | 1368 |
1369 (defun calc-kbd-pop () | |
1370 (interactive) | |
1371 (if (> calc-kbd-push-level 0) | |
1372 (progn | |
1373 (message "Mode settings restored") | |
1374 (exit-recursive-edit)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1375 (error "Unbalanced Z' in keyboard macro"))) |
40785 | 1376 |
1377 | |
1378 (defun calc-kbd-report (msg) | |
1379 (interactive "sMessage: ") | |
1380 (calc-wrapper | |
47697
34ce7f0515d0
(calc-kbd-report, calc-kbd-query): Don't bind `executing-kbd-macro'
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
1381 (math-working msg (calc-top-n 1)))) |
40785 | 1382 |
1383 (defun calc-kbd-query (msg) | |
1384 (interactive "sPrompt: ") | |
1385 (calc-wrapper | |
47697
34ce7f0515d0
(calc-kbd-report, calc-kbd-query): Don't bind `executing-kbd-macro'
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
1386 (calc-alg-entry nil (and (not (equal msg "")) msg)))) |
40785 | 1387 |
1388 ;;;; Logical operations. | |
1389 | |
1390 (defun calcFunc-eq (a b &rest more) | |
1391 (if more | |
1392 (let* ((args (cons a (cons b (copy-sequence more)))) | |
1393 (res 1) | |
1394 (p args) | |
1395 p2) | |
1396 (while (and (cdr p) (not (eq res 0))) | |
1397 (setq p2 p) | |
1398 (while (and (setq p2 (cdr p2)) (not (eq res 0))) | |
1399 (setq res (math-two-eq (car p) (car p2))) | |
1400 (if (eq res 1) | |
1401 (setcdr p (delq (car p2) (cdr p))))) | |
1402 (setq p (cdr p))) | |
1403 (if (eq res 0) | |
1404 0 | |
1405 (if (cdr args) | |
1406 (cons 'calcFunc-eq args) | |
1407 1))) | |
1408 (or (math-two-eq a b) | |
1409 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1410 (or (math-looks-negp b) (math-zerop b))) | |
1411 (list 'calcFunc-eq (math-neg a) (math-neg b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1412 (list 'calcFunc-eq a b))))) |
40785 | 1413 |
1414 (defun calcFunc-neq (a b &rest more) | |
1415 (if more | |
1416 (let* ((args (cons a (cons b more))) | |
1417 (res 0) | |
1418 (all t) | |
1419 (p args) | |
1420 p2) | |
1421 (while (and (cdr p) (not (eq res 1))) | |
1422 (setq p2 p) | |
1423 (while (and (setq p2 (cdr p2)) (not (eq res 1))) | |
1424 (setq res (math-two-eq (car p) (car p2))) | |
1425 (or res (setq all nil))) | |
1426 (setq p (cdr p))) | |
1427 (if (eq res 1) | |
1428 0 | |
1429 (if all | |
1430 1 | |
1431 (cons 'calcFunc-neq args)))) | |
1432 (or (cdr (assq (math-two-eq a b) '((0 . 1) (1 . 0)))) | |
1433 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1434 (or (math-looks-negp b) (math-zerop b))) | |
1435 (list 'calcFunc-neq (math-neg a) (math-neg b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1436 (list 'calcFunc-neq a b))))) |
40785 | 1437 |
1438 (defun math-two-eq (a b) | |
1439 (if (eq (car-safe a) 'vec) | |
1440 (if (eq (car-safe b) 'vec) | |
1441 (if (= (length a) (length b)) | |
1442 (let ((res 1)) | |
1443 (while (and (setq a (cdr a) b (cdr b)) (not (eq res 0))) | |
1444 (if res | |
1445 (setq res (math-two-eq (car a) (car b))) | |
1446 (if (eq (math-two-eq (car a) (car b)) 0) | |
1447 (setq res 0)))) | |
1448 res) | |
1449 0) | |
1450 (if (Math-objectp b) | |
1451 0 | |
1452 nil)) | |
1453 (if (eq (car-safe b) 'vec) | |
1454 (if (Math-objectp a) | |
1455 0 | |
1456 nil) | |
1457 (let ((res (math-compare a b))) | |
1458 (if (= res 0) | |
1459 1 | |
1460 (if (and (= res 2) (not (and (Math-scalarp a) (Math-scalarp b)))) | |
1461 nil | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1462 0)))))) |
40785 | 1463 |
1464 (defun calcFunc-lt (a b) | |
1465 (let ((res (math-compare a b))) | |
1466 (if (= res -1) | |
1467 1 | |
1468 (if (= res 2) | |
1469 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1470 (or (math-looks-negp b) (math-zerop b))) | |
1471 (list 'calcFunc-gt (math-neg a) (math-neg b)) | |
1472 (list 'calcFunc-lt a b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1473 0)))) |
40785 | 1474 |
1475 (defun calcFunc-gt (a b) | |
1476 (let ((res (math-compare a b))) | |
1477 (if (= res 1) | |
1478 1 | |
1479 (if (= res 2) | |
1480 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1481 (or (math-looks-negp b) (math-zerop b))) | |
1482 (list 'calcFunc-lt (math-neg a) (math-neg b)) | |
1483 (list 'calcFunc-gt a b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1484 0)))) |
40785 | 1485 |
1486 (defun calcFunc-leq (a b) | |
1487 (let ((res (math-compare a b))) | |
1488 (if (= res 1) | |
1489 0 | |
1490 (if (= res 2) | |
1491 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1492 (or (math-looks-negp b) (math-zerop b))) | |
1493 (list 'calcFunc-geq (math-neg a) (math-neg b)) | |
1494 (list 'calcFunc-leq a b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1495 1)))) |
40785 | 1496 |
1497 (defun calcFunc-geq (a b) | |
1498 (let ((res (math-compare a b))) | |
1499 (if (= res -1) | |
1500 0 | |
1501 (if (= res 2) | |
1502 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1503 (or (math-looks-negp b) (math-zerop b))) | |
1504 (list 'calcFunc-leq (math-neg a) (math-neg b)) | |
1505 (list 'calcFunc-geq a b)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1506 1)))) |
40785 | 1507 |
1508 (defun calcFunc-rmeq (a) | |
1509 (if (math-vectorp a) | |
1510 (math-map-vec 'calcFunc-rmeq a) | |
1511 (if (assq (car-safe a) calc-tweak-eqn-table) | |
1512 (if (and (eq (car-safe (nth 2 a)) 'var) | |
1513 (math-objectp (nth 1 a))) | |
1514 (nth 1 a) | |
1515 (nth 2 a)) | |
1516 (if (eq (car-safe a) 'calcFunc-assign) | |
1517 (nth 2 a) | |
1518 (if (eq (car-safe a) 'calcFunc-evalto) | |
1519 (nth 1 a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1520 (list 'calcFunc-rmeq a)))))) |
40785 | 1521 |
1522 (defun calcFunc-land (a b) | |
1523 (cond ((Math-zerop a) | |
1524 a) | |
1525 ((Math-zerop b) | |
1526 b) | |
1527 ((math-is-true a) | |
1528 b) | |
1529 ((math-is-true b) | |
1530 a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1531 (t (list 'calcFunc-land a b)))) |
40785 | 1532 |
1533 (defun calcFunc-lor (a b) | |
1534 (cond ((Math-zerop a) | |
1535 b) | |
1536 ((Math-zerop b) | |
1537 a) | |
1538 ((math-is-true a) | |
1539 a) | |
1540 ((math-is-true b) | |
1541 b) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1542 (t (list 'calcFunc-lor a b)))) |
40785 | 1543 |
1544 (defun calcFunc-lnot (a) | |
1545 (if (Math-zerop a) | |
1546 1 | |
1547 (if (math-is-true a) | |
1548 0 | |
1549 (let ((op (and (= (length a) 3) | |
1550 (assq (car a) calc-tweak-eqn-table)))) | |
1551 (if op | |
1552 (cons (nth 2 op) (cdr a)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1553 (list 'calcFunc-lnot a)))))) |
40785 | 1554 |
1555 (defun calcFunc-if (c e1 e2) | |
1556 (if (Math-zerop c) | |
1557 e2 | |
1558 (if (and (math-is-true c) (not (Math-vectorp c))) | |
1559 e1 | |
1560 (or (and (Math-vectorp c) | |
1561 (math-constp c) | |
1562 (let ((ee1 (if (Math-vectorp e1) | |
1563 (if (= (length c) (length e1)) | |
1564 (cdr e1) | |
1565 (calc-record-why "*Dimension error" e1)) | |
1566 (list e1))) | |
1567 (ee2 (if (Math-vectorp e2) | |
1568 (if (= (length c) (length e2)) | |
1569 (cdr e2) | |
1570 (calc-record-why "*Dimension error" e2)) | |
1571 (list e2)))) | |
1572 (and ee1 ee2 | |
1573 (cons 'vec (math-if-vector (cdr c) ee1 ee2))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1574 (list 'calcFunc-if c e1 e2))))) |
40785 | 1575 |
1576 (defun math-if-vector (c e1 e2) | |
1577 (and c | |
1578 (cons (if (Math-zerop (car c)) (car e2) (car e1)) | |
1579 (math-if-vector (cdr c) | |
1580 (or (cdr e1) e1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1581 (or (cdr e2) e2))))) |
40785 | 1582 |
1583 (defun math-normalize-logical-op (a) | |
1584 (or (and (eq (car a) 'calcFunc-if) | |
1585 (= (length a) 4) | |
1586 (let ((a1 (math-normalize (nth 1 a)))) | |
1587 (if (Math-zerop a1) | |
1588 (math-normalize (nth 3 a)) | |
1589 (if (Math-numberp a1) | |
1590 (math-normalize (nth 2 a)) | |
1591 (if (and (Math-vectorp (nth 1 a)) | |
1592 (math-constp (nth 1 a))) | |
1593 (calcFunc-if (nth 1 a) | |
1594 (math-normalize (nth 2 a)) | |
1595 (math-normalize (nth 3 a))) | |
1596 (let ((calc-simplify-mode 'none)) | |
1597 (list 'calcFunc-if a1 | |
1598 (math-normalize (nth 2 a)) | |
1599 (math-normalize (nth 3 a))))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1600 a)) |
40785 | 1601 |
1602 (defun calcFunc-in (a b) | |
1603 (or (and (eq (car-safe b) 'vec) | |
1604 (let ((bb b)) | |
1605 (while (and (setq bb (cdr bb)) | |
1606 (not (if (memq (car-safe (car bb)) '(vec intv)) | |
1607 (eq (calcFunc-in a (car bb)) 1) | |
1608 (Math-equal a (car bb)))))) | |
1609 (if bb 1 (and (math-constp a) (math-constp bb) 0)))) | |
1610 (and (eq (car-safe b) 'intv) | |
1611 (let ((res (math-compare a (nth 2 b))) res2) | |
1612 (cond ((= res -1) | |
1613 0) | |
1614 ((and (= res 0) | |
1615 (or (/= (nth 1 b) 2) | |
1616 (Math-lessp (nth 2 b) (nth 3 b)))) | |
1617 (if (memq (nth 1 b) '(2 3)) 1 0)) | |
1618 ((= (setq res2 (math-compare a (nth 3 b))) 1) | |
1619 0) | |
1620 ((and (= res2 0) | |
1621 (or (/= (nth 1 b) 1) | |
1622 (Math-lessp (nth 2 b) (nth 3 b)))) | |
1623 (if (memq (nth 1 b) '(1 3)) 1 0)) | |
1624 ((/= res 1) | |
1625 nil) | |
1626 ((/= res2 -1) | |
1627 nil) | |
1628 (t 1)))) | |
1629 (and (Math-equal a b) | |
1630 1) | |
1631 (and (math-constp a) (math-constp b) | |
1632 0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1633 (list 'calcFunc-in a b))) |
40785 | 1634 |
1635 (defun calcFunc-typeof (a) | |
1636 (cond ((Math-integerp a) 1) | |
1637 ((eq (car a) 'frac) 2) | |
1638 ((eq (car a) 'float) 3) | |
1639 ((eq (car a) 'hms) 4) | |
1640 ((eq (car a) 'cplx) 5) | |
1641 ((eq (car a) 'polar) 6) | |
1642 ((eq (car a) 'sdev) 7) | |
1643 ((eq (car a) 'intv) 8) | |
1644 ((eq (car a) 'mod) 9) | |
1645 ((eq (car a) 'date) (if (Math-integerp (nth 1 a)) 10 11)) | |
1646 ((eq (car a) 'var) | |
1647 (if (memq (nth 2 a) '(var-inf var-uinf var-nan)) 12 100)) | |
1648 ((eq (car a) 'vec) (if (math-matrixp a) 102 101)) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1649 (t (math-calcFunc-to-var (car a))))) |
40785 | 1650 |
1651 (defun calcFunc-integer (a) | |
1652 (if (Math-integerp a) | |
1653 1 | |
1654 (if (Math-objvecp a) | |
1655 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1656 (list 'calcFunc-integer a)))) |
40785 | 1657 |
1658 (defun calcFunc-real (a) | |
1659 (if (Math-realp a) | |
1660 1 | |
1661 (if (Math-objvecp a) | |
1662 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1663 (list 'calcFunc-real a)))) |
40785 | 1664 |
1665 (defun calcFunc-constant (a) | |
1666 (if (math-constp a) | |
1667 1 | |
1668 (if (Math-objvecp a) | |
1669 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1670 (list 'calcFunc-constant a)))) |
40785 | 1671 |
1672 (defun calcFunc-refers (a b) | |
1673 (if (math-expr-contains a b) | |
1674 1 | |
1675 (if (eq (car-safe a) 'var) | |
1676 (list 'calcFunc-refers a b) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1677 0))) |
40785 | 1678 |
1679 (defun calcFunc-negative (a) | |
1680 (if (math-looks-negp a) | |
1681 1 | |
1682 (if (or (math-zerop a) | |
1683 (math-posp a)) | |
1684 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1685 (list 'calcFunc-negative a)))) |
40785 | 1686 |
1687 (defun calcFunc-variable (a) | |
1688 (if (eq (car-safe a) 'var) | |
1689 1 | |
1690 (if (Math-objvecp a) | |
1691 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1692 (list 'calcFunc-variable a)))) |
40785 | 1693 |
1694 (defun calcFunc-nonvar (a) | |
1695 (if (eq (car-safe a) 'var) | |
1696 (list 'calcFunc-nonvar a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1697 1)) |
40785 | 1698 |
1699 (defun calcFunc-istrue (a) | |
1700 (if (math-is-true a) | |
1701 1 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1702 0)) |
40785 | 1703 |
1704 | |
1705 | |
1706 ;;;; User-programmability. | |
1707 | |
1708 ;;; Compiling Lisp-like forms to use the math library. | |
1709 | |
1710 (defun math-do-defmath (func args body) | |
58614
eba1cd703531
(calc-user-define-formula, calc-do-defmath): Replace calc-need-macros by
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58390
diff
changeset
|
1711 (require 'calc-macs) |
40785 | 1712 (let* ((fname (intern (concat "calcFunc-" (symbol-name func)))) |
1713 (doc (if (stringp (car body)) (list (car body)))) | |
1714 (clargs (mapcar 'math-clean-arg args)) | |
1715 (body (math-define-function-body | |
1716 (if (stringp (car body)) (cdr body) body) | |
1717 clargs))) | |
1718 (list 'progn | |
1719 (if (and (consp (car body)) | |
1720 (eq (car (car body)) 'interactive)) | |
1721 (let ((inter (car body))) | |
1722 (setq body (cdr body)) | |
1723 (if (or (> (length inter) 2) | |
1724 (integerp (nth 1 inter))) | |
1725 (let ((hasprefix nil) (hasmulti nil)) | |
1726 (if (stringp (nth 1 inter)) | |
1727 (progn | |
1728 (cond ((equal (nth 1 inter) "p") | |
1729 (setq hasprefix t)) | |
1730 ((equal (nth 1 inter) "m") | |
1731 (setq hasmulti t)) | |
1732 (t (error | |
1733 "Can't handle interactive code string \"%s\"" | |
1734 (nth 1 inter)))) | |
1735 (setq inter (cdr inter)))) | |
1736 (if (not (integerp (nth 1 inter))) | |
1737 (error | |
1738 "Expected an integer in interactive specification")) | |
1739 (append (list 'defun | |
1740 (intern (concat "calc-" | |
1741 (symbol-name func))) | |
1742 (if (or hasprefix hasmulti) | |
1743 '(&optional n) | |
1744 ())) | |
1745 doc | |
1746 (if (or hasprefix hasmulti) | |
1747 '((interactive "P")) | |
1748 '((interactive))) | |
1749 (list | |
1750 (append | |
1751 '(calc-slow-wrapper) | |
1752 (and hasmulti | |
1753 (list | |
1754 (list 'setq | |
1755 'n | |
1756 (list 'if | |
1757 'n | |
1758 (list 'prefix-numeric-value | |
1759 'n) | |
1760 (nth 1 inter))))) | |
1761 (list | |
1762 (list 'calc-enter-result | |
1763 (if hasmulti 'n (nth 1 inter)) | |
1764 (nth 2 inter) | |
1765 (if hasprefix | |
1766 (list 'append | |
1767 (list 'quote (list fname)) | |
1768 (list 'calc-top-list-n | |
1769 (nth 1 inter)) | |
1770 (list 'and | |
1771 'n | |
1772 (list | |
1773 'list | |
1774 (list | |
1775 'math-normalize | |
1776 (list | |
1777 'prefix-numeric-value | |
1778 'n))))) | |
1779 (list 'cons | |
1780 (list 'quote fname) | |
1781 (list 'calc-top-list-n | |
1782 (if hasmulti | |
1783 'n | |
1784 (nth 1 inter))))))))))) | |
1785 (append (list 'defun | |
1786 (intern (concat "calc-" (symbol-name func))) | |
1787 args) | |
1788 doc | |
1789 (list | |
1790 inter | |
1791 (cons 'calc-wrapper body)))))) | |
1792 (append (list 'defun fname clargs) | |
1793 doc | |
1794 (math-do-arg-list-check args nil nil) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1795 body)))) |
40785 | 1796 |
1797 (defun math-clean-arg (arg) | |
1798 (if (consp arg) | |
1799 (math-clean-arg (nth 1 arg)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1800 arg)) |
40785 | 1801 |
1802 (defun math-do-arg-check (arg var is-opt is-rest) | |
1803 (if is-opt | |
1804 (let ((chk (math-do-arg-check arg var nil nil))) | |
1805 (list (cons 'and | |
1806 (cons var | |
1807 (if (cdr chk) | |
1808 (setq chk (list (cons 'progn chk))) | |
1809 chk))))) | |
1810 (and (consp arg) | |
1811 (let* ((rest (math-do-arg-check (nth 1 arg) var is-opt is-rest)) | |
1812 (qual (car arg)) | |
1813 (qqual (list 'quote qual)) | |
1814 (qual-name (symbol-name qual)) | |
1815 (chk (intern (concat "math-check-" qual-name)))) | |
1816 (if (fboundp chk) | |
1817 (append rest | |
1818 (list | |
1819 (if is-rest | |
1820 (list 'setq var | |
1821 (list 'mapcar (list 'quote chk) var)) | |
1822 (list 'setq var (list chk var))))) | |
1823 (if (fboundp (setq chk (intern (concat "math-" qual-name)))) | |
1824 (append rest | |
1825 (list | |
1826 (if is-rest | |
1827 (list 'mapcar | |
1828 (list 'function | |
1829 (list 'lambda '(x) | |
1830 (list 'or | |
1831 (list chk 'x) | |
1832 (list 'math-reject-arg | |
1833 'x qqual)))) | |
1834 var) | |
1835 (list 'or | |
1836 (list chk var) | |
1837 (list 'math-reject-arg var qqual))))) | |
1838 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name) | |
1839 (fboundp (setq chk (intern | |
1840 (concat "math-" | |
1841 (math-match-substring | |
1842 qual-name 1)))))) | |
1843 (append rest | |
1844 (list | |
1845 (if is-rest | |
1846 (list 'mapcar | |
1847 (list 'function | |
1848 (list 'lambda '(x) | |
1849 (list 'and | |
1850 (list chk 'x) | |
1851 (list 'math-reject-arg | |
1852 'x qqual)))) | |
1853 var) | |
1854 (list 'and | |
1855 (list chk var) | |
1856 (list 'math-reject-arg var qqual))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1857 (error "Unknown qualifier `%s'" qual-name)))))))) |
40785 | 1858 |
1859 (defun math-do-arg-list-check (args is-opt is-rest) | |
1860 (cond ((null args) nil) | |
1861 ((consp (car args)) | |
1862 (append (math-do-arg-check (car args) | |
1863 (math-clean-arg (car args)) | |
1864 is-opt is-rest) | |
1865 (math-do-arg-list-check (cdr args) is-opt is-rest))) | |
1866 ((eq (car args) '&optional) | |
1867 (math-do-arg-list-check (cdr args) t nil)) | |
1868 ((eq (car args) '&rest) | |
1869 (math-do-arg-list-check (cdr args) nil t)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1870 (t (math-do-arg-list-check (cdr args) is-opt is-rest)))) |
40785 | 1871 |
1872 (defconst math-prim-funcs | |
1873 '( (~= . math-nearly-equal) | |
1874 (% . math-mod) | |
1875 (lsh . calcFunc-lsh) | |
1876 (ash . calcFunc-ash) | |
1877 (logand . calcFunc-and) | |
1878 (logandc2 . calcFunc-diff) | |
1879 (logior . calcFunc-or) | |
1880 (logxor . calcFunc-xor) | |
1881 (lognot . calcFunc-not) | |
1882 (equal . equal) ; need to leave these ones alone! | |
1883 (eq . eq) | |
1884 (and . and) | |
1885 (or . or) | |
1886 (if . if) | |
1887 (^ . math-pow) | |
1888 (expt . math-pow) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1889 )) |
40785 | 1890 |
1891 (defconst math-prim-vars | |
1892 '( (nil . nil) | |
1893 (t . t) | |
1894 (&optional . &optional) | |
1895 (&rest . &rest) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1896 )) |
40785 | 1897 |
1898 (defun math-define-function-body (body env) | |
1899 (let ((body (math-define-body body env))) | |
1900 (if (math-body-refers-to body 'math-return) | |
1901 (list (cons 'catch (cons '(quote math-return) body))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1902 body))) |
40785 | 1903 |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1904 ;; The variable math-exp-env is local to math-define-body, but is |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1905 ;; used by math-define-exp, which is called (indirectly) by |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1906 ;; by math-define-body. |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1907 (defvar math-exp-env) |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1908 |
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1909 (defun math-define-body (body math-exp-env) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1910 (math-define-list body)) |
40785 | 1911 |
1912 (defun math-define-list (body &optional quote) | |
1913 (cond ((null body) | |
1914 nil) | |
1915 ((and (eq (car body) ':) | |
1916 (stringp (nth 1 body))) | |
1917 (cons (let* ((math-read-expr-quotes t) | |
1918 (exp (math-read-plain-expr (nth 1 body) t))) | |
1919 (math-define-exp exp)) | |
1920 (math-define-list (cdr (cdr body))))) | |
1921 (quote | |
1922 (cons (cond ((consp (car body)) | |
1923 (math-define-list (cdr body) t)) | |
1924 (t | |
1925 (car body))) | |
1926 (math-define-list (cdr body)))) | |
1927 (t | |
1928 (cons (math-define-exp (car body)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1929 (math-define-list (cdr body)))))) |
40785 | 1930 |
1931 (defun math-define-exp (exp) | |
1932 (cond ((consp exp) | |
1933 (let ((func (car exp))) | |
1934 (cond ((memq func '(quote function)) | |
1935 (if (and (consp (nth 1 exp)) | |
1936 (eq (car (nth 1 exp)) 'lambda)) | |
1937 (cons 'quote | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1938 (math-define-lambda (nth 1 exp) math-exp-env)) |
40785 | 1939 exp)) |
1940 ((memq func '(let let* for foreach)) | |
1941 (let ((head (nth 1 exp)) | |
1942 (body (cdr (cdr exp)))) | |
1943 (if (memq func '(let let*)) | |
1944 () | |
1945 (setq func (cdr (assq func '((for . math-for) | |
1946 (foreach . math-foreach))))) | |
1947 (if (not (listp (car head))) | |
1948 (setq head (list head)))) | |
1949 (macroexpand | |
1950 (cons func | |
1951 (cons (math-define-let head) | |
1952 (math-define-body body | |
1953 (nconc | |
1954 (math-define-let-env head) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1955 math-exp-env))))))) |
40785 | 1956 ((and (memq func '(setq setf)) |
1957 (math-complicated-lhs (cdr exp))) | |
1958 (if (> (length exp) 3) | |
1959 (cons 'progn (math-define-setf-list (cdr exp))) | |
1960 (math-define-setf (nth 1 exp) (nth 2 exp)))) | |
1961 ((eq func 'condition-case) | |
1962 (cons func | |
1963 (cons (nth 1 exp) | |
1964 (math-define-body (cdr (cdr exp)) | |
1965 (cons (nth 1 exp) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
1966 math-exp-env))))) |
40785 | 1967 ((eq func 'cond) |
1968 (cons func | |
1969 (math-define-cond (cdr exp)))) | |
1970 ((and (consp func) ; ('spam a b) == force use of plain spam | |
1971 (eq (car func) 'quote)) | |
1972 (cons func (math-define-list (cdr exp)))) | |
1973 ((symbolp func) | |
1974 (let ((args (math-define-list (cdr exp))) | |
1975 (prim (assq func math-prim-funcs))) | |
1976 (cond (prim | |
1977 (cons (cdr prim) args)) | |
1978 ((eq func 'floatp) | |
1979 (list 'eq (car args) '(quote float))) | |
1980 ((eq func '+) | |
1981 (math-define-binop 'math-add 0 | |
1982 (car args) (cdr args))) | |
1983 ((eq func '-) | |
1984 (if (= (length args) 1) | |
1985 (cons 'math-neg args) | |
1986 (math-define-binop 'math-sub 0 | |
1987 (car args) (cdr args)))) | |
1988 ((eq func '*) | |
1989 (math-define-binop 'math-mul 1 | |
1990 (car args) (cdr args))) | |
1991 ((eq func '/) | |
1992 (math-define-binop 'math-div 1 | |
1993 (car args) (cdr args))) | |
1994 ((eq func 'min) | |
1995 (math-define-binop 'math-min 0 | |
1996 (car args) (cdr args))) | |
1997 ((eq func 'max) | |
1998 (math-define-binop 'math-max 0 | |
1999 (car args) (cdr args))) | |
2000 ((eq func '<) | |
2001 (if (and (math-numberp (nth 1 args)) | |
2002 (math-zerop (nth 1 args))) | |
2003 (list 'math-negp (car args)) | |
2004 (cons 'math-lessp args))) | |
2005 ((eq func '>) | |
2006 (if (and (math-numberp (nth 1 args)) | |
2007 (math-zerop (nth 1 args))) | |
2008 (list 'math-posp (car args)) | |
2009 (list 'math-lessp (nth 1 args) (nth 0 args)))) | |
2010 ((eq func '<=) | |
2011 (list 'not | |
2012 (if (and (math-numberp (nth 1 args)) | |
2013 (math-zerop (nth 1 args))) | |
2014 (list 'math-posp (car args)) | |
2015 (list 'math-lessp | |
2016 (nth 1 args) (nth 0 args))))) | |
2017 ((eq func '>=) | |
2018 (list 'not | |
2019 (if (and (math-numberp (nth 1 args)) | |
2020 (math-zerop (nth 1 args))) | |
2021 (list 'math-negp (car args)) | |
2022 (cons 'math-lessp args)))) | |
2023 ((eq func '=) | |
2024 (if (and (math-numberp (nth 1 args)) | |
2025 (math-zerop (nth 1 args))) | |
2026 (list 'math-zerop (nth 0 args)) | |
2027 (if (and (integerp (nth 1 args)) | |
2028 (/= (% (nth 1 args) 10) 0)) | |
2029 (cons 'math-equal-int args) | |
2030 (cons 'math-equal args)))) | |
2031 ((eq func '/=) | |
2032 (list 'not | |
2033 (if (and (math-numberp (nth 1 args)) | |
2034 (math-zerop (nth 1 args))) | |
2035 (list 'math-zerop (nth 0 args)) | |
2036 (if (and (integerp (nth 1 args)) | |
2037 (/= (% (nth 1 args) 10) 0)) | |
2038 (cons 'math-equal-int args) | |
2039 (cons 'math-equal args))))) | |
2040 ((eq func '1+) | |
2041 (list 'math-add (car args) 1)) | |
2042 ((eq func '1-) | |
2043 (list 'math-add (car args) -1)) | |
2044 ((eq func 'not) ; optimize (not (not x)) => x | |
2045 (if (eq (car-safe args) func) | |
2046 (car (nth 1 args)) | |
2047 (cons func args))) | |
2048 ((and (eq func 'elt) (cdr (cdr args))) | |
2049 (math-define-elt (car args) (cdr args))) | |
2050 (t | |
2051 (macroexpand | |
2052 (let* ((name (symbol-name func)) | |
2053 (cfunc (intern (concat "calcFunc-" name))) | |
2054 (mfunc (intern (concat "math-" name)))) | |
2055 (cond ((fboundp cfunc) | |
2056 (cons cfunc args)) | |
2057 ((fboundp mfunc) | |
2058 (cons mfunc args)) | |
2059 ((or (fboundp func) | |
2060 (string-match "\\`calcFunc-.*" name)) | |
2061 (cons func args)) | |
2062 (t | |
2063 (cons cfunc args))))))))) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2064 (t (cons func (math-define-list (cdr exp))))))) ;;args |
40785 | 2065 ((symbolp exp) |
2066 (let ((prim (assq exp math-prim-vars)) | |
2067 (name (symbol-name exp))) | |
2068 (cond (prim | |
2069 (cdr prim)) | |
58390
2c8f55b9ef8a
(math-integral-cache-state, calc-lang)
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
2070 ((memq exp math-exp-env) |
40785 | 2071 exp) |
2072 ((string-match "-" name) | |
2073 exp) | |
2074 (t | |
2075 (intern (concat "var-" name)))))) | |
2076 ((integerp exp) | |
2077 (if (or (<= exp -1000000) (>= exp 1000000)) | |
2078 (list 'quote (math-normalize exp)) | |
2079 exp)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2080 (t exp))) |
40785 | 2081 |
2082 (defun math-define-cond (forms) | |
2083 (and forms | |
2084 (cons (math-define-list (car forms)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2085 (math-define-cond (cdr forms))))) |
40785 | 2086 |
2087 (defun math-complicated-lhs (body) | |
2088 (and body | |
2089 (or (not (symbolp (car body))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2090 (math-complicated-lhs (cdr (cdr body)))))) |
40785 | 2091 |
2092 (defun math-define-setf-list (body) | |
2093 (and body | |
2094 (cons (math-define-setf (nth 0 body) (nth 1 body)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2095 (math-define-setf-list (cdr (cdr body)))))) |
40785 | 2096 |
2097 (defun math-define-setf (place value) | |
2098 (setq place (math-define-exp place) | |
2099 value (math-define-exp value)) | |
2100 (cond ((symbolp place) | |
2101 (list 'setq place value)) | |
2102 ((eq (car-safe place) 'nth) | |
2103 (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value)) | |
2104 ((eq (car-safe place) 'elt) | |
2105 (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value)) | |
2106 ((eq (car-safe place) 'car) | |
2107 (list 'setcar (nth 1 place) value)) | |
2108 ((eq (car-safe place) 'cdr) | |
2109 (list 'setcdr (nth 1 place) value)) | |
2110 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2111 (error "Bad place form for setf: %s" place)))) |
40785 | 2112 |
2113 (defun math-define-binop (op ident arg1 rest) | |
2114 (if rest | |
2115 (math-define-binop op ident | |
2116 (list op arg1 (car rest)) | |
2117 (cdr rest)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2118 (or arg1 ident))) |
40785 | 2119 |
2120 (defun math-define-let (vlist) | |
2121 (and vlist | |
2122 (cons (if (consp (car vlist)) | |
2123 (cons (car (car vlist)) | |
2124 (math-define-list (cdr (car vlist)))) | |
2125 (car vlist)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2126 (math-define-let (cdr vlist))))) |
40785 | 2127 |
2128 (defun math-define-let-env (vlist) | |
2129 (and vlist | |
2130 (cons (if (consp (car vlist)) | |
2131 (car (car vlist)) | |
2132 (car vlist)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2133 (math-define-let-env (cdr vlist))))) |
40785 | 2134 |
2135 (defun math-define-lambda (exp exp-env) | |
2136 (nconc (list (nth 0 exp) ; 'lambda | |
2137 (nth 1 exp)) ; arg list | |
2138 (math-define-function-body (cdr (cdr exp)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2139 (append (nth 1 exp) exp-env)))) |
40785 | 2140 |
2141 (defun math-define-elt (seq idx) | |
2142 (if idx | |
2143 (math-define-elt (list 'elt seq (car idx)) (cdr idx)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2144 seq)) |
40785 | 2145 |
2146 | |
2147 | |
2148 ;;; Useful programming macros. | |
2149 | |
2150 (defmacro math-while (head &rest body) | |
2151 (let ((body (cons 'while (cons head body)))) | |
2152 (if (math-body-refers-to body 'math-break) | |
2153 (cons 'catch (cons '(quote math-break) (list body))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2154 body))) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2155 ;; (put 'math-while 'lisp-indent-hook 1) |
40785 | 2156 |
2157 (defmacro math-for (head &rest body) | |
2158 (let ((body (if head | |
2159 (math-handle-for head body) | |
2160 (cons 'while (cons t body))))) | |
2161 (if (math-body-refers-to body 'math-break) | |
2162 (cons 'catch (cons '(quote math-break) (list body))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2163 body))) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2164 ;; (put 'math-for 'lisp-indent-hook 1) |
40785 | 2165 |
2166 (defun math-handle-for (head body) | |
2167 (let* ((var (nth 0 (car head))) | |
2168 (init (nth 1 (car head))) | |
2169 (limit (nth 2 (car head))) | |
2170 (step (or (nth 3 (car head)) 1)) | |
2171 (body (if (cdr head) | |
2172 (list (math-handle-for (cdr head) body)) | |
2173 body)) | |
2174 (all-ints (and (integerp init) (integerp limit) (integerp step))) | |
2175 (const-limit (or (integerp limit) | |
2176 (and (eq (car-safe limit) 'quote) | |
2177 (math-realp (nth 1 limit))))) | |
2178 (const-step (or (integerp step) | |
2179 (and (eq (car-safe step) 'quote) | |
2180 (math-realp (nth 1 step))))) | |
2181 (save-limit (if const-limit limit (make-symbol "<limit>"))) | |
2182 (save-step (if const-step step (make-symbol "<step>")))) | |
2183 (cons 'let | |
2184 (cons (append (if const-limit nil (list (list save-limit limit))) | |
2185 (if const-step nil (list (list save-step step))) | |
2186 (list (list var init))) | |
2187 (list | |
2188 (cons 'while | |
2189 (cons (if all-ints | |
2190 (if (> step 0) | |
2191 (list '<= var save-limit) | |
2192 (list '>= var save-limit)) | |
2193 (list 'not | |
2194 (if const-step | |
2195 (if (or (math-posp step) | |
2196 (math-posp | |
2197 (cdr-safe step))) | |
2198 (list 'math-lessp | |
2199 save-limit | |
2200 var) | |
2201 (list 'math-lessp | |
2202 var | |
2203 save-limit)) | |
2204 (list 'if | |
2205 (list 'math-posp | |
2206 save-step) | |
2207 (list 'math-lessp | |
2208 save-limit | |
2209 var) | |
2210 (list 'math-lessp | |
2211 var | |
2212 save-limit))))) | |
2213 (append body | |
2214 (list (list 'setq | |
2215 var | |
2216 (list (if all-ints | |
2217 '+ | |
2218 'math-add) | |
2219 var | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2220 save-step))))))))))) |
40785 | 2221 |
2222 (defmacro math-foreach (head &rest body) | |
2223 (let ((body (math-handle-foreach head body))) | |
2224 (if (math-body-refers-to body 'math-break) | |
2225 (cons 'catch (cons '(quote math-break) (list body))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2226 body))) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2227 ;; (put 'math-foreach 'lisp-indent-hook 1) |
40785 | 2228 |
2229 (defun math-handle-foreach (head body) | |
2230 (let ((var (nth 0 (car head))) | |
2231 (data (nth 1 (car head))) | |
2232 (body (if (cdr head) | |
2233 (list (math-handle-foreach (cdr head) body)) | |
2234 body))) | |
2235 (cons 'let | |
2236 (cons (list (list var data)) | |
2237 (list | |
2238 (cons 'while | |
2239 (cons var | |
2240 (append body | |
2241 (list (list 'setq | |
2242 var | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2243 (list 'cdr var))))))))))) |
40785 | 2244 |
2245 | |
2246 (defun math-body-refers-to (body thing) | |
2247 (or (equal body thing) | |
2248 (and (consp body) | |
2249 (or (math-body-refers-to (car body) thing) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2250 (math-body-refers-to (cdr body) thing))))) |
40785 | 2251 |
2252 (defun math-break (&optional value) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2253 (throw 'math-break value)) |
40785 | 2254 |
2255 (defun math-return (&optional value) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2256 (throw 'math-return value)) |
40785 | 2257 |
2258 | |
2259 | |
2260 | |
2261 | |
2262 (defun math-composite-inequalities (x op) | |
2263 (if (memq (nth 1 op) '(calcFunc-eq calcFunc-neq)) | |
2264 (if (eq (car x) (nth 1 op)) | |
2265 (append x (list (math-read-expr-level (nth 3 op)))) | |
2266 (throw 'syntax "Syntax error")) | |
2267 (list 'calcFunc-in | |
2268 (nth 2 x) | |
2269 (if (memq (nth 1 op) '(calcFunc-lt calcFunc-leq)) | |
2270 (if (memq (car x) '(calcFunc-lt calcFunc-leq)) | |
2271 (math-make-intv | |
2272 (+ (if (eq (car x) 'calcFunc-leq) 2 0) | |
2273 (if (eq (nth 1 op) 'calcFunc-leq) 1 0)) | |
2274 (nth 1 x) (math-read-expr-level (nth 3 op))) | |
2275 (throw 'syntax "Syntax error")) | |
2276 (if (memq (car x) '(calcFunc-gt calcFunc-geq)) | |
2277 (math-make-intv | |
2278 (+ (if (eq (nth 1 op) 'calcFunc-geq) 2 0) | |
2279 (if (eq (car x) 'calcFunc-geq) 1 0)) | |
2280 (math-read-expr-level (nth 3 op)) (nth 1 x)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2281 (throw 'syntax "Syntax error")))))) |
40785 | 2282 |
58668
827d00badeb5
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58614
diff
changeset
|
2283 (provide 'calc-prog) |
827d00badeb5
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58614
diff
changeset
|
2284 |
52401 | 2285 ;;; arch-tag: 4c5a183b-c9e5-4632-bb3f-e41a764518b0 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2286 ;;; calc-prog.el ends here |