                         (when (saw-script-output-overlay-p o)
                           (throw tag o)))
                       (error "No output at position %s" pos)))))
-      (let ((output (overlay-get o 'saw-script-output))
-            (buffer (get-buffer-create (saw-script-output-buffer-name (buffer-file-name)))))
-        (with-current-buffer buffer
-          (read-only-mode 1)
-          (let ((inhibit-read-only t))
-            (widen)
-            (erase-buffer)
-            (insert output)
-            (goto-char (point-min)))
-          (pop-to-buffer buffer))))))
+      (saw-script--view-overlay-output o))))
 
 ;;; Flycheck support
 

  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-c C-c") 'saw-script-run-current-buffer)
    (define-key map (kbd "C-c C-l") 'saw-script-run-current-buffer)
    map)
  "Keymap for SAWScript mode.")


          (overlay-put overlay 'saw-script-output output)
          (overlay-put overlay 'face 'underline))))))

(defun saw-script--context-menu-items (plist)
  "Compute context menu items for PLIST."
  (let ((output (plist-get plist 'saw-script-output)))
    (if output
        (list (list "Show Output"
                    (let ((loc (point)))
                      (lambda ()
                        (interactive)
                        (saw-script-view-output loc)))))
      (list))))

(defun saw-script-output-buffer-name (file)
  "Find the name for output from FILE."
  (format "*SAW Output[%s]*" file))

(defun saw-script--view-overlay-output (overlay)
  "View the SAWScript output in OVERLAY."
  (let ((output (overlay-get overlay 'saw-script-output))
        (buffer (get-buffer-create (saw-script-output-buffer-name (buffer-file-name)))))
    (with-current-buffer buffer
      (read-only-mode 1)
      (let ((inhibit-read-only t))
        (widen)
        (erase-buffer)
        (insert output)
        (goto-char (point-min)))
      (pop-to-buffer buffer))))


(defun saw-script-view-output (pos)
