aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConstantine Vetoshev <gepardcv@gmail.com>2020-04-13 16:54:28 -0700
committerConstantine Vetoshev <gepardcv@gmail.com>2020-04-13 16:54:28 -0700
commit67e1d33101885c1ab3dc1b76e17c0a8e32b1a077 (patch)
treebfab8dd9823f4c72a45b9efa2830a1ce1ab66d98
parent652bfb69e4ddaf6e99a7337ea68d2f996fdb5085 (diff)
Add persp-switch-to-buffer* function and documentation.
-rw-r--r--README.md13
-rw-r--r--perspective.el21
2 files changed, 34 insertions, 0 deletions
diff --git a/README.md b/README.md
index 60bde0f..17834ee 100644
--- a/README.md
+++ b/README.md
@@ -172,6 +172,19 @@ in all perspectives. The distinction between the `ivy` and `counsel` versions is
the same as between `ivy-switch-buffer` and `counsel-switch-buffer`: the latter
shows a preview of the buffer to switch to, and the former does not.
+**Frameworks which change `completing-read`**: For completion frameworks which
+aim to enhance `completing-read` (such as
+[Selectrum](https://github.com/raxod502/selectrum)), Perspective provides
+`persp-switch-to-buffer*`. It behaves like the built-in `switch-to-buffer`, but
+uses `completing-read` directly. When this function is called normally, it shows
+a list of buffers filtered by the current perspective. With a prefix argument,
+it shows a list of buffers in all perspectives. For users of a framework like
+Selectrum, it might make sense to remap the key binding of `switch-to-buffer` to
+`persp-switch-to-buffer*` and gain the benefits of both Perspective and enhanced
+`completing-read`. (This will also work with Ivy and Counsel running in full
+`ivy-mode`, but sacrifices some of their dedicated switchers' more advanced
+features, like dedicated keymaps.)
+
Globally binding one of these helper functions to a buffer-switching key is a
good idea, e.g.:
diff --git a/perspective.el b/perspective.el
index 2f89c45..0a3ab1d 100644
--- a/perspective.el
+++ b/perspective.el
@@ -1092,6 +1092,27 @@ perspective beginning with the given letter."
;; `other-buffer'.
(get-buffer-create (persp-scratch-buffer)))))
+;; Buffer switching integration: useful for frameworks which enhance the
+;; built-in completing-read (e.g., Selectrum).
+;;;###autoload
+(defun persp-switch-to-buffer* (buffer-or-name)
+ "Like `switch-to-buffer', restricted to the current perspective."
+ (interactive
+ (list
+ (if current-prefix-arg
+ (let ((read-buffer-function nil))
+ (read-buffer-to-switch "Switch to buffer: "))
+ (completing-read "Switch to buffer: "
+ (append
+ (list (buffer-name (persp-other-buffer)))
+ (cl-loop for buf in (persp-current-buffers)
+ if (and (buffer-live-p buf)
+ (not (equal buf (current-buffer)))
+ (not (equal buf (persp-other-buffer))))
+ collect (buffer-name buf)))))))
+ (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
+ (switch-to-buffer buffer)))
+
;; Buffer switching integration: bs.el.
;;;###autoload
(defun persp-bs-show (arg)