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

  `(:op (:source ,source :transform (:type (:edit ,transform)))))

(defun emdocs--write-stdin (x)
  (when (process-live-p (get-process emdocs--process-name))
    (let ((line (format "%s\n" (json-encode x))))
      (process-send-string emdocs--process-name line))))

(defun emdocs--make-process ()
  (make-process
   :name emdocs--process-name
   :buffer emdocs--process-buffer-name

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

(defun emdocs--process-live-p ()
  (process-live-p (get-process emdocs--process-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)))
      (let ((delete-op (unless (zerop prev-length)
                         (emdocs--create-delete point prev-length)))
            (insert-op (unless (= beg end)
                         (->> (buffer-substring beg end)
                              (emdocs--create-insert point)))))
        (cl-assert (or delete-op insert-op) t "every change must be an insert, delete, or both")
        (cl-loop for op in `(,delete-op ,insert-op)
                 when op
                 do (->> op
                         (emdocs--create-edit source)
                         (emdocs--write-stdin)))))))

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