diff options
| author | Constantine Vetoshev <gepardcv@gmail.com> | 2021-08-11 20:19:49 -0700 |
|---|---|---|
| committer | Constantine Vetoshev <gepardcv@gmail.com> | 2021-08-11 20:19:49 -0700 |
| commit | e697017957675e610ce6cf9ddd86e2ea73665061 (patch) | |
| tree | 4363f0859fda0720d16c56294b8c5f31bad6ce55 | |
| parent | c052ab2ce23f969ad2b7853ba0b3cbd4a5954c47 (diff) | |
Simplify and improve Ivy and Counsel integration.
| -rw-r--r-- | perspective.el | 64 |
1 files changed, 25 insertions, 39 deletions
diff --git a/perspective.el b/perspective.el index 1c615d0..038176d 100644 --- a/perspective.el +++ b/perspective.el @@ -1339,50 +1339,41 @@ PERSP-SET-IDO-BUFFERS)." (ibuffer)) (ibuffer))) -(defun persp--switch-buffer-ivy-counsel-helper (arg ivy-params fallback) +;; Buffer switching integration: Ivy. +;; +;; An alternative implementation, which has the drawback of not allowing a +;; prefix argument to list all buffers: +;; +;; (defun persp-ivy-read-advice (args) +;; (append args +;; (list :predicate +;; (lambda (b) (persp-is-current-buffer (cdr b)))))) +;; (advice-add 'ivy-read :filter-args #'persp-ivy-read-advice) +;; (advice-remove 'ivy-read #'persp-ivy-read-advice) + +(defun persp--switch-buffer-ivy-counsel-helper (arg fallback) (unless (featurep 'ivy) (user-error "Ivy not loaded")) - (defvar ivy-switch-buffer-map) (declare-function ivy-read "ivy.el") - (declare-function ivy-switch-buffer "ivy.el") - (declare-function ivy--switch-buffer-matcher "ivy.el") - (declare-function ivy--switch-buffer-action "ivy.el") (if (and persp-mode (null arg)) - (apply #'ivy-read - (append - (list - (format "Switch to buffer (%s): " (persp-current-name)) - (cl-remove-if #'null (mapcar #'buffer-name - ;; buffer-list is ordered by access time - ;; seq-intersection keeps the order - (seq-intersection (buffer-list) - (persp-current-buffers)))) - :preselect (buffer-name (persp-other-buffer (current-buffer))) - :keymap ivy-switch-buffer-map - :caller #'ivy-switch-buffer - :action #'ivy--switch-buffer-action - :matcher #'ivy--switch-buffer-matcher) - ivy-params)) + (let ((real-ivy-read (symbol-function 'ivy-read))) + (cl-letf (((symbol-function 'ivy-read) + (lambda (&rest args) + (apply real-ivy-read + (append args + (list :predicate + (lambda (b) + (persp-is-current-buffer (cdr b))))))))) + (funcall fallback))) (funcall fallback))) -;; Buffer switching integration: Ivy. ;;;###autoload (defun persp-ivy-switch-buffer (arg) "A version of `ivy-switch-buffer' which respects perspectives." (interactive "P") (declare-function ivy-switch-buffer "ivy.el") - (declare-function ivy--buffer-list "ivy.el") - (let ((saved-ivy-buffer-list (symbol-function 'ivy--buffer-list)) - (temp-ivy-buffer-list (lambda (_str &optional _virtual _predicate) - (persp-current-buffer-names)))) - (unwind-protect - (progn - (when (and persp-mode (null arg)) - (setf (symbol-function 'ivy--buffer-list) temp-ivy-buffer-list)) - (persp--switch-buffer-ivy-counsel-helper arg nil #'ivy-switch-buffer)) - (setf (symbol-function 'ivy--buffer-list) saved-ivy-buffer-list)))) - -;; Buffer switching integration: Counsel. + (persp--switch-buffer-ivy-counsel-helper arg #'ivy-switch-buffer)) + ;;;###autoload (defun persp-counsel-switch-buffer (arg) "A version of `counsel-switch-buffer' which respects perspectives." @@ -1390,12 +1381,7 @@ PERSP-SET-IDO-BUFFERS)." (unless (featurep 'counsel) (user-error "Counsel not loaded")) (declare-function counsel-switch-buffer "counsel.el") - (declare-function counsel--switch-buffer-unwind "counsel.el") - (declare-function counsel--switch-buffer-update-fn "counsel.el") - (persp--switch-buffer-ivy-counsel-helper arg - (list :unwind #'counsel--switch-buffer-unwind - :update-fn #'counsel--switch-buffer-update-fn) - #'counsel-switch-buffer)) + (persp--switch-buffer-ivy-counsel-helper arg #'counsel-switch-buffer)) ;;; --- durability implementation (persp-state-save and persp-state-load) |
