 (defun ems-stop ()
   (ems-teardown-bridge))
 
-
 (defun ems-test ()
   (ems-start)
   (ems-start-walker "\\\\frac{x}{y}")

(defun ems-start-walker (expr)
  (process-send-string
   ems-node-process
   ;TODO: Here we can get LaTeX errors.
   (concat 
    (format "mjx.typeset({math: '%s', format: 'TeX', mml:true}, " expr)
    (format "function(data) {sre.walk(data.mml)});\n"))))


;; Input: A key value
(defun ems-move-walker (key)
  (process-send-string
   ems-node-process
   (format "runWithCounter(%d, sre.move, [%d]);\n" (incf ems-request-counter) key)))


;; TODO Error handling after each step.
(defun ems-setup-bridge ()

(defvar ems-request-counter 0)
(defvar ems-results ())

(add-hook
 'inferior-js-mode-hook
 #'(lambda ()
     (add-to-list
      'comint-preoutput-filter-functions
      #'(lambda (output)
          (with-current-buffer (get-buffer "*ems-output*")
            (let ((result (ems-parse-output output)))
              (when result
                (setq ems-results (append result ems-results))
                (mapcar #'(lambda (x)
                            (insert (cdr x))
                            (insert "\n"))
                        result))
              output))))))

(defun ems-load-node ()
  (run-js inferior-js-program-command t)
