Custom Scratch Buffers

It can be useful to create temporary scratch buffers in specific languages. A number of packages exist to help you do this. I, however, prefer writing code over installing packages, so I came up with the following:

(defvar scratch-org-initial-message "# Org Mode Scratch Buffer\n\n"
  "Message to be inserted in org scratch buffer.")

(defvar scratch-org-buffer "*scratch-org*"
  "Name of org-mode scratch buffer.")

(defun scratch-buffer-org ()
  "Create a *scratch* buffer in Org Mode and switch to it."
  (let ((buf scratch-org-buffer))
    (if (get-buffer buf)
        (switch-to-buffer buf)
        (switch-to-buffer buf)
        (with-current-buffer buf
          (setq-local buffer-confirm-kill t)
          (setq-local buffer-offer-save t)
          (insert scratch-org-initial-message)

The above function sets the variable buffer-confirm-kill which I wrote a separate note about.