 ;; Logic
 (defun emdocs--interaction-filter (proc string)
   (when (process-live-p proc)
+    ;; TODO: would love to use the native json lib, but that's not available in most emacs (and
+    ;; likely never will be? consider making a native rust module?).
     (let ((msg (json-read-from-string string)))
       (unless (and (stringp msg) (string-equal msg "ok"))
         (let* ((op (alist-get 'op msg))

   :noquery t
   :stderr (get-buffer-create emdocs--error-buffer-name)))

(defun emdocs--after-change-function (beg end prev-length)
  (when emdocs-mode
    (let ((source (emdocs--create-buffer-id emdocs-buffer-id))
          (point (emdocs--create-point beg)))
      (->> (cond ((zerop prev-length)
                  (->> (buffer-substring-no-properties beg end)
                       (emdocs--create-insert point)))
                 ((= beg end)
                  (emdocs--create-delete point prev-length))
                 (t (error "should never get here")))
           (emdocs--create-edit source)
           (emdocs--write-stdin)))))

(defun emdocs--before-change-function (beg end)
  ;; TODO: set up undo list, etc

  (add-hook 'after-change-functions #'emdocs--after-change-function nil t)
  (add-hook 'before-change-functions #'emdocs--before-change-function nil t))

(defun emdocs--initiate-process-idempotent ()
  (unless (emdocs--process-live-p)
    (emdocs--make-process)))


;; Interactive

(defun emdocs-kill-process ()
  (interactive)
  (when (emdocs--process-live-p)
    (message "%s" "killing emdocs process...")
    (kill-process emdocs--process-name)))

(defun emdocs-restart-process ()
  (interactive)
  (emdocs-kill-process)
  (emdocs--make-process))

;;;###autoload
(define-minor-mode emdocs-mode
