Add +scratch-buffer

This commit is contained in:
Case Duckworth 2022-04-02 12:37:25 -05:00
parent a44a825f2c
commit 548e9b8acc
1 changed files with 25 additions and 0 deletions

View File

@ -386,5 +386,30 @@ the `format' call in a list."
`(with-eval-after-load ',this
(+with-eval-after-loads ,rest ,@body)))))
(defun +scratch-buffer (&optional nomode)
"Create a new scratch buffer and switch to it.
If the region is active, paste its contents into the scratch
buffer. The scratch buffer inherits the mode of the current
buffer unless NOMODE is non-nil. When called interactively,
NOMODE will be set when called with \\[universal-argument]."
(interactive "P")
(let* ((mode major-mode)
(bufname (generate-new-buffer-name (format "*scratch (%s)*" mode)))
(paste (and (region-active-p)
(prog1
(buffer-substring (mark t) (point))
(deactivate-mark)))))
(when (and (not nomode)
(bound-and-true-p ess-dialect)) ; Not sure what `ess-dialect' is
(setq mode (intern-soft (concat ess-dialect "-mode"))))
;; Set up buffer
(switch-to-buffer (get-buffer-create bufname))
(when (and (not nomode) mode)
(ignore-errors (funcall mode)))
(insert (format "%s Scratch buffer for %s%s\n\n"
comment-start mode comment-end))
(when paste (insert paste))
(get-buffer bufname)))
(provide 'acdw)
;;; acdw.el ends here