1 ;;; calc-store.el --- value storage functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
37 (defvar calc-store-keep nil
)
38 (defun calc-store (&optional var
)
40 (let ((calc-store-keep t
))
41 (calc-store-into var
)))
43 (defvar calc-given-value-flag nil
)
44 (defvar calc-given-value
)
46 (defun calc-store-into (&optional var
)
49 (let ((calc-given-value nil
)
50 (calc-given-value-flag 1))
51 (or var
(setq var
(calc-read-var-name "Store: " t
)))
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
) ))))
62 (calc-store-value var
(or calc-given-value
(calc-top 1))
63 "" calc-given-value-flag
)))
64 (message (concat "Stored to variable \"%s\"" msg
)
65 (calc-var-name var
)))))
66 (setq var
(calc-is-assignments (calc-top 1)))
70 (calc-store-value (car (car var
)) (cdr (car var
))
71 (if (not (cdr var
)) "")
72 (if (not (cdr var
)) 1))))
73 (message (concat "Stored to variable \"%s\"" msg
)
74 (calc-var-name (car (car var
)))))
75 (setq var
(cdr var
))))))))
77 (defun calc-store-plus (&optional var
)
79 (calc-store-binary var
"+" '+))
81 (defun calc-store-minus (&optional var
)
83 (calc-store-binary var
"-" '-
))
85 (defun calc-store-times (&optional var
)
87 (calc-store-binary var
"*" '*))
89 (defun calc-store-div (&optional var
)
91 (calc-store-binary var
"/" '/))
93 (defun calc-store-power (&optional var
)
95 (calc-store-binary var
"^" '^
))
97 (defun calc-store-concat (&optional var
)
99 (calc-store-binary var
"|" '|
))
101 (defun calc-store-neg (n &optional var
)
103 (calc-store-binary var
"n" '/ (- n
)))
105 (defun calc-store-inv (n &optional var
)
107 (calc-store-binary var
"&" '^
(- n
)))
109 (defun calc-store-incr (n &optional var
)
111 (calc-store-binary var
"n" '-
(- n
)))
113 (defun calc-store-decr (n &optional var
)
115 (calc-store-binary var
"n" '- n
))
117 (defun calc-store-value (var value tag
&optional pop
)
120 (let ((old (calc-var-value var
)))
122 (if pop
(or calc-store-keep
(calc-pop-stack pop
)))
123 (calc-record-undo (list 'store
(symbol-name var
) old
))
125 (let ((calc-full-trail-vectors nil
))
126 (calc-record value
(format ">%s%s" tag
(calc-var-name var
)))))
128 ((and (memq var
'(var-e var-i var-pi var-phi var-gamma
))
129 (eq (car-safe old
) 'special-const
))
130 (setq msg
(format " (Note: Built-in definition of %s has been lost)"
131 (calc-var-name var
))))
132 ((and (memq var
'(var-inf var-uinf var-nan
))
134 (setq msg
(format " (Note: %s has built-in meanings which may interfere)"
135 (calc-var-name var
)))))
136 (calc-refresh-evaltos var
)))
139 (defun calc-var-name (var)
140 (if (symbolp var
) (setq var
(symbol-name var
)))
141 (if (string-match "\\`var-." var
)
145 (defun calc-store-binary (var tag func
&optional val
)
147 (let ((calc-simplify-mode (if (eq calc-simplify-mode
'none
)
148 'num calc-simplify-mode
))
149 (value (or val
(calc-top 1))))
150 (or var
(setq var
(calc-read-var-name (format "Store %s: " tag
))))
152 (let ((old (calc-var-value var
)))
153 (if (eq (car-safe old
) 'special-const
)
154 (error "\"%s\" is a special constant" (calc-var-name var
)))
156 (if (memq var
'(var-inf var-uinf var-nan
))
157 (error "\"%s\" is a special variable" (calc-var-name var
))
158 (error "No such variable: \"%s\"" (calc-var-name var
))))
160 (setq old
(math-read-expr old
)))
161 (if (eq (car-safe old
) 'error
)
162 (error "Bad format in variable contents: %s" (nth 2 old
)))
163 (calc-store-value var
164 (calc-normalize (if (calc-is-inverse)
165 (list func value old
)
166 (list func old value
)))
167 tag
(and (not val
) 1))
168 (message "Variable \"%s\" changed" (calc-var-name var
)))))))
170 (defvar calc-var-name-map nil
"Keymap for reading Calc variable names.")
171 (if calc-var-name-map
173 (setq calc-var-name-map
(copy-keymap minibuffer-local-completion-map
))
174 (define-key calc-var-name-map
" " 'self-insert-command
)
177 (define-key calc-var-name-map
(char-to-string x
)
182 (define-key calc-var-name-map
(char-to-string x
)
186 (defvar calc-store-opers
)
188 (defun calc-read-var-name (prompt &optional calc-store-opers
)
189 (setq calc-given-value nil
190 calc-aborted-prefix nil
)
193 (let ((minibuffer-completion-table
194 (mapcar (lambda (x) (substring x
4))
195 (all-completions "var-" obarray
)))
196 (minibuffer-completion-predicate
197 (lambda (x) (boundp (intern (concat "var-" x
)))))
198 (minibuffer-completion-confirm t
))
199 (read-from-minibuffer prompt nil calc-var-name-map nil
)))))
200 (setq calc-aborted-prefix
"")
201 (and (not (equal var
"var-"))
202 (if (string-match "\\`\\([-a-zA-Z0-9]+\\) *:?=" var
)
203 (if (null calc-given-value-flag
)
204 (error "Assignment is not allowed in this command")
205 (let ((svar (intern (substring var
0 (match-end 1)))))
206 (setq calc-given-value-flag
0
207 calc-given-value
(math-read-expr
208 (substring var
(match-end 0))))
209 (if (eq (car-safe calc-given-value
) 'error
)
210 (error "Bad format: %s" (nth 2 calc-given-value
)))
211 (setq calc-given-value
(math-evaluate-expr calc-given-value
))
215 (defun calcVar-digit ()
217 (if (calc-minibuffer-contains "\\'")
218 (if (eq calc-store-opers
0)
221 (self-insert-and-exit))
222 (self-insert-command 1)))
224 (defun calcVar-oper ()
226 (if (and (eq calc-store-opers t
)
227 (calc-minibuffer-contains "\\'"))
230 (self-insert-and-exit))
231 (self-insert-command 1)))
233 (defun calc-store-map (&optional oper var
)
236 (let* ((sel-mode nil
)
237 (calc-dollar-values (mapcar 'calc-get-stack-element
238 (nthcdr calc-stack-top calc-stack
)))
240 (oper (or oper
(calc-get-operator "Store Mapping")))
242 (or var
(setq var
(calc-read-var-name (format "Store Mapping %s: "
245 (let ((old (calc-var-value var
)))
246 (if (eq (car-safe old
) 'special-const
)
247 (error "\"%s\" is a special constant" (calc-var-name var
)))
249 (if (memq var
'(var-inf var-uinf var-nan
))
250 (error "\"%s\" is a special variable" (calc-var-name var
))
251 (error "No such variable: \"%s\"" (calc-var-name var
))))
252 (let ((calc-simplify-mode (if (eq calc-simplify-mode
'none
)
253 'num calc-simplify-mode
))
254 (values (and (> nargs
1)
255 (calc-top-list (1- nargs
) (1+ calc-dollar-used
)))))
256 (message "Working...")
257 (calc-set-command-flag 'clear-message
)
259 (setq old
(math-read-expr old
)))
260 (if (eq (car-safe old
) 'error
)
261 (error "Bad format in variable contents: %s" (nth 2 old
)))
262 (setq values
(if (calc-is-inverse)
263 (append values
(list old
))
264 (append (list old
) values
)))
265 (calc-store-value var
266 (calc-normalize (cons (nth 1 oper
) values
))
268 (+ calc-dollar-used
(1- nargs
)))
269 (message "Variable \"%s\" changed" (calc-var-name var
))))))))
272 (defun calc-store-exchange (&optional var
)
275 (let ((calc-given-value nil
)
276 (calc-given-value-flag 1)
278 (or var
(setq var
(calc-read-var-name "Exchange with: ")))
280 (let ((value (calc-var-value var
)))
281 (if (eq (car-safe value
) 'special-const
)
282 (error "\"%s\" is a special constant" (calc-var-name var
)))
284 (if (memq var
'(var-inf var-uinf var-nan
))
285 (error "\"%s\" is a special variable" (calc-var-name var
))
286 (error "No such variable: \"%s\"" (calc-var-name var
))))
287 (setq top
(or calc-given-value
(calc-top 1)))
288 (calc-store-value var top nil
)
289 (calc-pop-push-record calc-given-value-flag
290 (concat "<>" (calc-var-name var
)) value
))))))
292 (defun calc-unstore (&optional var
)
295 (or var
(setq var
(calc-read-var-name "Unstore: ")))
298 (and (memq var
'(var-e var-i var-pi var-phi var-gamma
))
299 (eq (car-safe (calc-var-value var
)) 'special-const
)
300 (message "(Note: Built-in definition of %s has been lost)" var
))
301 (if (and (boundp var
) (symbol-value var
))
302 (message "Unstored variable \"%s\"" (calc-var-name var
))
303 (message "Variable \"%s\" remains unstored" (calc-var-name var
)))
305 (calc-refresh-evaltos var
)))))
307 (defun calc-let (&optional var
)
310 (let* ((calc-given-value nil
)
311 (calc-given-value-flag 1)
313 (or var
(setq var
(calc-read-var-name "Let variable: ")))
315 (setq value calc-given-value
317 (setq value
(calc-top 1)
320 (list (cons var value
))
321 (calc-is-assignments value
)))
323 (calc-pop-push-record
324 (1+ calc-given-value-flag
)
325 (concat "=" (calc-var-name (car (car var
))))
326 (let ((saved-val (mapcar (function
328 (and (boundp (car v
))
329 (symbol-value (car v
)))))
334 (set (car (car vv
)) (calc-normalize (cdr (car vv
))))
335 (calc-refresh-evaltos (car (car vv
)))
337 (math-evaluate-expr thing
))
340 (set (car (car var
)) (car saved-val
))
341 (makunbound (car (car var
))))
342 (setq saved-val
(cdr saved-val
)
344 (calc-handle-whys))))))))
346 (defun calc-is-assignments (value)
347 (if (memq (car-safe value
) '(calcFunc-eq calcFunc-assign
))
348 (and (eq (car-safe (nth 1 value
)) 'var
)
349 (list (cons (nth 2 (nth 1 value
)) (nth 2 value
))))
350 (if (eq (car-safe value
) 'vec
)
352 (while (and (setq value
(cdr value
))
353 (memq (car-safe (car value
))
354 '(calcFunc-eq calcFunc-assign
))
355 (eq (car-safe (nth 1 (car value
))) 'var
))
356 (setq vv
(cons (cons (nth 2 (nth 1 (car value
)))
362 (defun calc-recall (&optional var
)
365 (or var
(setq var
(calc-read-var-name "Recall: ")))
367 (let ((value (calc-var-value var
)))
369 (error "No such variable: \"%s\"" (calc-var-name var
)))
371 (setq value
(math-read-expr value
)))
372 (if (eq (car-safe value
) 'error
)
373 (error "Bad format in variable contents: %s" (nth 2 value
)))
374 (setq value
(calc-normalize value
))
375 (let ((calc-full-trail-vectors nil
))
376 (calc-record value
(concat "<" (calc-var-name var
))))
377 (calc-push value
)))))
379 (defun calc-store-quick ()
381 (calc-store (intern (format "var-q%c" last-command-char
))))
383 (defun calc-store-into-quick ()
385 (calc-store-into (intern (format "var-q%c" last-command-char
))))
387 (defun calc-recall-quick ()
389 (calc-recall (intern (format "var-q%c" last-command-char
))))
391 (defun calc-copy-special-constant (&optional sconst var
)
394 ("e" .
(special-const (math-e)))
395 ("pi" .
(special-const (math-pi)))
396 ("i" .
(special-const (math-imaginary 1)))
397 ("phi" .
(special-const (math-phi)))
398 ("gamma" .
(special-const (math-gamma-const))))))
400 (or sconst
(setq sconst
(completing-read "Special constant: " sc nil t
)))
401 (unless (string= sconst
"")
402 (let ((value (cdr (assoc sconst sc
))))
403 (or var
(setq var
(calc-read-var-name
404 (format "Copy special constant %s, to: "
407 (let ((msg (calc-store-value var value
"")))
408 (message (concat "Special constant \"%s\" copied to \"%s\"" msg
)
409 sconst
(calc-var-name var
)))))))))
411 (defun calc-copy-variable (&optional var1 var2
)
414 (or var1
(setq var1
(calc-read-var-name "Copy variable: ")))
416 (let ((value (calc-var-value var1
)))
418 (error "No such variable: \"%s\"" (calc-var-name var1
)))
419 (or var2
(setq var2
(calc-read-var-name
420 (format "Copy variable: %s, to: "
421 (calc-var-name var1
)))))
423 (let ((msg (calc-store-value var2 value
"")))
424 (message (concat "Variable \"%s\" copied to \"%s\"" msg
)
425 (calc-var-name var1
) (calc-var-name var2
))))))))
427 (defvar calc-last-edited-variable nil
)
428 (defun calc-edit-variable (&optional var
)
431 (or var
(setq var
(calc-read-var-name
432 (if calc-last-edited-variable
433 (format "Edit (default %s): "
434 (calc-var-name calc-last-edited-variable
))
436 (or var
(setq var calc-last-edited-variable
))
438 (let* ((value (calc-var-value var
)))
439 (if (eq (car-safe value
) 'special-const
)
440 (error "%s is a special constant" var
))
441 (setq calc-last-edited-variable var
)
442 (calc-edit-mode (list 'calc-finish-stack-edit
(list 'quote var
))
444 (concat "Editing variable `" (calc-var-name var
) "'. "))
446 (insert (math-format-nice-expr value
(frame-width)) "\n")))))
447 (calc-show-edit-buffer))
449 (defun calc-edit-Decls ()
451 (calc-edit-variable 'var-Decls
))
453 (defun calc-edit-EvalRules ()
455 (calc-edit-variable 'var-EvalRules
))
457 (defun calc-edit-FitRules ()
459 (calc-edit-variable 'var-FitRules
))
461 (defun calc-edit-GenCount ()
463 (calc-edit-variable 'var-GenCount
))
465 (defun calc-edit-Holidays ()
467 (calc-edit-variable 'var-Holidays
))
469 (defun calc-edit-IntegLimit ()
471 (calc-edit-variable 'var-IntegLimit
))
473 (defun calc-edit-LineStyles ()
475 (calc-edit-variable 'var-LineStyles
))
477 (defun calc-edit-PointStyles ()
479 (calc-edit-variable 'var-PointStyles
))
481 (defun calc-edit-PlotRejects ()
483 (calc-edit-variable 'var-PlotRejects
))
485 (defun calc-edit-AlgSimpRules ()
487 (calc-edit-variable 'var-AlgSimpRules
))
489 (defun calc-edit-TimeZone ()
491 (calc-edit-variable 'var-TimeZone
))
493 (defun calc-edit-Units ()
495 (calc-edit-variable 'var-Units
))
497 (defun calc-edit-ExtSimpRules ()
499 (calc-edit-variable 'var-ExtSimpRules
))
501 (defun calc-declare-variable (&optional var
)
504 (or var
(setq var
(calc-read-var-name "Declare: " 0)))
505 (or var
(setq var
'var-All
))
506 (let* (dp decl def row rp
)
507 (or (and (calc-var-value 'var-Decls
)
508 (eq (car-safe var-Decls
) 'vec
))
509 (setq var-Decls
(list 'vec
)))
511 (while (and (setq dp
(cdr dp
))
512 (or (not (eq (car-safe (car dp
)) 'vec
))
513 (/= (length (car dp
)) 3)
515 (setq row
(nth 1 (car dp
))
517 (if (eq (car-safe row
) 'vec
)
520 (and (setq rp
(cdr rp
))
521 (or (not (eq (car-safe (car rp
)) 'var
))
522 (not (eq (nth 2 (car rp
)) var
)))))
524 (if (or (not (eq (car-safe row
) 'var
))
525 (not (eq (nth 2 row
) var
)))
528 (calc-unread-command ?\C-a
)
529 (setq decl
(read-string (format "Declare: %s to be: " (calc-var-name var
))
531 (math-format-flat-expr (nth 2 (car dp
)) 0))))
532 (setq decl
(and (string-match "[^ \t]" decl
)
533 (math-read-exprs decl
)))
534 (if (eq (car-safe decl
) 'error
)
535 (error "Bad format in declaration: %s" (nth 2 decl
)))
537 (setq decl
(cons 'vec decl
))
538 (setq decl
(car decl
)))
539 (and (eq (car-safe decl
) 'vec
)
541 (setq decl
(nth 1 decl
)))
542 (calc-record (append '(vec) (list (math-build-var-name var
))
543 (and decl
(list decl
)))
545 (setq var-Decls
(copy-sequence var-Decls
))
546 (if (eq (car-safe row
) 'vec
)
548 (setcdr row
(delq rp
(cdr row
)))
550 (setq var-Decls
(delq (car dp
) var-Decls
))))
551 (setq var-Decls
(delq (car dp
) var-Decls
)))
554 (setq dp
(and (not (eq var
'var-All
)) var-Decls
))
555 (while (and (setq dp
(cdr dp
))
556 (or (not (eq (car-safe (car dp
)) 'vec
))
557 (/= (length (car dp
)) 3)
558 (not (equal (nth 2 (car dp
)) decl
)))))
560 (setcar (cdr (car dp
))
561 (append (if (eq (car-safe (nth 1 (car dp
))) 'vec
)
563 (list 'vec
(nth 1 (car dp
))))
564 (list (math-build-var-name var
))))
565 (setq var-Decls
(append var-Decls
567 (math-build-var-name var
)
569 (calc-refresh-evaltos 'var-Decls
))))
571 (defvar calc-dont-insert-variables
'(var-FitRules var-FactorRules
572 var-CommuteRules var-JumpRules
573 var-DistribRules var-MergeRules
574 var-NegateRules var-InvertRules
576 var-TimeZone var-PlotRejects
577 var-PlotData1 var-PlotData2
578 var-PlotData3 var-PlotData4
579 var-PlotData5 var-PlotData6
582 ;; The variable calc-pv-pos is local to calc-permanent-variable, but
583 ;; used by calc-insert-permanent-variable, which is called by
584 ;; calc-permanent-variable.
587 (defun calc-permanent-variable (&optional var
)
590 (or var
(setq var
(calc-read-var-name "Save variable (default all): ")))
592 (and var
(or (and (boundp var
) (symbol-value var
))
593 (error "No such variable")))
594 (set-buffer (find-file-noselect (substitute-in-file-name
595 calc-settings-file
)))
597 (calc-insert-permanent-variable var
)
600 (and (string-match "\\`var-" (symbol-name x
))
601 (not (memq x calc-dont-insert-variables
))
603 (not (eq (car-safe (symbol-value x
)) 'special-const
))
604 (calc-insert-permanent-variable x
))))))
609 (defun calc-insert-permanent-variable (var)
610 (goto-char (point-min))
611 (if (search-forward (concat "(setq " (symbol-name var
) " '") nil t
)
613 (setq calc-pv-pos
(point-marker))
615 (if (looking-at ";;; Variable .* stored by Calc on ")
617 (delete-region (match-end 0) (progn (end-of-line) (point)))
618 (insert (current-time-string))))
619 (goto-char (- calc-pv-pos
8 (length (symbol-name var
))))
622 (delete-region calc-pv-pos
(point)))
623 (goto-char (point-max))
624 (insert "\n;;; Variable \""
626 "\" stored by Calc on "
627 (current-time-string)
632 (insert (prin1-to-string (calc-var-value var
)))
635 (defun calc-insert-variables (buf)
636 (interactive "bBuffer in which to save variable values: ")
641 (and (string-match "\\`var-" (symbol-name x
))
642 (not (memq x calc-dont-insert-variables
))
644 (not (eq (car-safe (symbol-value x
)) 'special-const
))
645 (or (not (eq x
'var-Decls
))
646 (not (equal var-Decls
'(vec))))
647 (or (not (eq x
'var-Holidays
))
648 (not (equal var-Holidays
'(vec (var sat var-sat
)
649 (var sun var-sun
)))))
655 (if (memq calc-language
'(nil big
))
658 (math-format-value (symbol-value x
) 100000)))
661 (defun calc-assign (arg)
664 (calc-binary-op ":=" 'calcFunc-assign arg
)))
666 (defun calc-evalto (arg)
669 (calc-unary-op "=>" 'calcFunc-evalto arg
)))
671 (defun calc-subscript (arg)
674 (calc-binary-op "sub" 'calcFunc-subscr arg
)))
676 (provide 'calc-store
)
678 ;;; arch-tag: 2fbfec82-a521-42ca-bcd8-4f254ae6313e
679 ;;; calc-store.el ends here