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