 (defun vimgolf-challenge-url (challenge-id)
   (concat vimgolf-host (vimgolf-challenge-path challenge-id) vimgolf-challenge-extension))
 
-(defun vimgolf-scrub-buffer (buffer)
+(defun vimgolf-init-buffer (buffer text)
   (with-current-buffer buffer
+    (erase-buffer)
+    (insert text)
     (beginning-of-buffer)
-    (re-search-forward data-end-regexp nil t)
-    (delete-region (match-beginning 0) (point-max))
-    (decrease-left-margin (point-min) (point-max) 4)
-    (beginning-of-buffer)
-    (setq buffer-undo-list nil)
     (vimgolf-mode t)))
 
 (defun vimgolf-kill-existing-session ()
-  (when (get-buffer vimgolf-start-buffer-name) (kill-buffer vimgolf-start-buffer-name))
-  (when (get-buffer vimgolf-work-buffer-name) (kill-buffer vimgolf-work-buffer-name))
-  (when (get-buffer vimgolf-end-buffer-name) (kill-buffer vimgolf-end-buffer-name))
-  (when (get-buffer vimgolf-keystrokes-buffer-name) (kill-buffer vimgolf-keystrokes-buffer-name))
-  (when (get-buffer vimgolf-keystrokes-log-buffer-name) (kill-buffer vimgolf-keystrokes-log-buffer-name)))
+  (dolist (buf (list vimgolf-start-buffer-name
+                     vimgolf-work-buffer-name
+                     vimgolf-end-buffer-name
+                     vimgolf-keystrokes-buffer-name
+                     vimgolf-keystrokes-log-buffer-name))
+    (when (get-buffer buf)
+      (kill-buffer buf))))
+
+(defun vimgolf-read-next-data-chunk ()
+  "Return the next chunk of data as a string, leaving the point at the end of that chunk."
+  (let ((data-start-regexp "  data: |\\+\\{0,1\\}
+")
+        (data-end-regexp "\\([ 	]\\{4\\}\\|[ 	]\\{0\\}\\)
+  type: [-a-z]+"))
+    (unless (re-search-forward data-start-regexp nil t)
+      (error "Can't find data in response from vimgolf"))
+    (let ((start (point)))
+      (unless (re-search-forward data-end-regexp nil t)
+        (error "Unclosed data section in response from vimgolf"))
+      (let ((str (buffer-substring-no-properties start (match-beginning 0))))
+        (replace-regexp-in-string "^    " "" str)))))
 
 ;;;###autoload
 (defun vimgolf (challenge-id)

  (with-current-buffer (get-buffer-create vimgolf-keystrokes-buffer-name)
    (erase-buffer)))

(defun vimgolf-revert ()
  "Revert the work buffer to it's original state and reset keystrokes."
  (interactive)
  (with-current-buffer (get-buffer-create vimgolf-work-buffer-name)
    (when defining-kbd-macro
      (end-kbd-macro))
    (delete-region (point-min) (point-max))
    (insert-buffer (get-buffer-create vimgolf-start-buffer-name))
    (vimgolf-clear-keystrokes)
    (set-window-configuration vimgolf-working-window-configuration)
    (message "If at first you don't succeed, try, try again.")))

(defun vimgolf-diff ()
  "Pause the competition and view differences between the buffers."

  (vimgolf-clear-keystrokes)
  (setq vimgolf-prior-window-configuration (current-window-configuration)
        vimgolf-challenge challenge-id)
  (let ((vimgolf-yaml-buffer (url-retrieve-synchronously (vimgolf-challenge-url challenge-id))))
    (set-buffer vimgolf-yaml-buffer)
    (beginning-of-buffer)
    (let* ((start-text (vimgolf-read-next-data-chunk))
           (end-text (vimgolf-read-next-data-chunk)))

      (vimgolf-kill-existing-session)

      (let ((vimgolf-start-buffer (get-buffer-create vimgolf-start-buffer-name))
            (vimgolf-work-buffer (get-buffer-create vimgolf-work-buffer-name))
            (vimgolf-end-buffer (get-buffer-create vimgolf-end-buffer-name)))

        (vimgolf-init-buffer vimgolf-start-buffer start-text)
        (vimgolf-init-buffer vimgolf-end-buffer end-text)
        (vimgolf-reset-work-buffer)

        ;; Set up windows
        (delete-other-windows)
        (display-buffer vimgolf-end-buffer 'display-buffer-pop-up-window)
        (set-window-buffer (selected-window) vimgolf-work-buffer)
        (switch-to-buffer vimgolf-work-buffer)
        (setq vimgolf-working-window-configuration (current-window-configuration))

        (vimgolf-capture-keystrokes)))))

;;; vimgolf.el ends here
