summaryrefslogtreecommitdiff
path: root/lisp/calc/calc-store.el
blob: ccef6f745d9e5e2cbcae87e79da049dd7ff65a9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
;;; calc-store.el --- value storage functions for Calc

;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
;;   2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.

;; Author: David Gillespie <daveg@synaptics.com>
;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

;; This file is autoloaded from calc-ext.el.

(require 'calc-ext)
(require 'calc-macs)

;;; Memory commands.

(defvar calc-store-keep nil)
(defun calc-store (&optional var)
  (interactive)
  (let ((calc-store-keep t))
    (calc-store-into var)))

(defvar calc-given-value-flag nil)
(defvar calc-given-value)

(defun calc-store-into (&optional var)
  (interactive)
  (calc-wrapper
   (let ((calc-given-value nil)
	 (calc-given-value-flag 1))
     (or var (setq var (calc-read-var-name "Store: " t)))
     (if var
	 (let ((found (assq var '( ( + . calc-store-plus )
				   ( - . calc-store-minus )
				   ( * . calc-store-times )
				   ( / . calc-store-div )
				   ( ^ . calc-store-power )
				   ( | . calc-store-concat ) ))))
	   (if found
	       (funcall (cdr found))
             (let ((msg
                    (calc-store-value var (or calc-given-value (calc-top 1))
                                      "" calc-given-value-flag)))
               (message (concat "Stored to variable \"%s\"" msg)
                        (calc-var-name var)))))
       (setq var (calc-is-assignments (calc-top 1)))
       (if var
	   (while var
	     (let ((msg
                    (calc-store-value (car (car var)) (cdr (car var))
                                      (if (not (cdr var)) "")
                                      (if (not (cdr var)) 1))))
               (message (concat "Stored to variable \"%s\"" msg)
                        (calc-var-name (car (car var)))))
	     (setq var (cdr var))))))))

(defun calc-store-plus (&optional var)
  (interactive)
  (calc-store-binary var "+" '+))

(defun calc-store-minus (&optional var)
  (interactive)
  (calc-store-binary var "-" '-))

(defun calc-store-times (&optional var)
  (interactive)
  (calc-store-binary var "*" '*))

(defun calc-store-div (&optional var)
  (interactive)
  (calc-store-binary var "/" '/))

(defun calc-store-power (&optional var)
  (interactive)
  (calc-store-binary var "^" '^))

(defun calc-store-concat (&optional var)
  (interactive)
  (calc-store-binary var "|" '|))

(defun calc-store-neg (n &optional var)
  (interactive "p")
  (calc-store-binary var "n" '/ (- n)))

(defun calc-store-inv (n &optional var)
  (interactive "p")
  (calc-store-binary var "&" '^ (- n)))

(defun calc-store-incr (n &optional var)
  (interactive "p")
  (calc-store-binary var "n" '- (- n)))

(defun calc-store-decr (n &optional var)
  (interactive "p")
  (calc-store-binary var "n" '- n))

(defun calc-store-value (var value tag &optional pop)
  (let ((msg ""))
    (if var
        (let ((old (calc-var-value var)))
          (set var value)
          (if pop (or calc-store-keep (calc-pop-stack pop)))
          (calc-record-undo (list 'store (symbol-name var) old))
          (if tag
              (let ((calc-full-trail-vectors nil))
                (calc-record value (format ">%s%s" tag (calc-var-name var)))))
          (cond
           ((and (memq var '(var-e var-i var-pi var-phi var-gamma))
                 (eq (car-safe old) 'special-const))
            (setq msg (format " (Note: Built-in definition of %s has been lost)"
                              (calc-var-name var))))
           ((and (memq var '(var-inf var-uinf var-nan))
                 (null old))
            (setq msg (format " (Note: %s has built-in meanings which may interfere)"
                              (calc-var-name var)))))
          (calc-refresh-evaltos var)))
    msg))

(defun calc-var-name (var)
  (if (symbolp var) (setq var (symbol-name var)))
  (if (string-match "\\`var-." var)
      (substring var 4)
    var))

(defun calc-store-binary (var tag func &optional val)
  (calc-wrapper
   (let ((calc-simplify-mode (if (eq calc-simplify-mode 'none)
				 'num calc-simplify-mode))
	 (value (or val (calc-top 1))))
     (or var (setq var (calc-read-var-name (format "Store %s: " tag))))
     (if var
	 (let ((old (calc-var-value var)))
	   (if (eq (car-safe old) 'special-const)
	       (error "\"%s\" is a special constant" (calc-var-name var)))
	   (if (not old)
               (if (memq var '(var-inf var-uinf var-nan))
                   (error "\"%s\" is a special variable" (calc-var-name var))
                 (error "No such variable: \"%s\"" (calc-var-name var))))
	   (if (stringp old)
	       (setq old (math-read-expr old)))
	   (if (eq (car-safe old) 'error)
	       (error "Bad format in variable contents: %s" (nth 2 old)))
	   (calc-store-value var
			     (calc-normalize (if (calc-is-inverse)
						 (list func value old)
					       (list func old value)))
			     tag (and (not val) 1))
	   (message "Variable \"%s\" changed" (calc-var-name var)))))))

(defvar calc-var-name-map nil "Keymap for reading Calc variable names.")
(if calc-var-name-map
    ()
  (setq calc-var-name-map (copy-keymap minibuffer-local-completion-map))
  (define-key calc-var-name-map " " 'self-insert-command)
  (mapc (function
	 (lambda (x)
	  (define-key calc-var-name-map (char-to-string x)
	    'calcVar-digit)))
	"0123456789")
  (mapc (function
	 (lambda (x)
	  (define-key calc-var-name-map (char-to-string x)
	    'calcVar-oper)))
	"+-*/^|"))

(defvar calc-store-opers)

(defvar calc-read-var-name-history nil
  "History for reading variable names.")

(defun calc-read-var-name (prompt &optional calc-store-opers)
  (setq calc-given-value nil
	calc-aborted-prefix nil)
  (let ((var (concat
              "var-"
              (let ((minibuffer-completion-table
                     (mapcar (lambda (x) (substring x 4))
                             (all-completions "var-" obarray)))
                    (minibuffer-completion-predicate
                     (lambda (x) (boundp (intern (concat "var-" x)))))
                    (minibuffer-completion-confirm t))
                (read-from-minibuffer
                 prompt nil calc-var-name-map nil
                 'calc-read-var-name-history)))))
    (setq calc-aborted-prefix "")
    (and (not (equal var "var-"))
	 (if (string-match "\\`\\([-a-zA-Zα-ωΑ-Ω0-9]+\\) *:?=" var)
	     (if (null calc-given-value-flag)
		 (error "Assignment is not allowed in this command")
	       (let ((svar (intern (substring var 0 (match-end 1)))))
		 (setq calc-given-value-flag 0
		       calc-given-value (math-read-expr
					 (substring var (match-end 0))))
		 (if (eq (car-safe calc-given-value) 'error)
		     (error "Bad format: %s" (nth 2 calc-given-value)))
		 (setq calc-given-value (math-evaluate-expr calc-given-value))
		 svar))
	   (intern var)))))

(defun calcVar-digit ()
  (interactive)
  (if (calc-minibuffer-contains "\\'")
      (if (eq calc-store-opers 0)
	  (beep)
	(insert "q")
	(self-insert-and-exit))
    (self-insert-command 1)))

(defun calcVar-oper ()
  (interactive)
  (if (and (eq calc-store-opers t)
	   (calc-minibuffer-contains "\\'"))
      (progn
	(erase-buffer)
	(self-insert-and-exit))
    (self-insert-command 1)))

(defun calc-store-map (&optional oper var)
  (interactive)
  (calc-wrapper
   (let* ((sel-mode nil)
	  (calc-dollar-values (mapcar 'calc-get-stack-element
				      (nthcdr calc-stack-top calc-stack)))
	  (calc-dollar-used 0)
	  (oper (or oper (calc-get-operator "Store Mapping")))
	  (nargs (car oper)))
     (or var (setq var (calc-read-var-name (format "Store Mapping %s: "
						   (nth 2 oper)))))
     (if var
	 (let ((old (calc-var-value var)))
	   (if (eq (car-safe old) 'special-const)
	       (error "\"%s\" is a special constant" (calc-var-name var)))
	   (if (not old)
               (if (memq var '(var-inf var-uinf var-nan))
                   (error "\"%s\" is a special variable" (calc-var-name var))
                 (error "No such variable: \"%s\"" (calc-var-name var))))
           (let ((calc-simplify-mode (if (eq calc-simplify-mode 'none)
                                         'num calc-simplify-mode))
                 (values (and (> nargs 1)
                              (calc-top-list (1- nargs) (1+ calc-dollar-used)))))
             (message "Working...")
             (calc-set-command-flag 'clear-message)
             (if (stringp old)
                 (setq old (math-read-expr old)))
             (if (eq (car-safe old) 'error)
                 (error "Bad format in variable contents: %s" (nth 2 old)))
             (setq values (if (calc-is-inverse)
                              (append values (list old))
                            (append (list old) values)))
             (calc-store-value var
                               (calc-normalize (cons (nth 1 oper) values))
                               (nth 2 oper)
                               (+ calc-dollar-used (1- nargs)))
             (message "Variable \"%s\" changed" (calc-var-name var))))))))


(defun calc-store-exchange (&optional var)
  (interactive)
  (calc-wrapper
   (let ((calc-given-value nil)
	 (calc-given-value-flag 1)
	 top)
     (or var (setq var (calc-read-var-name "Exchange with: ")))
     (if var
	 (let ((value (calc-var-value var)))
	   (if (eq (car-safe value) 'special-const)
	       (error "\"%s\" is a special constant" (calc-var-name var)))
	   (if (not value)
               (if (memq var '(var-inf var-uinf var-nan))
                   (error "\"%s\" is a special variable" (calc-var-name var))
                 (error "No such variable: \"%s\"" (calc-var-name var))))
	   (setq top (or calc-given-value (calc-top 1)))
	   (calc-store-value var top nil)
	   (calc-pop-push-record calc-given-value-flag
				 (concat "<>" (calc-var-name var)) value))))))

(defun calc-unstore (&optional var)
  (interactive)
  (calc-wrapper
   (or var (setq var (calc-read-var-name "Unstore: ")))
   (if var
       (progn
	 (and (memq var '(var-e var-i var-pi var-phi var-gamma))
	      (eq (car-safe (calc-var-value var)) 'special-const)
	      (message "(Note: Built-in definition of %s has been lost)" var))
	 (if (and (boundp var) (symbol-value var))
	     (message "Unstored variable \"%s\"" (calc-var-name var))
	   (message "Variable \"%s\" remains unstored" (calc-var-name var)))
	 (makunbound var)
	 (calc-refresh-evaltos var)))))

(defun calc-let (&optional var)
  (interactive)
  (calc-wrapper
   (let* ((calc-given-value nil)
	  (calc-given-value-flag 1)
	  thing value)
     (or var (setq var (calc-read-var-name "Let variable: ")))
     (if calc-given-value
	 (setq value calc-given-value
	       thing (calc-top 1))
       (setq value (calc-top 1)
	     thing (calc-top 2)))
     (setq var (if var
		   (list (cons var value))
		 (calc-is-assignments value)))
     (if var
	 (calc-pop-push-record
	  (1+ calc-given-value-flag)
	  (concat "=" (calc-var-name (car (car var))))
	  (let ((saved-val (mapcar (function
				    (lambda (v)
				      (and (boundp (car v))
					   (symbol-value (car v)))))
				   var)))
	    (unwind-protect
		(let ((vv var))
		  (while vv
		    (set (car (car vv)) (calc-normalize (cdr (car vv))))
		    (calc-refresh-evaltos (car (car vv)))
		    (setq vv (cdr vv)))
		  (math-evaluate-expr thing))
	      (while saved-val
		(if (car saved-val)
		    (set (car (car var)) (car saved-val))
		  (makunbound (car (car var))))
		(setq saved-val (cdr saved-val)
		      var (cdr var)))
	      (calc-handle-whys))))))))

(defun calc-is-assignments (value)
  (if (memq (car-safe value) '(calcFunc-eq calcFunc-assign))
      (and (eq (car-safe (nth 1 value)) 'var)
	   (list (cons (nth 2 (nth 1 value)) (nth 2 value))))
    (if (eq (car-safe value) 'vec)
	(let ((vv nil))
	  (while (and (setq value (cdr value))
		      (memq (car-safe (car value))
			    '(calcFunc-eq calcFunc-assign))
		      (eq (car-safe (nth 1 (car value))) 'var))
	    (setq vv (cons (cons (nth 2 (nth 1 (car value)))
				 (nth 2 (car value)))
			   vv)))
	  (and (not value)
	       vv)))))

(defun calc-recall (&optional var)
  (interactive)
  (calc-wrapper
   (or var (setq var (calc-read-var-name "Recall: ")))
   (if var
       (let ((value (calc-var-value var)))
	 (or value
	     (error "No such variable: \"%s\"" (calc-var-name var)))
	 (if (stringp value)
	     (setq value (math-read-expr value)))
	 (if (eq (car-safe value) 'error)
	     (error "Bad format in variable contents: %s" (nth 2 value)))
	 (setq value (calc-normalize value))
	 (let ((calc-full-trail-vectors nil))
	   (calc-record value (concat "<" (calc-var-name var))))
	 (calc-push value)))))

(defun calc-store-quick ()
  (interactive)
  (calc-store (intern (format "var-q%c" last-command-event))))

(defun calc-store-into-quick ()
  (interactive)
  (calc-store-into (intern (format "var-q%c" last-command-event))))

(defun calc-recall-quick ()
  (interactive)
  (calc-recall (intern (format "var-q%c" last-command-event))))

(defun calc-copy-special-constant (&optional sconst var)
  (interactive)
  (let ((sc '(("")
              ("e" . (special-const (math-e)))
              ("pi" . (special-const (math-pi)))
              ("i" . (special-const (math-imaginary 1)))
              ("phi" . (special-const (math-phi)))
              ("gamma" . (special-const (math-gamma-const))))))
  (calc-wrapper
   (or sconst (setq sconst (completing-read "Special constant: " sc nil t)))
   (unless (string= sconst "")
     (let ((value (cdr (assoc sconst sc))))
       (or var (setq var (calc-read-var-name
                            (format "Copy special constant %s, to: "
                                    sconst))))
       (if var
           (let ((msg (calc-store-value var value "")))
             (message (concat "Special constant \"%s\" copied to \"%s\"" msg)
                      sconst (calc-var-name var)))))))))

(defun calc-copy-variable (&optional var1 var2)
  (interactive)
  (calc-wrapper
   (or var1 (setq var1 (calc-read-var-name "Copy variable: ")))
   (if var1
       (let ((value (calc-var-value var1)))
	 (or value
	     (error "No such variable: \"%s\"" (calc-var-name var1)))
	 (or var2 (setq var2 (calc-read-var-name
			      (format "Copy variable: %s, to: "
                                      (calc-var-name var1)))))
	 (if var2
	     (let ((msg (calc-store-value var2 value "")))
               (message (concat "Variable \"%s\" copied to \"%s\"" msg)
                        (calc-var-name var1) (calc-var-name var2))))))))

(defvar calc-last-edited-variable nil)
(defun calc-edit-variable (&optional var)
  (interactive)
  (calc-wrapper
   (or var (setq var (calc-read-var-name
		      (if calc-last-edited-variable
			  (format "Edit (default %s): "
				  (calc-var-name calc-last-edited-variable))
			"Edit: "))))
   (or var (setq var calc-last-edited-variable))
   (if var
       (let* ((value (calc-var-value var)))
	 (if (eq (car-safe value) 'special-const)
	     (error "%s is a special constant" var))
	 (setq calc-last-edited-variable var)
	 (calc-edit-mode (list 'calc-finish-stack-edit (list 'quote var))
			 t
			 (concat "Editing variable `" (calc-var-name var) "'. "))
	 (and value
	      (insert (math-format-nice-expr value (frame-width)) "\n")))))
  (calc-show-edit-buffer))

(defun calc-edit-Decls ()
  (interactive)
  (calc-edit-variable 'var-Decls))

(defun calc-edit-EvalRules ()
  (interactive)
  (calc-edit-variable 'var-EvalRules))

(defun calc-edit-FitRules ()
  (interactive)
  (calc-edit-variable 'var-FitRules))

(defun calc-edit-GenCount ()
  (interactive)
  (calc-edit-variable 'var-GenCount))

(defun calc-edit-Holidays ()
  (interactive)
  (calc-edit-variable 'var-Holidays))

(defun calc-edit-IntegLimit ()
  (interactive)
  (calc-edit-variable 'var-IntegLimit))

(defun calc-edit-LineStyles ()
  (interactive)
  (calc-edit-variable 'var-LineStyles))

(defun calc-edit-PointStyles ()
  (interactive)
  (calc-edit-variable 'var-PointStyles))

(defun calc-edit-PlotRejects ()
  (interactive)
  (calc-edit-variable 'var-PlotRejects))

(defun calc-edit-AlgSimpRules ()
  (interactive)
  (calc-edit-variable 'var-AlgSimpRules))

(defun calc-edit-TimeZone ()
  (interactive)
  (calc-edit-variable 'var-TimeZone))

(defun calc-edit-Units ()
  (interactive)
  (calc-edit-variable 'var-Units))

(defun calc-edit-ExtSimpRules ()
  (interactive)
  (calc-edit-variable 'var-ExtSimpRules))

(defun calc-declare-variable (&optional var)
  (interactive)
  (calc-wrapper
   (or var (setq var (calc-read-var-name "Declare: " 0)))
   (or var (setq var 'var-All))
   (let* (dp decl def row rp)
     (or (and (calc-var-value 'var-Decls)
	      (eq (car-safe var-Decls) 'vec))
	 (setq var-Decls (list 'vec)))
     (setq dp var-Decls)
     (while (and (setq dp (cdr dp))
		 (or (not (eq (car-safe (car dp)) 'vec))
		     (/= (length (car dp)) 3)
		     (progn
		       (setq row (nth 1 (car dp))
			     rp row)
		       (if (eq (car-safe row) 'vec)
			   (progn
			     (while
				 (and (setq rp (cdr rp))
				      (or (not (eq (car-safe (car rp)) 'var))
					  (not (eq (nth 2 (car rp)) var)))))
			     (setq rp (car rp)))
			 (if (or (not (eq (car-safe row) 'var))
				 (not (eq (nth 2 row) var)))
			     (setq rp nil)))
		       (not rp)))))
     (calc-unread-command ?\C-a)
     (setq decl (read-string (format "Declare: %s  to be: " (calc-var-name var))
			     (and rp
				  (math-format-flat-expr (nth 2 (car dp)) 0))))
     (setq decl (and (string-match "[^ \t]" decl)
		     (math-read-exprs decl)))
     (if (eq (car-safe decl) 'error)
	 (error "Bad format in declaration: %s" (nth 2 decl)))
     (if (cdr decl)
	 (setq decl (cons 'vec decl))
       (setq decl (car decl)))
     (and (eq (car-safe decl) 'vec)
	  (= (length decl) 2)
	  (setq decl (nth 1 decl)))
     (calc-record (append '(vec) (list (math-build-var-name var))
			  (and decl (list decl)))
		  "decl")
     (setq var-Decls (copy-sequence var-Decls))
     (if (eq (car-safe row) 'vec)
	 (progn
	   (setcdr row (delq rp (cdr row)))
	   (or (cdr row)
	       (setq var-Decls (delq (car dp) var-Decls))))
       (setq var-Decls (delq (car dp) var-Decls)))
     (if decl
	 (progn
	   (setq dp (and (not (eq var 'var-All)) var-Decls))
	   (while (and (setq dp (cdr dp))
		       (or (not (eq (car-safe (car dp)) 'vec))
			   (/= (length (car dp)) 3)
			   (not (equal (nth 2 (car dp)) decl)))))
	   (if dp
	       (setcar (cdr (car dp))
		       (append (if (eq (car-safe (nth 1 (car dp))) 'vec)
				   (nth 1 (car dp))
				 (list 'vec (nth 1 (car dp))))
			       (list (math-build-var-name var))))
	     (setq var-Decls (append var-Decls
				     (list (list 'vec
						 (math-build-var-name var)
						 decl)))))))
     (calc-refresh-evaltos 'var-Decls))))

(defvar calc-dont-insert-variables '(var-FitRules var-FactorRules
				     var-CommuteRules var-JumpRules
				     var-DistribRules var-MergeRules
				     var-NegateRules var-InvertRules
				     var-IntegAfterRules
				     var-TimeZone var-PlotRejects
				     var-PlotData1 var-PlotData2
				     var-PlotData3 var-PlotData4
				     var-PlotData5 var-PlotData6
				     var-DUMMY))

;; The variable calc-pv-pos is local to calc-permanent-variable, but
;; used by calc-insert-permanent-variable, which is called by
;; calc-permanent-variable.
(defvar calc-pv-pos)

(defun calc-permanent-variable (&optional var)
  (interactive)
  (calc-wrapper
   (or var (setq var (calc-read-var-name "Save variable (default all): ")))
   (let (calc-pv-pos)
     (and var (or (and (boundp var) (symbol-value var))
		  (error "No such variable")))
     (set-buffer (find-file-noselect (substitute-in-file-name
				      calc-settings-file)))
     (if var
	 (calc-insert-permanent-variable var)
       (mapatoms (function
		  (lambda (x)
		    (and (string-match "\\`var-" (symbol-name x))
			 (not (memq x calc-dont-insert-variables))
			 (calc-var-value x)
			 (not (eq (car-safe (symbol-value x)) 'special-const))
			 (calc-insert-permanent-variable x))))))
     (save-buffer))))



(defun calc-insert-permanent-variable (var)
  (goto-char (point-min))
  (if (search-forward (concat "(setq " (symbol-name var) " '") nil t)
      (progn
	(setq calc-pv-pos (point-marker))
	(forward-line -1)
	(if (looking-at ";;; Variable .* stored by Calc on ")
	    (progn
	      (delete-region (match-end 0) (progn (end-of-line) (point)))
	      (insert (current-time-string))))
	(goto-char (- calc-pv-pos 8 (length (symbol-name var))))
	(forward-sexp 1)
	(backward-char 1)
	(delete-region calc-pv-pos (point)))
    (goto-char (point-max))
    (insert "\n;;; Variable \""
	    (symbol-name var)
	    "\" stored by Calc on "
	    (current-time-string)
	    "\n(setq "
	    (symbol-name var)
	    " ')\n")
    (backward-char 2))
  (insert (prin1-to-string (calc-var-value var)))
  (forward-line 1))

(defun calc-insert-variables (buf)
  (interactive "bBuffer in which to save variable values: ")
  (with-current-buffer buf
    (mapatoms (function
	       (lambda (x)
		 (and (string-match "\\`var-" (symbol-name x))
		      (not (memq x calc-dont-insert-variables))
		      (calc-var-value x)
		      (not (eq (car-safe (symbol-value x)) 'special-const))
		      (or (not (eq x 'var-Decls))
			  (not (equal var-Decls '(vec))))
		      (or (not (eq x 'var-Holidays))
			  (not (equal var-Holidays '(vec (var sat var-sat)
							 (var sun var-sun)))))
		      (insert "(setq "
			      (symbol-name x)
			      " "
			      (prin1-to-string
			       (let ((calc-language
				      (if (memq calc-language '(nil big))
					  'flat
					calc-language)))
				 (math-format-value (symbol-value x) 100000)))
			      ")\n")))))))

(defun calc-assign (arg)
  (interactive "P")
  (calc-slow-wrapper
   (calc-binary-op ":=" 'calcFunc-assign arg)))

(defun calc-evalto (arg)
  (interactive "P")
  (calc-slow-wrapper
   (calc-unary-op "=>" 'calcFunc-evalto arg)))

(defun calc-subscript (arg)
  (interactive "P")
  (calc-slow-wrapper
   (calc-binary-op "sub" 'calcFunc-subscr arg)))

(provide 'calc-store)

;; Local variables:
;; coding: utf-8
;; End:

;; arch-tag: 2fbfec82-a521-42ca-bcd8-4f254ae6313e
;;; calc-store.el ends here