]> code.delx.au - gnu-emacs-elpa/commitdiff
swiper.el (swiper-all): New command to swiper all file buffers
authorOleh Krehel <ohwoeowho@gmail.com>
Sat, 14 Nov 2015 15:27:41 +0000 (16:27 +0100)
committerOleh Krehel <ohwoeowho@gmail.com>
Sat, 14 Nov 2015 15:27:41 +0000 (16:27 +0100)
* swiper.el (swiper--candidates): Add NUMBERS-WIDTH arg.  It could be
done better by calculating the line count of each buffer and then
getting the max of that, but this way is faster, since the collections
are traversed only once.
(swiper-multi): Update.
(swiper-all): New command. This is like `swiper-multi' where the buffer
list is pre-selected to be all file visiting buffers.
(swiper--multi-candidates): New defun.
(swiper-multi-action-1): Use `swiper--multi-candidates'.
(swiper-multi-action-2): Update - the line number is in the 'display
property of the first char.

Re #234

swiper.el

index 15d54789b37e557f2a2409cbc0c90c739e908281..56247ed60b0110dac3ee2efb52cd8e38d8ab5049 100644 (file)
--- a/swiper.el
+++ b/swiper.el
 (defvar swiper-use-visual-line nil
   "When non-nil, use `line-move' instead of `forward-line'.")
 
-(defun swiper--candidates ()
-  "Return a list of this buffer lines."
+(defun swiper--candidates (&optional numbers-width)
+  "Return a list of this buffer lines.
+
+NUMBERS-WIDTH, when specified, is used for line numbers width
+spec, instead of calculating it as the log of the buffer line
+count."
   (setq swiper-use-visual-line
         (and (not (eq major-mode 'org-mode))
              visual-line-mode
              (< (buffer-size) 20000)))
   (let ((n-lines (count-lines (point-min) (point-max))))
     (unless (zerop n-lines)
-      (setq swiper--width (1+ (floor (log n-lines 10))))
+      (setq swiper--width (or numbers-width
+                              (1+ (floor (log n-lines 10)))))
       (setq swiper--format-spec
             (format "%%-%dd " swiper--width))
       (let ((line-number 0)
@@ -537,7 +542,6 @@ WND, when specified is the window."
 Run `swiper' for those buffers."
   (interactive)
   (setq swiper-multi-buffers nil)
-  (setq swiper-multi-candidates nil)
   (ivy-read (swiper-multi-prompt)
             'internal-complete-buffer
             :action 'swiper-multi-action-1)
@@ -546,6 +550,48 @@ Run `swiper' for those buffers."
             :unwind #'swiper--cleanup
             :caller 'swiper-multi))
 
+(defun swiper-all ()
+  "Run `swiper' for all opened buffers."
+  (interactive)
+  (ivy-read "Swiper: " (swiper--multi-candidates
+                        (cl-remove-if-not
+                         #'buffer-file-name
+                         (buffer-list)))
+            :action 'swiper-multi-action-2
+            :unwind #'swiper--cleanup
+            :caller 'swiper-multi))
+
+(defun swiper--multi-candidates (buffers)
+  (let* ((ww (window-width))
+         (res nil)
+         (column-2 (apply #'max
+                          (mapcar
+                           (lambda (b)
+                             (length (buffer-name b)))
+                           buffers)))
+         (column-1 (- ww 4 column-2 1)))
+    (dolist (buf buffers)
+      (with-current-buffer buf
+        (setq res
+              (append
+               (mapcar
+                (lambda (s)
+                  (setq s (concat (ivy--truncate-string s column-1) " "))
+                  (let ((len (length s)))
+                    (put-text-property
+                     (1- len) len 'display
+                     (concat
+                      (make-string
+                       (- ww (string-width s) (length (buffer-name)) 3)
+                       ?\ )
+                      (buffer-name))
+                     s)
+                    s))
+                (swiper--candidates 4))
+               res))
+        nil))
+    res))
+
 (defun swiper-multi-action-1 (x)
   (if (member x swiper-multi-buffers)
       (progn
@@ -558,31 +604,9 @@ Run `swiper' for those buffers."
   (cond ((memq this-command '(ivy-done
                               ivy-alt-done
                               ivy-immediate-done))
-         (let ((ww (window-width)))
-           (dolist (buf swiper-multi-buffers)
-             (with-current-buffer buf
-               (setq swiper-multi-candidates
-                     (append
-                      (mapcar
-                       (lambda (s)
-                         (setq s (concat s " "))
-                         (let ((len (length s)))
-                           (put-text-property
-                            (1- len) len 'display
-                            (concat
-                             (make-string
-                              (max
-                               (- ww
-                                  (string-width s)
-                                  (length (buffer-name))
-                                  1)
-                               0)
-                              ?\ )
-                             (buffer-name))
-                            s)
-                           s))
-                       (swiper--candidates))
-                      swiper-multi-candidates))))))
+         (setq swiper-multi-candidates
+               (swiper--multi-candidates
+                (mapcar #'get-buffer swiper-multi-buffers))))
         ((eq this-command 'ivy-call)
          (delete-minibuffer-contents))))
 
@@ -592,7 +616,7 @@ Run `swiper' for those buffers."
       (when (string-match "\\` *\\([^ ]+\\)\\'" buf-space)
         (switch-to-buffer (match-string 1 buf-space))
         (goto-char (point-min))
-        (forward-line (1- (read x)))
+        (forward-line (1- (read (get-text-property 0 'display x))))
         (re-search-forward
          (ivy--regex ivy-text)
          (line-end-position) t)