]> code.delx.au - gnu-emacs-elpa/commitdiff
Merge commit '294ec7f480908268055b273da96674382b84e198' from swiper
authorOleh Krehel <ohwoeowho@gmail.com>
Mon, 20 Apr 2015 17:26:54 +0000 (19:26 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Mon, 20 Apr 2015 17:26:54 +0000 (19:26 +0200)
1  2 
packages/swiper/counsel.el
packages/swiper/ivy.el

index 1b5e260702bfad59b4222fba8deef5d78338715e,03c0b296a6d0ee9d3b767a1e946c1270ee2bf9dc..03c0b296a6d0ee9d3b767a1e946c1270ee2bf9dc
  
  (defun counsel-git-grep-count (str)
    "Quickly count the amount of git grep STR matches."
-   (shell-command-to-string
-    (format "git grep -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'" str)))
+   (let ((out (shell-command-to-string
+               (format "git grep -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
+                       (ivy--regex str)))))
+     (string-to-number out)))
+ (defvar counsel--git-grep-count nil
+   "Store the line count in current repository.")
  
  (defun counsel-git-grep-function (string &optional _pred &rest _unused)
    "Grep in the current git repository for STRING."
-   (split-string
-    (shell-command-to-string
-     (format "git --no-pager grep --full-name -n --no-color -i -e \"%s\"" string))
-    "\n"
-    t))
+   (if (and (> counsel--git-grep-count 20000)
+            (< (length string) 3))
+       (progn
+         (setq ivy--full-length counsel--git-grep-count)
+         (list ""
+               (format "%d chars more" (- 3 (length ivy-text)))))
+     (let ((cmd-t "git --no-pager grep --full-name -n --no-color -i -e \"%s\"")
+           res)
+       (if (<= counsel--git-grep-count 20000)
+           (progn
+             (setq res (shell-command-to-string (format cmd-t string)))
+             (setq ivy--full-length nil))
+         (setq res (shell-command-to-string (concat (format cmd-t (ivy--regex string)) " | head -n 5000")))
+         (setq ivy--full-length (counsel-git-grep-count ivy-text)))
+       (split-string res "\n" t))))
  
  (defun counsel-git-grep ()
    "Grep for a string in the current git repository."
    (interactive)
-   (let ((default-directory (locate-dominating-file
+   (let* ((counsel--git-grep-count (counsel-git-grep-count ""))
+          (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
+                                   'counsel-git-grep-function))
+          (default-directory (locate-dominating-file
                               default-directory ".git"))
-         (val (ivy-read "pattern: " 'counsel-git-grep-function))
-         lst)
+          (val (ivy-read "pattern: " 'counsel-git-grep-function))
+          lst)
      (when val
        (setq lst (split-string val ":"))
        (find-file (car lst))
        (goto-char (point-min))
        (forward-line (1- (string-to-number (cadr lst)))))))
  
+ (defun counsel-locate-function (str &rest _u)
+   (if (< (length str) 3)
+       (list ""
+             (format "%d chars more" (- 3 (length ivy-text))))
+     (split-string
+      (shell-command-to-string (concat "locate -i -l 20 --regex " (ivy--regex str))) "\n" t)))
+ (defun counsel-locate ()
+   "Call locate."
+   (interactive)
+   (let* ((ivy--dynamic-function 'counsel-locate-function)
+          (val (ivy-read "pattern: " 'counsel-locate-function)))
+     (when val
+       (find-file val))))
  (defun counsel--generic (completion-fn)
    "Complete thing at point with COMPLETION-FN."
    (let* ((bnd (bounds-of-thing-at-point 'symbol))
diff --combined packages/swiper/ivy.el
index c4a491399888e528da59c1f56c1d2a06710b23d4,88825aa1518b375409952cbcabd145f8405d114d..131ad0d5fd3771b6880c4eca52adbbe5c4fcaac7
--- 2/ivy.el
  ;; re-building it into a regex.
  ;; So "for example" is transformed into "\\(for\\).*\\(example\\)".
  
 +(require 'cl-lib)
 +
  ;;; Code:
+ (require 'cl-lib)
  ;;* Customization
  (defgroup ivy nil
    "Incremental vertical completion."
@@@ -188,7 -188,7 +190,7 @@@ When non-nil, it should contain one %d.
             (ivy-done))
  
            ((and ivy--directory
-                 (plusp ivy--length)
+                 (cl-plusp ivy--length)
                  (file-directory-p
                   (setq dir (expand-file-name
                              ivy--current ivy--directory))))
@@@ -524,6 -524,15 +526,15 @@@ Turning on Ivy mode will set `completin
      (goto-char (minibuffer-prompt-end))
      (delete-region (line-end-position) (point-max))))
  
+ (defvar ivy--dynamic-function nil
+   "When this is non-nil, call it for each input change to get new candidates.")
+ (defvar ivy--full-length nil
+   "When `ivy--dynamic-function' is non-nil, this can be the total amount of candidates.")
+ (defvar ivy--old-text nil
+   "Store old `ivy-text' for dynamic completion.")
  (defun ivy--insert-prompt ()
    "Update the prompt according to `ivy--prompt'."
    (when ivy--prompt
             (format
              (if ivy--directory
                  (concat ivy--prompt (abbreviate-file-name ivy--directory))
-               ivy--prompt) ivy--length)))
+               ivy--prompt)
+             (or (and ivy--dynamic-function
+                      ivy--full-length)
+                 ivy--length))))
        (save-excursion
          (goto-char (point-min))
          (delete-region (point-min) (minibuffer-prompt-end))
    "Insert Ivy completions display.
  Should be run via minibuffer `post-command-hook'."
    (setq ivy-text (ivy--input))
+   (if ivy--dynamic-function
+       ;; while-no-input would cause annoying
+       ;; "Waiting for process to die...done" message interruptions
+       (progn
+         (unless (equal ivy--old-text ivy-text)
+           (let ((store ivy--dynamic-function)
+                 (ivy--dynamic-function nil))
+             (setq ivy--all-candidates (funcall store ivy-text)))
+           (setq ivy--old-text ivy-text))
+         (ivy--insert-minibuffer (ivy--format ivy--all-candidates)))
+     (when ivy--directory
+       (if (string-match "/$" ivy-text)
+           (if (member ivy-text ivy--all-candidates)
+               (ivy--cd (expand-file-name ivy-text ivy--directory))
+             (ivy--cd "/"))
+         (if (string-match "~$" ivy-text)
+             (ivy--cd (expand-file-name "~/")))))
+     (ivy--insert-minibuffer
+      (ivy--format
+       (ivy--filter ivy-text ivy--all-candidates)))))
+ (defun ivy--insert-minibuffer (text)
    (ivy--cleanup)
-   (when ivy--directory
-     (if (string-match "/$" ivy-text)
-         (if (member ivy-text ivy--all-candidates)
-             (ivy--cd (expand-file-name ivy-text ivy--directory))
-           (ivy--cd "/"))
-       (if (string-match "~$" ivy-text)
-           (ivy--cd (expand-file-name "~/")))))
-   (let ((text (ivy-completions
-                ivy-text
-                ivy--all-candidates))
-         (buffer-undo-list t)
+   (let ((buffer-undo-list t)
          deactivate-mark)
      (when ivy--update-fn
        (funcall ivy--update-fn))
    (font-lock-append-text-property 0 (length str) 'face face str)
    str)
  
- (defun ivy-completions (name candidates)
-   "Return as text the current completions.
- NAME is a string of words separated by spaces that is used to
- build a regex.
- CANDIDATES is a list of strings."
+ (defun ivy--filter (name candidates)
+   "Return the matches for NAME for CANDIDATES.
+ CANDIDATES are assumed to be static."
    (let* ((re (ivy--regex name))
           (cands (cond ((and (equal re ivy--old-re)
                              ivy--old-cands)
                            (lambda (x) (string-match re x))
                            candidates)))))
           (tail (nthcdr ivy--index ivy--old-cands))
-          (ww (window-width))
           idx)
      (when (and tail ivy--old-cands)
        (unless (and (not (equal re ivy--old-re))
            (setq idx (cl-position (pop tail) cands)))
          (setq ivy--index (or idx 0))))
      (setq ivy--old-re re)
-     (setq ivy--length (length cands))
-     (setq ivy--old-cands cands)
-     (when (>= ivy--index ivy--length)
-       (setq ivy--index (max (1- ivy--length) 0)))
-     (if (null cands)
-         ""
-       (let* ((half-height (/ ivy-height 2))
-              (start (max 0 (- ivy--index half-height)))
-              (end (min (+ start (1- ivy-height)) ivy--length))
-              (cands (cl-subseq cands start end))
-              (index (min ivy--index half-height (1- (length cands)))))
-         (when ivy--directory
-           (setq cands (mapcar (lambda (x)
-                                 (if (string-match-p "/$" x)
-                                     (propertize x 'face 'ivy-subdir)
-                                   x))
-                               cands)))
-         (setq ivy--current (copy-sequence (nth index cands)))
-         (setf (nth index cands)
-               (ivy--add-face ivy--current 'ivy-current-match))
-         (let ((res (concat "\n" (mapconcat
-                                  (lambda (s)
-                                    (if (> (length s) ww)
-                                        (concat (substring s 0 (- ww 3)) "...")
-                                      s))
-                                  cands "\n"))))
-           (put-text-property 0 (length res) 'read-only nil res)
-           res)))))
+     (setq ivy--old-cands cands)))
+ (defun ivy--format (cands)
+   "Return a string for CANDS suitable for display in the minibuffer.
+ CANDS is a list of strings."
+   (setq ivy--length (length cands))
+   (when (>= ivy--index ivy--length)
+     (setq ivy--index (max (1- ivy--length) 0)))
+   (if (null cands)
+       ""
+     (let* ((half-height (/ ivy-height 2))
+            (start (max 0 (- ivy--index half-height)))
+            (end (min (+ start (1- ivy-height)) ivy--length))
+            (cands (cl-subseq cands start end))
+            (index (min ivy--index half-height (1- (length cands)))))
+       (when ivy--directory
+         (setq cands (mapcar (lambda (x)
+                               (if (string-match-p "/$" x)
+                                   (propertize x 'face 'ivy-subdir)
+                                 x))
+                             cands)))
+       (setq ivy--current (copy-sequence (nth index cands)))
+       (setf (nth index cands)
+             (ivy--add-face ivy--current 'ivy-current-match))
+       (let* ((ww (window-width))
+              (res (concat "\n" (mapconcat
+                                 (lambda (s)
+                                   (if (> (length s) ww)
+                                       (concat (substring s 0 (- ww 3)) "...")
+                                     s))
+                                 cands "\n"))))
+         (put-text-property 0 (length res) 'read-only nil res)
+         res))))
  
  (provide 'ivy)