1 ;;; calc-store.el --- value storage functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
33 (defvar calc-store-keep nil
)
34 (defun calc-store (&optional var
)
36 (let ((calc-store-keep t
))
37 (calc-store-into var
)))
39 (defvar calc-given-value-flag nil
)
40 (defvar calc-given-value
)
42 (defun calc-store-into (&optional var
)
45 (let ((calc-given-value nil
)
46 (calc-given-value-flag 1))
47 (or var
(setq var
(calc-read-var-name "Store: " t
)))
49 (let ((found (assq var
'( ( + . calc-store-plus
)
50 ( - . calc-store-minus
)
51 ( * . calc-store-times
)
52 ( / . calc-store-div
)
53 ( ^ . calc-store-power
)
54 ( | . calc-store-concat
) ))))
58 (calc-store-value var
(or calc-given-value
(calc-top 1))
59 "" calc-given-value-flag
)))
60 (message "Stored to variable \"%s\"%s"
61 (calc-var-name var
) msg
))))
62 (setq var
(calc-is-assignments (calc-top 1)))
66 (calc-store-value (car (car var
)) (cdr (car var
))
67 (if (not (cdr var
)) "")
68 (if (not (cdr var
)) 1))))
69 (message "Stored to variable \"%s\"%s"
70 (calc-var-name (car (car var
))) msg
))
71 (setq var
(cdr var
))))))))
73 (defun calc-store-plus (&optional var
)
75 (calc-store-binary var
"+" '+))
77 (defun calc-store-minus (&optional var
)
79 (calc-store-binary var
"-" '-
))
81 (defun calc-store-times (&optional var
)
83 (calc-store-binary var
"*" '*))
85 (defun calc-store-div (&optional var
)
87 (calc-store-binary var
"/" '/))
89 (defun calc-store-power (&optional var
)
91 (calc-store-binary var
"^" '^
))
93 (defun calc-store-concat (&optional var
)
95 (calc-store-binary var
"|" '|
))
97 (defun calc-store-neg (n &optional var
)
99 (calc-store-binary var
"n" '/ (- n
)))
101 (defun calc-store-inv (n &optional var
)
103 (calc-store-binary var
"&" '^
(- n
)))
105 (defun calc-store-incr (n &optional var
)
107 (calc-store-binary var
"n" '-
(- n
)))
109 (defun calc-store-decr (n &optional var
)
111 (calc-store-binary var
"n" '- n
))
113 (defun calc-store-value (var value tag
&optional pop
)
116 (let ((old (calc-var-value var
)))
118 (if pop
(or calc-store-keep
(calc-pop-stack pop
)))
119 (calc-record-undo (list 'store
(symbol-name var
) old
))
121 (let ((calc-full-trail-vectors nil
))
122 (calc-record value
(format ">%s%s" tag
(calc-var-name var
)))))
124 ((and (memq var
'(var-e var-i var-pi var-phi var-gamma
))
125 (eq (car-safe old
) 'special-const
))
126 (setq msg
(format " (Note: Built-in definition of %s has been lost)"
127 (calc-var-name var
))))
128 ((and (memq var
'(var-inf var-uinf var-nan
))
130 (setq msg
(format " (Note: %s has built-in meanings which may interfere)"
131 (calc-var-name var
)))))
132 (calc-refresh-evaltos var
)))
135 (defun calc-var-name (var)
136 (if (symbolp var
) (setq var
(symbol-name var
)))
137 (if (string-match "\\`var-." var
)
141 (defun calc-store-binary (var tag func
&optional val
)
143 (let ((calc-simplify-mode (if (eq calc-simplify-mode
'none
)
144 'num calc-simplify-mode
))
145 (value (or val
(calc-top 1))))
146 (or var
(setq var
(calc-read-var-name (format "Store %s: " tag
))))
148 (let ((old (calc-var-value var
)))
149 (if (eq (car-safe old
) 'special-const
)
150 (error "\"%s\" is a special constant" (calc-var-name var
)))
152 (if (memq var
'(var-inf var-uinf var-nan
))
153 (error "\"%s\" is a special variable" (calc-var-name var
))
154 (error "No such variable: \"%s\"" (calc-var-name var
))))
156 (setq old
(math-read-expr old
)))
157 (if (eq (car-safe old
) 'error
)
158 (error "Bad format in variable contents: %s" (nth 2 old
)))
159 (calc-store-value var
160 (calc-normalize (if (calc-is-inverse)
161 (list func value old
)
162 (list func old value
)))
163 tag
(and (not val
) 1))
164 (message "Variable \"%s\" changed" (calc-var-name var
)))))))
166 (defvar calc-var-name-map nil
"Keymap for reading Calc variable names.")
167 (if calc-var-name-map
169 (setq calc-var-name-map
(copy-keymap minibuffer-local-completion-map
))
170 (define-key calc-var-name-map
" " 'self-insert-command
)
173 (define-key calc-var-name-map
(char-to-string x
)
178 (define-key calc-var-name-map
(char-to-string x
)
182 (defvar calc-store-opers
)
184 (defvar calc-read-var-name-history nil
185 "History for reading variable names.")
187 (defun calc-read-var-name (prompt &optional calc-store-opers
)
188 (setq calc-given-value nil
189 calc-aborted-prefix nil
)
192 (let ((minibuffer-completion-table
193 (mapcar (lambda (x) (substring x
4))
194 (all-completions "var-" obarray
)))
195 (minibuffer-completion-predicate
196 (lambda (x) (boundp (intern (concat "var-" x
)))))
197 (minibuffer-completion-confirm t
))
198 (read-from-minibuffer
199 prompt nil calc-var-name-map nil
200 'calc-read-var-name-history
)))))
201 (setq calc-aborted-prefix
"")
202 (and (not (equal var
"var-"))
203 (if (string-match "\\`\\([-a-zA-Zα-ωΑ-Ω0-9]+\\) *:?=" var
)
204 (if (null calc-given-value-flag
)
205 (error "Assignment is not allowed in this command")
206 (let ((svar (intern (substring var
0 (match-end 1)))))
207 (setq calc-given-value-flag
0
208 calc-given-value
(math-read-expr
209 (substring var
(match-end 0))))
210 (if (eq (car-safe calc-given-value
) 'error
)
211 (error "Bad format: %s" (nth 2 calc-given-value
)))
212 (setq calc-given-value
(math-evaluate-expr calc-given-value
))
216 (defun calcVar-digit ()
218 (if (calc-minibuffer-contains "\\'")
219 (if (eq calc-store-opers
0)
222 (self-insert-and-exit))
223 (self-insert-command 1)))
225 (defun calcVar-oper ()
227 (if (and (eq calc-store-opers t
)
228 (calc-minibuffer-contains "\\'"))
231 (self-insert-and-exit))
232 (self-insert-command 1)))
234 (defun calc-store-map (&optional oper var
)
237 (let* ((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-event
))))
383 (defun calc-store-into-quick ()
385 (calc-store-into (intern (format "var-q%c" last-command-event
))))
387 (defun calc-recall-quick ()
389 (calc-recall (intern (format "var-q%c" last-command-event
))))
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 "Variable \"%s\" copied to \"%s\"%s"
425 (calc-var-name var1
) (calc-var-name var2
) msg
)))))))
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
))
445 "Editing variable `%s'" (calc-var-name var
)))
447 (insert (math-format-nice-expr value
(frame-width)) "\n")))))
448 (calc-show-edit-buffer))
450 (defun calc-edit-Decls ()
452 (calc-edit-variable 'var-Decls
))
454 (defun calc-edit-EvalRules ()
456 (calc-edit-variable 'var-EvalRules
))
458 (defun calc-edit-FitRules ()
460 (calc-edit-variable 'var-FitRules
))
462 (defun calc-edit-GenCount ()
464 (calc-edit-variable 'var-GenCount
))
466 (defun calc-edit-Holidays ()
468 (calc-edit-variable 'var-Holidays
))
470 (defun calc-edit-IntegLimit ()
472 (calc-edit-variable 'var-IntegLimit
))
474 (defun calc-edit-LineStyles ()
476 (calc-edit-variable 'var-LineStyles
))
478 (defun calc-edit-PointStyles ()
480 (calc-edit-variable 'var-PointStyles
))
482 (defun calc-edit-PlotRejects ()
484 (calc-edit-variable 'var-PlotRejects
))
486 (defun calc-edit-AlgSimpRules ()
488 (calc-edit-variable 'var-AlgSimpRules
))
490 (defun calc-edit-TimeZone ()
492 (calc-edit-variable 'var-TimeZone
))
494 (defun calc-edit-Units ()
496 (calc-edit-variable 'var-Units
))
498 (defun calc-edit-ExtSimpRules ()
500 (calc-edit-variable 'var-ExtSimpRules
))
502 (defun calc-declare-variable (&optional var
)
505 (or var
(setq var
(calc-read-var-name "Declare: " 0)))
506 (or var
(setq var
'var-All
))
507 (let* (dp decl def row rp
)
508 (or (and (calc-var-value 'var-Decls
)
509 (eq (car-safe var-Decls
) 'vec
))
510 (setq var-Decls
(list 'vec
)))
512 (while (and (setq dp
(cdr dp
))
513 (or (not (eq (car-safe (car dp
)) 'vec
))
514 (/= (length (car dp
)) 3)
516 (setq row
(nth 1 (car dp
))
518 (if (eq (car-safe row
) 'vec
)
521 (and (setq rp
(cdr rp
))
522 (or (not (eq (car-safe (car rp
)) 'var
))
523 (not (eq (nth 2 (car rp
)) var
)))))
525 (if (or (not (eq (car-safe row
) 'var
))
526 (not (eq (nth 2 row
) var
)))
529 (calc-unread-command ?\C-a
)
530 (setq decl
(read-string (format "Declare: %s to be: " (calc-var-name var
))
532 (math-format-flat-expr (nth 2 (car dp
)) 0))))
533 (setq decl
(and (string-match "[^ \t]" decl
)
534 (math-read-exprs decl
)))
535 (if (eq (car-safe decl
) 'error
)
536 (error "Bad format in declaration: %s" (nth 2 decl
)))
538 (setq decl
(cons 'vec decl
))
539 (setq decl
(car decl
)))
540 (and (eq (car-safe decl
) 'vec
)
542 (setq decl
(nth 1 decl
)))
543 (calc-record (append '(vec) (list (math-build-var-name var
))
544 (and decl
(list decl
)))
546 (setq var-Decls
(copy-sequence var-Decls
))
547 (if (eq (car-safe row
) 'vec
)
549 (setcdr row
(delq rp
(cdr row
)))
551 (setq var-Decls
(delq (car dp
) var-Decls
))))
552 (setq var-Decls
(delq (car dp
) var-Decls
)))
555 (setq dp
(and (not (eq var
'var-All
)) var-Decls
))
556 (while (and (setq dp
(cdr dp
))
557 (or (not (eq (car-safe (car dp
)) 'vec
))
558 (/= (length (car dp
)) 3)
559 (not (equal (nth 2 (car dp
)) decl
)))))
561 (setcar (cdr (car dp
))
562 (append (if (eq (car-safe (nth 1 (car dp
))) 'vec
)
564 (list 'vec
(nth 1 (car dp
))))
565 (list (math-build-var-name var
))))
566 (setq var-Decls
(append var-Decls
568 (math-build-var-name var
)
570 (calc-refresh-evaltos 'var-Decls
))))
572 (defvar calc-dont-insert-variables
'(var-FitRules var-FactorRules
573 var-CommuteRules var-JumpRules
574 var-DistribRules var-MergeRules
575 var-NegateRules var-InvertRules
577 var-TimeZone var-PlotRejects
578 var-PlotData1 var-PlotData2
579 var-PlotData3 var-PlotData4
580 var-PlotData5 var-PlotData6
583 ;; The variable calc-pv-pos is local to calc-permanent-variable, but
584 ;; used by calc-insert-permanent-variable, which is called by
585 ;; calc-permanent-variable.
588 (defun calc-permanent-variable (&optional var
)
591 (or var
(setq var
(calc-read-var-name "Save variable (default all): ")))
593 (and var
(or (and (boundp var
) (symbol-value var
))
594 (error "No such variable")))
595 (set-buffer (find-file-noselect (substitute-in-file-name
596 calc-settings-file
)))
598 (calc-insert-permanent-variable var
)
601 (and (string-match "\\`var-" (symbol-name x
))
602 (not (memq x calc-dont-insert-variables
))
604 (not (eq (car-safe (symbol-value x
)) 'special-const
))
605 (calc-insert-permanent-variable x
))))))
610 (defun calc-insert-permanent-variable (var)
611 (goto-char (point-min))
612 (if (let (case-fold-search)
613 (search-forward (concat "(setq " (symbol-name var
) " '") nil t
))
615 (setq calc-pv-pos
(point-marker))
617 (if (looking-at ";;; Variable .* stored by Calc on ")
619 (delete-region (match-end 0) (progn (end-of-line) (point)))
620 (insert (current-time-string))))
621 (goto-char (- calc-pv-pos
8 (length (symbol-name var
))))
624 (delete-region calc-pv-pos
(point)))
625 (goto-char (point-max))
626 (insert "\n;;; Variable \""
628 "\" stored by Calc on "
629 (current-time-string)
634 (insert (prin1-to-string (calc-var-value var
)))
637 (defun calc-insert-variables (buf)
638 (interactive "bBuffer in which to save variable values: ")
639 (with-current-buffer buf
642 (and (string-match "\\`var-" (symbol-name x
))
643 (not (memq x calc-dont-insert-variables
))
645 (not (eq (car-safe (symbol-value x
)) 'special-const
))
646 (or (not (eq x
'var-Decls
))
647 (not (equal var-Decls
'(vec))))
648 (or (not (eq x
'var-Holidays
))
649 (not (equal var-Holidays
'(vec (var sat var-sat
)
650 (var sun var-sun
)))))
656 (if (memq calc-language
'(nil big
))
659 (math-format-value (symbol-value x
) 100000)))
662 (defun calc-assign (arg)
665 (calc-binary-op ":=" 'calcFunc-assign arg
)))
667 (defun calc-evalto (arg)
670 (calc-unary-op "=>" 'calcFunc-evalto arg
)))
672 (defun calc-subscript (arg)
675 (calc-binary-op "sub" 'calcFunc-subscr arg
)))
677 (provide 'calc-store
)
679 ;;; calc-store.el ends here