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

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

[elpa] master 0d2ab52 157/399: swiper.el (swiper--isearch-occur-cands):


From: Oleh Krehel
Subject: [elpa] master 0d2ab52 157/399: swiper.el (swiper--isearch-occur-cands): Faster version
Date: Sat, 20 Jul 2019 14:57:14 -0400 (EDT)

branch: master
commit 0d2ab52f8b6ba684ea7c26fa89f97d01813b8bb7
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    swiper.el (swiper--isearch-occur-cands): Faster version
    
    Don't use `line-number-at-pos', since it's O(N*2) in terms of the
    number of candidates. Instead, use `count-lines' between each
    consecutive candidate which is O(N).
---
 swiper.el | 51 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/swiper.el b/swiper.el
index d688907..bb41cf5 100644
--- a/swiper.el
+++ b/swiper.el
@@ -480,26 +480,37 @@ such as `scroll-conservatively' are set to a high value.")
     (nreverse res)))
 
 (defun swiper--occur-cands (fname cands)
-  (with-current-buffer (ivy-state-buffer ivy-last)
-    (let* ((pt-min (point-min))
-           (line-delta
-            (save-restriction
-              (widen)
-              (1- (line-number-at-pos pt-min)))))
-      (mapcar
-       (lambda (s)
-         (let* ((n (get-text-property 0 'swiper-line-number s))
-                (pt (get-text-property 0 'point s))
-                (nn (number-to-string
-                     (if n
-                         (progn
-                           (setq s (substring s 1))
-                           (+ (read n) line-delta))
-                       (line-number-at-pos pt)))))
-           (put-text-property 0 1 'point pt fname)
-           (put-text-property 0 (length nn) 'face 'ivy-grep-line-number nn)
-           (format "%s:%s:%s" fname nn s)))
-       cands))))
+  (when cands
+    (with-current-buffer (ivy-state-buffer ivy-last)
+      (if (eq (ivy-state-caller ivy-last) 'swiper-isearch)
+          (swiper--isearch-occur-cands fname cands)
+        (let* ((pt-min (point-min))
+               (line-delta
+                (save-restriction
+                  (widen)
+                  (1- (line-number-at-pos pt-min)))))
+          (mapcar
+           (lambda (s)
+             (let* ((n (get-text-property 0 'swiper-line-number s))
+                    (nn (number-to-string
+                         (+ (read n) line-delta))))
+               (put-text-property 0 (length nn) 'face 'ivy-grep-line-number nn)
+               (format "%s:%s:%s" fname nn (substring s 1))))
+           cands))))))
+
+(defun swiper--isearch-occur-cands (fname cands)
+  (let* ((last-pt (get-text-property 0 'point (car cands)))
+         (line (1+ (line-number-at-pos last-pt)))
+         res pt nn)
+    (dolist (cand cands)
+      (setq pt (get-text-property 0 'point cand))
+      (cl-incf line (1- (count-lines last-pt pt)))
+      (setq nn (number-to-string line))
+      (put-text-property 0 (length nn) 'face 'ivy-grep-line-number nn)
+      (put-text-property 0 1 'point pt fname)
+      (push (format "%s:%s:%s" fname nn cand) res)
+      (setq last-pt pt))
+    (nreverse res)))
 
 (defun swiper-occur (&optional revert)
   "Generate a custom occur buffer for `swiper'.



reply via email to

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