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 ()
     (set-mark-command nil)
     (search-forward "]=")
     (delete-region (region-beginning) (region-end)))

(defun remove-^M ()
  "Get rid of ^M characters"
  (message "remove-^M called!")
    (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")
    (mathematica-execute arg)
    ;; (delete-to-out)

;;; Define some mathematica mode keyboard bindings
(if mathematica-mode-map
      (define-key mathematica-mode-map [remap mathematica-execute]
      (define-key mathematica-mode-map (kbd "C-c 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.

  • 1
    $\begingroup$ possibly relevant gnu.org/software/emacs/manual/html_node/efaq/… also you might find more emacs experts on stackoverflow.com $\endgroup$
    – george2079
    Commented Dec 6, 2014 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
    Commented Dec 6, 2014 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
    Commented Dec 6, 2014 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
    Commented Dec 6, 2014 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
    Commented Dec 6, 2014 at 23:56

1 Answer 1


The problem seems to be related to an encoding settings of intermediate shell between Emacs and mathematica kernel. Therefore a solution is to tell Emacs to communicate with mathematica kernel using pipes, not PTYs.

Specifically, in mathematica.el wrap the call to start-process in setting process-connection-type to NIL

(let ((process-connection-type nil))                                
    (format "mathematica<%s>" (buffer-name workbuf))                   
    (format "*mathematica<%s>*" (buffer-name workbuf))                 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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