2014-12-06 73 views
0

我在Mathematica 10中成功地在Emacs中使用了mathematica.el。但是,我遇到了一些与字符编码有关的问题,当我调用Mathematica时 - 在Mathematica表达式上执行时,结果会在输出中打印大量^ M字符(I我正在OS X Mavericks上运行所有进程)。我最初确定Mathematica中的$ CharacterEncoding与Emacs的文件和进程编码相匹配,以便尽可能使用缓冲区:utf-8。那里可能还存在一个问题,但是这条路线没有提供解决方案。接下来我想,为什么不创建我自己的函数来调用mathematica-execute,然后从结果输出中移除^ M个字符。以下是相关的代码:如何解决Emacs,mathematica.el和Mathematica 10的字符编码问题?

(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))) 

调用my-mathematica-execute可以工作,但不能成功移除^ M个字符。随后调用remove-^M或从零开始删除。它必须是同步问题或计时问题?任何帮助,将不胜感激。替代解决方案当然受到欢迎

回答

1

^M确实看起来像解码问题,但它不是字符编码,而是EOL编码。两者通常在Emacs中一起指定,因此您可能需要在某处使用utf-8-dos而不是utf-8

+0

感谢@Stefan的建议。我和你一样想,但发现不是这样。 Mathematica使用utf-8,就像我的Emacs(尽我所知)相关的上下文(也就是说,这是Emacs,我们有终端,键盘,文件,进程和可能的其他上下文来设置编码)。 – Joe 2014-12-06 22:41:53

+0

我能找到一个合适的位置来破解一个名为mathematica-filter的函数,它被设置为mathematica过程输出的过程过滤器。在那里我插入了一个replace-regexp-in-string调用来摆脱所有\ r字符。这样可行。现在我只是稍微调整一下它:它现在回应输出到输出(和结果一起),我可能不希望这样。通常这是由于底层shell回显(因为Mathematica在shell中执行,zsh)。至少我现在有了我的切入点,我可以根据自己的喜好操纵输出流。 ... – Joe 2014-12-06 22:42:13

+0

希望有人会过来告诉我如何使Emacs和zsh一起工作来阻止输入的回应,但这可能是另一个问题,我可能会单独提出。再次感谢您的帮助。 – Joe 2014-12-06 22:42:50