1 ;;; calc-store.el --- value storage functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
34 (defvar calc-store-keep nil
)
35 (defun calc-store (&optional var
)
37 (let ((calc-store-keep t
))
38 (calc-store-into var
)))
40 (defvar calc-given-value-flag nil
)
41 (defvar calc-given-value
)
43 (defun calc-store-into (&optional var
)
46 (let ((calc-given-value nil
)
47 (calc-given-value-flag 1))
48 (or var
(setq var
(calc-read-var-name "Store: " t
)))
50 (let ((found (assq var
'( ( + . calc-store-plus
)
51 ( - . calc-store-minus
)
52 ( * . calc-store-times
)
53 ( / . calc-store-div
)
54 ( ^ . calc-store-power
)
55 ( | . calc-store-concat
) ))))
59 (calc-store-value var
(or calc-given-value
(calc-top 1))
60 "" calc-given-value-flag
)))
61 (message (concat "Stored to variable \"%s\"" msg
)
62 (calc-var-name var
)))))
63 (setq var
(calc-is-assignments (calc-top 1)))
67 (calc-store-value (car (car var
)) (cdr (car var
))
68 (if (not (cdr var
)) "")
69 (if (not (cdr var
)) 1))))
70 (message (concat "Stored to variable \"%s\"" msg
)
71 (calc-var-name (car (car var
)))))
72 (setq var
(cdr var
))))))))
74 (defun calc-store-plus (&optional var
)
76 (calc-store-binary var
"+" '+))
78 (defun calc-store-minus (&optional var
)
80 (calc-store-binary var
"-" '-
))
82 (defun calc-store-times (&optional var
)
84 (calc-store-binary var
"*" '*))
86 (defun calc-store-div (&optional var
)
88 (calc-store-binary var
"/" '/))
90 (defun calc-store-power (&optional var
)
92 (calc-store-binary var
"^" '^
))
94 (defun calc-store-concat (&optional var
)
96 (calc-store-binary var
"|" '|
))
98 (defun calc-store-neg (n &optional var
)
100 (calc-store-binary var
"n" '/ (- n
)))
102 (defun calc-store-inv (n &optional var
)
104 (calc-store-binary var
"&" '^
(- n
)))
106 (defun calc-store-incr (n &optional var
)
108 (calc-store-binary var
"n" '-
(- n
)))
110 (defun calc-store-decr (n &optional var
)
112 (calc-store-binary var
"n" '- n
))
114 (defun calc-store-value (var value tag
&optional pop
)
117 (let ((old (calc-var-value var
)))
119 (if pop
(or calc-store-keep
(calc-pop-stack pop
)))
120 (calc-record-undo (list 'store
(symbol-name var
) old
))
122 (let ((calc-full-trail-vectors nil
))
123 (calc-record value
(format ">%s%s" tag
(calc-var-name var
)))))
125 ((and (memq var
'(var-e var-i var-pi var-phi var-gamma
))
126 (eq (car-safe old
) 'special-const
))
127 (setq msg
(format " (Note: Built-in definition of %s has been lost)"
128 (calc-var-name var
))))
129 ((and (memq var
'(var-inf var-uinf var-nan
))
131 (setq msg
(format " (Note: %s has built-in meanings which may interfere)"
132 (calc-var-name var
)))))
133 (calc-refresh-evaltos var
)))
136 (defun calc-var-name (var)
137 (if (symbolp var
) (setq var
(symbol-name var
)))
138 (if (string-match "\\`var-." var
)
142 (defun calc-store-binary (var tag func
&optional val
)
144 (let ((calc-simplify-mode (if (eq calc-simplify-mode
'none
)
145 'num calc-simplify-mode
))
146 (value (or val
(calc-top 1))))
147 (or var
(setq var
(calc-read-var-name (format "Store %s: " tag
))))
149 (let ((old (calc-var-value var
)))
150 (if (eq (car-safe old
) 'special-const
)
151 (error "\"%s\" is a special constant" (calc-var-name var
)))
153 (if (memq var
'(var-inf var-uinf var-nan
))
154 (error "\"%s\" is a special variable" (calc-var-name var
))
155 (error "No such variable: \"%s\"" (calc-var-name var
))))
157 (setq old
(math-read-expr old
)))
158 (if (eq (car-safe old
) 'error
)
159 (error "Bad format in variable contents: %s" (nth 2 old
)))
160 (calc-store-value var
161 (calc-normalize (if (calc-is-inverse)
162 (list func value old
)
163 (list func old value
)))
164 tag
(and (not val
) 1))
165 (message "Variable \"%s\" changed" (calc-var-name var
)))))))
167 (defvar calc-var-name-map nil
"Keymap for reading Calc variable names.")
168 (if calc-var-name-map
170 (setq calc-var-name-map
(copy-keymap minibuffer-local-completion-map
))
171 (define-key calc-var-name-map
" " 'self-insert-command
)
174 (define-key calc-var-name-map
(char-to-string x
)
179 (define-key calc-var-name-map
(char-to-string x
)
183 (defvar calc-store-opers
)
185 (defvar calc-read-var-name-history nil
186 "History for reading variable names.")
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
200 prompt nil calc-var-name-map nil
201 'calc-read-var-name-history
)))))
202 (setq calc-aborted-prefix
"")
203 (and (not (equal var
"var-"))
204 (if (string-match "\\`\\([-a-zA-Zα-ωΑ-Ω0-9]+\\) *:?=" var
)
205 (if (null calc-given-value-flag
)
206 (error "Assignment is not allowed in this command")
207 (let ((svar (intern (substring var
0 (match-end 1)))))
208 (setq calc-given-value-flag
0
209 calc-given-value
(math-read-expr
210 (substring var
(match-end 0))))
211 (if (eq (car-safe calc-given-value
) 'error
)
212 (error "Bad format: %s" (nth 2 calc-given-value
)))
213 (setq calc-given-value
(math-evaluate-expr calc-given-value
))
217 (defun calcVar-digit ()
219 (if (calc-minibuffer-contains "\\'")
220 (if (eq calc-store-opers
0)
223 (self-insert-and-exit))
224 (self-insert-command 1)))
226 (defun calcVar-oper ()
228 (if (and (eq calc-store-opers t
)
229 (calc-minibuffer-contains "\\'"))
232 (self-insert-and-exit))
233 (self-insert-command 1)))
235 (defun calc-store-map (&optional oper var
)
238 (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
239 (nthcdr calc-stack-top calc-stack
)))
241 (oper (or oper
(calc-get-operator "Store Mapping")))
243 (or var
(setq var
(calc-read-var-name (format "Store Mapping %s: "
246 (let ((old (calc-var-value var
)))
247 (if (eq (car-safe old
) 'special-const
)
248 (error "\"%s\" is a special constant" (calc-var-name var
)))
250 (if (memq var
'(var-inf var-uinf var-nan
))
251 (error "\"%s\" is a special variable" (calc-var-name var
))
252 (error "No such variable: \"%s\"" (calc-var-name var
))))
253 (let ((calc-simplify-mode (if (eq calc-simplify-mode
'none
)
254 'num calc-simplify-mode
))
255 (values (and (> nargs
1)
256 (calc-top-list (1- nargs
) (1+ calc-dollar-used
)))))
257 (message "Working...")
258 (calc-set-command-flag 'clear-message
)
260 (setq old
(math-read-expr old
)))
261 (if (eq (car-safe old
) 'error
)
262 (error "Bad format in variable contents: %s" (nth 2 old
)))
263 (setq values
(if (calc-is-inverse)
264 (append values
(list old
))
265 (append (list old
) values
)))
266 (calc-store-value var
267 (calc-normalize (cons (nth 1 oper
) values
))
269 (+ calc-dollar-used
(1- nargs
)))
270 (message "Variable \"%s\" changed" (calc-var-name var
))))))))
273 (defun calc-store-exchange (&optional var
)
276 (let ((calc-given-value nil
)
277 (calc-given-value-flag 1)
279 (or var
(setq var
(calc-read-var-name "Exchange with: ")))
281 (let ((value (calc-var-value var
)))
282 (if (eq (car-safe value
) 'special-const
)
283 (error "\"%s\" is a special constant" (calc-var-name var
)))
285 (if (memq var
'(var-inf var-uinf var-nan
))
286 (error "\"%s\" is a special variable" (calc-var-name var
))
287 (error "No such variable: \"%s\"" (calc-var-name var
))))
288 (setq top
(or calc-given-value
(calc-top 1)))
289 (calc-store-value var top nil
)
290 (calc-pop-push-record calc-given-value-flag
291 (concat "<>" (calc-var-name var
)) value
))))))
293 (defun calc-unstore (&optional var
)
296 (or var
(setq var
(calc-read-var-name "Unstore: ")))
299 (and (memq var
'(var-e var-i var-pi var-phi var-gamma
))
300 (eq (car-safe (calc-var-value var
)) 'special-const
)
301 (message "(Note: Built-in definition of %s has been lost)" var
))
302 (if (and (boundp var
) (symbol-value var
))
303 (message "Unstored variable \"%s\"" (calc-var-name var
))
304 (message "Variable \"%s\" remains unstored" (calc-var-name var
)))
306 (calc-refresh-evaltos var
)))))
308 (defun calc-let (&optional var
)
311 (let* ((calc-given-value nil
)
312 (calc-given-value-flag 1)
314 (or var
(setq var
(calc-read-var-name "Let variable: ")))
316 (setq value calc-given-value
318 (setq value
(calc-top 1)
321 (list (cons var value
))
322 (calc-is-assignments value
)))
324 (calc-pop-push-record
325 (1+ calc-given-value-flag
)
326 (concat "=" (calc-var-name (car (car var
))))
327 (let ((saved-val (mapcar (function
329 (and (boundp (car v
))
330 (symbol-value (car v
)))))
335 (set (car (car vv
)) (calc-normalize (cdr (car vv
))))
336 (calc-refresh-evaltos (car (car vv
)))
338 (math-evaluate-expr thing
))
341 (set (car (car var
)) (car saved-val
))
342 (makunbound (car (car var
))))
343 (setq saved-val
(cdr saved-val
)
345 (calc-handle-whys))))))))
347 (defun calc-is-assignments (value)
348 (if (memq (car-safe value
) '(calcFunc-eq calcFunc-assign
))
349 (and (eq (car-safe (nth 1 value
)) 'var
)
350 (list (cons (nth 2 (nth 1 value
)) (nth 2 value
))))
351 (if (eq (car-safe value
) 'vec
)
353 (while (and (setq value
(cdr value
))
354 (memq (car-safe (car value
))
355 '(calcFunc-eq calcFunc-assign
))
356 (eq (car-safe (nth 1 (car value
))) 'var
))
357 (setq vv
(cons (cons (nth 2 (nth 1 (car value
)))
363 (defun calc-recall (&optional var
)
366 (or var
(setq var
(calc-read-var-name "Recall: ")))
368 (let ((value (calc-var-value var
)))
370 (error "No such variable: \"%s\"" (calc-var-name var
)))
372 (setq value
(math-read-expr value
)))
373 (if (eq (car-safe value
) 'error
)
374 (error "Bad format in variable contents: %s" (nth 2 value
)))
375 (setq value
(calc-normalize value
))
376 (let ((calc-full-trail-vectors nil
))
377 (calc-record value
(concat "<" (calc-var-name var
))))
378 (calc-push value
)))))
380 (defun calc-store-quick ()
382 (calc-store (intern (format "var-q%c" last-command-event
))))
384 (defun calc-store-into-quick ()
386 (calc-store-into (intern (format "var-q%c" last-command-event
))))
388 (defun calc-recall-quick ()
390 (calc-recall (intern (format "var-q%c" last-command-event
))))
392 (defun calc-copy-special-constant (&optional sconst var
)
395 ("e" .
(special-const (math-e)))
396 ("pi" .
(special-const (math-pi)))
397 ("i" .
(special-const (math-imaginary 1)))
398 ("phi" .
(special-const (math-phi)))
399 ("gamma" .
(special-const (math-gamma-const))))))
401 (or sconst
(setq sconst
(completing-read "Special constant: " sc nil t
)))
402 (unless (string= sconst
"")
403 (let ((value (cdr (assoc sconst sc
))))
404 (or var
(setq var
(calc-read-var-name
405 (format "Copy special constant %s, to: "
408 (let ((msg (calc-store-value var value
"")))
409 (message (concat "Special constant \"%s\" copied to \"%s\"" msg
)
410 sconst
(calc-var-name var
)))))))))
412 (defun calc-copy-variable (&optional var1 var2
)
415 (or var1
(setq var1
(calc-read-var-name "Copy variable: ")))
417 (let ((value (calc-var-value var1
)))
419 (error "No such variable: \"%s\"" (calc-var-name var1
)))
420 (or var2
(setq var2
(calc-read-var-name
421 (format "Copy variable: %s, to: "
422 (calc-var-name var1
)))))
424 (let ((msg (calc-store-value var2 value
"")))
425 (message (concat "Variable \"%s\" copied to \"%s\"" msg
)
426 (calc-var-name var1
) (calc-var-name var2
))))))))
428 (defvar calc-last-edited-variable nil
)
429 (defun calc-edit-variable (&optional var
)
432 (or var
(setq var
(calc-read-var-name
433 (if calc-last-edited-variable
434 (format "Edit (default %s): "
435 (calc-var-name calc-last-edited-variable
))
437 (or var
(setq var calc-last-edited-variable
))
439 (let* ((value (calc-var-value var
)))
440 (if (eq (car-safe value
) 'special-const
)
441 (error "%s is a special constant" var
))
442 (setq calc-last-edited-variable var
)
443 (calc-edit-mode (list 'calc-finish-stack-edit
(list 'quote var
))
446 "Editing variable ‘%s’" (calc-var-name var
)))
448 (insert (math-format-nice-expr value
(frame-width)) "\n")))))
449 (calc-show-edit-buffer))
451 (defun calc-edit-Decls ()
453 (calc-edit-variable 'var-Decls
))
455 (defun calc-edit-EvalRules ()
457 (calc-edit-variable 'var-EvalRules
))
459 (defun calc-edit-FitRules ()
461 (calc-edit-variable 'var-FitRules
))
463 (defun calc-edit-GenCount ()
465 (calc-edit-variable 'var-GenCount
))
467 (defun calc-edit-Holidays ()
469 (calc-edit-variable 'var-Holidays
))
471 (defun calc-edit-IntegLimit ()
473 (calc-edit-variable 'var-IntegLimit
))
475 (defun calc-edit-LineStyles ()
477 (calc-edit-variable 'var-LineStyles
))
479 (defun calc-edit-PointStyles ()
481 (calc-edit-variable 'var-PointStyles
))
483 (defun calc-edit-PlotRejects ()
485 (calc-edit-variable 'var-PlotRejects
))
487 (defun calc-edit-AlgSimpRules ()
489 (calc-edit-variable 'var-AlgSimpRules
))
491 (defun calc-edit-TimeZone ()
493 (calc-edit-variable 'var-TimeZone
))
495 (defun calc-edit-Units ()
497 (calc-edit-variable 'var-Units
))
499 (defun calc-edit-ExtSimpRules ()
501 (calc-edit-variable 'var-ExtSimpRules
))
503 (defun calc-declare-variable (&optional var
)
506 (or var
(setq var
(calc-read-var-name "Declare: " 0)))
507 (or var
(setq var
'var-All
))
508 (let* (dp decl def row rp
)
509 (or (and (calc-var-value 'var-Decls
)
510 (eq (car-safe var-Decls
) 'vec
))
511 (setq var-Decls
(list 'vec
)))
513 (while (and (setq dp
(cdr dp
))
514 (or (not (eq (car-safe (car dp
)) 'vec
))
515 (/= (length (car dp
)) 3)
517 (setq row
(nth 1 (car dp
))
519 (if (eq (car-safe row
) 'vec
)
522 (and (setq rp
(cdr rp
))
523 (or (not (eq (car-safe (car rp
)) 'var
))
524 (not (eq (nth 2 (car rp
)) var
)))))
526 (if (or (not (eq (car-safe row
) 'var
))
527 (not (eq (nth 2 row
) var
)))
530 (calc-unread-command ?\C-a
)
531 (setq decl
(read-string (format "Declare: %s to be: " (calc-var-name var
))
533 (math-format-flat-expr (nth 2 (car dp
)) 0))))
534 (setq decl
(and (string-match "[^ \t]" decl
)
535 (math-read-exprs decl
)))
536 (if (eq (car-safe decl
) 'error
)
537 (error "Bad format in declaration: %s" (nth 2 decl
)))
539 (setq decl
(cons 'vec decl
))
540 (setq decl
(car decl
)))
541 (and (eq (car-safe decl
) 'vec
)
543 (setq decl
(nth 1 decl
)))
544 (calc-record (append '(vec) (list (math-build-var-name var
))
545 (and decl
(list decl
)))
547 (setq var-Decls
(copy-sequence var-Decls
))
548 (if (eq (car-safe row
) 'vec
)
550 (setcdr row
(delq rp
(cdr row
)))
552 (setq var-Decls
(delq (car dp
) var-Decls
))))
553 (setq var-Decls
(delq (car dp
) var-Decls
)))
556 (setq dp
(and (not (eq var
'var-All
)) var-Decls
))
557 (while (and (setq dp
(cdr dp
))
558 (or (not (eq (car-safe (car dp
)) 'vec
))
559 (/= (length (car dp
)) 3)
560 (not (equal (nth 2 (car dp
)) decl
)))))
562 (setcar (cdr (car dp
))
563 (append (if (eq (car-safe (nth 1 (car dp
))) 'vec
)
565 (list 'vec
(nth 1 (car dp
))))
566 (list (math-build-var-name var
))))
567 (setq var-Decls
(append var-Decls
569 (math-build-var-name var
)
571 (calc-refresh-evaltos 'var-Decls
))))
573 (defvar calc-dont-insert-variables
'(var-FitRules var-FactorRules
574 var-CommuteRules var-JumpRules
575 var-DistribRules var-MergeRules
576 var-NegateRules var-InvertRules
578 var-TimeZone var-PlotRejects
579 var-PlotData1 var-PlotData2
580 var-PlotData3 var-PlotData4
581 var-PlotData5 var-PlotData6
584 ;; The variable calc-pv-pos is local to calc-permanent-variable, but
585 ;; used by calc-insert-permanent-variable, which is called by
586 ;; calc-permanent-variable.
589 (defun calc-permanent-variable (&optional var
)
592 (or var
(setq var
(calc-read-var-name "Save variable (default all): ")))
594 (and var
(or (and (boundp var
) (symbol-value var
))
595 (error "No such variable")))
596 (set-buffer (find-file-noselect (substitute-in-file-name
597 calc-settings-file
)))
599 (calc-insert-permanent-variable var
)
602 (and (string-match "\\`var-" (symbol-name x
))
603 (not (memq x calc-dont-insert-variables
))
605 (not (eq (car-safe (symbol-value x
)) 'special-const
))
606 (calc-insert-permanent-variable x
))))))
611 (defun calc-insert-permanent-variable (var)
612 (goto-char (point-min))
613 (if (let (case-fold-search)
614 (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: ")
640 (with-current-buffer buf
643 (and (string-match "\\`var-" (symbol-name x
))
644 (not (memq x calc-dont-insert-variables
))
646 (not (eq (car-safe (symbol-value x
)) 'special-const
))
647 (or (not (eq x
'var-Decls
))
648 (not (equal var-Decls
'(vec))))
649 (or (not (eq x
'var-Holidays
))
650 (not (equal var-Holidays
'(vec (var sat var-sat
)
651 (var sun var-sun
)))))
657 (if (memq calc-language
'(nil big
))
660 (math-format-value (symbol-value x
) 100000)))
663 (defun calc-assign (arg)
666 (calc-binary-op ":=" 'calcFunc-assign arg
)))
668 (defun calc-evalto (arg)
671 (calc-unary-op "=>" 'calcFunc-evalto arg
)))
673 (defun calc-subscript (arg)
676 (calc-binary-op "sub" 'calcFunc-subscr arg
)))
678 (provide 'calc-store
)
684 ;;; calc-store.el ends here