   (with-current-buffer factor--listener-buffer
     (factor-listener-mode)))
 
-(defun factor--listener-process ()
+(defun factor--listener-process (&optional start)
   (or (and (buffer-live-p factor--listener-buffer)
            (get-buffer-process factor--listener-buffer))
-      (progn (factor--listener-start-process)
-             (factor--listener-process))))
+      (when start
+        (factor--listener-start-process)
+        (factor--listener-process t))))
 
 ;;;###autoload
 (defalias 'switch-to-factor 'run-factor)


;;;;; Synchronous interaction:

(defun factor--listener-sync-cmds (cmds &optional vocab)
  (factor--with-vocab vocab
    (mapcar #'(lambda (c)
                (comint-redirect-results-list-from-process
                 (factor--listener-process) c ".+" 0))
            cmds)))

(defsubst factor--listener-sync-cmd (cmd &optional vocab)
  (car (factor--listener-sync-cmds (list cmd) vocab)))

;;;;; Interface: see


;;; Factor listener interaction:

(defun factor--listener-send-cmd (cmd)
  (let ((proc (factor--listener-process)))
    (when proc
      (let* ((out (get-buffer-create "*factor messages*"))
             (beg (with-current-buffer out (goto-char (point-max)))))
        (comint-redirect-send-command-to-process cmd out proc nil t)
        (with-current-buffer factor--listener-buffer
          (while (not comint-redirect-completed) (sleep-for 0 1)))
        (with-current-buffer out
          (split-string (buffer-substring-no-properties beg (point-max))
                        "[\"\f\n\r\v]+" t))))))

;;;;; Current vocabulary:
(make-variable-buffer-local
