   (with-current-buffer (or buffer (current-buffer))
     (prog1
         (or intero-temp-file-name
-            (setq intero-temp-file-name
-                  (intero-canonicalize-path
-                   (intero-make-temp-file
-                    "intero" nil
-                    (concat "." (if (buffer-file-name)
-                                    (file-name-extension (buffer-file-name))
-                                  "hs"))))))
+            (progn (setq intero-temp-file-name
+                         (intero-canonicalize-path
+                          (intero-make-temp-file
+                           "intero" nil
+                           (concat "." (if (buffer-file-name)
+                                           (file-name-extension (buffer-file-name))
+                                         "hs")))))
+                   (puthash intero-temp-file-name
+                            (current-buffer)
+                            intero-temp-file-buffer-mapping)
+                   intero-temp-file-name))
       (let ((contents (buffer-string)))
         (with-temp-file intero-temp-file-name
           (insert contents))))))


(defvar intero-global-mode nil
  "Global mode is enabled?")

(defun global-intero-mode ()
  "Enable Intero on all Haskell mode buffers."


(defun intero-temp-file-origin-buffer (temp-file)
  "Get the original buffer that TEMP-FILE was created for."
  (or
   (gethash (intero-canonicalize-path temp-file)
            intero-temp-file-buffer-mapping)
   (cl-loop
    for buffer in (buffer-list)
    when (string= (intero-canonicalize-path temp-file)
                  (buffer-local-value 'intero-temp-file-name buffer))
    return buffer)))

(defun intero-make-temp-file (prefix &optional dir-flag suffix)
  "Like `make-temp-file', but using a different temp directory.
