aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew White <mehw.is.me@inventati.org>2021-06-24 16:52:37 +0000
committerMatthew White <mehw.is.me@inventati.org>2021-10-21 01:27:37 +0200
commit969d666f59cb948d36f139b0b10001ca99ef4dc2 (patch)
tree17b6fb0fa87d4692fa3be0a7bacc680bb8610c9f
parentb3e9de8cd76622d56f549e1cd9353a45ddecb4d6 (diff)
persp-get-scratch-buffer: get or create a scratch buffer
Delegate 'persp-get-scratch-buffer' to get/create a perspective's scratch buffer named "*scratch* (NAME)". Modify 'persp-new' into using this new function. When a scratch buffer is created by 'persp-get-scratch-buffer' it is properly set. An existing buffer is returned as is.
-rw-r--r--CHANGELOG.md2
-rw-r--r--perspective.el25
2 files changed, 20 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2d61c0b..e5f6b19 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
+- `persp-get-scratch-buffer`: utility function to properly get/create a scratch buffer.
- `persp-forget-buffer`: disassociate buffer with perspective without the risk of killing it. This balances `persp-add-buffer`. Newly created buffers via `get-buffer-create` are rogue buffers not found in any perspective, this function allows to get back to that state.
- `persp-maybe-kill-buffer`: designed as `kill-buffer-query-functions` hook to keep a perspective's last left buffer from being killed.
- `persp-get-buffer-names`: get any perspective's list of live buffers.
@@ -50,6 +51,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
+- `persp-new`: call `persp-get-scratch-buffer` to get/create a scratch buffer.
- `persp-switch`: remove duplicated code. It's now possible to call `persp-new` either to get an existing perspective or to create a new one.
- `persp-mode`: add/remove `persp-maybe-kill-buffer` hook.
- `persp-kill`: switch `persp-maybe-kill-buffer` on/off to allow killing a perspective's last left buffer.
diff --git a/perspective.el b/perspective.el
index bffda68..f178edf 100644
--- a/perspective.el
+++ b/perspective.el
@@ -216,6 +216,23 @@ filtering in buffer display modes like ibuffer."
(unless initial-persp
(format " (%s)" name)))))
+(defun persp-get-scratch-buffer (&optional name)
+ "Return the \"*scratch* (NAME)\" buffer.
+Create it if the current perspective doesn't have one yet."
+ (let* ((scratch-buffer-name (persp-scratch-buffer name))
+ (scratch-buffer (get-buffer scratch-buffer-name)))
+ ;; Do not interfere with an existing scratch buffer's status.
+ (unless scratch-buffer
+ (setq scratch-buffer (get-buffer-create scratch-buffer-name))
+ (with-current-buffer scratch-buffer
+ (when (eq major-mode 'fundamental-mode)
+ (funcall initial-major-mode))
+ (when (and (zerop (buffer-size))
+ initial-scratch-message)
+ (insert (substitute-command-keys initial-scratch-message))
+ (set-buffer-modified-p nil))))
+ scratch-buffer))
+
(defalias 'persp-killed-p 'persp-killed
"Return whether the perspective CL-X has been killed.")
@@ -556,13 +573,7 @@ The new perspective will start with only an `initial-major-mode'
buffer called \"*scratch* (NAME)\"."
(or (gethash name (perspectives-hash))
(make-persp :name name
- (switch-to-buffer (persp-scratch-buffer name))
- (when (eq major-mode 'fundamental-mode)
- (funcall initial-major-mode))
- (when (and (zerop (buffer-size))
- initial-scratch-message)
- (insert (substitute-command-keys initial-scratch-message))
- (set-buffer-modified-p nil))
+ (switch-to-buffer (persp-get-scratch-buffer name))
(persp-reset-windows))))
(defun persp-reactivate-buffers (buffers)