]> code.delx.au - gnu-emacs-elpa/blobdiff - counsel.el
ivy.el (ivy--sort-files-by-date): Fix due to destructive cl-sort
[gnu-emacs-elpa] / counsel.el
index ce23179b5285c27003129718602b3f7749e212be..8bfcbdcd594bad73767a6cabb0983f3428e92ccd 100644 (file)
 
 (defun counsel--find-symbol (x)
   "Find symbol definition that corresponds to string X."
-  (ring-insert find-tag-marker-ring (point-marker))
+  (with-no-warnings
+    (ring-insert find-tag-marker-ring (point-marker)))
   (let ((full-name (get-text-property 0 'full-name x)))
     (if full-name
         (find-library full-name)
       (let ((sym (read x)))
-        (cond ((boundp sym)
+        (cond ((and (eq (ivy-state-caller ivy-last)
+                        'counsel-describe-variable)
+                    (boundp sym))
                (find-variable sym))
               ((fboundp sym)
                (find-function sym))
+              ((boundp sym)
+               (find-variable sym))
               ((or (featurep sym)
                    (locate-library
                     (prin1-to-string sym)))
      :sort t
      :action (lambda (x)
                (describe-variable
-                (intern x))))))
+                (intern x)))
+     :caller 'counsel-describe-variable)))
 
 (ivy-set-actions
  'counsel-describe-variable
  '(("i" counsel-info-lookup-symbol "info")
    ("d" counsel--find-symbol "definition")))
 
+(ivy-set-actions
+ 'counsel-M-x
+ '(("d" counsel--find-symbol "definition")))
+
 ;;;###autoload
 (defun counsel-describe-function ()
   "Forward to `describe-function'."
               :sort t
               :action (lambda (x)
                         (describe-function
-                         (intern x))))))
+                         (intern x)))
+              :caller 'counsel-describe-function)))
 
 (defvar info-lookup-mode)
 (declare-function info-lookup->completions "info-look")
                   "git ls-files --full-name --")
                  "\n"
                  t))
