(set-process-sentinel proc (or process-sentinel #'counsel--async-sentinel))
(set-process-filter proc (or process-filter #'counsel--async-filter))))
+(defvar counsel-grep-last-line nil)
+
(defun counsel--async-sentinel (process event)
(let ((cands
(cond ((string= event "finished\n")
(ivy--set-candidates
(ivy--sort-maybe
cands))
- (setq counsel--async-duration
- (time-to-seconds (time-since counsel--async-start)))
+ (setq counsel-grep-last-line nil)
+ (when counsel--async-start
+ (setq counsel--async-duration
+ (time-to-seconds (time-since counsel--async-start))))
(let ((re (funcall ivy--regex-function ivy-text)))
(unless (stringp re)
(setq re (caar re)))
(setq line-number (match-string-no-properties 2 x)))
(t nil))
(find-file file-name)
- (goto-char (point-min))
- (forward-line (1- (string-to-number line-number)))
+ (setq line-number (string-to-number line-number))
+ (if (null counsel-grep-last-line)
+ (progn
+ (goto-char (point-min))
+ (forward-line (1- (setq counsel-grep-last-line line-number))))
+ (forward-line (- line-number counsel-grep-last-line))
+ (setq counsel-grep-last-line line-number))
(re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
(if (eq ivy-exit 'done)
(swiper--ensure-visible)
(defun counsel-grep ()
"Grep for a string in the current file."
(interactive)
+ (setq counsel-grep-last-line nil)
(setq counsel--git-grep-dir (buffer-file-name))
(let ((init-point (point))
res)
(unless res
(goto-char init-point)))))
+;;** `counsel-grep-or-swiper'
+(defcustom counsel-grep-swiper-limit 300000
+ "When the buffer is larger than this, use `counsel-grep' instead of `swiper'."
+ :type 'integer
+ :group 'ivy)
+
+;;;###autoload
+(defun counsel-grep-or-swiper ()
+ "Call `swiper' for small buffers and `counsel-grep' for large ones."
+ (interactive)
+ (if (and (buffer-file-name)
+ (not (buffer-narrowed-p))
+ (not (ignore-errors
+ (file-remote-p (buffer-file-name))))
+ (> (buffer-size)
+ (if (eq major-mode 'org-mode)
+ (/ counsel-grep-swiper-limit 4)
+ counsel-grep-swiper-limit)))
+ (progn
+ (save-buffer)
+ (counsel-grep))
+ (swiper--ivy (swiper--candidates))))
+
;;** `counsel-recoll'
(defun counsel-recoll-function (string)
"Grep in the current directory for STRING."