branch: master
commit 3f5561bbb6aac0282fd5a37efc6cc375c1a4c226
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>

    Rewrite counsel-switch-to-shell-buffer to ivy style
    
    * counsel.el (counsel-list-buffers-with-mode): Add nreverse so that the
      buffers are in proper order.
    (counsel-switch-to-shell-buffer): Simplify. Entering a candidate not on
    the list results in a new `shell'.
    (counsel-switch-to-buffer-or-window): Update.
    
    Re #990.
---
 counsel.el | 51 +++++++++++++++++++++++----------------------------
 1 file changed, 23 insertions(+), 28 deletions(-)

diff --git a/counsel.el b/counsel.el
index b5607aa..470e8b9 100644
--- a/counsel.el
+++ b/counsel.el
@@ -3757,43 +3757,38 @@ active."
 MODE is a symbol."
   (save-current-buffer
     (let (bufs)
-      (dolist (buf (buffer-list) bufs)
+      (dolist (buf (buffer-list))
         (set-buffer buf)
         (and (equal major-mode mode)
-             (push (buffer-name buf) bufs))))))
+             (push (buffer-name buf) bufs)))
+      (nreverse bufs))))
 
 ;;;###autoload
-(defun counsel-switch-to-shell-buffer (force-create)
-  "Switch to a shell buffer, or create one.
+(defun counsel-switch-to-shell-buffer ()
+  "Switch to a shell buffer, or create one."
+  (interactive)
+  (ivy-read "Switch to shell buffer: "
+            (counsel-list-buffers-with-mode 'shell-mode)
+            :action #'counsel-switch-to-buffer-or-window
+            :caller 'counsel-switch-to-shell-buffer))
 
-List all the buffers in `shell-mode'. Is there is none or
-if FORCE-CREATE is non nil, create one; if there is exactly one,
-switch to it; otherwise, select one of them and switch to it."
-  (interactive "P")
-  (require 'pcase)
-  (counsel-switch-to-buffer-or-window
-   (pcase (counsel-list-buffers-with-mode 'shell-mode)
-     ((or (guard force-create) `())
-      (shell (read-string "Buffer name: "
-                          (generate-new-buffer-name "*shell*"))))
-     (`(,buf) buf)
-     ((and `(,_ . ,_) bufs) (ivy-read "Switch to shell buffer: " bufs)))))
-
-(defun counsel-switch-to-buffer-or-window (buffer-or-name)
-  "Display buffer BUFFER-OR-NAME and select its window.
+(defun counsel-switch-to-buffer-or-window (buffer-name)
+  "Display buffer BUFFER-NAME and select its window.
 
 This behaves as `switch-to-buffer', except when the buffer is
 already visible; in that case, select the window corresponding to
 the buffer."
-  (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
-    (let (window-of-buffer-visible)
-      (catch 'found
-        (walk-windows (lambda (window)
-                        (and (equal (window-buffer window) buffer)
-                             (throw 'found (setq window-of-buffer-visible 
window))))))
-      (if window-of-buffer-visible
-          (select-window window-of-buffer-visible)
-        (switch-to-buffer buffer)))))
+  (let ((buffer (get-buffer buffer-or-name)))
+    (if (not buffer)
+        (shell buffer-or-name)
+      (let (window-of-buffer-visible)
+        (catch 'found
+          (walk-windows (lambda (window)
+                          (and (equal (window-buffer window) buffer)
+                               (throw 'found (setq window-of-buffer-visible 
window))))))
+        (if window-of-buffer-visible
+            (select-window window-of-buffer-visible)
+          (switch-to-buffer buffer))))))
 
 ;;;###autoload
 (define-minor-mode counsel-mode

Reply via email to