5
$\begingroup$

I am successfully using mathematica.el in Emacs with Mathematica 10. However, I have some kind of issue with character encoding whereby when I call mathematica-execute on a Mathematica expression the result prints lots of ^M characters to the output (I am running all processes on OS X Mavericks). I initially made sure that the $CharacterEncoding in Mathematica matched the file and process encoding of Emacs for the buffers being used as best I could: utf-8. There could still be an issue there, but that route didn't yield a solution. Next I thought, why not just create my own function that calls mathematica-execute and then removes the ^M characters from the resulting output. Here is the relevant code:

(defun delete-to-out ()
     (next-line)
     (set-mark-command nil)
     (search-forward "]=")
     (previous-line)
     (delete-region (region-beginning) (region-end)))

(defun remove-^M ()
  "Get rid of ^M characters"
  (interactive)
  (message "remove-^M called!")
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "\r" nil t)
      (replace-match ""))))

(defun my-mathematica-execute (arg)
  "Call mathematica-execute and then clean out the ^M characters
  it inserts by calling remove-^M"
  (interactive "P")
  (save-excursion
    (mathematica-execute arg)
    (remove-^M)
    ;; (delete-to-out)
    ))

;;; Define some mathematica mode keyboard bindings
(if mathematica-mode-map
    (progn
      (define-key mathematica-mode-map [remap mathematica-execute]
        'my-mathematica-execute)
      (define-key mathematica-mode-map (kbd "C-c m")
        'remove-^M)))

Calling my-mathematica-execute works, but isn't successful at removing the ^M characters. Calling remove-^M or delete-to-out work fine in isolation subsequently. It must be a synchronization issue or timing issue? Any help would be appreciated. Alternative solutions are certainly welcome.

$\endgroup$
  • 1
    $\begingroup$ possibly relevant gnu.org/software/emacs/manual/html_node/efaq/… also you might find more emacs experts on stackoverflow.com $\endgroup$ – george2079 Dec 6 '14 at 13:25
  • $\begingroup$ Thanks @george2079, I am trying the suggestions mentioned at the link you provided. That was a good find. My Emacs shell doesn't have this problem, but since mathematica.el uses the command line this may be related. I especially think so since the input command gets echoed which is a symptom mentioned in the linked help page. So far the suggestions haven't solved the problem, but I may need to adjust a few things for the fact that my default shell is zsh, so I am working on that as a possibility amongst other things. $\endgroup$ – Joe Dec 6 '14 at 18:18
  • $\begingroup$ I was able to find an appropriate place to hack in a function called mathematica-filter which gets set as the process filter for the mathematica process output. There I inserted a replace-regexp-in-string call to get rid of all \r characters on the output string. That works. Now I just will tweak things a bit: it currently echoes the input to output (along with the result) and I may not want that. Typically that is due to the underlying shell echoing (since Mathematica is executing in the shell, zsh). ... $\endgroup$ – Joe Dec 6 '14 at 22:45
  • $\begingroup$ I'd like to solve that fundamental issue of having zsh not echo input to output in the Emacs->zsh->Mathematica->Emacs communication, but I'll use this hack until someone who knows better comes along and shows me a better way. $\endgroup$ – Joe Dec 6 '14 at 22:46
  • $\begingroup$ I was able to get my desired outcome: no ^M characters and no input echoing by hacking on mathematica-filter in two places. For those interested, here is a link to the diffs. $\endgroup$ – Joe Dec 6 '14 at 23:56

Your Answer

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

Browse other questions tagged or ask your own question.