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:
parent
0a9b06ac16
commit
9fa76836c1
3 changed files with 58 additions and 30 deletions
|
@ -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.
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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)
|
||||||
|
(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)
|
(when (file-readable-p scratch-file)
|
||||||
(let ((pt (point)))
|
(let ((pt (point)))
|
||||||
(erase-buffer)
|
(erase-buffer)
|
||||||
(insert-file-contents scratch-file)
|
(insert-file-contents scratch-file)
|
||||||
(set-auto-mode)
|
(set-auto-mode)
|
||||||
(goto-char pt))
|
(goto-char pt))
|
||||||
t)))
|
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)))
|
||||||
|
(with-current-buffer
|
||||||
|
(or buffer (get-buffer-create buffer-name))
|
||||||
(setq default-directory directory)
|
(setq default-directory directory)
|
||||||
(unless doom-scratch-current-project
|
(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-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 '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)
|
(run-hooks 'doom-scratch-buffer-created-hook)
|
||||||
(current-buffer)))
|
(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))
|
||||||
|
(with-temp-file
|
||||||
|
(expand-file-name (concat (or doom-scratch-current-project
|
||||||
doom-scratch-default-file)
|
doom-scratch-default-file)
|
||||||
doom-scratch-dir)))
|
".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 ()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue