Code for Nethack screenshots in Emacs

(defvar nethack-screenshot-file "~/.nethack-notes" "Filename to store Nethack data in.")
(defun sacha/nethack-take-screenshot (caption)
  (interactive "MCaption: ")
  (save-window-excursion
    (save-excursion
      (find-file nethack-screenshot-file)
      (goto-char (point-min))
      (insert
       ".#1 " caption "\n\n"
       (with-current-buffer (get-buffer "*nethack map*")
         (buffer-substring-no-properties (point-min) (point-max)))
       "\n\n"
       (with-current-buffer (get-buffer "*nethack status*")
         (buffer-substring-no-properties (point-min) (point-max)))
       "\n\n")
       (save-buffer))))