Refactor scratch buffer; add doom-scratch-buffer-major-mode option #490

Also set the default scratch buffer major mode to fundamental-mode
This commit is contained in:
Henrik Lissner 2018-03-28 18:38:55 -04:00
parent 229d4de390
commit f44a227e74
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -5,7 +5,16 @@
`doom/open-project-scratch-buffer'.") `doom/open-project-scratch-buffer'.")
(defvar doom-scratch-buffer-display-fn #'display-buffer (defvar doom-scratch-buffer-display-fn #'display-buffer
"TODO") "The function to use to display the scratch buffer. Must accept one argument:
the buffer to display.")
(defvar doom-scratch-buffer-major-mode nil
"What major mode to use in scratch buffers. This can be one of the
following:
t Inherits the major mode of the last buffer you had selected.
nil Uses `fundamental-mode'
MAJOR-MODE Any major mode symbol")
(defvar doom-scratch-buffer-hook () (defvar doom-scratch-buffer-hook ()
"The hooks to run after a scratch buffer is made.") "The hooks to run after a scratch buffer is made.")
@ -28,8 +37,8 @@ If FILE is a valid path, open it as if it were a persistent scratchpad."
(current-buffer)) (current-buffer))
(get-buffer-create "*doom:scratch*")))) (get-buffer-create "*doom:scratch*"))))
(with-current-buffer buffer (with-current-buffer buffer
(when (and (not (eq major-mode mode)) (when (and (functionp mode)
(functionp mode)) (not (eq major-mode mode)))
(funcall mode)) (funcall mode))
(when text (when text
(insert text)) (insert text))
@ -54,7 +63,14 @@ If a region is active, copy its contents to the scratch pad."
(if-let* ((file (read-file-name "Open scratch file > " doom-scratch-files-dir "scratch"))) (if-let* ((file (read-file-name "Open scratch file > " doom-scratch-files-dir "scratch")))
file file
(user-error "Aborting"))) (user-error "Aborting")))
major-mode (cond ((eq doom-scratch-buffer-major-mode t)
(unless (or buffer-read-only
(derived-mode-p 'special-mode)
(string-match-p "^ ?\\*" (buffer-name)))
major-mode))
((null doom-scratch-buffer-major-mode) nil)
((symbolp doom-scratch-buffer-major-mode)
doom-scratch-buffer-major-mode))
(and (region-active-p) (and (region-active-p)
(buffer-substring-no-properties (buffer-substring-no-properties
(region-beginning) (region-end))))))) (region-beginning) (region-end)))))))