       (erase-buffer)
       (insert rst))
     (goto-char point)
-    (funcall setup-fn)
+    (push `(apply ,major-mode) buffer-undo-list)
+    (funcall new-mode)
     (set-buffer-modified-p modified)))
 
 (defun fstar-literate--rst-save ()


;;; Literate F*

;; FIXME undoing past translation yields wrong mode

(defvar-local fstar-literate--fst-name nil)
(put 'fstar-literate--fst-name 'permanent-local t)


                    (substring-no-properties output (match-end 0)))
            (1+ (match-beginning 0))))))

(defun fstar-literate--toggle (flag new-mode)
  "Run converter with FLAG, fill buffer with output, and run NEW-MODE."
  (pcase-let* ((modified (buffer-modified-p))
               (`(,rst . ,point)
                (fstar--widened-excursion (fstar-literate--run-converter flag))))
