Is there a possibility to save the Emacs Calculator Trail (as a file) and to restore it later? If not, should it be feature?

For example, when I have an incomplete calculation but have to shutdown my Emacs, it would be great if i can continue afterwards.

Second case: I do some calculations and want to store how I did it, to later check if I did it correctly or need to modify it.

The second reason is why I use MATLAB (I know, too much) for little calculations, just so i can restore them. But I would like to use Emacs Calc only!

Thanks for the answers

  • 1
    Maybe Embedded Mode is what you want? – npostavs Jun 30 '15 at 10:18
  • I know Calc is well integrated with keyboard macros and even extends them. I don't see why you can't save them as regular kmacros. Also have you tried GNU Octave? It is supposed to be mostly compatible with Matlab and comes with an emacs mode. – Kevin Holmes Jul 1 '15 at 13:16
  • 1
    I believe that Org's src blocks (besides embedded mode) for calc will be the way to go, especially for your second case. Unfortunately the src block code is not (yet) sophisticated enough for more than basic stuff. Please have a look at: home.fnal.gov/~neilsen/notebook/orgExamples/… – Dieter.Wilhelm Aug 29 '15 at 9:33
  • @estownya - Please post some example calc code with the answer returned by calc. – Melioratus Dec 12 '15 at 21:37
  • Well, the Calc Trail is a buffer like any other. You can switch to it and C-x C-w to write it out. I don't think there's any easy way to reconstruct all of the Calc state when you want to reload. – MAP Jul 5 '16 at 8:19

Since the processing of the calc trail is text based one can essentially use write-region and insert-file-contents.

Nevertheless some details must be considered. The following Elisp code lets you write the current calc-trail buffer to disk with C-x C-s and you can read back this stuff at current cursor position with C-x i.

Afterwards you can use the calc-trail-mode bindings to evaluate parts of the read-in calc commands.

(defvar-local calc-trail-buffer-file-name nil
  "Like `buffer-file-name' for calc-trail buffers.")

(defun calc-trail-save (&optional filename)
  "Save current calc trail buffer.
To be used in `write-contents-functions'.
Append with current prefix arg."
  (interactive "FCalc Trail File: ")
  (unless filename
    (setq calc-trail-buffer-file-name
      (expand-file-name (setq filename
                  (read-file-name "Calc Trail File: " nil calc-trail-buffer-file-name)))))
    (when (null (derived-mode-p 'calc-trail-mode))
    (user-error "Saving calc trail buffers requires calc-trail-mode"))
     (let* ((b-trail (progn (goto-char 1) (1+ (line-end-position))))
        (b (progn (goto-char (max (or (and (use-region-p) (region-beginning)) (point-min)) b-trail))
        (e (progn (goto-char (max (or (and (use-region-p) (region-end)) (point-max)) b-trail))
       (write-region b e filename current-prefix-arg)))))

(defun calc-insert-file (filename)
  "Insert calc-trail file FILENAME at point."
  (interactive "FCalc trail file: ")
  (when (= (line-beginning-position) 1)
    (goto-char (1+ (line-end-position))))
  (goto-char (line-beginning-position
          (if (looking-at "[[:space:]]*$")
  (let ((inhibit-read-only t))
    (insert-file-contents filename)
    (when (and (null (looking-at "[[:space:]]*$"))
           (null (looking-back "^[[:space:]]*" (line-beginning-position))))
      (insert "\n"))))

(defun calc-trail-install-save ()
  "Install `calc-trail-save' in `write-contents-functions' of `calc-trail-mode' buffers."
  (push #'calc-trail-save write-contents-functions)
  (local-set-key (kbd "C-x i") #'calc-insert-file))

(add-hook 'calc-trail-mode-hook #'calc-trail-install-save)
| improve this answer | |

you can use s p which mean (calc-permanent-variable &optional VAR) to save all variable to ~/.emacs.d/calc.el or calc-settings-file .

| improve this answer | |
  • 2
    But this just saves the current value, rather than the history of the calculations. – Andrew Swann Mar 18 '19 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.