Refactor scratch buffer init
This commit is contained in:
parent
7d714b7531
commit
7ded14f548
1 changed files with 27 additions and 25 deletions
|
@ -19,18 +19,22 @@ following:
|
||||||
(defvar doom-scratch-buffers nil
|
(defvar doom-scratch-buffers nil
|
||||||
"A list of active scratch buffers.")
|
"A list of active scratch buffers.")
|
||||||
|
|
||||||
(defvar-local doom-scratch-current-project nil
|
(defvar doom-scratch-current-project nil
|
||||||
"The name of the project associated with the current scratch buffer.")
|
"The name of the project associated with the current scratch buffer.")
|
||||||
|
|
||||||
(defvar doom-scratch-buffer-hook ()
|
(defvar doom-scratch-buffer-hook ()
|
||||||
"The hooks to run after a scratch buffer is created.")
|
"The hooks to run after a scratch buffer is created.")
|
||||||
|
|
||||||
|
|
||||||
(defun doom--load-persistent-scratch-buffer (name)
|
(defun doom--load-persistent-scratch-buffer (name)
|
||||||
(let ((scratch-file (expand-file-name (or name doom-scratch-default-file)
|
(setq-local doom-scratch-current-project
|
||||||
doom-scratch-dir)))
|
(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)
|
(make-directory doom-scratch-dir t)
|
||||||
(if (not (file-readable-p scratch-file))
|
(when (file-readable-p scratch-file)
|
||||||
nil
|
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
(insert-file-contents scratch-file)
|
(insert-file-contents scratch-file)
|
||||||
(set-auto-mode)
|
(set-auto-mode)
|
||||||
|
@ -39,24 +43,20 @@ following:
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-scratch-buffer (&optional mode directory project-name)
|
(defun doom-scratch-buffer (&optional mode directory project-name)
|
||||||
"Return a scratchpad buffer in major MODE."
|
"Return a scratchpad buffer in major MODE."
|
||||||
(let* ((buffer-name (if project-name
|
(with-current-buffer
|
||||||
(format "*doom:scratch (%s)*" project-name)
|
(get-buffer-create (if project-name
|
||||||
"*doom:scratch*"))
|
(format "*doom:scratch (%s)*" project-name)
|
||||||
(buffer (get-buffer buffer-name)))
|
"*doom:scratch*"))
|
||||||
(with-current-buffer (get-buffer-create buffer-name)
|
(setq default-directory directory)
|
||||||
(unless buffer
|
(unless doom-scratch-current-project
|
||||||
(setq buffer (current-buffer)
|
(doom--load-persistent-scratch-buffer project-name)
|
||||||
default-directory directory
|
(when (and (eq major-mode 'fundamental-mode)
|
||||||
doom-scratch-current-project project-name)
|
(functionp mode))
|
||||||
(setq doom-scratch-buffers (cl-delete-if-not #'buffer-live-p doom-scratch-buffers))
|
(funcall mode)))
|
||||||
(cl-pushnew buffer doom-scratch-buffers)
|
(cl-pushnew (current-buffer) doom-scratch-buffers)
|
||||||
(doom--load-persistent-scratch-buffer project-name)
|
(add-hook 'kill-buffer-hook #'doom-persist-scratch-buffer-h nil 'local)
|
||||||
(when (and (eq major-mode 'fundamental-mode)
|
(run-hooks 'doom-scratch-buffer-created-hook)
|
||||||
(functionp mode))
|
(current-buffer)))
|
||||||
(funcall mode))
|
|
||||||
(add-hook 'kill-buffer-hook #'doom-persist-scratch-buffer-h nil 'local)
|
|
||||||
(run-hooks 'doom-scratch-buffer-created-hook))
|
|
||||||
buffer)))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
|
@ -67,13 +67,15 @@ following:
|
||||||
"Save the current buffer to `doom-scratch-dir'."
|
"Save the current buffer to `doom-scratch-dir'."
|
||||||
(write-region
|
(write-region
|
||||||
(point-min) (point-max)
|
(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)))
|
doom-scratch-dir)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-persist-scratch-buffers-h ()
|
(defun doom-persist-scratch-buffers-h ()
|
||||||
"Save all scratch buffers to `doom-scratch-dir'."
|
"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)
|
(dolist (buffer doom-scratch-buffers)
|
||||||
(with-current-buffer buffer
|
(with-current-buffer buffer
|
||||||
(doom-persist-scratch-buffer-h))))
|
(doom-persist-scratch-buffer-h))))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue