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