branch: master
commit 8ab84007f68ab74ba41c23d2bdb413834dcd8a33
Author: James Nguyen <[email protected]>
Commit: Oleh Krehel <[email protected]>

    Add new command counsel-fzf
    
    Fixes #1151
---
 counsel.el | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/counsel.el b/counsel.el
index 639b6e0..ad51d55 100644
--- a/counsel.el
+++ b/counsel.el
@@ -1899,6 +1899,50 @@ INITIAL-INPUT can be given as the initial minibuffer 
input."
             :unwind #'counsel-delete-process
             :caller 'counsel-locate))
 
+;;** `counsel-fzf'
+(defvar counsel-fzf-cmd "fzf -f %s"
+  "Command for `counsel-fzf'.")
+
+(defvar counsel--fzf-dir nil
+  "Store the base fzf directory.")
+
+(defun counsel-fzf-function (str)
+  (let ((default-directory counsel--fzf-dir))
+    (counsel--async-command
+     (format counsel-fzf-cmd
+             (if (string-equal str "")
+                 "\"\""
+               (counsel-unquote-regex-parens
+                (ivy--regex str))))))
+  nil)
+
+;;;###autoload
+(defun counsel-fzf (&optional initial-input)
+  "Call the \"fzf\" shell command.
+INITIAL-INPUT can be given as the initial minibuffer input."
+  (interactive)
+  (counsel-require-program (car (split-string counsel-fzf-cmd)))
+  (setq counsel--fzf-dir (if (and
+                              (fboundp 'projectile-project-p)
+                              (fboundp 'projectile-project-root)
+                              (projectile-project-p))
+                             (projectile-project-root)
+                           default-directory))
+  (ivy-read "> " #'counsel-fzf-function
+            :initial-input initial-input
+            :dynamic-collection t
+            :action #'counsel-fzf-action
+            :unwind #'counsel-delete-process
+            :caller 'counsel-fzf))
+
+(defun counsel-fzf-action (x)
+  "Find file X in current fzf directory."
+  (with-ivy-window
+    (let ((default-directory counsel--fzf-dir))
+      (find-file x))))
+
+(counsel-set-async-exit-code 'counsel-fzf 1 "Nothing found")
+
 ;;** `counsel-dpkg'
 ;;;###autoload
 (defun counsel-dpkg ()

Reply via email to