[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/bufferlo 43c7c3fdaa 3/4: Add functions for handling loc
|
From: |
ELPA Syncer |
|
Subject: |
[elpa] externals/bufferlo 43c7c3fdaa 3/4: Add functions for handling local scratch buffers |
|
Date: |
Sun, 19 Nov 2023 15:57:24 -0500 (EST) |
branch: externals/bufferlo
commit 43c7c3fdaafc729fd4406250a91d313064ff94e4
Author: Florian Rommel <mail@florommel.de>
Commit: Florian Rommel <mail@florommel.de>
Add functions for handling local scratch buffers
---
README.org | 30 ++++++++++++++++++++++------
bufferlo.el | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 90 insertions(+), 6 deletions(-)
diff --git a/README.org b/README.org
index c802e7bd72..e5baba1d79 100644
--- a/README.org
+++ b/README.org
@@ -201,7 +201,7 @@ You can also integrate bufferlo with ivy.
#+end_src
-** Tips
+** Initial Buffer
By default, the currently active buffer is shown in a newly created tab, so
this buffer inevitably ends up in the new tab's local list.
@@ -211,10 +211,28 @@ You can change the initial buffer by customizing
~tab-bar-new-tab-choice~:
#+end_src
This lets new tabs always start with the scratch buffer.
-To get the same behavior for frames:
+You can also create a local scratch buffer for each tab:
+#+BEGIN_SRC emacs-lisp
+ (setq tab-bar-new-tab-choice #'bufferlo-create-local-scratch-buffer)
+#+END_SRC
+You can customize the name of the local scratch buffers by setting
+~bufferlo-local-scratch-buffer-name~ accordingly.
+
+The same can be achieved for new frames.
+Use this to set the scratch buffer as the initial buffer for new frames:
#+begin_src emacs-lisp
- (defun my-set-scratch-buffer (frame)
- (with-selected-frame frame
- (switch-to-buffer "*scratch*")))
- (add-hook 'after-make-frame-functions #'my-set-scratch-buffer)
+ (add-hook 'after-make-frame-functions #'bufferlo-switch-to-scratch-buffer)
#+end_src
+
+Alternatively, create a new local scratch buffer for new frames:
+#+BEGIN_SRC emacs-lisp
+ (add-hook 'after-make-frame-functions
#'bufferlo-switch-to-local-scratch-buffer)
+#+END_SRC
+
+Of course, you can also set an arbitrary buffer as the initial frame buffer:
+#+BEGIN_SRC emacs-lisp
+ (defun my-set-initial-frame-buffer (frame)
+ (with-selected-frame frame
+ (switch-to-buffer "<BUFFER_NAME>")))
+ (add-hook 'after-make-frame-functions #'my-set-initial-frame-buffer)
+#+END_SRC
diff --git a/bufferlo.el b/bufferlo.el
index 4791201d11..b2e96066c4 100644
--- a/bufferlo.el
+++ b/bufferlo.el
@@ -109,6 +109,25 @@ This is a list of regular expressions that match buffer
names."
"If non-nil, the local buffer filter is bound to \"/ l\" in ibuffer."
:type '(repeat string))
+(defcustom bufferlo-local-scratch-buffer-name "*local scratch*"
+ "Base name of the local scratch buffer.
+Multiple frame/tabs will use `generate-new-buffer-name' (which
+appends \"<N>\" to the name) in order to get a unique buffer.
+
+Local scratch buffers are optional and not used by default.
+Use the following functions to create and work with them:
+`bufferlo-create-local-scratch-buffer',
+`bufferlo-get-local-scratch-buffer',
+`bufferlo-switch-to-local-scratch-buffer',
+and `bufferlo-toggle-local-scratch-buffer'.
+For example, create a dedicated local scratch buffer for all tabs and frames:
+ (setq \\='tab-bar-new-tab-choice #\\='bufferlo-create-local-scratch-buffer)
+ (add-hook \\='after-make-frame-functions
+ #\\='bufferlo-switch-to-local-scratch-buffer)
+
+You can set this to \"*scratch*\"."
+ :type 'string)
+
(defvar bufferlo--desktop-advice-active nil)
(defvar bufferlo--desktop-advice-active-force nil)
@@ -593,6 +612,53 @@ This is like `bufferlo-find-buffer' but additionally
selects the buffer."
(when (bufferlo-find-buffer buffer-or-name)
(switch-to-buffer buffer-or-name)))
+(defun bufferlo-get-local-scratch-buffer ()
+ "Get the local scratch buffer or create it if not already existent and
return it."
+ (let ((buffer (seq-find (lambda (b)
+ (string-match-p
+ (concat
+ "^"
+ (regexp-quote bufferlo-local-scratch-buffer-name)
+ "\\(<[0-9]*>\\)?$")
+ (buffer-name b)))
+ (bufferlo-buffer-list nil nil t))))
+ (or buffer
+ (get-buffer-create
+ (generate-new-buffer-name bufferlo-local-scratch-buffer-name)))))
+
+(defun bufferlo-create-local-scratch-buffer ()
+ "Create a local scratch buffer and return it."
+ (get-buffer-create (generate-new-buffer-name
bufferlo-local-scratch-buffer-name)))
+
+(defun bufferlo-switch-to-scratch-buffer (&optional frame)
+ "Switch to the scratch buffer.
+When FRAME is non-nil, switch to the scratch buffer in the specified frame
+instead of the current one."
+ (interactive)
+ (if frame
+ (with-selected-frame frame
+ (switch-to-buffer "*scratch*"))
+ (switch-to-buffer "*scratch*")))
+
+(defun bufferlo-switch-to-local-scratch-buffer (&optional frame)
+ "Switch to the local scratch buffer.
+When FRAME is non-nil, switch to the local scratch buffer in the specified
frame
+instead of the current one."
+ (interactive)
+ (if frame
+ (with-selected-frame frame
+ (switch-to-buffer (bufferlo-get-local-scratch-buffer)))
+ (switch-to-buffer (bufferlo-get-local-scratch-buffer))))
+
+(defun bufferlo-toggle-local-scratch-buffer ()
+ "Switch to the local scratch buffer or bury it if it is already selected.
+Creates a new local scratch buffer if none exists for this frame/tab."
+ (interactive)
+ (let ((buffer (bufferlo-get-local-scratch-buffer)))
+ (if (eq buffer (current-buffer))
+ (bury-buffer)
+ (switch-to-buffer buffer))))
+
(defun bufferlo-switch-to-buffer (buffer &optional norecord force-same-window)
"Display the BUFFER in the selected window.
Completion includes only local buffers.