Mercurial > emacs
annotate lisp/calc/calc-prog.el @ 43956:858065a2176d
(codepage-setup): Don't define a codepage if it is already defined.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sat, 16 Mar 2002 16:42:05 +0000 |
parents | fcd507927105 |
children | 34ce7f0515d0 |
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 | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
1347 (let ((executing-kbd-macro nil) |
40785 | 1348 (defining-kbd-macro nil)) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1349 (math-working msg (calc-top-n 1))))) |
40785 | 1350 |
1351 (defun calc-kbd-query (msg) | |
1352 (interactive "sPrompt: ") | |
1353 (calc-wrapper | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
1354 (let ((executing-kbd-macro nil) |
40785 | 1355 (defining-kbd-macro nil)) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1356 (calc-alg-entry nil (and (not (equal msg "")) msg))))) |
40785 | 1357 |
1358 | |
1359 | |
1360 | |
1361 | |
1362 | |
1363 | |
1364 ;;;; Logical operations. | |
1365 | |
1366 (defun calcFunc-eq (a b &rest more) | |
1367 (if more | |
1368 (let* ((args (cons a (cons b (copy-sequence more)))) | |
1369 (res 1) | |
1370 (p args) | |
1371 p2) | |
1372 (while (and (cdr p) (not (eq res 0))) | |
1373 (setq p2 p) | |
1374 (while (and (setq p2 (cdr p2)) (not (eq res 0))) | |
1375 (setq res (math-two-eq (car p) (car p2))) | |
1376 (if (eq res 1) | |
1377 (setcdr p (delq (car p2) (cdr p))))) | |
1378 (setq p (cdr p))) | |
1379 (if (eq res 0) | |
1380 0 | |
1381 (if (cdr args) | |
1382 (cons 'calcFunc-eq args) | |
1383 1))) | |
1384 (or (math-two-eq a b) | |
1385 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1386 (or (math-looks-negp b) (math-zerop b))) | |
1387 (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
|
1388 (list 'calcFunc-eq a b))))) |
40785 | 1389 |
1390 (defun calcFunc-neq (a b &rest more) | |
1391 (if more | |
1392 (let* ((args (cons a (cons b more))) | |
1393 (res 0) | |
1394 (all t) | |
1395 (p args) | |
1396 p2) | |
1397 (while (and (cdr p) (not (eq res 1))) | |
1398 (setq p2 p) | |
1399 (while (and (setq p2 (cdr p2)) (not (eq res 1))) | |
1400 (setq res (math-two-eq (car p) (car p2))) | |
1401 (or res (setq all nil))) | |
1402 (setq p (cdr p))) | |
1403 (if (eq res 1) | |
1404 0 | |
1405 (if all | |
1406 1 | |
1407 (cons 'calcFunc-neq args)))) | |
1408 (or (cdr (assq (math-two-eq a b) '((0 . 1) (1 . 0)))) | |
1409 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1410 (or (math-looks-negp b) (math-zerop b))) | |
1411 (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
|
1412 (list 'calcFunc-neq a b))))) |
40785 | 1413 |
1414 (defun math-two-eq (a b) | |
1415 (if (eq (car-safe a) 'vec) | |
1416 (if (eq (car-safe b) 'vec) | |
1417 (if (= (length a) (length b)) | |
1418 (let ((res 1)) | |
1419 (while (and (setq a (cdr a) b (cdr b)) (not (eq res 0))) | |
1420 (if res | |
1421 (setq res (math-two-eq (car a) (car b))) | |
1422 (if (eq (math-two-eq (car a) (car b)) 0) | |
1423 (setq res 0)))) | |
1424 res) | |
1425 0) | |
1426 (if (Math-objectp b) | |
1427 0 | |
1428 nil)) | |
1429 (if (eq (car-safe b) 'vec) | |
1430 (if (Math-objectp a) | |
1431 0 | |
1432 nil) | |
1433 (let ((res (math-compare a b))) | |
1434 (if (= res 0) | |
1435 1 | |
1436 (if (and (= res 2) (not (and (Math-scalarp a) (Math-scalarp b)))) | |
1437 nil | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1438 0)))))) |
40785 | 1439 |
1440 (defun calcFunc-lt (a b) | |
1441 (let ((res (math-compare a b))) | |
1442 (if (= res -1) | |
1443 1 | |
1444 (if (= res 2) | |
1445 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1446 (or (math-looks-negp b) (math-zerop b))) | |
1447 (list 'calcFunc-gt (math-neg a) (math-neg b)) | |
1448 (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
|
1449 0)))) |
40785 | 1450 |
1451 (defun calcFunc-gt (a b) | |
1452 (let ((res (math-compare a b))) | |
1453 (if (= res 1) | |
1454 1 | |
1455 (if (= res 2) | |
1456 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1457 (or (math-looks-negp b) (math-zerop b))) | |
1458 (list 'calcFunc-lt (math-neg a) (math-neg b)) | |
1459 (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
|
1460 0)))) |
40785 | 1461 |
1462 (defun calcFunc-leq (a b) | |
1463 (let ((res (math-compare a b))) | |
1464 (if (= res 1) | |
1465 0 | |
1466 (if (= res 2) | |
1467 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1468 (or (math-looks-negp b) (math-zerop b))) | |
1469 (list 'calcFunc-geq (math-neg a) (math-neg b)) | |
1470 (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
|
1471 1)))) |
40785 | 1472 |
1473 (defun calcFunc-geq (a b) | |
1474 (let ((res (math-compare a b))) | |
1475 (if (= res -1) | |
1476 0 | |
1477 (if (= res 2) | |
1478 (if (and (or (math-looks-negp a) (math-zerop a)) | |
1479 (or (math-looks-negp b) (math-zerop b))) | |
1480 (list 'calcFunc-leq (math-neg a) (math-neg b)) | |
1481 (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
|
1482 1)))) |
40785 | 1483 |
1484 (defun calcFunc-rmeq (a) | |
1485 (if (math-vectorp a) | |
1486 (math-map-vec 'calcFunc-rmeq a) | |
1487 (if (assq (car-safe a) calc-tweak-eqn-table) | |
1488 (if (and (eq (car-safe (nth 2 a)) 'var) | |
1489 (math-objectp (nth 1 a))) | |
1490 (nth 1 a) | |
1491 (nth 2 a)) | |
1492 (if (eq (car-safe a) 'calcFunc-assign) | |
1493 (nth 2 a) | |
1494 (if (eq (car-safe a) 'calcFunc-evalto) | |
1495 (nth 1 a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1496 (list 'calcFunc-rmeq a)))))) |
40785 | 1497 |
1498 (defun calcFunc-land (a b) | |
1499 (cond ((Math-zerop a) | |
1500 a) | |
1501 ((Math-zerop b) | |
1502 b) | |
1503 ((math-is-true a) | |
1504 b) | |
1505 ((math-is-true b) | |
1506 a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1507 (t (list 'calcFunc-land a b)))) |
40785 | 1508 |
1509 (defun calcFunc-lor (a b) | |
1510 (cond ((Math-zerop a) | |
1511 b) | |
1512 ((Math-zerop b) | |
1513 a) | |
1514 ((math-is-true a) | |
1515 a) | |
1516 ((math-is-true b) | |
1517 b) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1518 (t (list 'calcFunc-lor a b)))) |
40785 | 1519 |
1520 (defun calcFunc-lnot (a) | |
1521 (if (Math-zerop a) | |
1522 1 | |
1523 (if (math-is-true a) | |
1524 0 | |
1525 (let ((op (and (= (length a) 3) | |
1526 (assq (car a) calc-tweak-eqn-table)))) | |
1527 (if op | |
1528 (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
|
1529 (list 'calcFunc-lnot a)))))) |
40785 | 1530 |
1531 (defun calcFunc-if (c e1 e2) | |
1532 (if (Math-zerop c) | |
1533 e2 | |
1534 (if (and (math-is-true c) (not (Math-vectorp c))) | |
1535 e1 | |
1536 (or (and (Math-vectorp c) | |
1537 (math-constp c) | |
1538 (let ((ee1 (if (Math-vectorp e1) | |
1539 (if (= (length c) (length e1)) | |
1540 (cdr e1) | |
1541 (calc-record-why "*Dimension error" e1)) | |
1542 (list e1))) | |
1543 (ee2 (if (Math-vectorp e2) | |
1544 (if (= (length c) (length e2)) | |
1545 (cdr e2) | |
1546 (calc-record-why "*Dimension error" e2)) | |
1547 (list e2)))) | |
1548 (and ee1 ee2 | |
1549 (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
|
1550 (list 'calcFunc-if c e1 e2))))) |
40785 | 1551 |
1552 (defun math-if-vector (c e1 e2) | |
1553 (and c | |
1554 (cons (if (Math-zerop (car c)) (car e2) (car e1)) | |
1555 (math-if-vector (cdr c) | |
1556 (or (cdr e1) e1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1557 (or (cdr e2) e2))))) |
40785 | 1558 |
1559 (defun math-normalize-logical-op (a) | |
1560 (or (and (eq (car a) 'calcFunc-if) | |
1561 (= (length a) 4) | |
1562 (let ((a1 (math-normalize (nth 1 a)))) | |
1563 (if (Math-zerop a1) | |
1564 (math-normalize (nth 3 a)) | |
1565 (if (Math-numberp a1) | |
1566 (math-normalize (nth 2 a)) | |
1567 (if (and (Math-vectorp (nth 1 a)) | |
1568 (math-constp (nth 1 a))) | |
1569 (calcFunc-if (nth 1 a) | |
1570 (math-normalize (nth 2 a)) | |
1571 (math-normalize (nth 3 a))) | |
1572 (let ((calc-simplify-mode 'none)) | |
1573 (list 'calcFunc-if a1 | |
1574 (math-normalize (nth 2 a)) | |
1575 (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
|
1576 a)) |
40785 | 1577 |
1578 (defun calcFunc-in (a b) | |
1579 (or (and (eq (car-safe b) 'vec) | |
1580 (let ((bb b)) | |
1581 (while (and (setq bb (cdr bb)) | |
1582 (not (if (memq (car-safe (car bb)) '(vec intv)) | |
1583 (eq (calcFunc-in a (car bb)) 1) | |
1584 (Math-equal a (car bb)))))) | |
1585 (if bb 1 (and (math-constp a) (math-constp bb) 0)))) | |
1586 (and (eq (car-safe b) 'intv) | |
1587 (let ((res (math-compare a (nth 2 b))) res2) | |
1588 (cond ((= res -1) | |
1589 0) | |
1590 ((and (= res 0) | |
1591 (or (/= (nth 1 b) 2) | |
1592 (Math-lessp (nth 2 b) (nth 3 b)))) | |
1593 (if (memq (nth 1 b) '(2 3)) 1 0)) | |
1594 ((= (setq res2 (math-compare a (nth 3 b))) 1) | |
1595 0) | |
1596 ((and (= res2 0) | |
1597 (or (/= (nth 1 b) 1) | |
1598 (Math-lessp (nth 2 b) (nth 3 b)))) | |
1599 (if (memq (nth 1 b) '(1 3)) 1 0)) | |
1600 ((/= res 1) | |
1601 nil) | |
1602 ((/= res2 -1) | |
1603 nil) | |
1604 (t 1)))) | |
1605 (and (Math-equal a b) | |
1606 1) | |
1607 (and (math-constp a) (math-constp b) | |
1608 0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1609 (list 'calcFunc-in a b))) |
40785 | 1610 |
1611 (defun calcFunc-typeof (a) | |
1612 (cond ((Math-integerp a) 1) | |
1613 ((eq (car a) 'frac) 2) | |
1614 ((eq (car a) 'float) 3) | |
1615 ((eq (car a) 'hms) 4) | |
1616 ((eq (car a) 'cplx) 5) | |
1617 ((eq (car a) 'polar) 6) | |
1618 ((eq (car a) 'sdev) 7) | |
1619 ((eq (car a) 'intv) 8) | |
1620 ((eq (car a) 'mod) 9) | |
1621 ((eq (car a) 'date) (if (Math-integerp (nth 1 a)) 10 11)) | |
1622 ((eq (car a) 'var) | |
1623 (if (memq (nth 2 a) '(var-inf var-uinf var-nan)) 12 100)) | |
1624 ((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
|
1625 (t (math-calcFunc-to-var func)))) |
40785 | 1626 |
1627 (defun calcFunc-integer (a) | |
1628 (if (Math-integerp a) | |
1629 1 | |
1630 (if (Math-objvecp a) | |
1631 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1632 (list 'calcFunc-integer a)))) |
40785 | 1633 |
1634 (defun calcFunc-real (a) | |
1635 (if (Math-realp a) | |
1636 1 | |
1637 (if (Math-objvecp a) | |
1638 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1639 (list 'calcFunc-real a)))) |
40785 | 1640 |
1641 (defun calcFunc-constant (a) | |
1642 (if (math-constp a) | |
1643 1 | |
1644 (if (Math-objvecp a) | |
1645 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1646 (list 'calcFunc-constant a)))) |
40785 | 1647 |
1648 (defun calcFunc-refers (a b) | |
1649 (if (math-expr-contains a b) | |
1650 1 | |
1651 (if (eq (car-safe a) 'var) | |
1652 (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
|
1653 0))) |
40785 | 1654 |
1655 (defun calcFunc-negative (a) | |
1656 (if (math-looks-negp a) | |
1657 1 | |
1658 (if (or (math-zerop a) | |
1659 (math-posp a)) | |
1660 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1661 (list 'calcFunc-negative a)))) |
40785 | 1662 |
1663 (defun calcFunc-variable (a) | |
1664 (if (eq (car-safe a) 'var) | |
1665 1 | |
1666 (if (Math-objvecp a) | |
1667 0 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1668 (list 'calcFunc-variable a)))) |
40785 | 1669 |
1670 (defun calcFunc-nonvar (a) | |
1671 (if (eq (car-safe a) 'var) | |
1672 (list 'calcFunc-nonvar a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1673 1)) |
40785 | 1674 |
1675 (defun calcFunc-istrue (a) | |
1676 (if (math-is-true a) | |
1677 1 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1678 0)) |
40785 | 1679 |
1680 | |
1681 | |
1682 ;;;; User-programmability. | |
1683 | |
1684 ;;; Compiling Lisp-like forms to use the math library. | |
1685 | |
1686 (defun math-do-defmath (func args body) | |
1687 (calc-need-macros) | |
1688 (let* ((fname (intern (concat "calcFunc-" (symbol-name func)))) | |
1689 (doc (if (stringp (car body)) (list (car body)))) | |
1690 (clargs (mapcar 'math-clean-arg args)) | |
1691 (body (math-define-function-body | |
1692 (if (stringp (car body)) (cdr body) body) | |
1693 clargs))) | |
1694 (list 'progn | |
1695 (if (and (consp (car body)) | |
1696 (eq (car (car body)) 'interactive)) | |
1697 (let ((inter (car body))) | |
1698 (setq body (cdr body)) | |
1699 (if (or (> (length inter) 2) | |
1700 (integerp (nth 1 inter))) | |
1701 (let ((hasprefix nil) (hasmulti nil)) | |
1702 (if (stringp (nth 1 inter)) | |
1703 (progn | |
1704 (cond ((equal (nth 1 inter) "p") | |
1705 (setq hasprefix t)) | |
1706 ((equal (nth 1 inter) "m") | |
1707 (setq hasmulti t)) | |
1708 (t (error | |
1709 "Can't handle interactive code string \"%s\"" | |
1710 (nth 1 inter)))) | |
1711 (setq inter (cdr inter)))) | |
1712 (if (not (integerp (nth 1 inter))) | |
1713 (error | |
1714 "Expected an integer in interactive specification")) | |
1715 (append (list 'defun | |
1716 (intern (concat "calc-" | |
1717 (symbol-name func))) | |
1718 (if (or hasprefix hasmulti) | |
1719 '(&optional n) | |
1720 ())) | |
1721 doc | |
1722 (if (or hasprefix hasmulti) | |
1723 '((interactive "P")) | |
1724 '((interactive))) | |
1725 (list | |
1726 (append | |
1727 '(calc-slow-wrapper) | |
1728 (and hasmulti | |
1729 (list | |
1730 (list 'setq | |
1731 'n | |
1732 (list 'if | |
1733 'n | |
1734 (list 'prefix-numeric-value | |
1735 'n) | |
1736 (nth 1 inter))))) | |
1737 (list | |
1738 (list 'calc-enter-result | |
1739 (if hasmulti 'n (nth 1 inter)) | |
1740 (nth 2 inter) | |
1741 (if hasprefix | |
1742 (list 'append | |
1743 (list 'quote (list fname)) | |
1744 (list 'calc-top-list-n | |
1745 (nth 1 inter)) | |
1746 (list 'and | |
1747 'n | |
1748 (list | |
1749 'list | |
1750 (list | |
1751 'math-normalize | |
1752 (list | |
1753 'prefix-numeric-value | |
1754 'n))))) | |
1755 (list 'cons | |
1756 (list 'quote fname) | |
1757 (list 'calc-top-list-n | |
1758 (if hasmulti | |
1759 'n | |
1760 (nth 1 inter))))))))))) | |
1761 (append (list 'defun | |
1762 (intern (concat "calc-" (symbol-name func))) | |
1763 args) | |
1764 doc | |
1765 (list | |
1766 inter | |
1767 (cons 'calc-wrapper body)))))) | |
1768 (append (list 'defun fname clargs) | |
1769 doc | |
1770 (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
|
1771 body)))) |
40785 | 1772 |
1773 (defun math-clean-arg (arg) | |
1774 (if (consp arg) | |
1775 (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
|
1776 arg)) |
40785 | 1777 |
1778 (defun math-do-arg-check (arg var is-opt is-rest) | |
1779 (if is-opt | |
1780 (let ((chk (math-do-arg-check arg var nil nil))) | |
1781 (list (cons 'and | |
1782 (cons var | |
1783 (if (cdr chk) | |
1784 (setq chk (list (cons 'progn chk))) | |
1785 chk))))) | |
1786 (and (consp arg) | |
1787 (let* ((rest (math-do-arg-check (nth 1 arg) var is-opt is-rest)) | |
1788 (qual (car arg)) | |
1789 (qqual (list 'quote qual)) | |
1790 (qual-name (symbol-name qual)) | |
1791 (chk (intern (concat "math-check-" qual-name)))) | |
1792 (if (fboundp chk) | |
1793 (append rest | |
1794 (list | |
1795 (if is-rest | |
1796 (list 'setq var | |
1797 (list 'mapcar (list 'quote chk) var)) | |
1798 (list 'setq var (list chk var))))) | |
1799 (if (fboundp (setq chk (intern (concat "math-" qual-name)))) | |
1800 (append rest | |
1801 (list | |
1802 (if is-rest | |
1803 (list 'mapcar | |
1804 (list 'function | |
1805 (list 'lambda '(x) | |
1806 (list 'or | |
1807 (list chk 'x) | |
1808 (list 'math-reject-arg | |
1809 'x qqual)))) | |
1810 var) | |
1811 (list 'or | |
1812 (list chk var) | |
1813 (list 'math-reject-arg var qqual))))) | |
1814 (if (and (string-match "\\`not-\\(.*\\)\\'" qual-name) | |
1815 (fboundp (setq chk (intern | |
1816 (concat "math-" | |
1817 (math-match-substring | |
1818 qual-name 1)))))) | |
1819 (append rest | |
1820 (list | |
1821 (if is-rest | |
1822 (list 'mapcar | |
1823 (list 'function | |
1824 (list 'lambda '(x) | |
1825 (list 'and | |
1826 (list chk 'x) | |
1827 (list 'math-reject-arg | |
1828 'x qqual)))) | |
1829 var) | |
1830 (list 'and | |
1831 (list chk var) | |
1832 (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
|
1833 (error "Unknown qualifier `%s'" qual-name)))))))) |
40785 | 1834 |
1835 (defun math-do-arg-list-check (args is-opt is-rest) | |
1836 (cond ((null args) nil) | |
1837 ((consp (car args)) | |
1838 (append (math-do-arg-check (car args) | |
1839 (math-clean-arg (car args)) | |
1840 is-opt is-rest) | |
1841 (math-do-arg-list-check (cdr args) is-opt is-rest))) | |
1842 ((eq (car args) '&optional) | |
1843 (math-do-arg-list-check (cdr args) t nil)) | |
1844 ((eq (car args) '&rest) | |
1845 (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
|
1846 (t (math-do-arg-list-check (cdr args) is-opt is-rest)))) |
40785 | 1847 |
1848 (defconst math-prim-funcs | |
1849 '( (~= . math-nearly-equal) | |
1850 (% . math-mod) | |
1851 (lsh . calcFunc-lsh) | |
1852 (ash . calcFunc-ash) | |
1853 (logand . calcFunc-and) | |
1854 (logandc2 . calcFunc-diff) | |
1855 (logior . calcFunc-or) | |
1856 (logxor . calcFunc-xor) | |
1857 (lognot . calcFunc-not) | |
1858 (equal . equal) ; need to leave these ones alone! | |
1859 (eq . eq) | |
1860 (and . and) | |
1861 (or . or) | |
1862 (if . if) | |
1863 (^ . math-pow) | |
1864 (expt . math-pow) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1865 )) |
40785 | 1866 |
1867 (defconst math-prim-vars | |
1868 '( (nil . nil) | |
1869 (t . t) | |
1870 (&optional . &optional) | |
1871 (&rest . &rest) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
1872 )) |
40785 | 1873 |
1874 (defun math-define-function-body (body env) | |
1875 (let ((body (math-define-body body env))) | |
1876 (if (math-body-refers-to body 'math-return) | |
1877 (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
|
1878 body))) |
40785 | 1879 |
1880 (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
|
1881 (math-define-list body)) |
40785 | 1882 |
1883 (defun math-define-list (body &optional quote) | |
1884 (cond ((null body) | |
1885 nil) | |
1886 ((and (eq (car body) ':) | |
1887 (stringp (nth 1 body))) | |
1888 (cons (let* ((math-read-expr-quotes t) | |
1889 (exp (math-read-plain-expr (nth 1 body) t))) | |
1890 (math-define-exp exp)) | |
1891 (math-define-list (cdr (cdr body))))) | |
1892 (quote | |
1893 (cons (cond ((consp (car body)) | |
1894 (math-define-list (cdr body) t)) | |
1895 (t | |
1896 (car body))) | |
1897 (math-define-list (cdr body)))) | |
1898 (t | |
1899 (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
|
1900 (math-define-list (cdr body)))))) |
40785 | 1901 |
1902 (defun math-define-exp (exp) | |
1903 (cond ((consp exp) | |
1904 (let ((func (car exp))) | |
1905 (cond ((memq func '(quote function)) | |
1906 (if (and (consp (nth 1 exp)) | |
1907 (eq (car (nth 1 exp)) 'lambda)) | |
1908 (cons 'quote | |
1909 (math-define-lambda (nth 1 exp) exp-env)) | |
1910 exp)) | |
1911 ((memq func '(let let* for foreach)) | |
1912 (let ((head (nth 1 exp)) | |
1913 (body (cdr (cdr exp)))) | |
1914 (if (memq func '(let let*)) | |
1915 () | |
1916 (setq func (cdr (assq func '((for . math-for) | |
1917 (foreach . math-foreach))))) | |
1918 (if (not (listp (car head))) | |
1919 (setq head (list head)))) | |
1920 (macroexpand | |
1921 (cons func | |
1922 (cons (math-define-let head) | |
1923 (math-define-body body | |
1924 (nconc | |
1925 (math-define-let-env head) | |
1926 exp-env))))))) | |
1927 ((and (memq func '(setq setf)) | |
1928 (math-complicated-lhs (cdr exp))) | |
1929 (if (> (length exp) 3) | |
1930 (cons 'progn (math-define-setf-list (cdr exp))) | |
1931 (math-define-setf (nth 1 exp) (nth 2 exp)))) | |
1932 ((eq func 'condition-case) | |
1933 (cons func | |
1934 (cons (nth 1 exp) | |
1935 (math-define-body (cdr (cdr exp)) | |
1936 (cons (nth 1 exp) | |
1937 exp-env))))) | |
1938 ((eq func 'cond) | |
1939 (cons func | |
1940 (math-define-cond (cdr exp)))) | |
1941 ((and (consp func) ; ('spam a b) == force use of plain spam | |
1942 (eq (car func) 'quote)) | |
1943 (cons func (math-define-list (cdr exp)))) | |
1944 ((symbolp func) | |
1945 (let ((args (math-define-list (cdr exp))) | |
1946 (prim (assq func math-prim-funcs))) | |
1947 (cond (prim | |
1948 (cons (cdr prim) args)) | |
1949 ((eq func 'floatp) | |
1950 (list 'eq (car args) '(quote float))) | |
1951 ((eq func '+) | |
1952 (math-define-binop 'math-add 0 | |
1953 (car args) (cdr args))) | |
1954 ((eq func '-) | |
1955 (if (= (length args) 1) | |
1956 (cons 'math-neg args) | |
1957 (math-define-binop 'math-sub 0 | |
1958 (car args) (cdr args)))) | |
1959 ((eq func '*) | |
1960 (math-define-binop 'math-mul 1 | |
1961 (car args) (cdr args))) | |
1962 ((eq func '/) | |
1963 (math-define-binop 'math-div 1 | |
1964 (car args) (cdr args))) | |
1965 ((eq func 'min) | |
1966 (math-define-binop 'math-min 0 | |
1967 (car args) (cdr args))) | |
1968 ((eq func 'max) | |
1969 (math-define-binop 'math-max 0 | |
1970 (car args) (cdr args))) | |
1971 ((eq func '<) | |
1972 (if (and (math-numberp (nth 1 args)) | |
1973 (math-zerop (nth 1 args))) | |
1974 (list 'math-negp (car args)) | |
1975 (cons 'math-lessp args))) | |
1976 ((eq func '>) | |
1977 (if (and (math-numberp (nth 1 args)) | |
1978 (math-zerop (nth 1 args))) | |
1979 (list 'math-posp (car args)) | |
1980 (list 'math-lessp (nth 1 args) (nth 0 args)))) | |
1981 ((eq func '<=) | |
1982 (list 'not | |
1983 (if (and (math-numberp (nth 1 args)) | |
1984 (math-zerop (nth 1 args))) | |
1985 (list 'math-posp (car args)) | |
1986 (list 'math-lessp | |
1987 (nth 1 args) (nth 0 args))))) | |
1988 ((eq func '>=) | |
1989 (list 'not | |
1990 (if (and (math-numberp (nth 1 args)) | |
1991 (math-zerop (nth 1 args))) | |
1992 (list 'math-negp (car args)) | |
1993 (cons 'math-lessp args)))) | |
1994 ((eq func '=) | |
1995 (if (and (math-numberp (nth 1 args)) | |
1996 (math-zerop (nth 1 args))) | |
1997 (list 'math-zerop (nth 0 args)) | |
1998 (if (and (integerp (nth 1 args)) | |
1999 (/= (% (nth 1 args) 10) 0)) | |
2000 (cons 'math-equal-int args) | |
2001 (cons 'math-equal args)))) | |
2002 ((eq func '/=) | |
2003 (list 'not | |
2004 (if (and (math-numberp (nth 1 args)) | |
2005 (math-zerop (nth 1 args))) | |
2006 (list 'math-zerop (nth 0 args)) | |
2007 (if (and (integerp (nth 1 args)) | |
2008 (/= (% (nth 1 args) 10) 0)) | |
2009 (cons 'math-equal-int args) | |
2010 (cons 'math-equal args))))) | |
2011 ((eq func '1+) | |
2012 (list 'math-add (car args) 1)) | |
2013 ((eq func '1-) | |
2014 (list 'math-add (car args) -1)) | |
2015 ((eq func 'not) ; optimize (not (not x)) => x | |
2016 (if (eq (car-safe args) func) | |
2017 (car (nth 1 args)) | |
2018 (cons func args))) | |
2019 ((and (eq func 'elt) (cdr (cdr args))) | |
2020 (math-define-elt (car args) (cdr args))) | |
2021 (t | |
2022 (macroexpand | |
2023 (let* ((name (symbol-name func)) | |
2024 (cfunc (intern (concat "calcFunc-" name))) | |
2025 (mfunc (intern (concat "math-" name)))) | |
2026 (cond ((fboundp cfunc) | |
2027 (cons cfunc args)) | |
2028 ((fboundp mfunc) | |
2029 (cons mfunc args)) | |
2030 ((or (fboundp func) | |
2031 (string-match "\\`calcFunc-.*" name)) | |
2032 (cons func args)) | |
2033 (t | |
2034 (cons cfunc args))))))))) | |
2035 (t (cons func args))))) | |
2036 ((symbolp exp) | |
2037 (let ((prim (assq exp math-prim-vars)) | |
2038 (name (symbol-name exp))) | |
2039 (cond (prim | |
2040 (cdr prim)) | |
2041 ((memq exp exp-env) | |
2042 exp) | |
2043 ((string-match "-" name) | |
2044 exp) | |
2045 (t | |
2046 (intern (concat "var-" name)))))) | |
2047 ((integerp exp) | |
2048 (if (or (<= exp -1000000) (>= exp 1000000)) | |
2049 (list 'quote (math-normalize exp)) | |
2050 exp)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2051 (t exp))) |
40785 | 2052 |
2053 (defun math-define-cond (forms) | |
2054 (and forms | |
2055 (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
|
2056 (math-define-cond (cdr forms))))) |
40785 | 2057 |
2058 (defun math-complicated-lhs (body) | |
2059 (and body | |
2060 (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
|
2061 (math-complicated-lhs (cdr (cdr body)))))) |
40785 | 2062 |
2063 (defun math-define-setf-list (body) | |
2064 (and body | |
2065 (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
|
2066 (math-define-setf-list (cdr (cdr body)))))) |
40785 | 2067 |
2068 (defun math-define-setf (place value) | |
2069 (setq place (math-define-exp place) | |
2070 value (math-define-exp value)) | |
2071 (cond ((symbolp place) | |
2072 (list 'setq place value)) | |
2073 ((eq (car-safe place) 'nth) | |
2074 (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value)) | |
2075 ((eq (car-safe place) 'elt) | |
2076 (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value)) | |
2077 ((eq (car-safe place) 'car) | |
2078 (list 'setcar (nth 1 place) value)) | |
2079 ((eq (car-safe place) 'cdr) | |
2080 (list 'setcdr (nth 1 place) value)) | |
2081 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2082 (error "Bad place form for setf: %s" place)))) |
40785 | 2083 |
2084 (defun math-define-binop (op ident arg1 rest) | |
2085 (if rest | |
2086 (math-define-binop op ident | |
2087 (list op arg1 (car rest)) | |
2088 (cdr rest)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2089 (or arg1 ident))) |
40785 | 2090 |
2091 (defun math-define-let (vlist) | |
2092 (and vlist | |
2093 (cons (if (consp (car vlist)) | |
2094 (cons (car (car vlist)) | |
2095 (math-define-list (cdr (car vlist)))) | |
2096 (car vlist)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2097 (math-define-let (cdr vlist))))) |
40785 | 2098 |
2099 (defun math-define-let-env (vlist) | |
2100 (and vlist | |
2101 (cons (if (consp (car vlist)) | |
2102 (car (car vlist)) | |
2103 (car vlist)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2104 (math-define-let-env (cdr vlist))))) |
40785 | 2105 |
2106 (defun math-define-lambda (exp exp-env) | |
2107 (nconc (list (nth 0 exp) ; 'lambda | |
2108 (nth 1 exp)) ; arg list | |
2109 (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
|
2110 (append (nth 1 exp) exp-env)))) |
40785 | 2111 |
2112 (defun math-define-elt (seq idx) | |
2113 (if idx | |
2114 (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
|
2115 seq)) |
40785 | 2116 |
2117 | |
2118 | |
2119 ;;; Useful programming macros. | |
2120 | |
2121 (defmacro math-while (head &rest body) | |
2122 (let ((body (cons 'while (cons head body)))) | |
2123 (if (math-body-refers-to body 'math-break) | |
2124 (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
|
2125 body))) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2126 ;; (put 'math-while 'lisp-indent-hook 1) |
40785 | 2127 |
2128 (defmacro math-for (head &rest body) | |
2129 (let ((body (if head | |
2130 (math-handle-for head body) | |
2131 (cons 'while (cons t body))))) | |
2132 (if (math-body-refers-to body 'math-break) | |
2133 (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
|
2134 body))) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2135 ;; (put 'math-for 'lisp-indent-hook 1) |
40785 | 2136 |
2137 (defun math-handle-for (head body) | |
2138 (let* ((var (nth 0 (car head))) | |
2139 (init (nth 1 (car head))) | |
2140 (limit (nth 2 (car head))) | |
2141 (step (or (nth 3 (car head)) 1)) | |
2142 (body (if (cdr head) | |
2143 (list (math-handle-for (cdr head) body)) | |
2144 body)) | |
2145 (all-ints (and (integerp init) (integerp limit) (integerp step))) | |
2146 (const-limit (or (integerp limit) | |
2147 (and (eq (car-safe limit) 'quote) | |
2148 (math-realp (nth 1 limit))))) | |
2149 (const-step (or (integerp step) | |
2150 (and (eq (car-safe step) 'quote) | |
2151 (math-realp (nth 1 step))))) | |
2152 (save-limit (if const-limit limit (make-symbol "<limit>"))) | |
2153 (save-step (if const-step step (make-symbol "<step>")))) | |
2154 (cons 'let | |
2155 (cons (append (if const-limit nil (list (list save-limit limit))) | |
2156 (if const-step nil (list (list save-step step))) | |
2157 (list (list var init))) | |
2158 (list | |
2159 (cons 'while | |
2160 (cons (if all-ints | |
2161 (if (> step 0) | |
2162 (list '<= var save-limit) | |
2163 (list '>= var save-limit)) | |
2164 (list 'not | |
2165 (if const-step | |
2166 (if (or (math-posp step) | |
2167 (math-posp | |
2168 (cdr-safe step))) | |
2169 (list 'math-lessp | |
2170 save-limit | |
2171 var) | |
2172 (list 'math-lessp | |
2173 var | |
2174 save-limit)) | |
2175 (list 'if | |
2176 (list 'math-posp | |
2177 save-step) | |
2178 (list 'math-lessp | |
2179 save-limit | |
2180 var) | |
2181 (list 'math-lessp | |
2182 var | |
2183 save-limit))))) | |
2184 (append body | |
2185 (list (list 'setq | |
2186 var | |
2187 (list (if all-ints | |
2188 '+ | |
2189 'math-add) | |
2190 var | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2191 save-step))))))))))) |
40785 | 2192 |
2193 (defmacro math-foreach (head &rest body) | |
2194 (let ((body (math-handle-foreach head body))) | |
2195 (if (math-body-refers-to body 'math-break) | |
2196 (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
|
2197 body))) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2198 ;; (put 'math-foreach 'lisp-indent-hook 1) |
40785 | 2199 |
2200 (defun math-handle-foreach (head body) | |
2201 (let ((var (nth 0 (car head))) | |
2202 (data (nth 1 (car head))) | |
2203 (body (if (cdr head) | |
2204 (list (math-handle-foreach (cdr head) body)) | |
2205 body))) | |
2206 (cons 'let | |
2207 (cons (list (list var data)) | |
2208 (list | |
2209 (cons 'while | |
2210 (cons var | |
2211 (append body | |
2212 (list (list 'setq | |
2213 var | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2214 (list 'cdr var))))))))))) |
40785 | 2215 |
2216 | |
2217 (defun math-body-refers-to (body thing) | |
2218 (or (equal body thing) | |
2219 (and (consp body) | |
2220 (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
|
2221 (math-body-refers-to (cdr body) thing))))) |
40785 | 2222 |
2223 (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
|
2224 (throw 'math-break value)) |
40785 | 2225 |
2226 (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
|
2227 (throw 'math-return value)) |
40785 | 2228 |
2229 | |
2230 | |
2231 | |
2232 | |
2233 (defun math-composite-inequalities (x op) | |
2234 (if (memq (nth 1 op) '(calcFunc-eq calcFunc-neq)) | |
2235 (if (eq (car x) (nth 1 op)) | |
2236 (append x (list (math-read-expr-level (nth 3 op)))) | |
2237 (throw 'syntax "Syntax error")) | |
2238 (list 'calcFunc-in | |
2239 (nth 2 x) | |
2240 (if (memq (nth 1 op) '(calcFunc-lt calcFunc-leq)) | |
2241 (if (memq (car x) '(calcFunc-lt calcFunc-leq)) | |
2242 (math-make-intv | |
2243 (+ (if (eq (car x) 'calcFunc-leq) 2 0) | |
2244 (if (eq (nth 1 op) 'calcFunc-leq) 1 0)) | |
2245 (nth 1 x) (math-read-expr-level (nth 3 op))) | |
2246 (throw 'syntax "Syntax error")) | |
2247 (if (memq (car x) '(calcFunc-gt calcFunc-geq)) | |
2248 (math-make-intv | |
2249 (+ (if (eq (nth 1 op) 'calcFunc-geq) 2 0) | |
2250 (if (eq (car x) 'calcFunc-geq) 1 0)) | |
2251 (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
|
2252 (throw 'syntax "Syntax error")))))) |
40785 | 2253 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
2254 ;;; calc-prog.el ends here |