Mention `delete-frame-functions' replacing `delete-frame-hook',
[emacs.git] / lisp / calc / calc-store.el
blob4485a471e41ca9827b297a2567953a2966c4685b
1 ;;; calc-store.el --- value storage functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
8 ;; This file is part of GNU Emacs.
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.
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.
25 ;;; Commentary:
27 ;;; Code:
29 ;; This file is autoloaded from calc-ext.el.
30 (require 'calc-ext)
32 (require 'calc-macs)
34 (defun calc-Need-calc-store () nil)
37 ;;; Memory commands.
39 (defvar calc-store-keep nil)
40 (defun calc-store (&optional var)
41 (interactive)
42 (let ((calc-store-keep t))
43 (calc-store-into var)))
45 (defvar calc-given-value-flag nil)
46 (defun calc-store-into (&optional var)
47 (interactive)
48 (calc-wrapper
49 (let ((calc-given-value nil)
50 (calc-given-value-flag 1))
51 (or var (setq var (calc-read-var-name "Store: " t)))
52 (if var
53 (let ((found (assq var '( ( + . calc-store-plus )
54 ( - . calc-store-minus )
55 ( * . calc-store-times )
56 ( / . calc-store-div )
57 ( ^ . calc-store-power )
58 ( | . calc-store-concat ) ))))
59 (if found
60 (funcall (cdr found))
61 (calc-store-value var (or calc-given-value (calc-top 1))
62 "" calc-given-value-flag)
63 (message "Stored to variable \"%s\"" (calc-var-name var))))
64 (setq var (calc-is-assignments (calc-top 1)))
65 (if var
66 (while var
67 (calc-store-value (car (car var)) (cdr (car var))
68 (if (not (cdr var)) "")
69 (if (not (cdr var)) 1))
70 (setq var (cdr var))))))))
72 (defun calc-store-plus (&optional var)
73 (interactive)
74 (calc-store-binary var "+" '+))
76 (defun calc-store-minus (&optional var)
77 (interactive)
78 (calc-store-binary var "-" '-))
80 (defun calc-store-times (&optional var)
81 (interactive)
82 (calc-store-binary var "*" '*))
84 (defun calc-store-div (&optional var)
85 (interactive)
86 (calc-store-binary var "/" '/))
88 (defun calc-store-power (&optional var)
89 (interactive)
90 (calc-store-binary var "^" '^))
92 (defun calc-store-concat (&optional var)
93 (interactive)
94 (calc-store-binary var "|" '|))
96 (defun calc-store-neg (n &optional var)
97 (interactive "p")
98 (calc-store-binary var "n" '/ (- n)))
100 (defun calc-store-inv (n &optional var)
101 (interactive "p")
102 (calc-store-binary var "&" '^ (- n)))
104 (defun calc-store-incr (n &optional var)
105 (interactive "p")
106 (calc-store-binary var "n" '- (- n)))
108 (defun calc-store-decr (n &optional var)
109 (interactive "p")
110 (calc-store-binary var "n" '- n))
112 (defun calc-store-value (var value tag &optional pop)
113 (if var
114 (let ((old (calc-var-value var)))
115 (set var value)
116 (if pop (or calc-store-keep (calc-pop-stack pop)))
117 (calc-record-undo (list 'store (symbol-name var) old))
118 (if tag
119 (let ((calc-full-trail-vectors nil))
120 (calc-record value (format ">%s%s" tag (calc-var-name var)))))
121 (and (memq var '(var-e var-i var-pi var-phi var-gamma))
122 (eq (car-safe old) 'special-const)
123 (message "(Note: Built-in definition of %s has been lost)" var))
124 (and (memq var '(var-inf var-uinf var-nan))
125 (null old)
126 (message "(Note: %s has built-in meanings which may interfere)"
127 var))
128 (calc-refresh-evaltos var))))
130 (defun calc-var-name (var)
131 (if (symbolp var) (setq var (symbol-name var)))
132 (if (string-match "\\`var-." var)
133 (substring var 4)
134 var))
136 (defun calc-store-binary (var tag func &optional val)
137 (calc-wrapper
138 (let ((calc-simplify-mode (if (eq calc-simplify-mode 'none)
139 'num calc-simplify-mode))
140 (value (or val (calc-top 1))))
141 (or var (setq var (calc-read-var-name (format "Store %s: " tag))))
142 (if var
143 (let ((old (calc-var-value var)))
144 (or old
145 (error "No such variable: \"%s\"" (calc-var-name var)))
146 (if (stringp old)
147 (setq old (math-read-expr old)))
148 (if (eq (car-safe old) 'error)
149 (error "Bad format in variable contents: %s" (nth 2 old)))
150 (calc-store-value var
151 (calc-normalize (if (calc-is-inverse)
152 (list func value old)
153 (list func old value)))
154 tag (and (not val) 1))
155 (message "Stored to variable \"%s\"" (calc-var-name var)))))))
157 (defun calc-read-var-name (prompt &optional calc-store-opers)
158 (setq calc-given-value nil
159 calc-aborted-prefix nil)
160 (let ((var (let ((minibuffer-completion-table obarray)
161 (minibuffer-completion-predicate 'boundp)
162 (minibuffer-completion-confirm t))
163 (read-from-minibuffer prompt "var-" calc-var-name-map nil))))
164 (setq calc-aborted-prefix "")
165 (and (not (equal var ""))
166 (not (equal var "var-"))
167 (if (string-match "\\`\\([-a-zA-Z0-9]+\\) *:?=" var)
168 (if (null calc-given-value-flag)
169 (error "Assignment is not allowed in this command")
170 (let ((svar (intern (substring var 0 (match-end 1)))))
171 (setq calc-given-value-flag 0
172 calc-given-value (math-read-expr
173 (substring var (match-end 0))))
174 (if (eq (car-safe calc-given-value) 'error)
175 (error "Bad format: %s" (nth 2 calc-given-value)))
176 (setq calc-given-value (math-evaluate-expr calc-given-value))
177 svar))
178 (intern var)))))
180 (defvar calc-var-name-map nil "Keymap for reading Calc variable names.")
181 (if calc-var-name-map
183 (setq calc-var-name-map (copy-keymap minibuffer-local-completion-map))
184 (define-key calc-var-name-map " " 'self-insert-command)
185 (mapcar (function
186 (lambda (x)
187 (define-key calc-var-name-map (char-to-string x)
188 'calcVar-digit)))
189 "0123456789")
190 (mapcar (function
191 (lambda (x)
192 (define-key calc-var-name-map (char-to-string x)
193 'calcVar-oper)))
194 "+-*/^|"))
196 (defun calcVar-digit ()
197 (interactive)
198 (if (calc-minibuffer-contains "var-\\'")
199 (if (eq calc-store-opers 0)
200 (beep)
201 (insert "q")
202 (self-insert-and-exit))
203 (self-insert-command 1)))
205 (defun calcVar-oper ()
206 (interactive)
207 (if (and (eq calc-store-opers t)
208 (calc-minibuffer-contains "var-\\'"))
209 (progn
210 (erase-buffer)
211 (self-insert-and-exit))
212 (self-insert-command 1)))
214 (defun calc-store-map (&optional oper var)
215 (interactive)
216 (calc-wrapper
217 (let* ((sel-mode nil)
218 (calc-dollar-values (mapcar 'calc-get-stack-element
219 (nthcdr calc-stack-top calc-stack)))
220 (calc-dollar-used 0)
221 (oper (or oper (calc-get-operator "Store Mapping")))
222 (nargs (car oper)))
223 (or var (setq var (calc-read-var-name (format "Store Mapping %s: "
224 (nth 2 oper)))))
225 (if var
226 (let ((old (or (calc-var-value var)
227 (error "No such variable: \"%s\""
228 (calc-var-name var))))
229 (calc-simplify-mode (if (eq calc-simplify-mode 'none)
230 'num calc-simplify-mode))
231 (values (and (> nargs 1)
232 (calc-top-list (1- nargs) (1+ calc-dollar-used)))))
233 (message "Working...")
234 (calc-set-command-flag 'clear-message)
235 (if (stringp old)
236 (setq old (math-read-expr old)))
237 (if (eq (car-safe old) 'error)
238 (error "Bad format in variable contents: %s" (nth 2 old)))
239 (setq values (if (calc-is-inverse)
240 (append values (list old))
241 (append (list old) values)))
242 (calc-store-value var
243 (calc-normalize (cons (nth 1 oper) values))
244 (nth 2 oper)
245 (+ calc-dollar-used (1- nargs))))))))
247 (defun calc-store-exchange (&optional var)
248 (interactive)
249 (calc-wrapper
250 (let ((calc-given-value nil)
251 (calc-given-value-flag 1)
252 top)
253 (or var (setq var (calc-read-var-name "Exchange with: ")))
254 (if var
255 (let ((value (calc-var-value var)))
256 (or value
257 (error "No such variable: \"%s\"" (calc-var-name var)))
258 (if (eq (car-safe value) 'special-const)
259 (error "%s is a special constant" var))
260 (setq top (or calc-given-value (calc-top 1)))
261 (calc-store-value var top nil)
262 (calc-pop-push-record calc-given-value-flag
263 (concat "<>" (calc-var-name var)) value))))))
265 (defun calc-unstore (&optional var)
266 (interactive)
267 (calc-wrapper
268 (or var (setq var (calc-read-var-name "Unstore: ")))
269 (if var
270 (progn
271 (and (memq var '(var-e var-i var-pi var-phi var-gamma))
272 (eq (car-safe (calc-var-value var)) 'special-const)
273 (message "(Note: Built-in definition of %s has been lost)" var))
274 (if (and (boundp var) (symbol-value var))
275 (message "Unstored variable \"%s\"" (calc-var-name var))
276 (message "Variable \"%s\" remains unstored" (calc-var-name var)))
277 (makunbound var)
278 (calc-refresh-evaltos var)))))
280 (defun calc-let (&optional var)
281 (interactive)
282 (calc-wrapper
283 (let* ((calc-given-value nil)
284 (calc-given-value-flag 1)
285 thing value)
286 (or var (setq var (calc-read-var-name "Let variable: ")))
287 (if calc-given-value
288 (setq value calc-given-value
289 thing (calc-top 1))
290 (setq value (calc-top 1)
291 thing (calc-top 2)))
292 (setq var (if var
293 (list (cons var value))
294 (calc-is-assignments value)))
295 (if var
296 (calc-pop-push-record
297 (1+ calc-given-value-flag)
298 (concat "=" (calc-var-name (car (car var))))
299 (let ((saved-val (mapcar (function
300 (lambda (v)
301 (and (boundp (car v))
302 (symbol-value (car v)))))
303 var)))
304 (unwind-protect
305 (let ((vv var))
306 (while vv
307 (set (car (car vv)) (calc-normalize (cdr (car vv))))
308 (calc-refresh-evaltos (car (car vv)))
309 (setq vv (cdr vv)))
310 (math-evaluate-expr thing))
311 (while saved-val
312 (if (car saved-val)
313 (set (car (car var)) (car saved-val))
314 (makunbound (car (car var))))
315 (setq saved-val (cdr saved-val)
316 var (cdr var)))
317 (calc-handle-whys))))))))
319 (defun calc-is-assignments (value)
320 (if (memq (car-safe value) '(calcFunc-eq calcFunc-assign))
321 (and (eq (car-safe (nth 1 value)) 'var)
322 (list (cons (nth 2 (nth 1 value)) (nth 2 value))))
323 (if (eq (car-safe value) 'vec)
324 (let ((vv nil))
325 (while (and (setq value (cdr value))
326 (memq (car-safe (car value))
327 '(calcFunc-eq calcFunc-assign))
328 (eq (car-safe (nth 1 (car value))) 'var))
329 (setq vv (cons (cons (nth 2 (nth 1 (car value)))
330 (nth 2 (car value)))
331 vv)))
332 (and (not value)
333 vv)))))
335 (defun calc-recall (&optional var)
336 (interactive)
337 (calc-wrapper
338 (or var (setq var (calc-read-var-name "Recall: ")))
339 (if var
340 (let ((value (calc-var-value var)))
341 (or value
342 (error "No such variable: \"%s\"" (calc-var-name var)))
343 (if (stringp value)
344 (setq value (math-read-expr value)))
345 (if (eq (car-safe value) 'error)
346 (error "Bad format in variable contents: %s" (nth 2 value)))
347 (setq value (calc-normalize value))
348 (let ((calc-full-trail-vectors nil))
349 (calc-record value (concat "<" (calc-var-name var))))
350 (calc-push value)))))
352 (defun calc-store-quick ()
353 (interactive)
354 (calc-store (intern (format "var-q%c" last-command-char))))
356 (defun calc-store-into-quick ()
357 (interactive)
358 (calc-store-into (intern (format "var-q%c" last-command-char))))
360 (defun calc-recall-quick ()
361 (interactive)
362 (calc-recall (intern (format "var-q%c" last-command-char))))
364 (defun calc-copy-variable (&optional var1 var2)
365 (interactive)
366 (calc-wrapper
367 (or var1 (setq var1 (calc-read-var-name "Copy variable: ")))
368 (if var1
369 (let ((value (calc-var-value var1)))
370 (or value
371 (error "No such variable: \"%s\"" (calc-var-name var)))
372 (or var2 (setq var2 (calc-read-var-name
373 (format "Copy variable: %s, to: " var1))))
374 (if var2
375 (calc-store-value var2 value ""))))))
377 (defvar calc-last-edited-variable nil)
378 (defun calc-edit-variable (&optional var)
379 (interactive)
380 (calc-wrapper
381 (or var (setq var (calc-read-var-name
382 (if calc-last-edited-variable
383 (format "Edit: (default %s) "
384 (calc-var-name calc-last-edited-variable))
385 "Edit: "))))
386 (or var (setq var calc-last-edited-variable))
387 (if var
388 (let* ((value (calc-var-value var)))
389 (if (eq (car-safe value) 'special-const)
390 (error "%s is a special constant" var))
391 (setq calc-last-edited-variable var)
392 (calc-edit-mode (list 'calc-finish-stack-edit (list 'quote var))
394 (concat "Editing " (calc-var-name var)))
395 (and value
396 (insert (math-format-nice-expr value (frame-width)) "\n")))))
397 (calc-show-edit-buffer))
399 (defun calc-edit-Decls ()
400 (interactive)
401 (calc-edit-variable 'var-Decls))
403 (defun calc-edit-EvalRules ()
404 (interactive)
405 (calc-edit-variable 'var-EvalRules))
407 (defun calc-edit-FitRules ()
408 (interactive)
409 (calc-edit-variable 'var-FitRules))
411 (defun calc-edit-GenCount ()
412 (interactive)
413 (calc-edit-variable 'var-GenCount))
415 (defun calc-edit-Holidays ()
416 (interactive)
417 (calc-edit-variable 'var-Holidays))
419 (defun calc-edit-IntegLimit ()
420 (interactive)
421 (calc-edit-variable 'var-IntegLimit))
423 (defun calc-edit-LineStyles ()
424 (interactive)
425 (calc-edit-variable 'var-LineStyles))
427 (defun calc-edit-PointStyles ()
428 (interactive)
429 (calc-edit-variable 'var-PointStyles))
431 (defun calc-edit-PlotRejects ()
432 (interactive)
433 (calc-edit-variable 'var-PlotRejects))
435 (defun calc-edit-AlgSimpRules ()
436 (interactive)
437 (calc-edit-variable 'var-AlgSimpRules))
439 (defun calc-edit-TimeZone ()
440 (interactive)
441 (calc-edit-variable 'var-TimeZone))
443 (defun calc-edit-Units ()
444 (interactive)
445 (calc-edit-variable 'var-Units))
447 (defun calc-edit-ExtSimpRules ()
448 (interactive)
449 (calc-edit-variable 'var-ExtSimpRules))
451 (defun calc-declare-variable (&optional var)
452 (interactive)
453 (calc-wrapper
454 (or var (setq var (calc-read-var-name "Declare: " 0)))
455 (or var (setq var 'var-All))
456 (let* (dp decl def row rp)
457 (or (and (calc-var-value 'var-Decls)
458 (eq (car-safe var-Decls) 'vec))
459 (setq var-Decls (list 'vec)))
460 (setq dp var-Decls)
461 (while (and (setq dp (cdr dp))
462 (or (not (eq (car-safe (car dp)) 'vec))
463 (/= (length (car dp)) 3)
464 (progn
465 (setq row (nth 1 (car dp))
466 rp row)
467 (if (eq (car-safe row) 'vec)
468 (progn
469 (while
470 (and (setq rp (cdr rp))
471 (or (not (eq (car-safe (car rp)) 'var))
472 (not (eq (nth 2 (car rp)) var)))))
473 (setq rp (car rp)))
474 (if (or (not (eq (car-safe row) 'var))
475 (not (eq (nth 2 row) var)))
476 (setq rp nil)))
477 (not rp)))))
478 (calc-unread-command ?\C-a)
479 (setq decl (read-string (format "Declare: %s to be: " var)
480 (and rp
481 (math-format-flat-expr (nth 2 (car dp)) 0))))
482 (setq decl (and (string-match "[^ \t]" decl)
483 (math-read-exprs decl)))
484 (if (eq (car-safe decl) 'error)
485 (error "Bad format in declaration: %s" (nth 2 decl)))
486 (if (cdr decl)
487 (setq decl (cons 'vec decl))
488 (setq decl (car decl)))
489 (and (eq (car-safe decl) 'vec)
490 (= (length decl) 2)
491 (setq decl (nth 1 decl)))
492 (calc-record (append '(vec) (list (math-build-var-name var))
493 (and decl (list decl)))
494 "decl")
495 (setq var-Decls (copy-sequence var-Decls))
496 (if (eq (car-safe row) 'vec)
497 (progn
498 (setcdr row (delq rp (cdr row)))
499 (or (cdr row)
500 (setq var-Decls (delq (car dp) var-Decls))))
501 (setq var-Decls (delq (car dp) var-Decls)))
502 (if decl
503 (progn
504 (setq dp (and (not (eq var 'var-All)) var-Decls))
505 (while (and (setq dp (cdr dp))
506 (or (not (eq (car-safe (car dp)) 'vec))
507 (/= (length (car dp)) 3)
508 (not (equal (nth 2 (car dp)) decl)))))
509 (if dp
510 (setcar (cdr (car dp))
511 (append (if (eq (car-safe (nth 1 (car dp))) 'vec)
512 (nth 1 (car dp))
513 (list 'vec (nth 1 (car dp))))
514 (list (math-build-var-name var))))
515 (setq var-Decls (append var-Decls
516 (list (list 'vec
517 (math-build-var-name var)
518 decl)))))))
519 (calc-refresh-evaltos 'var-Decls))))
521 (defvar calc-dont-insert-variables '(var-FitRules var-FactorRules
522 var-CommuteRules var-JumpRules
523 var-DistribRules var-MergeRules
524 var-NegateRules var-InvertRules
525 var-IntegAfterRules
526 var-TimeZone var-PlotRejects
527 var-PlotData1 var-PlotData2
528 var-PlotData3 var-PlotData4
529 var-PlotData5 var-PlotData6
530 var-DUMMY))
532 (defun calc-permanent-variable (&optional var)
533 (interactive)
534 (calc-wrapper
535 (or var (setq var (calc-read-var-name "Save variable (default=all): ")))
536 (let (pos)
537 (and var (or (and (boundp var) (symbol-value var))
538 (error "No such variable")))
539 (set-buffer (find-file-noselect (substitute-in-file-name
540 calc-settings-file)))
541 (if var
542 (calc-insert-permanent-variable var)
543 (mapatoms (function
544 (lambda (x)
545 (and (string-match "\\`var-" (symbol-name x))
546 (not (memq x calc-dont-insert-variables))
547 (calc-var-value x)
548 (not (eq (car-safe (symbol-value x)) 'special-const))
549 (calc-insert-permanent-variable x))))))
550 (save-buffer))))
554 (defun calc-insert-permanent-variable (var)
555 (goto-char (point-min))
556 (if (search-forward (concat "(setq " (symbol-name var) " '") nil t)
557 (progn
558 (setq pos (point-marker))
559 (forward-line -1)
560 (if (looking-at ";;; Variable .* stored by Calc on ")
561 (progn
562 (delete-region (match-end 0) (progn (end-of-line) (point)))
563 (insert (current-time-string))))
564 (goto-char (- pos 8 (length (symbol-name var))))
565 (forward-sexp 1)
566 (backward-char 1)
567 (delete-region pos (point)))
568 (goto-char (point-max))
569 (insert "\n;;; Variable \""
570 (symbol-name var)
571 "\" stored by Calc on "
572 (current-time-string)
573 "\n(setq "
574 (symbol-name var)
575 " ')\n")
576 (backward-char 2))
577 (insert (prin1-to-string (calc-var-value var)))
578 (forward-line 1))
580 (defun calc-insert-variables (buf)
581 (interactive "bBuffer in which to save variable values: ")
582 (save-excursion
583 (set-buffer buf)
584 (mapatoms (function
585 (lambda (x)
586 (and (string-match "\\`var-" (symbol-name x))
587 (not (memq x calc-dont-insert-variables))
588 (calc-var-value x)
589 (not (eq (car-safe (symbol-value x)) 'special-const))
590 (or (not (eq x 'var-Decls))
591 (not (equal var-Decls '(vec))))
592 (or (not (eq x 'var-Holidays))
593 (not (equal var-Holidays '(vec (var sat var-sat)
594 (var sun var-sun)))))
595 (insert "(setq "
596 (symbol-name x)
598 (prin1-to-string
599 (let ((calc-language
600 (if (memq calc-language '(nil big))
601 'flat
602 calc-language)))
603 (math-format-value (symbol-value x) 100000)))
604 ")\n")))))))
606 (defun calc-assign (arg)
607 (interactive "P")
608 (calc-slow-wrapper
609 (calc-binary-op ":=" 'calcFunc-assign arg)))
611 (defun calc-evalto (arg)
612 (interactive "P")
613 (calc-slow-wrapper
614 (calc-unary-op "=>" 'calcFunc-evalto arg)))
616 (defun calc-subscript (arg)
617 (interactive "P")
618 (calc-slow-wrapper
619 (calc-binary-op "sub" 'calcFunc-subscr arg)))
621 ;;; calc-store.el ends here