(show-paren-function): Change calls to syntax-after
[emacs.git] / lisp / calc / calc-store.el
blobcb7156bfc78b6ef99890a466ff14f939356bf626
1 ;;; calc-store.el --- value storage functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
26 ;;; Commentary:
28 ;;; Code:
30 ;; This file is autoloaded from calc-ext.el.
31 (require 'calc-ext)
33 (require 'calc-macs)
35 (defun calc-Need-calc-store () nil)
38 ;;; Memory commands.
40 (defvar calc-store-keep nil)
41 (defun calc-store (&optional var)
42 (interactive)
43 (let ((calc-store-keep t))
44 (calc-store-into var)))
46 (defvar calc-given-value-flag nil)
47 (defun calc-store-into (&optional var)
48 (interactive)
49 (calc-wrapper
50 (let ((calc-given-value nil)
51 (calc-given-value-flag 1))
52 (or var (setq var (calc-read-var-name "Store: " t)))
53 (if var
54 (let ((found (assq var '( ( + . calc-store-plus )
55 ( - . calc-store-minus )
56 ( * . calc-store-times )
57 ( / . calc-store-div )
58 ( ^ . calc-store-power )
59 ( | . calc-store-concat ) ))))
60 (if found
61 (funcall (cdr found))
62 (calc-store-value var (or calc-given-value (calc-top 1))
63 "" calc-given-value-flag)
64 (message "Stored to variable \"%s\"" (calc-var-name var))))
65 (setq var (calc-is-assignments (calc-top 1)))
66 (if var
67 (while var
68 (calc-store-value (car (car var)) (cdr (car var))
69 (if (not (cdr var)) "")
70 (if (not (cdr var)) 1))
71 (setq var (cdr var))))))))
73 (defun calc-store-plus (&optional var)
74 (interactive)
75 (calc-store-binary var "+" '+))
77 (defun calc-store-minus (&optional var)
78 (interactive)
79 (calc-store-binary var "-" '-))
81 (defun calc-store-times (&optional var)
82 (interactive)
83 (calc-store-binary var "*" '*))
85 (defun calc-store-div (&optional var)
86 (interactive)
87 (calc-store-binary var "/" '/))
89 (defun calc-store-power (&optional var)
90 (interactive)
91 (calc-store-binary var "^" '^))
93 (defun calc-store-concat (&optional var)
94 (interactive)
95 (calc-store-binary var "|" '|))
97 (defun calc-store-neg (n &optional var)
98 (interactive "p")
99 (calc-store-binary var "n" '/ (- n)))
101 (defun calc-store-inv (n &optional var)
102 (interactive "p")
103 (calc-store-binary var "&" '^ (- n)))
105 (defun calc-store-incr (n &optional var)
106 (interactive "p")
107 (calc-store-binary var "n" '- (- n)))
109 (defun calc-store-decr (n &optional var)
110 (interactive "p")
111 (calc-store-binary var "n" '- n))
113 (defun calc-store-value (var value tag &optional pop)
114 (if var
115 (let ((old (calc-var-value var)))
116 (set var value)
117 (if pop (or calc-store-keep (calc-pop-stack pop)))
118 (calc-record-undo (list 'store (symbol-name var) old))
119 (if tag
120 (let ((calc-full-trail-vectors nil))
121 (calc-record value (format ">%s%s" tag (calc-var-name var)))))
122 (and (memq var '(var-e var-i var-pi var-phi var-gamma))
123 (eq (car-safe old) 'special-const)
124 (message "(Note: Built-in definition of %s has been lost)" var))
125 (and (memq var '(var-inf var-uinf var-nan))
126 (null old)
127 (message "(Note: %s has built-in meanings which may interfere)"
128 var))
129 (calc-refresh-evaltos var))))
131 (defun calc-var-name (var)
132 (if (symbolp var) (setq var (symbol-name var)))
133 (if (string-match "\\`var-." var)
134 (substring var 4)
135 var))
137 (defun calc-store-binary (var tag func &optional val)
138 (calc-wrapper
139 (let ((calc-simplify-mode (if (eq calc-simplify-mode 'none)
140 'num calc-simplify-mode))
141 (value (or val (calc-top 1))))
142 (or var (setq var (calc-read-var-name (format "Store %s: " tag))))
143 (if var
144 (let ((old (calc-var-value var)))
145 (or old
146 (error "No such variable: \"%s\"" (calc-var-name var)))
147 (if (stringp old)
148 (setq old (math-read-expr old)))
149 (if (eq (car-safe old) 'error)
150 (error "Bad format in variable contents: %s" (nth 2 old)))
151 (calc-store-value var
152 (calc-normalize (if (calc-is-inverse)
153 (list func value old)
154 (list func old value)))
155 tag (and (not val) 1))
156 (message "Stored to variable \"%s\"" (calc-var-name var)))))))
158 (defun calc-read-var-name (prompt &optional calc-store-opers)
159 (setq calc-given-value nil
160 calc-aborted-prefix nil)
161 (let ((var (let ((minibuffer-completion-table obarray)
162 (minibuffer-completion-predicate 'boundp)
163 (minibuffer-completion-confirm t))
164 (read-from-minibuffer prompt "var-" calc-var-name-map nil))))
165 (setq calc-aborted-prefix "")
166 (and (not (equal var ""))
167 (not (equal var "var-"))
168 (if (string-match "\\`\\([-a-zA-Z0-9]+\\) *:?=" var)
169 (if (null calc-given-value-flag)
170 (error "Assignment is not allowed in this command")
171 (let ((svar (intern (substring var 0 (match-end 1)))))
172 (setq calc-given-value-flag 0
173 calc-given-value (math-read-expr
174 (substring var (match-end 0))))
175 (if (eq (car-safe calc-given-value) 'error)
176 (error "Bad format: %s" (nth 2 calc-given-value)))
177 (setq calc-given-value (math-evaluate-expr calc-given-value))
178 svar))
179 (intern var)))))
181 (defvar calc-var-name-map nil "Keymap for reading Calc variable names.")
182 (if calc-var-name-map
184 (setq calc-var-name-map (copy-keymap minibuffer-local-completion-map))
185 (define-key calc-var-name-map " " 'self-insert-command)
186 (mapcar (function
187 (lambda (x)
188 (define-key calc-var-name-map (char-to-string x)
189 'calcVar-digit)))
190 "0123456789")
191 (mapcar (function
192 (lambda (x)
193 (define-key calc-var-name-map (char-to-string x)
194 'calcVar-oper)))
195 "+-*/^|"))
197 (defun calcVar-digit ()
198 (interactive)
199 (if (calc-minibuffer-contains "var-\\'")
200 (if (eq calc-store-opers 0)
201 (beep)
202 (insert "q")
203 (self-insert-and-exit))
204 (self-insert-command 1)))
206 (defun calcVar-oper ()
207 (interactive)
208 (if (and (eq calc-store-opers t)
209 (calc-minibuffer-contains "var-\\'"))
210 (progn
211 (erase-buffer)
212 (self-insert-and-exit))
213 (self-insert-command 1)))
215 (defun calc-store-map (&optional oper var)
216 (interactive)
217 (calc-wrapper
218 (let* ((sel-mode nil)
219 (calc-dollar-values (mapcar 'calc-get-stack-element
220 (nthcdr calc-stack-top calc-stack)))
221 (calc-dollar-used 0)
222 (oper (or oper (calc-get-operator "Store Mapping")))
223 (nargs (car oper)))
224 (or var (setq var (calc-read-var-name (format "Store Mapping %s: "
225 (nth 2 oper)))))
226 (if var
227 (let ((old (or (calc-var-value var)
228 (error "No such variable: \"%s\""
229 (calc-var-name var))))
230 (calc-simplify-mode (if (eq calc-simplify-mode 'none)
231 'num calc-simplify-mode))
232 (values (and (> nargs 1)
233 (calc-top-list (1- nargs) (1+ calc-dollar-used)))))
234 (message "Working...")
235 (calc-set-command-flag 'clear-message)
236 (if (stringp old)
237 (setq old (math-read-expr old)))
238 (if (eq (car-safe old) 'error)
239 (error "Bad format in variable contents: %s" (nth 2 old)))
240 (setq values (if (calc-is-inverse)
241 (append values (list old))
242 (append (list old) values)))
243 (calc-store-value var
244 (calc-normalize (cons (nth 1 oper) values))
245 (nth 2 oper)
246 (+ calc-dollar-used (1- nargs))))))))
248 (defun calc-store-exchange (&optional var)
249 (interactive)
250 (calc-wrapper
251 (let ((calc-given-value nil)
252 (calc-given-value-flag 1)
253 top)
254 (or var (setq var (calc-read-var-name "Exchange with: ")))
255 (if var
256 (let ((value (calc-var-value var)))
257 (or value
258 (error "No such variable: \"%s\"" (calc-var-name var)))
259 (if (eq (car-safe value) 'special-const)
260 (error "%s is a special constant" var))
261 (setq top (or calc-given-value (calc-top 1)))
262 (calc-store-value var top nil)
263 (calc-pop-push-record calc-given-value-flag
264 (concat "<>" (calc-var-name var)) value))))))
266 (defun calc-unstore (&optional var)
267 (interactive)
268 (calc-wrapper
269 (or var (setq var (calc-read-var-name "Unstore: ")))
270 (if var
271 (progn
272 (and (memq var '(var-e var-i var-pi var-phi var-gamma))
273 (eq (car-safe (calc-var-value var)) 'special-const)
274 (message "(Note: Built-in definition of %s has been lost)" var))
275 (if (and (boundp var) (symbol-value var))
276 (message "Unstored variable \"%s\"" (calc-var-name var))
277 (message "Variable \"%s\" remains unstored" (calc-var-name var)))
278 (makunbound var)
279 (calc-refresh-evaltos var)))))
281 (defun calc-let (&optional var)
282 (interactive)
283 (calc-wrapper
284 (let* ((calc-given-value nil)
285 (calc-given-value-flag 1)
286 thing value)
287 (or var (setq var (calc-read-var-name "Let variable: ")))
288 (if calc-given-value
289 (setq value calc-given-value
290 thing (calc-top 1))
291 (setq value (calc-top 1)
292 thing (calc-top 2)))
293 (setq var (if var
294 (list (cons var value))
295 (calc-is-assignments value)))
296 (if var
297 (calc-pop-push-record
298 (1+ calc-given-value-flag)
299 (concat "=" (calc-var-name (car (car var))))
300 (let ((saved-val (mapcar (function
301 (lambda (v)
302 (and (boundp (car v))
303 (symbol-value (car v)))))
304 var)))
305 (unwind-protect
306 (let ((vv var))
307 (while vv
308 (set (car (car vv)) (calc-normalize (cdr (car vv))))
309 (calc-refresh-evaltos (car (car vv)))
310 (setq vv (cdr vv)))
311 (math-evaluate-expr thing))
312 (while saved-val
313 (if (car saved-val)
314 (set (car (car var)) (car saved-val))
315 (makunbound (car (car var))))
316 (setq saved-val (cdr saved-val)
317 var (cdr var)))
318 (calc-handle-whys))))))))
320 (defun calc-is-assignments (value)
321 (if (memq (car-safe value) '(calcFunc-eq calcFunc-assign))
322 (and (eq (car-safe (nth 1 value)) 'var)
323 (list (cons (nth 2 (nth 1 value)) (nth 2 value))))
324 (if (eq (car-safe value) 'vec)
325 (let ((vv nil))
326 (while (and (setq value (cdr value))
327 (memq (car-safe (car value))
328 '(calcFunc-eq calcFunc-assign))
329 (eq (car-safe (nth 1 (car value))) 'var))
330 (setq vv (cons (cons (nth 2 (nth 1 (car value)))
331 (nth 2 (car value)))
332 vv)))
333 (and (not value)
334 vv)))))
336 (defun calc-recall (&optional var)
337 (interactive)
338 (calc-wrapper
339 (or var (setq var (calc-read-var-name "Recall: ")))
340 (if var
341 (let ((value (calc-var-value var)))
342 (or value
343 (error "No such variable: \"%s\"" (calc-var-name var)))
344 (if (stringp value)
345 (setq value (math-read-expr value)))
346 (if (eq (car-safe value) 'error)
347 (error "Bad format in variable contents: %s" (nth 2 value)))
348 (setq value (calc-normalize value))
349 (let ((calc-full-trail-vectors nil))
350 (calc-record value (concat "<" (calc-var-name var))))
351 (calc-push value)))))
353 (defun calc-store-quick ()
354 (interactive)
355 (calc-store (intern (format "var-q%c" last-command-char))))
357 (defun calc-store-into-quick ()
358 (interactive)
359 (calc-store-into (intern (format "var-q%c" last-command-char))))
361 (defun calc-recall-quick ()
362 (interactive)
363 (calc-recall (intern (format "var-q%c" last-command-char))))
365 (defun calc-copy-variable (&optional var1 var2)
366 (interactive)
367 (calc-wrapper
368 (or var1 (setq var1 (calc-read-var-name "Copy variable: ")))
369 (if var1
370 (let ((value (calc-var-value var1)))
371 (or value
372 (error "No such variable: \"%s\"" (calc-var-name var)))
373 (or var2 (setq var2 (calc-read-var-name
374 (format "Copy variable: %s, to: " var1))))
375 (if var2
376 (calc-store-value var2 value ""))))))
378 (defvar calc-last-edited-variable nil)
379 (defun calc-edit-variable (&optional var)
380 (interactive)
381 (calc-wrapper
382 (or var (setq var (calc-read-var-name
383 (if calc-last-edited-variable
384 (format "Edit: (default %s) "
385 (calc-var-name calc-last-edited-variable))
386 "Edit: "))))
387 (or var (setq var calc-last-edited-variable))
388 (if var
389 (let* ((value (calc-var-value var)))
390 (if (eq (car-safe value) 'special-const)
391 (error "%s is a special constant" var))
392 (setq calc-last-edited-variable var)
393 (calc-edit-mode (list 'calc-finish-stack-edit (list 'quote var))
395 (concat "Editing " (calc-var-name var)))
396 (and value
397 (insert (math-format-nice-expr value (frame-width)) "\n")))))
398 (calc-show-edit-buffer))
400 (defun calc-edit-Decls ()
401 (interactive)
402 (calc-edit-variable 'var-Decls))
404 (defun calc-edit-EvalRules ()
405 (interactive)
406 (calc-edit-variable 'var-EvalRules))
408 (defun calc-edit-FitRules ()
409 (interactive)
410 (calc-edit-variable 'var-FitRules))
412 (defun calc-edit-GenCount ()
413 (interactive)
414 (calc-edit-variable 'var-GenCount))
416 (defun calc-edit-Holidays ()
417 (interactive)
418 (calc-edit-variable 'var-Holidays))
420 (defun calc-edit-IntegLimit ()
421 (interactive)
422 (calc-edit-variable 'var-IntegLimit))
424 (defun calc-edit-LineStyles ()
425 (interactive)
426 (calc-edit-variable 'var-LineStyles))
428 (defun calc-edit-PointStyles ()
429 (interactive)
430 (calc-edit-variable 'var-PointStyles))
432 (defun calc-edit-PlotRejects ()
433 (interactive)
434 (calc-edit-variable 'var-PlotRejects))
436 (defun calc-edit-AlgSimpRules ()
437 (interactive)
438 (calc-edit-variable 'var-AlgSimpRules))
440 (defun calc-edit-TimeZone ()
441 (interactive)
442 (calc-edit-variable 'var-TimeZone))
444 (defun calc-edit-Units ()
445 (interactive)
446 (calc-edit-variable 'var-Units))
448 (defun calc-edit-ExtSimpRules ()
449 (interactive)
450 (calc-edit-variable 'var-ExtSimpRules))
452 (defun calc-declare-variable (&optional var)
453 (interactive)
454 (calc-wrapper
455 (or var (setq var (calc-read-var-name "Declare: " 0)))
456 (or var (setq var 'var-All))
457 (let* (dp decl def row rp)
458 (or (and (calc-var-value 'var-Decls)
459 (eq (car-safe var-Decls) 'vec))
460 (setq var-Decls (list 'vec)))
461 (setq dp var-Decls)
462 (while (and (setq dp (cdr dp))
463 (or (not (eq (car-safe (car dp)) 'vec))
464 (/= (length (car dp)) 3)
465 (progn
466 (setq row (nth 1 (car dp))
467 rp row)
468 (if (eq (car-safe row) 'vec)
469 (progn
470 (while
471 (and (setq rp (cdr rp))
472 (or (not (eq (car-safe (car rp)) 'var))
473 (not (eq (nth 2 (car rp)) var)))))
474 (setq rp (car rp)))
475 (if (or (not (eq (car-safe row) 'var))
476 (not (eq (nth 2 row) var)))
477 (setq rp nil)))
478 (not rp)))))
479 (calc-unread-command ?\C-a)
480 (setq decl (read-string (format "Declare: %s to be: " var)
481 (and rp
482 (math-format-flat-expr (nth 2 (car dp)) 0))))
483 (setq decl (and (string-match "[^ \t]" decl)
484 (math-read-exprs decl)))
485 (if (eq (car-safe decl) 'error)
486 (error "Bad format in declaration: %s" (nth 2 decl)))
487 (if (cdr decl)
488 (setq decl (cons 'vec decl))
489 (setq decl (car decl)))
490 (and (eq (car-safe decl) 'vec)
491 (= (length decl) 2)
492 (setq decl (nth 1 decl)))
493 (calc-record (append '(vec) (list (math-build-var-name var))
494 (and decl (list decl)))
495 "decl")
496 (setq var-Decls (copy-sequence var-Decls))
497 (if (eq (car-safe row) 'vec)
498 (progn
499 (setcdr row (delq rp (cdr row)))
500 (or (cdr row)
501 (setq var-Decls (delq (car dp) var-Decls))))
502 (setq var-Decls (delq (car dp) var-Decls)))
503 (if decl
504 (progn
505 (setq dp (and (not (eq var 'var-All)) var-Decls))
506 (while (and (setq dp (cdr dp))
507 (or (not (eq (car-safe (car dp)) 'vec))
508 (/= (length (car dp)) 3)
509 (not (equal (nth 2 (car dp)) decl)))))
510 (if dp
511 (setcar (cdr (car dp))
512 (append (if (eq (car-safe (nth 1 (car dp))) 'vec)
513 (nth 1 (car dp))
514 (list 'vec (nth 1 (car dp))))
515 (list (math-build-var-name var))))
516 (setq var-Decls (append var-Decls
517 (list (list 'vec
518 (math-build-var-name var)
519 decl)))))))
520 (calc-refresh-evaltos 'var-Decls))))
522 (defvar calc-dont-insert-variables '(var-FitRules var-FactorRules
523 var-CommuteRules var-JumpRules
524 var-DistribRules var-MergeRules
525 var-NegateRules var-InvertRules
526 var-IntegAfterRules
527 var-TimeZone var-PlotRejects
528 var-PlotData1 var-PlotData2
529 var-PlotData3 var-PlotData4
530 var-PlotData5 var-PlotData6
531 var-DUMMY))
533 (defun calc-permanent-variable (&optional var)
534 (interactive)
535 (calc-wrapper
536 (or var (setq var (calc-read-var-name "Save variable (default=all): ")))
537 (let (pos)
538 (and var (or (and (boundp var) (symbol-value var))
539 (error "No such variable")))
540 (set-buffer (find-file-noselect (substitute-in-file-name
541 calc-settings-file)))
542 (if var
543 (calc-insert-permanent-variable var)
544 (mapatoms (function
545 (lambda (x)
546 (and (string-match "\\`var-" (symbol-name x))
547 (not (memq x calc-dont-insert-variables))
548 (calc-var-value x)
549 (not (eq (car-safe (symbol-value x)) 'special-const))
550 (calc-insert-permanent-variable x))))))
551 (save-buffer))))
555 (defun calc-insert-permanent-variable (var)
556 (goto-char (point-min))
557 (if (search-forward (concat "(setq " (symbol-name var) " '") nil t)
558 (progn
559 (setq pos (point-marker))
560 (forward-line -1)
561 (if (looking-at ";;; Variable .* stored by Calc on ")
562 (progn
563 (delete-region (match-end 0) (progn (end-of-line) (point)))
564 (insert (current-time-string))))
565 (goto-char (- pos 8 (length (symbol-name var))))
566 (forward-sexp 1)
567 (backward-char 1)
568 (delete-region pos (point)))
569 (goto-char (point-max))
570 (insert "\n;;; Variable \""
571 (symbol-name var)
572 "\" stored by Calc on "
573 (current-time-string)
574 "\n(setq "
575 (symbol-name var)
576 " ')\n")
577 (backward-char 2))
578 (insert (prin1-to-string (calc-var-value var)))
579 (forward-line 1))
581 (defun calc-insert-variables (buf)
582 (interactive "bBuffer in which to save variable values: ")
583 (save-excursion
584 (set-buffer buf)
585 (mapatoms (function
586 (lambda (x)
587 (and (string-match "\\`var-" (symbol-name x))
588 (not (memq x calc-dont-insert-variables))
589 (calc-var-value x)
590 (not (eq (car-safe (symbol-value x)) 'special-const))
591 (or (not (eq x 'var-Decls))
592 (not (equal var-Decls '(vec))))
593 (or (not (eq x 'var-Holidays))
594 (not (equal var-Holidays '(vec (var sat var-sat)
595 (var sun var-sun)))))
596 (insert "(setq "
597 (symbol-name x)
599 (prin1-to-string
600 (let ((calc-language
601 (if (memq calc-language '(nil big))
602 'flat
603 calc-language)))
604 (math-format-value (symbol-value x) 100000)))
605 ")\n")))))))
607 (defun calc-assign (arg)
608 (interactive "P")
609 (calc-slow-wrapper
610 (calc-binary-op ":=" 'calcFunc-assign arg)))
612 (defun calc-evalto (arg)
613 (interactive "P")
614 (calc-slow-wrapper
615 (calc-unary-op "=>" 'calcFunc-evalto arg)))
617 (defun calc-subscript (arg)
618 (interactive "P")
619 (calc-slow-wrapper
620 (calc-binary-op "sub" 'calcFunc-subscr arg)))
622 ;;; arch-tag: 2fbfec82-a521-42ca-bcd8-4f254ae6313e
623 ;;; calc-store.el ends here