1 ;;; calc-store.el --- value storage functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
35 (defvar calc-store-keep nil
)
36 (defun calc-store (&optional var
)
38 (let ((calc-store-keep t
))
39 (calc-store-into var
)))
41 (defvar calc-given-value-flag nil
)
42 (defvar calc-given-value
)
44 (defun calc-store-into (&optional var
)
47 (let ((calc-given-value nil
)
48 (calc-given-value-flag 1))
49 (or var
(setq var
(calc-read-var-name "Store: " t
)))
51 (let ((found (assq var
'( ( + . calc-store-plus
)
52 ( - . calc-store-minus
)
53 ( * . calc-store-times
)
54 ( / . calc-store-div
)
55 ( ^ . calc-store-power
)
56 ( | . calc-store-concat
) ))))
60 (calc-store-value var
(or calc-given-value
(calc-top 1))
61 "" calc-given-value-flag
)))
62 (message (concat "Stored to variable \"%s\"" msg
)
63 (calc-var-name var
)))))
64 (setq var
(calc-is-assignments (calc-top 1)))
68 (calc-store-value (car (car var
)) (cdr (car var
))
69 (if (not (cdr var
)) "")
70 (if (not (cdr var
)) 1))))
71 (message (concat "Stored to variable \"%s\"" msg
)
72 (calc-var-name (car (car var
)))))
73 (setq var
(cdr var
))))))))
75 (defun calc-store-plus (&optional var
)
77 (calc-store-binary var
"+" '+))
79 (defun calc-store-minus (&optional var
)
81 (calc-store-binary var
"-" '-
))
83 (defun calc-store-times (&optional var
)
85 (calc-store-binary var
"*" '*))
87 (defun calc-store-div (&optional var
)
89 (calc-store-binary var
"/" '/))
91 (defun calc-store-power (&optional var
)
93 (calc-store-binary var
"^" '^
))
95 (defun calc-store-concat (&optional var
)
97 (calc-store-binary var
"|" '|
))
99 (defun calc-store-neg (n &optional var
)
101 (calc-store-binary var
"n" '/ (- n
)))
103 (defun calc-store-inv (n &optional var
)
105 (calc-store-binary var
"&" '^
(- n
)))
107 (defun calc-store-incr (n &optional var
)
109 (calc-store-binary var
"n" '-
(- n
)))
111 (defun calc-store-decr (n &optional var
)
113 (calc-store-binary var
"n" '- n
))
115 (defun calc-store-value (var value tag
&optional pop
)
118 (let ((old (calc-var-value var
)))
120 (if pop
(or calc-store-keep
(calc-pop-stack pop
)))
121 (calc-record-undo (list 'store
(symbol-name var
) old
))
123 (let ((calc-full-trail-vectors nil
))
124 (calc-record value
(format ">%s%s" tag
(calc-var-name var
)))))
126 ((and (memq var
'(var-e var-i var-pi var-phi var-gamma
))
127 (eq (car-safe old
) 'special-const
))
128 (setq msg
(format " (Note: Built-in definition of %s has been lost)"
129 (calc-var-name var
))))
130 ((and (memq var
'(var-inf var-uinf var-nan
))
132 (setq msg
(format " (Note: %s has built-in meanings which may interfere)"
133 (calc-var-name var
)))))
134 (calc-refresh-evaltos var
)))
137 (defun calc-var-name (var)
138 (if (symbolp var
) (setq var
(symbol-name var
)))
139 (if (string-match "\\`var-." var
)
143 (defun calc-store-binary (var tag func
&optional val
)
145 (let ((calc-simplify-mode (if (eq calc-simplify-mode
'none
)
146 'num calc-simplify-mode
))
147 (value (or val
(calc-top 1))))
148 (or var
(setq var
(calc-read-var-name (format "Store %s: " tag
))))
150 (let ((old (calc-var-value var
)))
151 (if (eq (car-safe old
) 'special-const
)
152 (error "\"%s\" is a special constant" (calc-var-name var
)))
154 (if (memq var
'(var-inf var-uinf var-nan
))
155 (error "\"%s\" is a special variable" (calc-var-name var
))
156 (error "No such variable: \"%s\"" (calc-var-name var
))))
158 (setq old
(math-read-expr old
)))
159 (if (eq (car-safe old
) 'error
)
160 (error "Bad format in variable contents: %s" (nth 2 old
)))
161 (calc-store-value var
162 (calc-normalize (if (calc-is-inverse)
163 (list func value old
)
164 (list func old value
)))
165 tag
(and (not val
) 1))
166 (message "Variable \"%s\" changed" (calc-var-name var
)))))))
168 (defvar calc-var-name-map nil
"Keymap for reading Calc variable names.")
169 (if calc-var-name-map
171 (setq calc-var-name-map
(copy-keymap minibuffer-local-completion-map
))
172 (define-key calc-var-name-map
" " 'self-insert-command
)
175 (define-key calc-var-name-map
(char-to-string x
)
180 (define-key calc-var-name-map
(char-to-string x
)
184 (defvar calc-store-opers
)
186 (defvar calc-read-var-name-history nil
187 "History for reading variable names.")
189 (defun calc-read-var-name (prompt &optional calc-store-opers
)
190 (setq calc-given-value nil
191 calc-aborted-prefix nil
)
194 (let ((minibuffer-completion-table
195 (mapcar (lambda (x) (substring x
4))
196 (all-completions "var-" obarray
)))
197 (minibuffer-completion-predicate
198 (lambda (x) (boundp (intern (concat "var-" x
)))))
199 (minibuffer-completion-confirm t
))
200 (read-from-minibuffer
201 prompt nil calc-var-name-map nil
202 'calc-read-var-name-history
)))))
203 (setq calc-aborted-prefix
"")
204 (and (not (equal var
"var-"))
205 (if (string-match "\\`\\([-a-zA-Z0-9]+\\) *:?=" var
)
206 (if (null calc-given-value-flag
)
207 (error "Assignment is not allowed in this command")
208 (let ((svar (intern (substring var
0 (match-end 1)))))
209 (setq calc-given-value-flag
0
210 calc-given-value
(math-read-expr
211 (substring var
(match-end 0))))
212 (if (eq (car-safe calc-given-value
) 'error
)
213 (error "Bad format: %s" (nth 2 calc-given-value
)))
214 (setq calc-given-value
(math-evaluate-expr calc-given-value
))
218 (defun calcVar-digit ()
220 (if (calc-minibuffer-contains "\\'")
221 (if (eq calc-store-opers
0)
224 (self-insert-and-exit))
225 (self-insert-command 1)))
227 (defun calcVar-oper ()
229 (if (and (eq calc-store-opers t
)
230 (calc-minibuffer-contains "\\'"))
233 (self-insert-and-exit))
234 (self-insert-command 1)))
236 (defun calc-store-map (&optional oper var
)
239 (let* ((sel-mode nil
)
240 (calc-dollar-values (mapcar 'calc-get-stack-element
241 (nthcdr calc-stack-top calc-stack
)))
243 (oper (or oper
(calc-get-operator "Store Mapping")))
245 (or var
(setq var
(calc-read-var-name (format "Store Mapping %s: "
248 (let ((old (calc-var-value var
)))
249 (if (eq (car-safe old
) 'special-const
)
250 (error "\"%s\" is a special constant" (calc-var-name var
)))
252 (if (memq var
'(var-inf var-uinf var-nan
))
253 (error "\"%s\" is a special variable" (calc-var-name var
))
254 (error "No such variable: \"%s\"" (calc-var-name var
))))
255 (let ((calc-simplify-mode (if (eq calc-simplify-mode
'none
)
256 'num calc-simplify-mode
))
257 (values (and (> nargs
1)
258 (calc-top-list (1- nargs
) (1+ calc-dollar-used
)))))
259 (message "Working...")
260 (calc-set-command-flag 'clear-message
)
262 (setq old
(math-read-expr old
)))
263 (if (eq (car-safe old
) 'error
)
264 (error "Bad format in variable contents: %s" (nth 2 old
)))
265 (setq values
(if (calc-is-inverse)
266 (append values
(list old
))
267 (append (list old
) values
)))
268 (calc-store-value var
269 (calc-normalize (cons (nth 1 oper
) values
))
271 (+ calc-dollar-used
(1- nargs
)))
272 (message "Variable \"%s\" changed" (calc-var-name var
))))))))
275 (defun calc-store-exchange (&optional var
)
278 (let ((calc-given-value nil
)
279 (calc-given-value-flag 1)
281 (or var
(setq var
(calc-read-var-name "Exchange with: ")))
283 (let ((value (calc-var-value var
)))
284 (if (eq (car-safe value
) 'special-const
)
285 (error "\"%s\" is a special constant" (calc-var-name var
)))
287 (if (memq var
'(var-inf var-uinf var-nan
))
288 (error "\"%s\" is a special variable" (calc-var-name var
))
289 (error "No such variable: \"%s\"" (calc-var-name var
))))
290 (setq top
(or calc-given-value
(calc-top 1)))
291 (calc-store-value var top nil
)
292 (calc-pop-push-record calc-given-value-flag
293 (concat "<>" (calc-var-name var
)) value
))))))
295 (defun calc-unstore (&optional var
)
298 (or var
(setq var
(calc-read-var-name "Unstore: ")))
301 (and (memq var
'(var-e var-i var-pi var-phi var-gamma
))
302 (eq (car-safe (calc-var-value var
)) 'special-const
)
303 (message "(Note: Built-in definition of %s has been lost)" var
))
304 (if (and (boundp var
) (symbol-value var
))
305 (message "Unstored variable \"%s\"" (calc-var-name var
))
306 (message "Variable \"%s\" remains unstored" (calc-var-name var
)))
308 (calc-refresh-evaltos var
)))))
310 (defun calc-let (&optional var
)
313 (let* ((calc-given-value nil
)
314 (calc-given-value-flag 1)
316 (or var
(setq var
(calc-read-var-name "Let variable: ")))
318 (setq value calc-given-value
320 (setq value
(calc-top 1)
323 (list (cons var value
))
324 (calc-is-assignments value
)))
326 (calc-pop-push-record
327 (1+ calc-given-value-flag
)
328 (concat "=" (calc-var-name (car (car var
))))
329 (let ((saved-val (mapcar (function
331 (and (boundp (car v
))
332 (symbol-value (car v
)))))
337 (set (car (car vv
)) (calc-normalize (cdr (car vv
))))
338 (calc-refresh-evaltos (car (car vv
)))
340 (math-evaluate-expr thing
))
343 (set (car (car var
)) (car saved-val
))
344 (makunbound (car (car var
))))
345 (setq saved-val
(cdr saved-val
)
347 (calc-handle-whys))))))))
349 (defun calc-is-assignments (value)
350 (if (memq (car-safe value
) '(calcFunc-eq calcFunc-assign
))
351 (and (eq (car-safe (nth 1 value
)) 'var
)
352 (list (cons (nth 2 (nth 1 value
)) (nth 2 value
))))
353 (if (eq (car-safe value
) 'vec
)
355 (while (and (setq value
(cdr value
))
356 (memq (car-safe (car value
))
357 '(calcFunc-eq calcFunc-assign
))
358 (eq (car-safe (nth 1 (car value
))) 'var
))
359 (setq vv
(cons (cons (nth 2 (nth 1 (car value
)))
365 (defun calc-recall (&optional var
)
368 (or var
(setq var
(calc-read-var-name "Recall: ")))
370 (let ((value (calc-var-value var
)))
372 (error "No such variable: \"%s\"" (calc-var-name var
)))
374 (setq value
(math-read-expr value
)))
375 (if (eq (car-safe value
) 'error
)
376 (error "Bad format in variable contents: %s" (nth 2 value
)))
377 (setq value
(calc-normalize value
))
378 (let ((calc-full-trail-vectors nil
))
379 (calc-record value
(concat "<" (calc-var-name var
))))
380 (calc-push value
)))))
382 (defun calc-store-quick ()
384 (calc-store (intern (format "var-q%c" last-command-char
))))
386 (defun calc-store-into-quick ()
388 (calc-store-into (intern (format "var-q%c" last-command-char
))))
390 (defun calc-recall-quick ()
392 (calc-recall (intern (format "var-q%c" last-command-char
))))
394 (defun calc-copy-special-constant (&optional sconst var
)
397 ("e" .
(special-const (math-e)))
398 ("pi" .
(special-const (math-pi)))
399 ("i" .
(special-const (math-imaginary 1)))
400 ("phi" .
(special-const (math-phi)))
401 ("gamma" .
(special-const (math-gamma-const))))))
403 (or sconst
(setq sconst
(completing-read "Special constant: " sc nil t
)))
404 (unless (string= sconst
"")
405 (let ((value (cdr (assoc sconst sc
))))
406 (or var
(setq var
(calc-read-var-name
407 (format "Copy special constant %s, to: "
410 (let ((msg (calc-store-value var value
"")))
411 (message (concat "Special constant \"%s\" copied to \"%s\"" msg
)
412 sconst
(calc-var-name var
)))))))))
414 (defun calc-copy-variable (&optional var1 var2
)
417 (or var1
(setq var1
(calc-read-var-name "Copy variable: ")))
419 (let ((value (calc-var-value var1
)))
421 (error "No such variable: \"%s\"" (calc-var-name var1
)))
422 (or var2
(setq var2
(calc-read-var-name
423 (format "Copy variable: %s, to: "
424 (calc-var-name var1
)))))
426 (let ((msg (calc-store-value var2 value
"")))
427 (message (concat "Variable \"%s\" copied to \"%s\"" msg
)
428 (calc-var-name var1
) (calc-var-name var2
))))))))
430 (defvar calc-last-edited-variable nil
)
431 (defun calc-edit-variable (&optional var
)
434 (or var
(setq var
(calc-read-var-name
435 (if calc-last-edited-variable
436 (format "Edit (default %s): "
437 (calc-var-name calc-last-edited-variable
))
439 (or var
(setq var calc-last-edited-variable
))
441 (let* ((value (calc-var-value var
)))
442 (if (eq (car-safe value
) 'special-const
)
443 (error "%s is a special constant" var
))
444 (setq calc-last-edited-variable var
)
445 (calc-edit-mode (list 'calc-finish-stack-edit
(list 'quote var
))
447 (concat "Editing variable `" (calc-var-name var
) "'. "))
449 (insert (math-format-nice-expr value
(frame-width)) "\n")))))
450 (calc-show-edit-buffer))
452 (defun calc-edit-Decls ()
454 (calc-edit-variable 'var-Decls
))
456 (defun calc-edit-EvalRules ()
458 (calc-edit-variable 'var-EvalRules
))
460 (defun calc-edit-FitRules ()
462 (calc-edit-variable 'var-FitRules
))
464 (defun calc-edit-GenCount ()
466 (calc-edit-variable 'var-GenCount
))
468 (defun calc-edit-Holidays ()
470 (calc-edit-variable 'var-Holidays
))
472 (defun calc-edit-IntegLimit ()
474 (calc-edit-variable 'var-IntegLimit
))
476 (defun calc-edit-LineStyles ()
478 (calc-edit-variable 'var-LineStyles
))
480 (defun calc-edit-PointStyles ()
482 (calc-edit-variable 'var-PointStyles
))
484 (defun calc-edit-PlotRejects ()
486 (calc-edit-variable 'var-PlotRejects
))
488 (defun calc-edit-AlgSimpRules ()
490 (calc-edit-variable 'var-AlgSimpRules
))
492 (defun calc-edit-TimeZone ()
494 (calc-edit-variable 'var-TimeZone
))
496 (defun calc-edit-Units ()
498 (calc-edit-variable 'var-Units
))
500 (defun calc-edit-ExtSimpRules ()
502 (calc-edit-variable 'var-ExtSimpRules
))
504 (defun calc-declare-variable (&optional var
)
507 (or var
(setq var
(calc-read-var-name "Declare: " 0)))
508 (or var
(setq var
'var-All
))
509 (let* (dp decl def row rp
)
510 (or (and (calc-var-value 'var-Decls
)
511 (eq (car-safe var-Decls
) 'vec
))
512 (setq var-Decls
(list 'vec
)))
514 (while (and (setq dp
(cdr dp
))
515 (or (not (eq (car-safe (car dp
)) 'vec
))
516 (/= (length (car dp
)) 3)
518 (setq row
(nth 1 (car dp
))
520 (if (eq (car-safe row
) 'vec
)
523 (and (setq rp
(cdr rp
))
524 (or (not (eq (car-safe (car rp
)) 'var
))
525 (not (eq (nth 2 (car rp
)) var
)))))
527 (if (or (not (eq (car-safe row
) 'var
))
528 (not (eq (nth 2 row
) var
)))
531 (calc-unread-command ?\C-a
)
532 (setq decl
(read-string (format "Declare: %s to be: " (calc-var-name var
))
534 (math-format-flat-expr (nth 2 (car dp
)) 0))))
535 (setq decl
(and (string-match "[^ \t]" decl
)
536 (math-read-exprs decl
)))
537 (if (eq (car-safe decl
) 'error
)
538 (error "Bad format in declaration: %s" (nth 2 decl
)))
540 (setq decl
(cons 'vec decl
))
541 (setq decl
(car decl
)))
542 (and (eq (car-safe decl
) 'vec
)
544 (setq decl
(nth 1 decl
)))
545 (calc-record (append '(vec) (list (math-build-var-name var
))
546 (and decl
(list decl
)))
548 (setq var-Decls
(copy-sequence var-Decls
))
549 (if (eq (car-safe row
) 'vec
)
551 (setcdr row
(delq rp
(cdr row
)))
553 (setq var-Decls
(delq (car dp
) var-Decls
))))
554 (setq var-Decls
(delq (car dp
) var-Decls
)))
557 (setq dp
(and (not (eq var
'var-All
)) var-Decls
))
558 (while (and (setq dp
(cdr dp
))
559 (or (not (eq (car-safe (car dp
)) 'vec
))
560 (/= (length (car dp
)) 3)
561 (not (equal (nth 2 (car dp
)) decl
)))))
563 (setcar (cdr (car dp
))
564 (append (if (eq (car-safe (nth 1 (car dp
))) 'vec
)
566 (list 'vec
(nth 1 (car dp
))))
567 (list (math-build-var-name var
))))
568 (setq var-Decls
(append var-Decls
570 (math-build-var-name var
)
572 (calc-refresh-evaltos 'var-Decls
))))
574 (defvar calc-dont-insert-variables
'(var-FitRules var-FactorRules
575 var-CommuteRules var-JumpRules
576 var-DistribRules var-MergeRules
577 var-NegateRules var-InvertRules
579 var-TimeZone var-PlotRejects
580 var-PlotData1 var-PlotData2
581 var-PlotData3 var-PlotData4
582 var-PlotData5 var-PlotData6
585 ;; The variable calc-pv-pos is local to calc-permanent-variable, but
586 ;; used by calc-insert-permanent-variable, which is called by
587 ;; calc-permanent-variable.
590 (defun calc-permanent-variable (&optional var
)
593 (or var
(setq var
(calc-read-var-name "Save variable (default all): ")))
595 (and var
(or (and (boundp var
) (symbol-value var
))
596 (error "No such variable")))
597 (set-buffer (find-file-noselect (substitute-in-file-name
598 calc-settings-file
)))
600 (calc-insert-permanent-variable var
)
603 (and (string-match "\\`var-" (symbol-name x
))
604 (not (memq x calc-dont-insert-variables
))
606 (not (eq (car-safe (symbol-value x
)) 'special-const
))
607 (calc-insert-permanent-variable x
))))))
612 (defun calc-insert-permanent-variable (var)
613 (goto-char (point-min))
614 (if (search-forward (concat "(setq " (symbol-name var
) " '") nil t
)
616 (setq calc-pv-pos
(point-marker))
618 (if (looking-at ";;; Variable .* stored by Calc on ")
620 (delete-region (match-end 0) (progn (end-of-line) (point)))
621 (insert (current-time-string))))
622 (goto-char (- calc-pv-pos
8 (length (symbol-name var
))))
625 (delete-region calc-pv-pos
(point)))
626 (goto-char (point-max))
627 (insert "\n;;; Variable \""
629 "\" stored by Calc on "
630 (current-time-string)
635 (insert (prin1-to-string (calc-var-value var
)))
638 (defun calc-insert-variables (buf)
639 (interactive "bBuffer in which to save variable values: ")
644 (and (string-match "\\`var-" (symbol-name x
))
645 (not (memq x calc-dont-insert-variables
))
647 (not (eq (car-safe (symbol-value x
)) 'special-const
))
648 (or (not (eq x
'var-Decls
))
649 (not (equal var-Decls
'(vec))))
650 (or (not (eq x
'var-Holidays
))
651 (not (equal var-Holidays
'(vec (var sat var-sat
)
652 (var sun var-sun
)))))
658 (if (memq calc-language
'(nil big
))
661 (math-format-value (symbol-value x
) 100000)))
664 (defun calc-assign (arg)
667 (calc-binary-op ":=" 'calcFunc-assign arg
)))
669 (defun calc-evalto (arg)
672 (calc-unary-op "=>" 'calcFunc-evalto arg
)))
674 (defun calc-subscript (arg)
677 (calc-binary-op "sub" 'calcFunc-subscr arg
)))
679 (provide 'calc-store
)
681 ;; arch-tag: 2fbfec82-a521-42ca-bcd8-4f254ae6313e
682 ;;; calc-store.el ends here