From e99731ea852d1faac5390594cd980708edf826cf Mon Sep 17 00:00:00 2001
From: Junegunn Choi <junegunn.c@gmail.com>
Date: Sun, 8 Nov 2015 00:11:46 +0900
Subject: [PATCH] [shell] Add FZF_ALT_C_COMMAND for ALT-C (#408)

---
 shell/key-bindings.bash |  9 +++++----
 shell/key-bindings.fish |  6 ++++--
 shell/key-bindings.zsh  |  7 ++++---
 test/test_go.rb         | 16 ++++++++++++++++
 4 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash
index 0bfd50d6..e9bea78d 100644
--- a/shell/key-bindings.bash
+++ b/shell/key-bindings.bash
@@ -1,7 +1,7 @@
 # Key bindings
 # ------------
 __fzf_select__() {
-  local cmd="${FZF_CTRL_T_COMMAND:-"command \\find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
+  local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
     -o -type f -print \
     -o -type d -print \
     -o -type l -print 2> /dev/null | sed 1d | cut -b3-"}"
@@ -29,9 +29,10 @@ __fzf_select_tmux__() {
 }
 
 __fzf_cd__() {
-  local dir
-  dir=$(command \find -L ${1:-.} \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
-    -o -type d -print 2> /dev/null | sed 1d | cut -b3- | $(__fzfcmd) +m) && printf 'cd %q' "$dir"
+  local cmd dir
+  cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
+    -o -type d -print 2> /dev/null | sed 1d | cut -b3-"}"
+  dir=$(eval "$cmd" | $(__fzfcmd) +m) && printf 'cd %q' "$dir"
 }
 
 __fzf_history__() (
diff --git a/shell/key-bindings.fish b/shell/key-bindings.fish
index a08df3df..3d3c2a72 100644
--- a/shell/key-bindings.fish
+++ b/shell/key-bindings.fish
@@ -33,9 +33,11 @@ function fzf_key_bindings
   end
 
   function __fzf_alt_c
+    set -q FZF_ALT_C_COMMAND; or set -l FZF_ALT_C_COMMAND "
+    command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
+      -o -type d -print 2> /dev/null | sed 1d | cut -b3-"
     # Fish hangs if the command before pipe redirects (2> /dev/null)
-    command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) \
-      -prune -o -type d -print 2> /dev/null | sed 1d | cut -b3- | eval (__fzfcmd) +m > $TMPDIR/fzf.result
+    eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m > $TMPDIR/fzf.result"
     [ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
     and cd (cat $TMPDIR/fzf.result)
     commandline -f repaint
diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh
index eccc8266..29721fc9 100644
--- a/shell/key-bindings.zsh
+++ b/shell/key-bindings.zsh
@@ -4,7 +4,7 @@ if [[ $- == *i* ]]; then
 
 # CTRL-T - Paste the selected file path(s) into the command line
 __fsel() {
-  local cmd="${FZF_CTRL_T_COMMAND:-"command \\find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
+  local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
     -o -type f -print \
     -o -type d -print \
     -o -type l -print 2> /dev/null | sed 1d | cut -b3-"}"
@@ -27,8 +27,9 @@ bindkey '^T' fzf-file-widget
 
 # ALT-C - cd into the selected directory
 fzf-cd-widget() {
-  cd "${$(command \find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
-    -o -type d -print 2> /dev/null | sed 1d | cut -b3- | $(__fzfcmd) +m):-.}"
+  local cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \
+    -o -type d -print 2> /dev/null | sed 1d | cut -b3-"}"
+  cd "${$(eval "$cmd" | $(__fzfcmd) +m):-.}"
   zle reset-prompt
 }
 zle     -N    fzf-cd-widget
diff --git a/test/test_go.rb b/test/test_go.rb
index 50d401c3..7143d369 100644
--- a/test/test_go.rb
+++ b/test/test_go.rb
@@ -981,6 +981,22 @@ module TestShell
     tmux.until { |lines| lines[-1].end_with?(expected) }
   end
 
+  def test_alt_c_command
+    set_var 'FZF_ALT_C_COMMAND', 'echo /tmp'
+
+    tmux.prepare
+    tmux.send_keys 'cd /', :Enter
+
+    tmux.prepare
+    tmux.send_keys :Escape, :c, pane: 0
+    lines = tmux.until(1) { |lines| lines.item_count == 1 }
+    tmux.send_keys :Enter, pane: 1
+
+    tmux.prepare
+    tmux.send_keys :pwd, :Enter
+    tmux.until { |lines| lines[-1].end_with? '/tmp' }
+  end
+
   def test_ctrl_r
     tmux.prepare
     tmux.send_keys 'echo 1st', :Enter; tmux.prepare
-- 
GitLab