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

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

[elpa] master 478e4e4 32/54: Make line numbers in swiper into display pr


From: Oleh Krehel
Subject: [elpa] master 478e4e4 32/54: Make line numbers in swiper into display properties
Date: Tue, 29 Sep 2015 14:09:58 +0000

branch: master
commit 478e4e4fe29abf11fd3ccbf9a8c10c9a1cb34dfc
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Make line numbers in swiper into display properties
    
    * swiper.el (swiper--candidates): Each candidate is now a single space
      plus the original string. The display property of the single space
      holds the line number. This means that it's no longer possible to
      match line numbers in queries. Also, the preselect of the current line
      is slightly worse (in case there are two identical lines in a buffer).
    (swiper--ivy): Update the preselect to not include the line
    number. Also, call `ivy--action' on `ivy-current' instead of `res',
    because `res' doesn't have the string property that points to the line
    number.
    (swiper--update-input-ivy): Update.
    (swiper--action): Update.
    
    Fixes #224
---
 swiper.el |   70 ++++++++++++++++++++++++++++++------------------------------
 1 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/swiper.el b/swiper.el
index ac1b0cc..258b530 100644
--- a/swiper.el
+++ b/swiper.el
@@ -191,19 +191,21 @@
     (unless (zerop n-lines)
       (setq swiper--width (1+ (floor (log n-lines 10))))
       (setq swiper--format-spec
-            (format "%%-%dd %%s" swiper--width))
+            (format "%%-%dd " swiper--width))
       (let ((line-number 0)
             candidates)
         (save-excursion
           (goto-char (point-min))
           (swiper-font-lock-ensure)
           (while (< (point) (point-max))
-            (push (format swiper--format-spec
-                          (cl-incf line-number)
-                          (buffer-substring
-                           (line-beginning-position)
-                           (line-end-position)))
-                  candidates)
+            (let ((str (concat " " (buffer-substring
+                                    (line-beginning-position)
+                                    (line-end-position)))))
+              (put-text-property 0 1 'display
+                                 (format swiper--format-spec
+                                         (cl-incf line-number))
+                                 str)
+              (push str candidates))
             (forward-line 1))
           (nreverse candidates))))))
 
@@ -262,12 +264,9 @@ When non-nil, INITIAL-INPUT is the initial search pattern."
 Please remove it and update the \"swiper\" package."))
   (swiper--init)
   (let ((candidates (swiper--candidates))
-        (preselect (format
-                    swiper--format-spec
-                    (line-number-at-pos)
-                    (buffer-substring-no-properties
-                     (line-beginning-position)
-                     (line-end-position))))
+        (preselect (buffer-substring-no-properties
+                    (line-beginning-position)
+                    (line-end-position)))
         res)
     (unwind-protect
          (setq res (ivy-read
@@ -283,7 +282,7 @@ Please remove it and update the \"swiper\" package."))
                     :history 'swiper-history))
       (if (null ivy-exit)
           (goto-char swiper--opoint)
-        (swiper--action res ivy-text)))))
+        (swiper--action ivy--current ivy-text)))))
 
 (defun swiper--ensure-visible ()
   "Remove overlays hiding point."
@@ -309,27 +308,28 @@ Please remove it and update the \"swiper\" package."))
   "Called when `ivy' input is updated."
   (with-ivy-window
     (swiper--cleanup)
-    (let* ((re (funcall ivy--regex-function ivy-text))
-           (re (if (stringp re) re (caar re)))
-           (str ivy--current)
-           (num (if (string-match "^[0-9]+" str)
-                    (string-to-number (match-string 0 str))
-                  0)))
-      (goto-char (point-min))
-      (when (cl-plusp num)
+    (when (> (length ivy--current) 0)
+      (let* ((re (funcall ivy--regex-function ivy-text))
+             (re (if (stringp re) re (caar re)))
+             (str (get-text-property 0 'display ivy--current))
+             (num (if (string-match "^[0-9]+" str)
+                      (string-to-number (match-string 0 str))
+                    0)))
         (goto-char (point-min))
-        (forward-line (1- num))
-        (if (and (equal ivy-text "")
-                 (>= swiper--opoint (line-beginning-position))
-                 (<= swiper--opoint (line-end-position)))
-            (goto-char swiper--opoint)
-          (re-search-forward re (line-end-position) t))
-        (isearch-range-invisible (line-beginning-position)
-                                 (line-end-position))
-        (unless (and (>= (point) (window-start))
-                     (<= (point) (window-end (ivy-state-window ivy-last) t)))
-          (recenter)))
-      (swiper--add-overlays re))))
+        (when (cl-plusp num)
+          (goto-char (point-min))
+          (forward-line (1- num))
+          (if (and (equal ivy-text "")
+                   (>= swiper--opoint (line-beginning-position))
+                   (<= swiper--opoint (line-end-position)))
+              (goto-char swiper--opoint)
+            (re-search-forward re (line-end-position) t))
+          (isearch-range-invisible (line-beginning-position)
+                                   (line-end-position))
+          (unless (and (>= (point) (window-start))
+                       (<= (point) (window-end (ivy-state-window ivy-last) t)))
+            (recenter)))
+        (swiper--add-overlays re)))))
 
 (defun swiper--add-overlays (re &optional beg end)
   "Add overlays for RE regexp in visible part of the current buffer.
@@ -377,7 +377,7 @@ BEG and END, when specified, are the point bounds."
   (if (null x)
       (user-error "No candidates")
     (goto-char (point-min))
-    (forward-line (1- (read x)))
+    (forward-line (1- (read (get-text-property 0 'display x))))
     (re-search-forward
      (ivy--regex input) (line-end-position) t)
     (swiper--ensure-visible)



reply via email to

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