   "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)
+   (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))
+  (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))))

;;;###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)))


;;

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

(defvar 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)
  (setq-local doom-scratch-current-project
              (or name
                  doom-scratch-default-file))
  (let ((scratch-file
         (expand-file-name doom-scratch-current-project
                           doom-scratch-dir)))
    (make-directory doom-scratch-dir t)
    (when (file-readable-p scratch-file)
      (erase-buffer)
      (insert-file-contents scratch-file)
      (set-auto-mode)
