 ;;;###autoload
 (defun doom-scratch-buffer (&optional mode directory project-name)
   "Return a scratchpad buffer in major MODE."
-  (let* ((buffer-name (if project-name
-                          (format "*doom:scratch (%s)*" project-name)
-                        "*doom:scratch*"))
-         (buffer (get-buffer buffer-name)))
-    (with-current-buffer (get-buffer-create buffer-name)
-      (unless buffer
-        (setq buffer (current-buffer)
-              default-directory directory
-              doom-scratch-current-project project-name)
-        (setq doom-scratch-buffers (cl-delete-if-not #'buffer-live-p doom-scratch-buffers))
-        (cl-pushnew buffer doom-scratch-buffers)
-        (doom--load-persistent-scratch-buffer project-name)
-        (when (and (eq major-mode 'fundamental-mode)
-                   (functionp mode))
-          (funcall mode))
-        (add-hook 'kill-buffer-hook #'doom-persist-scratch-buffer-h nil 'local)
-        (run-hooks 'doom-scratch-buffer-created-hook))
-      buffer)))
+  (with-current-buffer
+      (get-buffer-create (if project-name
+                             (format "*doom:scratch (%s)*" project-name)
+                           "*doom:scratch*"))
+    (setq default-directory directory)
+    (unless doom-scratch-current-project
+      (doom--load-persistent-scratch-buffer project-name)
+      (when (and (eq major-mode 'fundamental-mode)
+                 (functionp mode))
+        (funcall mode)))
+    (cl-pushnew (current-buffer) doom-scratch-buffers)
+    (add-hook 'kill-buffer-hook #'doom-persist-scratch-buffer-h nil 'local)
+    (run-hooks 'doom-scratch-buffer-created-hook)
+    (current-buffer)))
 
 
 ;;

(defvar doom-scratch-buffers nil
  "A list of active scratch buffers.")

(defvar-local doom-scratch-current-project nil
  "The name of the project associated with the current scratch buffer.")

(defvar doom-scratch-buffer-hook ()
  "The hooks to run after a scratch buffer is created.")

(defun doom--load-persistent-scratch-buffer (name)
  (let ((scratch-file (expand-file-name (or name doom-scratch-default-file)
                                        doom-scratch-dir)))
    (make-directory doom-scratch-dir t)
    (if (not (file-readable-p scratch-file))
        nil
      (erase-buffer)
      (insert-file-contents scratch-file)
      (set-auto-mode)

  "Save the current buffer to `doom-scratch-dir'."
  (write-region
   (point-min) (point-max)
   (expand-file-name (or doom-scratch-current-project
                         doom-scratch-default-file)
                     doom-scratch-dir)))

;;;###autoload
(defun doom-persist-scratch-buffers-h ()
  "Save all scratch buffers to `doom-scratch-dir'."
  (setq doom-scratch-buffers
        (cl-delete-if-not #'buffer-live-p doom-scratch-buffers))
  (dolist (buffer doom-scratch-buffers)
    (with-current-buffer buffer
      (doom-persist-scratch-buffer-h))))
