]> code.delx.au - gnu-emacs-elpa/blobdiff - multishell-list.el
multishell - 1.1.5, preserve listing recency, increment multishell-version
[gnu-emacs-elpa] / multishell-list.el
index 773b799fe59df386ffa24ef7a222c341f8558265..dc7529b6ae237d6219e8425f2e6cb8dcc7faff98 100644 (file)
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2016 Free Software Foundation, Inc. and Ken Manheimer
 
 ;; Author: Ken Manheimer <ken.manheimer@gmail.com>
-;; Version: 1.1.3
+;; Version: 1.1.5
 ;; Created: 2016 -- first public availability
 ;; Keywords: processes
 ;; URL: https://github.com/kenmanheimer/EmacsMultishell
@@ -33,7 +33,7 @@ pop to the buffer but don't change its run state."
     (if arg
         (pop-to-buffer
          (multishell-bracket (multishell-name-from-entry entry)))
-      (multishell-list-selected entry t))
+      (multishell-list-dispatch-selected entry t))
     (with-current-buffer list-buffer
       (revert-buffer)
       (multishell-list-goto-item-by-entry entry))))
@@ -44,7 +44,7 @@ pop to the buffer but don't change its run state."
   (let ((list-buffer (current-buffer))
         (entry (tabulated-list-get-id)))
     (message "%s <==" (multishell-name-from-entry entry))
-    (multishell-list-selected entry t t)
+    (multishell-list-dispatch-selected entry t t)
     (with-current-buffer list-buffer
       (revert-buffer)
       (multishell-list-goto-item-by-entry entry))))
@@ -61,7 +61,7 @@ switch to the buffer but don't activate (or deactivate) it it."
     (if arg
         (switch-to-buffer
          (multishell-bracket (multishell-name-from-entry entry)))
-      (multishell-list-selected entry nil))
+      (multishell-list-dispatch-selected entry nil))
     (with-current-buffer list-buffer
       (revert-buffer))))
 
@@ -105,7 +105,7 @@ starting it if it's not already going."
         (with-current-buffer buffer
           (rename-buffer (multishell-bracket revised-name)))))
     (when arg
-      (multishell-list-selected revised-name t))
+      (multishell-list-dispatch-selected revised-name t))
     (with-current-buffer list-buffer
       (revert-buffer)
       (multishell-list-goto-item-by-entry revised))))
@@ -131,24 +131,26 @@ The already existing original entry is left untouched."
       (revert-buffer)
       (multishell-list-goto-item-by-entry new)
       (when arg
-        (multishell-list-selected new-name t)))))
+        (multishell-list-dispatch-selected new-name t)))))
 
 (defun multishell-list-mouse-select (event)
   "Select the shell whose line is clicked."
   (interactive "e")
   (select-window (posn-window (event-end event)))
   (let ((entry (tabulated-list-get-id (posn-point (event-end event)))))
-    (multishell-list-selected entry nil)))
+    (multishell-list-dispatch-selected entry nil)))
 
-(defun multishell-list-selected (entry pop &optional set-primary)
-  "Select multishell ENTRY, popping to window if POP is non-nil.
+(defun multishell-list-dispatch-selected (entry pop &optional set-primary)
+  "Go to multishell ENTRY, popping to window if POP is non-nil.
 
 Optional arg SET-PRIMARY non-nil sets `multishell-primary-name' to entry.
 
 Provide for concluding minibuffer interaction if we're in completing mode."
-  (if multishell-completing
-        (throw 'multishell-minibuffer-exit entry)
-      (multishell-pop-to-shell (and set-primary '(16)) entry (not pop))))
+  (let ((set-primary-as-arg (and set-primary '(16))))
+    (if multishell-completing-read
+        ;; In multishell completing-read, arrange to conclude minibuffer input:
+        (throw 'multishell-minibuffer-exit (list entry pop set-primary-as-arg))
+      (multishell-pop-to-shell set-primary-as-arg entry (not pop)))))
 
 (defun multishell-list-placeholder (value default)
   "Return VALUE if non-empty string, else DEFAULT."
@@ -245,6 +247,25 @@ Initial sort is from most to least recently used:
         tabulated-list-entries #'multishell-list-entries)
   (tabulated-list-init-header))
 
+(defun multishell-list-cull-dups (entries)
+  "Return list of multishell ENTRIES sans ones with duplicate names.
+
+For duplicates, we prefer the ones that have paths."
+  (let ((tally (make-hash-table :test #'equal))
+        got name name-order-reversed already)
+    (mapcar #'(lambda (entry)
+                (setq name (multishell-name-from-entry entry)
+                      already (gethash name tally nil))
+                (when (not already)
+                  (push name name-order-reversed))
+                (when (or (not already) (< (length already) (length entry)))
+                  ;; Add new or replace shorter prior entry for name:
+                  (puthash name entry tally)))
+            entries)
+    (dolist (name name-order-reversed)
+      (push (gethash name tally) got))
+    got))
+
 ;;;###autoload
 (defun multishell-list (&optional completing)
   "Edit your current and historic list of shell buffers.
@@ -276,13 +297,11 @@ You can get to the shells listing by recursively invoking
     (progv
         ;; Temporarily assign multishell-history only when completing:
         (when completing '(multishell-history))
-        (when completing (list (mapcar 'substring-no-properties completing)))
+        (when completing
+          (list (multishell-list-cull-dups (mapcar 'substring-no-properties
+                                                   completing))))
       (tabulated-list-print))
     (when completing
-      ;; XXX Do proper completion prep.
-      ;; - looks like stuff in minibuffer.el.gz is too hairy w/too little gain.
-      ;; - Add a buffer-change hook that throws a 'multishell-completion-done,
-      ;;   and revise the catch in multishell-pop-to-shell
       )
     (when from-entry
       (multishell-list-goto-item-by-entry from-entry))))