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