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

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

[elpa] master 56a1f7d 386/399: Add counsel-grep-backward and counsel-gre


From: Oleh Krehel
Subject: [elpa] master 56a1f7d 386/399: Add counsel-grep-backward and counsel-grep-or-swiper-backward commands
Date: Sat, 20 Jul 2019 14:58:05 -0400 (EDT)

branch: master
commit 56a1f7d4c369f9d9b6f88e84aa5a55d842d3a165
Author: Joel Rosdahl <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Add counsel-grep-backward and counsel-grep-or-swiper-backward commands
    
    Fixes #2132
---
 counsel.el | 21 +++++++++++++++++++++
 ivy.el     | 13 +++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/counsel.el b/counsel.el
index 16a08b4..feb3669 100644
--- a/counsel.el
+++ b/counsel.el
@@ -2978,6 +2978,16 @@ When non-nil, INITIAL-INPUT is the initial search 
pattern."
       (unless res
         (goto-char init-point)))))
 
+;;;###autoload
+(defun counsel-grep-backward (&optional initial-input)
+  "Grep for a string in the file visited by the current buffer going
+backward similar to `swiper-backward'. When non-nil, INITIAL-INPUT is
+the initial search pattern."
+  (interactive)
+  (let ((ivy-index-functions-alist
+         '((counsel-grep . ivy-recompute-index-swiper-async-backward))))
+    (counsel-grep initial-input)))
+
 ;;** `counsel-grep-or-swiper'
 (defcustom counsel-grep-swiper-limit 300000
   "Buffer size threshold for `counsel-grep-or-swiper'.
@@ -3014,6 +3024,17 @@ When non-nil, INITIAL-INPUT is the initial search 
pattern."
       (save-buffer))
     (counsel-grep initial-input)))
 
+;;** `counsel-grep-or-swiper-backward'
+;;;###autoload
+(defun counsel-grep-or-swiper-backward (&optional initial-input)
+  "Call `swiper-backward' for small buffers and `counsel-grep-backward' for
+large ones.  When non-nil, INITIAL-INPUT is the initial search pattern."
+  (interactive)
+  (let ((ivy-index-functions-alist
+         '((swiper . ivy-recompute-index-swiper-backward)
+           (counsel-grep . ivy-recompute-index-swiper-async-backward))))
+    (counsel-grep-or-swiper initial-input)))
+
 ;;** `counsel-recoll'
 (defun counsel-recoll-function (str)
   "Run recoll for STR."
diff --git a/ivy.el b/ivy.el
index 22ff77c..21c317c 100644
--- a/ivy.el
+++ b/ivy.el
@@ -3222,6 +3222,7 @@ CANDIDATES are assumed to be static."
                              ivy-index-functions-alist))
                   '(ivy-recompute-index-swiper
                     ivy-recompute-index-swiper-async
+                    ivy-recompute-index-swiper-async-backward
                     ivy-recompute-index-swiper-backward))
             (progn
               (ivy--recompute-index name re-str cands)
@@ -3479,6 +3480,18 @@ CANDS are the current candidates."
             (or idx 0))
         ivy--index))))
 
+(defun ivy-recompute-index-swiper-async-backward (re-str cands)
+  "Recompute index of selected candidate when using `swiper-backward'
+asynchronously. CANDS are the current candidates."
+  (if (= (length cands) 0)
+      0
+    (let ((idx (ivy-recompute-index-swiper-async re-str cands)))
+      (if
+          (<= (string-to-number (nth idx cands))
+              (with-ivy-window (line-number-at-pos)))
+          idx
+        (- idx 1)))))
+
 (defun ivy-recompute-index-zero (_re-str _cands)
   "Recompute index of selected candidate.
 This function serves as a fallback when nothing else is available."



reply via email to

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