-         (action (lambda (x) (find-file x))))
+         (action `(lambda (x)
+                    (let ((default-directory ,default-directory))
+                      (find-file x)))))
     (ivy-read "Find file: " cands
               :action action)))
 
 (defvar counsel-git-grep-map
   (let ((map (make-sparse-keymap)))
     (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
+    (define-key map (kbd "M-q") 'counsel-git-grep-query-replace)
     map))
 
+(defun counsel-git-grep-query-replace ()
+  "Start `query-replace' with string to replace from last search string."
+  (interactive)
+  (if (null (window-minibuffer-p))
+      (user-error
+       "Should only be called in the minibuffer through `counsel-git-grep-map'")
+    (let* ((enable-recursive-minibuffers t)
+           (from (ivy--regex ivy-text))
+           (to (query-replace-read-to from "Query replace" t)))
+      (ivy-set-action
+       (lambda (_)
+         (let (done-buffers)
+           (dolist (cand ivy--old-cands)
+             (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" cand)
+               (with-ivy-window
+                 (let ((file-name (match-string-no-properties 1 cand)))
+                   (setq file-name (expand-file-name file-name counsel--git-grep-dir))
+                   (unless (member file-name done-buffers)
+                     (push file-name done-buffers)
+                     (find-file file-name)
+                     (goto-char (point-min)))
+                   (perform-replace from to t t nil))))))))
+      (setq ivy-exit 'done)
+      (exit-minibuffer))))
+
 (defun counsel-git-grep-recenter ()
   (interactive)
   (with-ivy-window
@@ -425,7 +464,8 @@ INITIAL-INPUT can be given as the initial minibuffer input."
               :keymap counsel-git-grep-map
               :action #'counsel-git-grep-action
               :unwind #'swiper--cleanup
-              :history 'counsel-git-grep-history)))
+              :history 'counsel-git-grep-history
+              :caller 'counsel-git-grep)))
 
 (defcustom counsel-find-file-at-point nil
   "When non-nil, add file-at-point to the list of candidates."
@@ -499,6 +539,10 @@ Skip some dotfiles unless `ivy-text' requires them."
                  candidates))
         (setq ivy--old-re regexp))))
 
+(defvar counsel--async-time nil
+  "Store the time when a new process was started.
+Or the time of the last minibuffer update.")
+
 (defun counsel--async-command (cmd)
   (let* ((counsel--process " *counsel*")
          (proc (get-process counsel--process))
@@ -511,7 +555,9 @@ Skip some dotfiles unless `ivy-text' requires them."
                 counsel--process
                 counsel--process
                 cmd))
-    (set-process-sentinel proc #'counsel--async-sentinel)))
+    (setq counsel--async-time (current-time))
+    (set-process-sentinel proc #'counsel--async-sentinel)
+    (set-process-filter proc #'counsel--async-filter)))
 
 (defun counsel--async-sentinel (process event)
   (if (string= event "finished\n")
@@ -528,6 +574,24 @@ Skip some dotfiles unless `ivy-text' requires them."
           (setq ivy--old-cands ivy--all-candidates)
           (ivy--exhibit)))))
 
+(defun counsel--async-filter (process str)
+  "Receive from PROCESS the output STR.
+Update the minibuffer with the amount of lines collected every
+0.5 seconds since the last update."
+  (with-current-buffer (process-buffer process)
+    (insert str))
+  (let (size)
+    (when (time-less-p
+           ;; 0.5s
+           '(0 0 500000 0)
+           (time-since counsel--async-time))
+      (with-current-buffer (process-buffer process)
+        (goto-char (point-min))
+        (setq size (- (buffer-size) (forward-line (buffer-size)))))
+      (ivy--insert-minibuffer
+       (format "\ncollected: %d" size))
+      (setq counsel--async-time (current-time)))))
+
 (defun counsel-locate-action-extern (x)
   "Use xdg-open shell command on X."
   (call-process shell-file-name nil
@@ -589,8 +653,9 @@ Skip some dotfiles unless `ivy-text' requires them."
             :dynamic-collection t
             :history 'counsel-locate-history
             :action (lambda (file)
-                      (when file
-                        (find-file file)))
+                      (with-ivy-window
+                        (when file
+                          (find-file file))))
             :unwind #'counsel-delete-process))
 
 (defun counsel--generic (completion-fn)
@@ -716,8 +781,17 @@ The libraries are offered from `load-path'."
   "Quickly and asynchronously count the amount of git grep REGEX matches.
 When NO-ASYNC is non-nil, do it synchronously."
   (let ((default-directory counsel--git-grep-dir)
-        (cmd (format "git grep -i -c '%s' | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"
-                     (replace-regexp-in-string "'" "''" regex)))
+        (cmd
+         (concat
+          (format
+           (replace-regexp-in-string
+            "--full-name" "-c"
+            counsel-git-grep-cmd)
+           ;; "git grep -i -c '%s'"
+           (replace-regexp-in-string
+            "-" "\\\\-"
+            (replace-regexp-in-string "'" "''" regex)))
+          " | sed 's/.*:\\(.*\\)/\\1/g' | awk '{s+=$1} END {print s}'"))
         (counsel-ggc-process " *counsel-gg-count*"))
     (if no-async
         (string-to-number (shell-command-to-string cmd))
@@ -757,6 +831,20 @@ CMD is a command name."
 (declare-function smex-update "ext:smex")
 (declare-function smex-rank "ext:smex")
 
+(defun counsel--M-x-prompt ()
+  "M-x plus the string representation of `current-prefix-arg'."
+  (if (not current-prefix-arg)
+      "M-x "
+    (concat
+     (if (eq current-prefix-arg '-)
+         "- "
+       (if (integerp current-prefix-arg)
+           (format "%d " current-prefix-arg)
+         (if (= (car current-prefix-arg) 4)
+             "C-u "
+           (format "%d " (car current-prefix-arg)))))
+     "M-x ")))
+
 ;;;###autoload
 (defun counsel-M-x (&optional initial-input)
   "Ivy version of `execute-extended-command'.
@@ -783,7 +871,7 @@ Optional INITIAL-INPUT is the initial input in the minibuffer."
       (setq cands smex-ido-cache)
       (setq pred nil)
       (setq sort nil))
-    (ivy-read "M-x " cands
+    (ivy-read (counsel--M-x-prompt) cands
               :predicate pred
               :require-match t
               :history 'extended-command-history
@@ -853,7 +941,8 @@ Usable with `ivy-resume', `ivy-next-line-and-call' and
             :action
             '(1
               ("p" helm-rhythmbox-play-song "Play song")
-              ("e" counsel-rhythmbox-enqueue-song "Enqueue song"))))
+              ("e" counsel-rhythmbox-enqueue-song "Enqueue song"))
+            :caller 'counsel-rhythmbox))
 
 (defvar counsel-org-tags nil
   "Store the current list of tags.")
@@ -1012,18 +1101,19 @@ Usable with `ivy-resume', `ivy-next-line-and-call' and
   "Grep in the current directory for STRING."
   (if (< (length string) 3)
       (counsel-more-chars 3)
-    (let ((regex (counsel-unquote-regex-parens
+    (let ((default-directory counsel--git-grep-dir)
+          (regex (counsel-unquote-regex-parens
                   (setq ivy--old-re
                         (ivy--regex string)))))
       (counsel--async-command
        (format "ag --noheading --nocolor %S" regex))
       nil)))
 
-(defun counsel-ag (&optional initial-input)
+(defun counsel-ag (&optional initial-input initial-directory)
   "Grep for a string in the current directory using ag.
 INITIAL-INPUT can be given as the initial minibuffer input."
   (interactive)
-  (setq counsel--git-grep-dir default-directory)
+  (setq counsel--git-grep-dir (or initial-directory default-directory))
   (ivy-read "ag: " 'counsel-ag-function
             :initial-input initial-input
             :dynamic-collection t