branch: master
commit 36e331248480faf5a7c323696fb975817b1bbfe8
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>

    counsel.el (counsel-fzf-occur): Add
    
    * counsel.el (counsel-cmd-to-dired): Rework for the sake of fzf
    
    fzf can't work without /dev/tty, so `shell-command-to-string' or even
    `shell-command' won't do.
    
    Use `start-process-shell-command'. Since we don't know when the process will
    finish, add a sentinel which will move the point to the first file found.
---
 counsel.el | 48 ++++++++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/counsel.el b/counsel.el
index baf51e9..285c767 100644
--- a/counsel.el
+++ b/counsel.el
@@ -1063,30 +1063,32 @@ INITIAL-INPUT can be given as the initial minibuffer 
input."
   "Occur function for `counsel-git' using `counsel-cmd-to-dired'."
   (cd counsel--git-dir)
   (counsel-cmd-to-dired
-   (format "%s | grep -i -E '%s' | xargs ls -alh"
+   (format "%s | grep -i -E '%s' | xargs ls -alh | sed -e 's/^/  /'"
            counsel-git-cmd ivy--old-re)))
 
 (defun counsel-cmd-to-dired (cmd)
   "Adapted from `find-dired'."
   (let ((inhibit-read-only t))
     (erase-buffer)
-    (let ((lines (split-string
-                  (shell-command-to-string cmd)
-                  "\n")))
-      (dired-mode default-directory "-alh")
-      (dolist (l lines)
-        (insert "  " l "\n"))
-      (goto-char (point-min))
-      (setq-local dired-sort-inhibit t)
-      (setq-local revert-buffer-function
-                  (lambda (_1 _2) (counsel-cmd-to-dired cmd)))
-      (setq-local dired-subdir-alist
-                  (list (cons default-directory (point-min-marker))))
-      (setq-local dired-subdir-switches "-alh")
-      (insert "  " default-directory ":\n")
-      (let ((point (point)))
-        (insert "  " cmd "\n")
-        (dired-insert-set-properties point (point))))))
+    (dired-mode default-directory "-alh")
+    (insert "  " default-directory ":\n")
+    (let ((point (point)))
+      (insert "  " cmd "\n")
+      (dired-insert-set-properties point (point)))
+    (setq-local dired-sort-inhibit t)
+    (setq-local revert-buffer-function
+                (lambda (_1 _2) (counsel-cmd-to-dired cmd)))
+    (setq-local dired-subdir-alist
+                (list (cons default-directory (point-min-marker))))
+    (setq-local dired-subdir-switches "-alh")
+    (let ((proc (start-process-shell-command "counsel-cmd" (current-buffer) 
cmd)))
+      (set-process-sentinel
+       proc
+       (lambda (_ state)
+         (when (equal state "finished\n")
+           (goto-char (point-min))
+           (forward-line 2)
+           (dired-move-to-filename)))))))
 
 (ivy-set-occur 'counsel-git 'counsel-git-occur)
 
@@ -1982,6 +1984,16 @@ INITIAL-INPUT can be given as the initial minibuffer 
input."
     (let ((default-directory counsel--fzf-dir))
       (find-file x))))
 
+(defun counsel-fzf-occur ()
+  "Occur function for `counsel-fzf' using `counsel-cmd-to-dired'."
+  (cd counsel--fzf-dir)
+  (counsel-cmd-to-dired
+   (format
+    "%s --print0 | xargs -0 ls -alh | sed -e 's/^/  /'"
+    (format counsel-fzf-cmd ivy-text))))
+
+(ivy-set-occur 'counsel-fzf 'counsel-fzf-occur)
+
 (ivy-set-actions
  'counsel-fzf
  '(("x" counsel-locate-action-extern "xdg-open")

Reply via email to