Commit | Line | Data |
---|---|---|
3132f345 CW |
1 | ;;; calc-store.el --- value storage functions for Calc |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1990-1993, 2001-2011 Free Software Foundation, Inc. |
3132f345 CW |
4 | |
5 | ;; Author: David Gillespie <daveg@synaptics.com> | |
e8fff8ed | 6 | ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com> |
136211a9 EZ |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
662c9c64 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7c671b23 | 11 | ;; it under the terms of the GNU General Public License as published by |
662c9c64 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
7c671b23 | 14 | |
136211a9 | 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
7c671b23 GM |
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. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
662c9c64 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
136211a9 | 22 | |
3132f345 | 23 | ;;; Commentary: |
136211a9 | 24 | |
3132f345 | 25 | ;;; Code: |
136211a9 EZ |
26 | |
27 | ;; This file is autoloaded from calc-ext.el. | |
136211a9 | 28 | |
4b02a252 | 29 | (require 'calc-ext) |
136211a9 EZ |
30 | (require 'calc-macs) |
31 | ||
136211a9 EZ |
32 | ;;; Memory commands. |
33 | ||
3132f345 | 34 | (defvar calc-store-keep nil) |
136211a9 EZ |
35 | (defun calc-store (&optional var) |
36 | (interactive) | |
37 | (let ((calc-store-keep t)) | |
bf77c646 | 38 | (calc-store-into var))) |
136211a9 | 39 | |
3132f345 | 40 | (defvar calc-given-value-flag nil) |
3a7a4adb JB |
41 | (defvar calc-given-value) |
42 | ||
136211a9 EZ |
43 | (defun calc-store-into (&optional var) |
44 | (interactive) | |
45 | (calc-wrapper | |
46 | (let ((calc-given-value nil) | |
47 | (calc-given-value-flag 1)) | |
48 | (or var (setq var (calc-read-var-name "Store: " t))) | |
49 | (if var | |
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 ) )))) | |
56 | (if found | |
57 | (funcall (cdr found)) | |
0dc72b8e JB |
58 | (let ((msg |
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))))) | |
136211a9 EZ |
63 | (setq var (calc-is-assignments (calc-top 1))) |
64 | (if var | |
65 | (while var | |
0dc72b8e JB |
66 | (let ((msg |
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) | |
970dd012 | 71 | (calc-var-name (car (car var))))) |
bf77c646 | 72 | (setq var (cdr var)))))))) |
136211a9 EZ |
73 | |
74 | (defun calc-store-plus (&optional var) | |
75 | (interactive) | |
bf77c646 | 76 | (calc-store-binary var "+" '+)) |
136211a9 EZ |
77 | |
78 | (defun calc-store-minus (&optional var) | |
79 | (interactive) | |
bf77c646 | 80 | (calc-store-binary var "-" '-)) |
136211a9 EZ |
81 | |
82 | (defun calc-store-times (&optional var) | |
83 | (interactive) | |
bf77c646 | 84 | (calc-store-binary var "*" '*)) |
136211a9 EZ |
85 | |
86 | (defun calc-store-div (&optional var) | |
87 | (interactive) | |
bf77c646 | 88 | (calc-store-binary var "/" '/)) |
136211a9 EZ |
89 | |
90 | (defun calc-store-power (&optional var) | |
91 | (interactive) | |
bf77c646 | 92 | (calc-store-binary var "^" '^)) |
136211a9 EZ |
93 | |
94 | (defun calc-store-concat (&optional var) | |
95 | (interactive) | |
bf77c646 | 96 | (calc-store-binary var "|" '|)) |
136211a9 EZ |
97 | |
98 | (defun calc-store-neg (n &optional var) | |
99 | (interactive "p") | |
bf77c646 | 100 | (calc-store-binary var "n" '/ (- n))) |
136211a9 EZ |
101 | |
102 | (defun calc-store-inv (n &optional var) | |
103 | (interactive "p") | |
bf77c646 | 104 | (calc-store-binary var "&" '^ (- n))) |
136211a9 EZ |
105 | |
106 | (defun calc-store-incr (n &optional var) | |
107 | (interactive "p") | |
bf77c646 | 108 | (calc-store-binary var "n" '- (- n))) |
136211a9 EZ |
109 | |
110 | (defun calc-store-decr (n &optional var) | |
111 | (interactive "p") | |
bf77c646 | 112 | (calc-store-binary var "n" '- n)) |
136211a9 EZ |
113 | |
114 | (defun calc-store-value (var value tag &optional pop) | |
0dc72b8e JB |
115 | (let ((msg "")) |
116 | (if var | |
117 | (let ((old (calc-var-value var))) | |
118 | (set var value) | |
119 | (if pop (or calc-store-keep (calc-pop-stack pop))) | |
120 | (calc-record-undo (list 'store (symbol-name var) old)) | |
121 | (if tag | |
122 | (let ((calc-full-trail-vectors nil)) | |
123 | (calc-record value (format ">%s%s" tag (calc-var-name var))))) | |
124 | (cond | |
125 | ((and (memq var '(var-e var-i var-pi var-phi var-gamma)) | |
126 | (eq (car-safe old) 'special-const)) | |
19449d1d | 127 | (setq msg (format " (Note: Built-in definition of %s has been lost)" |
0dc72b8e JB |
128 | (calc-var-name var)))) |
129 | ((and (memq var '(var-inf var-uinf var-nan)) | |
130 | (null old)) | |
131 | (setq msg (format " (Note: %s has built-in meanings which may interfere)" | |
132 | (calc-var-name var))))) | |
133 | (calc-refresh-evaltos var))) | |
134 | msg)) | |
136211a9 EZ |
135 | |
136 | (defun calc-var-name (var) | |
137 | (if (symbolp var) (setq var (symbol-name var))) | |
138 | (if (string-match "\\`var-." var) | |
139 | (substring var 4) | |
bf77c646 | 140 | var)) |
136211a9 EZ |
141 | |
142 | (defun calc-store-binary (var tag func &optional val) | |
143 | (calc-wrapper | |
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)))) | |
148 | (if var | |
149 | (let ((old (calc-var-value var))) | |
0dc72b8e JB |
150 | (if (eq (car-safe old) 'special-const) |
151 | (error "\"%s\" is a special constant" (calc-var-name var))) | |
152 | (if (not old) | |
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)))) | |
136211a9 EZ |
156 | (if (stringp old) |
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)) | |
0dc72b8e | 165 | (message "Variable \"%s\" changed" (calc-var-name var))))))) |
136211a9 | 166 | |
3a7a4adb JB |
167 | (defvar calc-var-name-map nil "Keymap for reading Calc variable names.") |
168 | (if calc-var-name-map | |
169 | () | |
170 | (setq calc-var-name-map (copy-keymap minibuffer-local-completion-map)) | |
171 | (define-key calc-var-name-map " " 'self-insert-command) | |
19449d1d JB |
172 | (mapc (function |
173 | (lambda (x) | |
174 | (define-key calc-var-name-map (char-to-string x) | |
175 | 'calcVar-digit))) | |
176 | "0123456789") | |
177 | (mapc (function | |
178 | (lambda (x) | |
179 | (define-key calc-var-name-map (char-to-string x) | |
180 | 'calcVar-oper))) | |
181 | "+-*/^|")) | |
3a7a4adb JB |
182 | |
183 | (defvar calc-store-opers) | |
184 | ||
6a1414ce JB |
185 | (defvar calc-read-var-name-history nil |
186 | "History for reading variable names.") | |
187 | ||
136211a9 EZ |
188 | (defun calc-read-var-name (prompt &optional calc-store-opers) |
189 | (setq calc-given-value nil | |
190 | calc-aborted-prefix nil) | |
19449d1d | 191 | (let ((var (concat |
8cdb7a17 JB |
192 | "var-" |
193 | (let ((minibuffer-completion-table | |
19449d1d | 194 | (mapcar (lambda (x) (substring x 4)) |
8cdb7a17 | 195 | (all-completions "var-" obarray))) |
19449d1d | 196 | (minibuffer-completion-predicate |
8cdb7a17 JB |
197 | (lambda (x) (boundp (intern (concat "var-" x))))) |
198 | (minibuffer-completion-confirm t)) | |
be19ef0b GM |
199 | (read-from-minibuffer |
200 | prompt nil calc-var-name-map nil | |
6a1414ce | 201 | 'calc-read-var-name-history))))) |
136211a9 | 202 | (setq calc-aborted-prefix "") |
8cdb7a17 | 203 | (and (not (equal var "var-")) |
ae6bc504 | 204 | (if (string-match "\\`\\([-a-zA-Zα-ωΑ-Ω0-9]+\\) *:?=" var) |
136211a9 EZ |
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)) | |
214 | svar)) | |
bf77c646 | 215 | (intern var))))) |
136211a9 | 216 | |
136211a9 EZ |
217 | (defun calcVar-digit () |
218 | (interactive) | |
4a74dd94 | 219 | (if (calc-minibuffer-contains "\\'") |
136211a9 EZ |
220 | (if (eq calc-store-opers 0) |
221 | (beep) | |
222 | (insert "q") | |
223 | (self-insert-and-exit)) | |
bf77c646 | 224 | (self-insert-command 1))) |
136211a9 EZ |
225 | |
226 | (defun calcVar-oper () | |
227 | (interactive) | |
228 | (if (and (eq calc-store-opers t) | |
4a74dd94 | 229 | (calc-minibuffer-contains "\\'")) |
136211a9 EZ |
230 | (progn |
231 | (erase-buffer) | |
232 | (self-insert-and-exit)) | |
bf77c646 | 233 | (self-insert-command 1))) |
136211a9 EZ |
234 | |
235 | (defun calc-store-map (&optional oper var) | |
236 | (interactive) | |
237 | (calc-wrapper | |
238 | (let* ((sel-mode nil) | |
239 | (calc-dollar-values (mapcar 'calc-get-stack-element | |
240 | (nthcdr calc-stack-top calc-stack))) | |
241 | (calc-dollar-used 0) | |
242 | (oper (or oper (calc-get-operator "Store Mapping"))) | |
243 | (nargs (car oper))) | |
244 | (or var (setq var (calc-read-var-name (format "Store Mapping %s: " | |
245 | (nth 2 oper))))) | |
246 | (if var | |
0dc72b8e JB |
247 | (let ((old (calc-var-value var))) |
248 | (if (eq (car-safe old) 'special-const) | |
249 | (error "\"%s\" is a special constant" (calc-var-name var))) | |
250 | (if (not old) | |
251 | (if (memq var '(var-inf var-uinf var-nan)) | |
252 | (error "\"%s\" is a special variable" (calc-var-name var)) | |
253 | (error "No such variable: \"%s\"" (calc-var-name var)))) | |
254 | (let ((calc-simplify-mode (if (eq calc-simplify-mode 'none) | |
255 | 'num calc-simplify-mode)) | |
256 | (values (and (> nargs 1) | |
257 | (calc-top-list (1- nargs) (1+ calc-dollar-used))))) | |
258 | (message "Working...") | |
259 | (calc-set-command-flag 'clear-message) | |
260 | (if (stringp old) | |
261 | (setq old (math-read-expr old))) | |
262 | (if (eq (car-safe old) 'error) | |
263 | (error "Bad format in variable contents: %s" (nth 2 old))) | |
264 | (setq values (if (calc-is-inverse) | |
265 | (append values (list old)) | |
266 | (append (list old) values))) | |
267 | (calc-store-value var | |
268 | (calc-normalize (cons (nth 1 oper) values)) | |
269 | (nth 2 oper) | |
270 | (+ calc-dollar-used (1- nargs))) | |
271 | (message "Variable \"%s\" changed" (calc-var-name var)))))))) | |
272 | ||
136211a9 EZ |
273 | |
274 | (defun calc-store-exchange (&optional var) | |
275 | (interactive) | |
276 | (calc-wrapper | |
277 | (let ((calc-given-value nil) | |
278 | (calc-given-value-flag 1) | |
279 | top) | |
280 | (or var (setq var (calc-read-var-name "Exchange with: "))) | |
281 | (if var | |
282 | (let ((value (calc-var-value var))) | |
136211a9 | 283 | (if (eq (car-safe value) 'special-const) |
0dc72b8e JB |
284 | (error "\"%s\" is a special constant" (calc-var-name var))) |
285 | (if (not value) | |
286 | (if (memq var '(var-inf var-uinf var-nan)) | |
287 | (error "\"%s\" is a special variable" (calc-var-name var)) | |
288 | (error "No such variable: \"%s\"" (calc-var-name var)))) | |
136211a9 EZ |
289 | (setq top (or calc-given-value (calc-top 1))) |
290 | (calc-store-value var top nil) | |
291 | (calc-pop-push-record calc-given-value-flag | |
bf77c646 | 292 | (concat "<>" (calc-var-name var)) value)))))) |
136211a9 EZ |
293 | |
294 | (defun calc-unstore (&optional var) | |
295 | (interactive) | |
296 | (calc-wrapper | |
297 | (or var (setq var (calc-read-var-name "Unstore: "))) | |
298 | (if var | |
299 | (progn | |
300 | (and (memq var '(var-e var-i var-pi var-phi var-gamma)) | |
301 | (eq (car-safe (calc-var-value var)) 'special-const) | |
302 | (message "(Note: Built-in definition of %s has been lost)" var)) | |
303 | (if (and (boundp var) (symbol-value var)) | |
304 | (message "Unstored variable \"%s\"" (calc-var-name var)) | |
305 | (message "Variable \"%s\" remains unstored" (calc-var-name var))) | |
306 | (makunbound var) | |
bf77c646 | 307 | (calc-refresh-evaltos var))))) |
136211a9 EZ |
308 | |
309 | (defun calc-let (&optional var) | |
310 | (interactive) | |
311 | (calc-wrapper | |
312 | (let* ((calc-given-value nil) | |
313 | (calc-given-value-flag 1) | |
314 | thing value) | |
315 | (or var (setq var (calc-read-var-name "Let variable: "))) | |
316 | (if calc-given-value | |
317 | (setq value calc-given-value | |
318 | thing (calc-top 1)) | |
319 | (setq value (calc-top 1) | |
320 | thing (calc-top 2))) | |
321 | (setq var (if var | |
322 | (list (cons var value)) | |
323 | (calc-is-assignments value))) | |
324 | (if var | |
325 | (calc-pop-push-record | |
326 | (1+ calc-given-value-flag) | |
327 | (concat "=" (calc-var-name (car (car var)))) | |
328 | (let ((saved-val (mapcar (function | |
329 | (lambda (v) | |
330 | (and (boundp (car v)) | |
331 | (symbol-value (car v))))) | |
332 | var))) | |
333 | (unwind-protect | |
334 | (let ((vv var)) | |
335 | (while vv | |
336 | (set (car (car vv)) (calc-normalize (cdr (car vv)))) | |
337 | (calc-refresh-evaltos (car (car vv))) | |
338 | (setq vv (cdr vv))) | |
339 | (math-evaluate-expr thing)) | |
340 | (while saved-val | |
341 | (if (car saved-val) | |
342 | (set (car (car var)) (car saved-val)) | |
343 | (makunbound (car (car var)))) | |
344 | (setq saved-val (cdr saved-val) | |
345 | var (cdr var))) | |
bf77c646 | 346 | (calc-handle-whys)))))))) |
136211a9 EZ |
347 | |
348 | (defun calc-is-assignments (value) | |
349 | (if (memq (car-safe value) '(calcFunc-eq calcFunc-assign)) | |
350 | (and (eq (car-safe (nth 1 value)) 'var) | |
351 | (list (cons (nth 2 (nth 1 value)) (nth 2 value)))) | |
352 | (if (eq (car-safe value) 'vec) | |
353 | (let ((vv nil)) | |
354 | (while (and (setq value (cdr value)) | |
355 | (memq (car-safe (car value)) | |
356 | '(calcFunc-eq calcFunc-assign)) | |
357 | (eq (car-safe (nth 1 (car value))) 'var)) | |
358 | (setq vv (cons (cons (nth 2 (nth 1 (car value))) | |
359 | (nth 2 (car value))) | |
360 | vv))) | |
361 | (and (not value) | |
bf77c646 | 362 | vv))))) |
136211a9 EZ |
363 | |
364 | (defun calc-recall (&optional var) | |
365 | (interactive) | |
366 | (calc-wrapper | |
367 | (or var (setq var (calc-read-var-name "Recall: "))) | |
368 | (if var | |
369 | (let ((value (calc-var-value var))) | |
370 | (or value | |
371 | (error "No such variable: \"%s\"" (calc-var-name var))) | |
372 | (if (stringp value) | |
373 | (setq value (math-read-expr value))) | |
374 | (if (eq (car-safe value) 'error) | |
375 | (error "Bad format in variable contents: %s" (nth 2 value))) | |
376 | (setq value (calc-normalize value)) | |
377 | (let ((calc-full-trail-vectors nil)) | |
378 | (calc-record value (concat "<" (calc-var-name var)))) | |
bf77c646 | 379 | (calc-push value))))) |
136211a9 EZ |
380 | |
381 | (defun calc-store-quick () | |
382 | (interactive) | |
e93c003e | 383 | (calc-store (intern (format "var-q%c" last-command-event)))) |
136211a9 EZ |
384 | |
385 | (defun calc-store-into-quick () | |
386 | (interactive) | |
e93c003e | 387 | (calc-store-into (intern (format "var-q%c" last-command-event)))) |
136211a9 EZ |
388 | |
389 | (defun calc-recall-quick () | |
390 | (interactive) | |
e93c003e | 391 | (calc-recall (intern (format "var-q%c" last-command-event)))) |
136211a9 | 392 | |
04587c6e JB |
393 | (defun calc-copy-special-constant (&optional sconst var) |
394 | (interactive) | |
395 | (let ((sc '(("") | |
396 | ("e" . (special-const (math-e))) | |
397 | ("pi" . (special-const (math-pi))) | |
398 | ("i" . (special-const (math-imaginary 1))) | |
399 | ("phi" . (special-const (math-phi))) | |
400 | ("gamma" . (special-const (math-gamma-const)))))) | |
401 | (calc-wrapper | |
402 | (or sconst (setq sconst (completing-read "Special constant: " sc nil t))) | |
403 | (unless (string= sconst "") | |
404 | (let ((value (cdr (assoc sconst sc)))) | |
405 | (or var (setq var (calc-read-var-name | |
19449d1d | 406 | (format "Copy special constant %s, to: " |
04587c6e JB |
407 | sconst)))) |
408 | (if var | |
409 | (let ((msg (calc-store-value var value ""))) | |
410 | (message (concat "Special constant \"%s\" copied to \"%s\"" msg) | |
411 | sconst (calc-var-name var))))))))) | |
412 | ||
136211a9 EZ |
413 | (defun calc-copy-variable (&optional var1 var2) |
414 | (interactive) | |
415 | (calc-wrapper | |
416 | (or var1 (setq var1 (calc-read-var-name "Copy variable: "))) | |
417 | (if var1 | |
418 | (let ((value (calc-var-value var1))) | |
419 | (or value | |
922e9c18 | 420 | (error "No such variable: \"%s\"" (calc-var-name var1))) |
136211a9 | 421 | (or var2 (setq var2 (calc-read-var-name |
19449d1d | 422 | (format "Copy variable: %s, to: " |
922e9c18 | 423 | (calc-var-name var1))))) |
136211a9 | 424 | (if var2 |
0dc72b8e JB |
425 | (let ((msg (calc-store-value var2 value ""))) |
426 | (message (concat "Variable \"%s\" copied to \"%s\"" msg) | |
427 | (calc-var-name var1) (calc-var-name var2)))))))) | |
136211a9 | 428 | |
3132f345 | 429 | (defvar calc-last-edited-variable nil) |
136211a9 EZ |
430 | (defun calc-edit-variable (&optional var) |
431 | (interactive) | |
432 | (calc-wrapper | |
433 | (or var (setq var (calc-read-var-name | |
434 | (if calc-last-edited-variable | |
5b76833f | 435 | (format "Edit (default %s): " |
136211a9 EZ |
436 | (calc-var-name calc-last-edited-variable)) |
437 | "Edit: ")))) | |
438 | (or var (setq var calc-last-edited-variable)) | |
439 | (if var | |
440 | (let* ((value (calc-var-value var))) | |
441 | (if (eq (car-safe value) 'special-const) | |
442 | (error "%s is a special constant" var)) | |
443 | (setq calc-last-edited-variable var) | |
444 | (calc-edit-mode (list 'calc-finish-stack-edit (list 'quote var)) | |
445 | t | |
2fa78285 | 446 | (concat "Editing variable `" (calc-var-name var) "'. ")) |
136211a9 | 447 | (and value |
bf77c646 CW |
448 | (insert (math-format-nice-expr value (frame-width)) "\n"))))) |
449 | (calc-show-edit-buffer)) | |
136211a9 EZ |
450 | |
451 | (defun calc-edit-Decls () | |
452 | (interactive) | |
bf77c646 | 453 | (calc-edit-variable 'var-Decls)) |
136211a9 EZ |
454 | |
455 | (defun calc-edit-EvalRules () | |
456 | (interactive) | |
bf77c646 | 457 | (calc-edit-variable 'var-EvalRules)) |
136211a9 EZ |
458 | |
459 | (defun calc-edit-FitRules () | |
460 | (interactive) | |
bf77c646 | 461 | (calc-edit-variable 'var-FitRules)) |
136211a9 EZ |
462 | |
463 | (defun calc-edit-GenCount () | |
464 | (interactive) | |
bf77c646 | 465 | (calc-edit-variable 'var-GenCount)) |
136211a9 EZ |
466 | |
467 | (defun calc-edit-Holidays () | |
468 | (interactive) | |
bf77c646 | 469 | (calc-edit-variable 'var-Holidays)) |
136211a9 EZ |
470 | |
471 | (defun calc-edit-IntegLimit () | |
472 | (interactive) | |
bf77c646 | 473 | (calc-edit-variable 'var-IntegLimit)) |
136211a9 EZ |
474 | |
475 | (defun calc-edit-LineStyles () | |
476 | (interactive) | |
bf77c646 | 477 | (calc-edit-variable 'var-LineStyles)) |
136211a9 EZ |
478 | |
479 | (defun calc-edit-PointStyles () | |
480 | (interactive) | |
bf77c646 | 481 | (calc-edit-variable 'var-PointStyles)) |
136211a9 EZ |
482 | |
483 | (defun calc-edit-PlotRejects () | |
484 | (interactive) | |
bf77c646 | 485 | (calc-edit-variable 'var-PlotRejects)) |
136211a9 EZ |
486 | |
487 | (defun calc-edit-AlgSimpRules () | |
488 | (interactive) | |
bf77c646 | 489 | (calc-edit-variable 'var-AlgSimpRules)) |
136211a9 EZ |
490 | |
491 | (defun calc-edit-TimeZone () | |
492 | (interactive) | |
bf77c646 | 493 | (calc-edit-variable 'var-TimeZone)) |
136211a9 EZ |
494 | |
495 | (defun calc-edit-Units () | |
496 | (interactive) | |
bf77c646 | 497 | (calc-edit-variable 'var-Units)) |
136211a9 EZ |
498 | |
499 | (defun calc-edit-ExtSimpRules () | |
500 | (interactive) | |
bf77c646 | 501 | (calc-edit-variable 'var-ExtSimpRules)) |
136211a9 EZ |
502 | |
503 | (defun calc-declare-variable (&optional var) | |
504 | (interactive) | |
505 | (calc-wrapper | |
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))) | |
512 | (setq dp var-Decls) | |
513 | (while (and (setq dp (cdr dp)) | |
514 | (or (not (eq (car-safe (car dp)) 'vec)) | |
515 | (/= (length (car dp)) 3) | |
516 | (progn | |
517 | (setq row (nth 1 (car dp)) | |
518 | rp row) | |
519 | (if (eq (car-safe row) 'vec) | |
520 | (progn | |
521 | (while | |
522 | (and (setq rp (cdr rp)) | |
523 | (or (not (eq (car-safe (car rp)) 'var)) | |
524 | (not (eq (nth 2 (car rp)) var))))) | |
525 | (setq rp (car rp))) | |
526 | (if (or (not (eq (car-safe row) 'var)) | |
527 | (not (eq (nth 2 row) var))) | |
528 | (setq rp nil))) | |
529 | (not rp))))) | |
530 | (calc-unread-command ?\C-a) | |
dacb2f70 | 531 | (setq decl (read-string (format "Declare: %s to be: " (calc-var-name var)) |
136211a9 EZ |
532 | (and rp |
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))) | |
538 | (if (cdr decl) | |
539 | (setq decl (cons 'vec decl)) | |
540 | (setq decl (car decl))) | |
541 | (and (eq (car-safe decl) 'vec) | |
542 | (= (length decl) 2) | |
543 | (setq decl (nth 1 decl))) | |
544 | (calc-record (append '(vec) (list (math-build-var-name var)) | |
545 | (and decl (list decl))) | |
546 | "decl") | |
547 | (setq var-Decls (copy-sequence var-Decls)) | |
548 | (if (eq (car-safe row) 'vec) | |
549 | (progn | |
550 | (setcdr row (delq rp (cdr row))) | |
551 | (or (cdr row) | |
552 | (setq var-Decls (delq (car dp) var-Decls)))) | |
553 | (setq var-Decls (delq (car dp) var-Decls))) | |
554 | (if decl | |
555 | (progn | |
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))))) | |
561 | (if dp | |
562 | (setcar (cdr (car dp)) | |
563 | (append (if (eq (car-safe (nth 1 (car dp))) 'vec) | |
564 | (nth 1 (car dp)) | |
565 | (list 'vec (nth 1 (car dp)))) | |
566 | (list (math-build-var-name var)))) | |
567 | (setq var-Decls (append var-Decls | |
568 | (list (list 'vec | |
569 | (math-build-var-name var) | |
570 | decl))))))) | |
bf77c646 | 571 | (calc-refresh-evaltos 'var-Decls)))) |
136211a9 | 572 | |
3132f345 CW |
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 | |
577 | var-IntegAfterRules | |
578 | var-TimeZone var-PlotRejects | |
579 | var-PlotData1 var-PlotData2 | |
580 | var-PlotData3 var-PlotData4 | |
581 | var-PlotData5 var-PlotData6 | |
582 | var-DUMMY)) | |
583 | ||
3a7a4adb JB |
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. | |
587 | (defvar calc-pv-pos) | |
588 | ||
136211a9 EZ |
589 | (defun calc-permanent-variable (&optional var) |
590 | (interactive) | |
591 | (calc-wrapper | |
5b76833f | 592 | (or var (setq var (calc-read-var-name "Save variable (default all): "))) |
3a7a4adb | 593 | (let (calc-pv-pos) |
136211a9 EZ |
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))) | |
598 | (if var | |
599 | (calc-insert-permanent-variable var) | |
600 | (mapatoms (function | |
601 | (lambda (x) | |
602 | (and (string-match "\\`var-" (symbol-name x)) | |
603 | (not (memq x calc-dont-insert-variables)) | |
604 | (calc-var-value x) | |
605 | (not (eq (car-safe (symbol-value x)) 'special-const)) | |
606 | (calc-insert-permanent-variable x)))))) | |
bf77c646 | 607 | (save-buffer)))) |
3132f345 CW |
608 | |
609 | ||
136211a9 EZ |
610 | |
611 | (defun calc-insert-permanent-variable (var) | |
612 | (goto-char (point-min)) | |
613 | (if (search-forward (concat "(setq " (symbol-name var) " '") nil t) | |
614 | (progn | |
3a7a4adb | 615 | (setq calc-pv-pos (point-marker)) |
136211a9 EZ |
616 | (forward-line -1) |
617 | (if (looking-at ";;; Variable .* stored by Calc on ") | |
618 | (progn | |
619 | (delete-region (match-end 0) (progn (end-of-line) (point))) | |
620 | (insert (current-time-string)))) | |
3a7a4adb | 621 | (goto-char (- calc-pv-pos 8 (length (symbol-name var)))) |
136211a9 EZ |
622 | (forward-sexp 1) |
623 | (backward-char 1) | |
3a7a4adb | 624 | (delete-region calc-pv-pos (point))) |
136211a9 EZ |
625 | (goto-char (point-max)) |
626 | (insert "\n;;; Variable \"" | |
627 | (symbol-name var) | |
628 | "\" stored by Calc on " | |
629 | (current-time-string) | |
630 | "\n(setq " | |
631 | (symbol-name var) | |
632 | " ')\n") | |
633 | (backward-char 2)) | |
634 | (insert (prin1-to-string (calc-var-value var))) | |
bf77c646 | 635 | (forward-line 1)) |
136211a9 EZ |
636 | |
637 | (defun calc-insert-variables (buf) | |
638 | (interactive "bBuffer in which to save variable values: ") | |
6df9b6d7 | 639 | (with-current-buffer buf |
136211a9 EZ |
640 | (mapatoms (function |
641 | (lambda (x) | |
642 | (and (string-match "\\`var-" (symbol-name x)) | |
643 | (not (memq x calc-dont-insert-variables)) | |
644 | (calc-var-value x) | |
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))))) | |
651 | (insert "(setq " | |
652 | (symbol-name x) | |
653 | " " | |
654 | (prin1-to-string | |
655 | (let ((calc-language | |
656 | (if (memq calc-language '(nil big)) | |
657 | 'flat | |
658 | calc-language))) | |
659 | (math-format-value (symbol-value x) 100000))) | |
bf77c646 | 660 | ")\n"))))))) |
136211a9 EZ |
661 | |
662 | (defun calc-assign (arg) | |
663 | (interactive "P") | |
664 | (calc-slow-wrapper | |
bf77c646 | 665 | (calc-binary-op ":=" 'calcFunc-assign arg))) |
136211a9 EZ |
666 | |
667 | (defun calc-evalto (arg) | |
668 | (interactive "P") | |
669 | (calc-slow-wrapper | |
bf77c646 | 670 | (calc-unary-op "=>" 'calcFunc-evalto arg))) |
136211a9 EZ |
671 | |
672 | (defun calc-subscript (arg) | |
673 | (interactive "P") | |
674 | (calc-slow-wrapper | |
bf77c646 | 675 | (calc-binary-op "sub" 'calcFunc-subscr arg))) |
136211a9 | 676 | |
4b02a252 JB |
677 | (provide 'calc-store) |
678 | ||
be19ef0b GM |
679 | ;; Local variables: |
680 | ;; coding: utf-8 | |
681 | ;; End: | |
682 | ||
bf77c646 | 683 | ;;; calc-store.el ends here |