Persist scratch buffer point, mode & contents

Also renames doom-scratch-buffer-major-mode ->
doom-scratch-initial-major-mode, since it only affects the initial
buffer now.

This was designed to be backwards compatible; you won't lose your
scratch buffers from this update. Though I may remove the old format in
3.1.
This commit is contained in:
Henrik Lissner 2020-02-25 20:30:23 -05:00
parent 0a9b06ac16
commit 9fa76836c1
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 58 additions and 30 deletions

View file

@ -54,7 +54,7 @@ the documentation](docs/getting_started.org#install).
<img src="https://github.com/hlissner/doom-emacs/raw/screenshots/cacochan.png" align="right" /> <img src="https://github.com/hlissner/doom-emacs/raw/screenshots/cacochan.png" align="right" />
</a> </a>
It is a story as old as time. A stubborn, shell-dwelling, and melodramatic It is a story as old as time. A stubborn, shell-dwelling, and melodramatic as
vimmer -- envious of the features of modern text editors -- spirals into despair vimmer -- envious of the features of modern text editors -- spirals into despair
before succumbing to the [dark side][url:evil-mode]. This is his config. before succumbing to the [dark side][url:evil-mode]. This is his config.

View file

@ -36,7 +36,7 @@ it if it doesn't exist).")
;; ;;
;; Functions ;;; Functions
;;;###autoload ;;;###autoload
(defun doom-buffer-frame-predicate (buf) (defun doom-buffer-frame-predicate (buf)

View file

@ -8,9 +8,11 @@ Will be saved in `doom-scratch-dir'.")
(defvar doom-scratch-dir (concat doom-etc-dir "scratch") (defvar doom-scratch-dir (concat doom-etc-dir "scratch")
"Where to save persistent scratch buffers.") "Where to save persistent scratch buffers.")
(defvar doom-scratch-buffer-major-mode nil (defvar doom-scratch-initial-major-mode nil
"What major mode to use in scratch buffers. This can be one of the "What major mode to start fresh scratch buffers in.
following:
Scratch buffers preserve their last major mode, however, so this only affects
the first, fresh scratch buffer you create. This accepts:
t Inherits the major mode of the last buffer you had selected. t Inherits the major mode of the last buffer you had selected.
nil Uses `fundamental-mode' nil Uses `fundamental-mode'
@ -30,36 +32,55 @@ following:
(setq-local doom-scratch-current-project (setq-local doom-scratch-current-project
(or name (or name
doom-scratch-default-file)) doom-scratch-default-file))
(let ((scratch-file (let ((smart-scratch-file
(expand-file-name (concat doom-scratch-current-project ".el")
doom-scratch-dir))
(scratch-file
(expand-file-name doom-scratch-current-project (expand-file-name doom-scratch-current-project
doom-scratch-dir))) doom-scratch-dir)))
(make-directory doom-scratch-dir t) (make-directory doom-scratch-dir t)
(when (file-readable-p scratch-file) (cond ((file-readable-p smart-scratch-file)
(let ((pt (point))) (message "Reading %s" smart-scratch-file)
(erase-buffer) (cl-destructuring-bind (content point mode)
(insert-file-contents scratch-file) (with-temp-buffer
(set-auto-mode) (save-excursion (insert-file-contents smart-scratch-file))
(goto-char pt)) (read (current-buffer)))
t))) (erase-buffer)
(funcall mode)
(insert content)
(goto-char point)
t))
((file-readable-p scratch-file) ; DEPRECATED
(when (file-readable-p scratch-file)
(let ((pt (point)))
(erase-buffer)
(insert-file-contents scratch-file)
(set-auto-mode)
(goto-char pt))
t)))))
;;;###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."
(with-current-buffer (let* ((buffer-name (if project-name
(get-buffer-create (if project-name (format "*doom:scratch (%s)*" project-name)
(format "*doom:scratch (%s)*" project-name) "*doom:scratch*"))
"*doom:scratch*")) (buffer (get-buffer buffer-name)))
(setq default-directory directory) (with-current-buffer
(unless doom-scratch-current-project (or buffer (get-buffer-create buffer-name))
(setq default-directory directory)
(setq-local so-long--inhibited t)
(doom--load-persistent-scratch-buffer project-name) (doom--load-persistent-scratch-buffer project-name)
(when (and (eq major-mode 'fundamental-mode) (when (and (eq major-mode 'fundamental-mode)
(functionp mode)) (functionp mode))
(funcall mode))) (funcall mode))
(cl-pushnew (current-buffer) doom-scratch-buffers) (cl-pushnew (current-buffer) doom-scratch-buffers)
(add-hook 'kill-buffer-hook #'doom-persist-scratch-buffer-h nil 'local) (add-transient-hook! 'doom-switch-buffer-hook (doom-persist-scratch-buffers-h))
(add-hook 'doom-switch-buffer-hook #'doom-persist-scratch-buffers-after-switch-h) (add-transient-hook! 'doom-switch-window-hook (doom-persist-scratch-buffers-h))
(run-hooks 'doom-scratch-buffer-created-hook) (add-hook 'kill-buffer-hook #'doom-persist-scratch-buffer-h nil 'local)
(current-buffer))) (add-hook 'window-configuration-change-hook #'doom-persist-scratch-buffers-after-switch-h)
(run-hooks 'doom-scratch-buffer-created-hook)
(current-buffer))))
;; ;;
@ -68,11 +89,18 @@ following:
;;;###autoload ;;;###autoload
(defun doom-persist-scratch-buffer-h () (defun doom-persist-scratch-buffer-h ()
"Save the current buffer to `doom-scratch-dir'." "Save the current buffer to `doom-scratch-dir'."
(write-region (let ((content (buffer-substring-no-properties (point-min) (point-max)))
(point-min) (point-max) (point (point))
(expand-file-name (or doom-scratch-current-project (mode major-mode))
doom-scratch-default-file) (with-temp-file
doom-scratch-dir))) (expand-file-name (concat (or doom-scratch-current-project
doom-scratch-default-file)
".el")
doom-scratch-dir)
(prin1 (list content
point
mode)
(current-buffer)))))
;;;###autoload ;;;###autoload
(defun doom-persist-scratch-buffers-h () (defun doom-persist-scratch-buffers-h ()