   (member this-command
           '(calc-dispatch)))
 
+(defvar vimgolf-keystrokes nil
+  "A list of (keys-vector . command) pairs for the keystrokes entered.
+
+Each entry is a cons cell containing a key sequence vector
+suitable for use with `key-description', and a symbol for the
+command that was executed as a result (which may be nil if un
+unknown key sequence was entered).")
+
 (defun vimgolf-maybe-capture-keystroke (pred)
-  "Log the keystrokes for `this-command' if function `PRED' returns a non-nil result."
+  "Store the keystrokes for `this-command' if result of calling function `PRED' is not nil."
   (vimgolf-with-saved-command-environment
    (when (funcall pred)
-     (with-current-buffer (get-buffer-create vimgolf-keystrokes-buffer-name)
-       (end-of-buffer)
-       (insert (key-description (this-command-keys)))
-       (insert " ")))))
+     (setq vimgolf-keystrokes
+           (append vimgolf-keystrokes (list (cons (this-command-keys-vector)
+                                                  this-command)))))))
 
 (defun vimgolf-capture-keystroke ()
   (vimgolf-maybe-capture-keystroke 'vimgolf-capturable-keystroke-p))

(defun vimgolf-capture-dangling-keystroke ()
  (vimgolf-maybe-capture-keystroke 'vimgolf-capturable-dangling-keystroke-p))

;; (setq vimgolf-logging-enabled t)
;; (setq vimgolf-logging-enabled)
(defvar vimgolf-logging-enabled nil)

(defun vimgolf-log-keystroke ()
  (when (and vimgolf-logging-enabled (not (< 0 (recursion-depth))))
    (vimgolf-with-saved-command-environment
     (with-current-buffer (get-buffer-create vimgolf-keystrokes-log-buffer-name)
       (end-of-buffer)
       (insert (key-description (this-command-keys)))
       (insert " ")
       (princ this-command (current-buffer))
       (insert "\n")))))

(defun vimgolf-enable-capture (enable)
  "Enable keystroke logging if `ENABLE' is non-nil otherwise disable it."
  (let ((f (if enable 'add-hook 'remove-hook)))
    (funcall f 'pre-command-hook 'vimgolf-capture-keystroke)
    (funcall f 'post-command-hook 'vimgolf-capture-dangling-keystroke)
    (funcall f 'pre-command-hook 'vimgolf-log-keystroke)
    (funcall f 'post-command-hook 'vimgolf-log-keystroke)))

(defun vimgolf-count-keystrokes ()
  (with-current-buffer (get-buffer vimgolf-keystrokes-buffer-name)
    (beginning-of-buffer)
    (let ((count 0))
      (while (search-forward " " nil t)
        (setq count (1+ count)))
      count)))

(defun vimgolf-right-solution ()
  (message "Hurray!")

  (if (vimgolf-solution-correct-p) (vimgolf-right-solution) (vimgolf-wrong-solution)))

(defun vimgolf-clear-keystrokes ()
  (setq vimgolf-keystrokes nil))

(defun vimgolf-reset-work-buffer ()
  "Reset the contents of the work buffer, and clear undo/macro history etc."
