(defun counsel-git-grep (&optional initial-input)
"Grep for a string in the current git repository."
(interactive)
- (unwind-protect
- (let* ((counsel--git-grep-dir (locate-dominating-file
- default-directory ".git"))
- (counsel--git-grep-count (counsel-git-grep-count ""))
- (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
- 'counsel-git-grep-function)))
- (ivy-read "pattern: " 'counsel-git-grep-function
- :initial-input initial-input
- :matcher #'counsel-git-grep-matcher
- :action
- (lambda ()
- (let ((lst (split-string ivy--current ":")))
- (find-file (expand-file-name (car lst) counsel--git-grep-dir))
- (goto-char (point-min))
- (forward-line (1- (string-to-number (cadr lst))))
- (setq swiper--window (selected-window))
- (swiper--cleanup)
- (swiper--add-overlays (ivy--regex ivy-text))))))
- (swiper--cleanup)))
+ (setq counsel--git-grep-dir
+ (locate-dominating-file default-directory ".git"))
+ (setq counsel--git-grep-count (counsel-git-grep-count ""))
+ (ivy-read "pattern: " 'counsel-git-grep-function
+ :initial-input initial-input
+ :matcher #'counsel-git-grep-matcher
+ :dynamic-collection (when (> counsel--git-grep-count 20000)
+ 'counsel-git-grep-function)
+ :action
+ (lambda ()
+ (let ((lst (split-string ivy--current ":")))
+ (find-file (expand-file-name (car lst) counsel--git-grep-dir))
+ (goto-char (point-min))
+ (forward-line (1- (string-to-number (cadr lst))))
+ (unless (eq ivy-exit 'done)
+ (setq swiper--window (selected-window))
+ (swiper--cleanup)
+ (swiper--add-overlays (ivy--regex ivy-text)))))
+ :unwind #'swiper--cleanup))
(defun counsel-git-grep-matcher (x)
(when (string-match "^[^:]+:[^:]+:" x)
action
unwind
re-builder
- matcher)
+ matcher
+ ;; When this is non-nil, call it for each input change to get new candidates
+ dynamic-collection)
(defvar ivy-last nil
"The last parameters passed to `ivy-read'.")
:action (ivy-state-action ivy-last)
:unwind (ivy-state-unwind ivy-last)
:re-builder (ivy-state-re-builder ivy-last)
- :matcher (ivy-state-matcher ivy-last)))
+ :matcher (ivy-state-matcher ivy-last)
+ :dynamic-collection (ivy-state-dynamic-collection ivy-last)))
(defun ivy-beginning-of-buffer ()
"Select the first completion candidate."
(cl-defun ivy-read (prompt collection
&key predicate require-match initial-input
history preselect keymap update-fn sort
- action unwind re-builder matcher)
+ action unwind re-builder matcher dynamic-collection)
"Read a string in the minibuffer, with completion.
PROMPT is a string to prompt with; normally it ends in a colon
RE-BUILDER is a lambda that transforms text into a regex.
-MATCHER can completely override matching."
+MATCHER can completely override matching.
+
+DYNAMIC-COLLECTION is a function to call to update the list of
+candidates with each input."
(setq ivy-last
(make-ivy-state
:prompt prompt
:window (selected-window)
:unwind unwind
:re-builder re-builder
- :matcher matcher))
+ :matcher matcher
+ :dynamic-collection dynamic-collection))
(setq ivy--directory nil)
(setq ivy--regex-function
(or re-builder
(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.")
+ "When :dynamic-collection is non-nil, this can be the total amount of candidates.")
(defvar ivy--old-text ""
"Store old `ivy-text' for dynamic completion.")
(if ivy--directory
(abbreviate-file-name ivy--directory)
""))
- (or (and ivy--dynamic-function
+ (or (and (ivy-state-dynamic-collection ivy-last)
ivy--full-length)
ivy--length))))
(save-excursion
"Insert Ivy completions display.
Should be run via minibuffer `post-command-hook'."
(setq ivy-text (ivy--input))
- (if ivy--dynamic-function
+ (if (ivy-state-dynamic-collection ivy-last)
;; while-no-input would cause annoying
;; "Waiting for process to die...done" message interruptions
(let ((inhibit-message t))
(while-no-input
(unless (equal ivy--old-text ivy-text)
- (let ((store ivy--dynamic-function)
- (ivy--dynamic-function nil))
+ (cl-letf ((store (ivy-state-dynamic-collection ivy-last))
+ ((ivy-state-dynamic-collection ivy-last) nil))
(setq ivy--all-candidates (funcall store ivy-text))))
(ivy--insert-minibuffer (ivy--format ivy--all-candidates))))
(cond (ivy--directory