emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/ivy-hydra 69fedaa 098/395: counsel.el (counsel-esh-hist


From: Basil L. Contovounesios
Subject: [elpa] externals/ivy-hydra 69fedaa 098/395: counsel.el (counsel-esh-history, counsel-shell-history): Set indices
Date: Thu, 25 Feb 2021 08:31:40 -0500 (EST)

branch: externals/ivy-hydra
commit 69fedaa67b6df93db899184d33cb225148b6c7b8
Author: Brian Leung <bkleung89@gmail.com>
Commit: Oleh Krehel <ohwoeowho@gmail.com>

    counsel.el (counsel-esh-history, counsel-shell-history): Set indices
    
    ivy.el (ivy-completion-in-region-action): Handle history pairs.
    ivy.el (ivy-reverse-i-search): Update action to handle different type.
    counsel.el (counsel-esh--index-last,
    counsel-shell-history--index-last,
    counsel-slime-repl-history--index-last): New variables.
    counsel.el (counsel-set-eshell-history-index,
    counsel-set-comint-history-index): New functions.
    
    Fixes #2234
---
 counsel.el | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 ivy.el     | 21 +++++++++++++++----
 2 files changed, 80 insertions(+), 11 deletions(-)

diff --git a/counsel.el b/counsel.el
index fa1afd4..76be2f8 100644
--- a/counsel.el
+++ b/counsel.el
@@ -4459,11 +4459,34 @@ An extra action allows to switch to the process buffer."
   (let ((enable-recursive-minibuffers t))
     (ivy-read "History: " (ivy-history-contents minibuffer-history-variable)
               :keymap ivy-reverse-i-search-map
-              :action #'insert
+              :action (lambda (x)
+                        (insert (substring-no-properties (car x))))
               :caller 'counsel-minibuffer-history)))
 
 ;;** `counsel-esh-history'
-(defun counsel--browse-history (ring)
+(defvar comint-input-ring-index)
+(defvar eshell-history-index)
+(defvar slime-repl-input-history-position)
+
+(defvar counsel-esh--index-last)
+(defvar counsel-shell-history--index-last)
+(defvar counsel-slime-repl-history--index-last)
+
+(defun counsel--browse-history-action (pair)
+  (let ((snd (cdr pair)))
+    (cl-case (ivy-state-caller ivy-last)
+      (counsel-esh-history
+       (setq eshell-history-index snd
+             counsel-esh--index-last snd))
+      (counsel-shell-history
+       (setq comint-input-ring-index snd
+             counsel-shell-history--index-last snd))
+      (counsel-slime-repl-history
+       (setq slime-repl-input-history-position snd
+             counsel-slime-repl-history--index-last snd)))
+    (ivy-completion-in-region-action (car pair))))
+
+(defun counsel--browse-history (ring &key caller)
   "Use Ivy to navigate through RING."
   (let* ((proc (get-buffer-process (current-buffer)))
          (end (point))
@@ -4477,35 +4500,68 @@ An extra action allows to switch to the process buffer."
     (ivy-read "History: " (ivy-history-contents ring)
               :keymap ivy-reverse-i-search-map
               :initial-input input
-              :action #'ivy-completion-in-region-action
-              :caller 'counsel-shell-history)))
+              :action #'counsel--browse-history-action
+              :caller caller)))
 
 (defvar eshell-history-ring)
+(defvar eshell-matching-input-from-input-string)
+
+(defvar counsel-esh--index-last nil
+  "Index corresponding to last selection with `counsel-esh-history'.")
 
 ;;;###autoload
 (defun counsel-esh-history ()
   "Browse Eshell history."
   (interactive)
   (require 'em-hist)
-  (counsel--browse-history eshell-history-ring))
+  (counsel--browse-history eshell-history-ring
+                           :caller #'counsel-esh-history))
+
+(defadvice eshell-previous-matching-input (before
+                                           counsel-set-eshell-history-index
+                                           activate)
+  "Reassign `eshell-history-index'."
+  (when (and (memq last-command '(ivy-alt-done ivy-done))
+             (equal (ivy-state-caller ivy-last) 'counsel-esh-history))
+    (setq eshell-history-index counsel-esh--index-last)))
 
 (defvar comint-input-ring)
+(defvar comint-matching-input-from-input-string)
+
+(defvar counsel-shell-history--index-last nil
+  "Index corresponding to last selection with `counsel-shell-history'.")
 
 ;;;###autoload
 (defun counsel-shell-history ()
   "Browse shell history."
   (interactive)
   (require 'comint)
-  (counsel--browse-history comint-input-ring))
+  (counsel--browse-history comint-input-ring
+                           :caller #'counsel-shell-history))
+
+(defadvice comint-previous-matching-input (before
+                                           counsel-set-comint-history-index
+                                           activate)
+  "Reassign `comint-input-ring-index'."
+  (when (and (memq last-command '(ivy-alt-done ivy-done))
+             (equal (ivy-state-caller ivy-last) 'counsel-shell-history))
+    (setq comint-input-ring-index counsel-shell-history--index-last)))
 
 (defvar slime-repl-input-history)
 
+(defvar counsel-slime-repl-history--index-last nil
+  "Index corresponding to last selection with `counsel-slime-repl-history'.")
+
 ;;;###autoload
 (defun counsel-slime-repl-history ()
   "Browse Slime REPL history."
   (interactive)
   (require 'slime-repl)
-  (counsel--browse-history slime-repl-input-history))
+  (counsel--browse-history slime-repl-input-history
+                           :caller #'counsel-slime-repl-history))
+
+;; TODO: add advice for slime-repl-input-previous/next to properly
+;; reassign the ring index and match string
 
 ;;** `counsel-hydra-heads'
 (defvar hydra-curr-body-fn)
diff --git a/ivy.el b/ivy.el
index fdabb01..88ad8ed 100644
--- a/ivy.el
+++ b/ivy.el
@@ -4644,6 +4644,19 @@ This list can be rotated with 
`ivy-rotate-preferred-builders'."
       (setq ivy--regex-function 'ivy--regex-plus)
     (setq ivy--regex-function 'ivy--regex-fuzzy)))
 
+(defun ivy--label-and-delete-dups (entries)
+  "Label ENTRIES with history indices."
+  (let ((ht (make-hash-table :test 'equal))
+        (idx 0)
+        entry
+        accum)
+    (while (setq entry (pop entries))
+      (unless (gethash entry ht)
+        (puthash entry t ht)
+        (push `(,entry . ,idx) accum))
+      (cl-incf idx))
+    (nreverse accum)))
+
 (defvar ivy--reverse-i-search-symbol nil
   "Store the history symbol.")
 
@@ -4673,14 +4686,14 @@ A copy is necessary so that we don't clobber any string 
attributes.
 Also set `ivy--reverse-i-search-symbol' to HISTORY."
   (setq ivy--reverse-i-search-symbol history)
   (cond ((symbolp history)
-         (delete-dups
+         (ivy--label-and-delete-dups
           (copy-sequence (symbol-value history))))
         ((ring-p history)
-         (delete-dups
+         (ivy--label-and-delete-dups
           (when (> (ring-size history) 0)
             (ring-elements history))))
         ((sequencep history)
-         (delete-dups
+         (ivy--label-and-delete-dups
           (copy-sequence history)))
         (t
          (error "Expected a symbol, ring, or sequence: %S" history))))
@@ -4708,7 +4721,7 @@ You can also delete an element from history with 
\\[ivy-reverse-i-search-kill]."
                            (ivy--reset-state
                             (setq ivy-last old-last))
                            (delete-minibuffer-contents)
-                           (insert (substring-no-properties x))
+                           (insert (substring-no-properties (car x)))
                            (ivy--cd-maybe))
                  :caller 'ivy-reverse-i-search)))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]