]> code.delx.au - gnu-emacs-elpa/blobdiff - counsel.el
ivy.el (ivy--format-minibuffer-line): Update
[gnu-emacs-elpa] / counsel.el
index a2ac55c62513a0138b39b6d1f3c801f9e18086fd..81c9c2565305731b00872aed8639b316c3d571d0 100644 (file)
   "Store the time when a new process was started.
 Or the time of the last minibuffer update.")
 
+(defvar counsel--async-start nil
+  "Store the time when a new process was started.
+Or the time of the last minibuffer update.")
+
+(defvar counsel--async-duration nil
+  "Store the time in seconds between starting a process and
+  receiving all candidates.")
+
 (defvar counsel--async-exit-code-plist nil
   "Associates exit codes with reasons.")
 
@@ -108,10 +116,13 @@ Or the time of the last minibuffer update.")
                 counsel--process
                 counsel--process
                 cmd))
-    (setq counsel--async-time (current-time))
+    (setq counsel--async-start
+          (setq counsel--async-time (current-time)))
     (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")
@@ -132,6 +143,10 @@ Or the time of the last minibuffer update.")
            (ivy--set-candidates
             (ivy--sort-maybe
              cands))
+           (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)))
@@ -1403,19 +1418,29 @@ the command."
 (defun counsel-grep-action (x)
   (with-ivy-window
     (swiper--cleanup)
-    (when (string-match "\\`\\([0-9]+\\):\\(.*\\)\\'" x)
-      (let ((file-name counsel--git-grep-dir)
-            (line-number (match-string-no-properties 1 x)))
+    (let ((default-directory (file-name-directory counsel--git-grep-dir))
+          file-name line-number)
+      (when (cond ((string-match "\\`\\([0-9]+\\):\\(.*\\)\\'" x)
+                   (setq file-name counsel--git-grep-dir)
+                   (setq line-number (match-string-no-properties 1 x)))
+                  ((string-match "\\`\\([^:]+\\):\\([0-9]+\\):\\(.*\\)\\'" x)
+                   (setq file-name (match-string-no-properties 1 x))
+                   (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)
-          (unless (eq ivy-exit 'done)
-            (isearch-range-invisible (line-beginning-position)
-                                     (line-end-position))
-            (swiper--add-overlays (ivy--regex ivy-text))))))))
+          (isearch-range-invisible (line-beginning-position)
+                                   (line-end-position))
+          (swiper--add-overlays (ivy--regex ivy-text)))))))
 
 (defun counsel-grep-occur ()
   "Generate a custom occur buffer for `counsel-grep'."
@@ -1449,6 +1474,7 @@ the command."
 (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)
@@ -1471,6 +1497,29 @@ the command."
       (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."
@@ -1872,11 +1921,42 @@ An extra action allows to switch to the process buffer."
 ;;** `counsel-expression-history'
 ;;;###autoload
 (defun counsel-expression-history ()
+  "Select an element of `read-expression-history'.
+And insert it into the minibuffer. Useful during
+`eval-expression'"
   (interactive)
   (let ((enable-recursive-minibuffers t))
     (ivy-read "Expr: " (delete-dups read-expression-history)
               :action #'insert)))
 
+;;** `counsel-esh-history'
+(defun counsel--browse-history (elements)
+  "Use Ivy to navigate through ELEMENTS."
+  (setq ivy-completion-beg (point))
+  (setq ivy-completion-end (point))
+  (ivy-read "Symbol name: "
+            (delete-dups
+             (ring-elements elements))
+            :action #'ivy-completion-in-region-action))
+
+(defvar eshell-history-ring)
+
+;;;###autoload
+(defun counsel-esh-history ()
+  "Browse Eshell history."
+  (interactive)
+  (require 'em-hist)
+  (counsel--browse-history eshell-history-ring))
+
+(defvar comint-input-ring)
+
+;;;###autoload
+(defun counsel-shell-history ()
+  "Browse shell history."
+  (interactive)
+  (require 'comint)
+  (counsel--browse-history comint-input-ring))
+
 ;;* Misc OS
 ;;** `counsel-rhythmbox'
 (defvar helm-rhythmbox-library)