Tuesday, December 17, 2013

Run mathematica commands in emacs -- updated

Sometime ago I posted how to run Mathematica code in Emacs. Now this is improved -- faster and cleaner version:


(defun wy-rl-beginning () ; "rl" denotes "region or line"
  (if mark-active (region-beginning) (line-beginning-position))) 
(defun wy-rl-end () ; "rl" denotes "region or line"
  (if mark-active (region-end) (line-end-position)))
(defun wy-get-rl ()
  (buffer-substring-no-properties (wy-rl-beginning) (wy-rl-end)) )
(defun wy-remove-comment (s)
  "Remove comment symbol (on the left only)."
  (if (string-match (concat "\\`[ \t\n\r]*" comment-start "+") s)
      (replace-match "" t t s)
    s))
(defun wy-math2 ()
  (interactive)
  (let ((expr) (code) (res))
    (setq expr (wy-remove-comment (wy-get-rl)))
    (setq code (concat "echo 'Print[" expr "]'>/tmp/Meval2.tmp.m;"
      "MathematicaScript -script /tmp/Meval2.tmp.m"))
    (setq res (shell-command-to-string code))
    (kill-new res)
    (princ res)))


No comments:

Post a Comment