(let
  [api vim.api
   fun vim.fn
   opt vim.opt
   name :Scratch]

  (api.nvim_create_user_command
    name
    (fn [args]
      (let
        [filetype (if (= args.args "") :markdown args.args)
         bufname (.. name " " filetype)
         set-buf (partial api.nvim_win_set_buf 0)]
        (case (fun.bufnr bufname)
          (where bufnr (> bufnr -1)) (set-buf bufnr)
          _
          (let [bufnr (api.nvim_create_buf true true)]
           (api.nvim_buf_set_name bufnr bufname)
           (set-buf bufnr)))
        (set opt.filetype filetype)
        (api.nvim_buf_set_name 0 bufname)))
    {:complete :filetype
     :force true
     :nargs :?})

  (api.nvim_create_autocmd
    :BufDelete
    {:callback
     (fn [] ; Deleting and wiping out should be the same
       (when (vim.startswith (fun.bufname) name)
         (set opt.bufhidden :wipe)))
     :group (api.nvim_create_augroup :scratch {:clear true})}))