fzf.plugin.zsh 3.36 KB
Newer Older
1
function setup_using_base_dir() {
2
3
4
5
6
    # Declare all variables local not no mess with outside env in any way
    local fzf_base
    local fzf_shell
    local fzfdirs
    local dir
7
8
9
10
11
12

    test -d "${FZF_BASE}" && fzf_base="${FZF_BASE}"

    if [[ -z "${fzf_base}" ]]; then
        fzfdirs=(
          "${HOME}/.fzf"
13
          "${HOME}/.nix-profile/share/fzf"
14
15
          "/usr/local/opt/fzf"
          "/usr/share/fzf"
16
          "/usr/local/share/examples/fzf"
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        )
        for dir in ${fzfdirs}; do
            if [[ -d "${dir}" ]]; then
                fzf_base="${dir}"
                break
            fi
        done

        if [[ -z "${fzf_base}" ]]; then
            if (( ${+commands[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
                if [[ -d "${dir}" ]]; then
                    fzf_base="${dir}"
                fi
            fi
        fi
    fi

    if [[ -d "${fzf_base}" ]]; then
35
36
        # Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
        if [[ ! -d "${fzf_base}/shell" ]]; then
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
          fzf_shell="${fzf_base}"
        else
          fzf_shell="${fzf_base}/shell"
        fi

        # Setup fzf binary path
        if ! (( ${+commands[fzf]} )) && [[ ! "$PATH" == *$fzf_base/bin* ]]; then
          export PATH="$PATH:$fzf_base/bin"
        fi

        # Auto-completion
        if [[ ! "$DISABLE_FZF_AUTO_COMPLETION" == "true" ]]; then
          [[ $- == *i* ]] && source "${fzf_shell}/completion.zsh" 2> /dev/null
        fi

        # Key bindings
        if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
          source "${fzf_shell}/key-bindings.zsh"
        fi
    else
        return 1
    fi
}


function setup_using_debian_package() {
63
    (( $+commands[dpkg] )) && dpkg -s fzf &> /dev/null
64
65
66
67
68
69
70
71
    if (( $? )); then
        # Either not a debian based distro, or no fzf installed. In any case skip ahead
        return 1
    fi

    # NOTE: There is no need to configure PATH for debian package, all binaries
    # are installed to /usr/bin by default

72
73
74
75
    # Determine completion file path: first bullseye/sid, then buster/stretch
    local completions="/usr/share/doc/fzf/examples/completion.zsh"
    [[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    local key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"

    # Auto-completion
    if [[ $- == *i* ]] && [[ ! "$DISABLE_FZF_AUTO_COMPLETION" == "true" ]]; then
         source $completions 2> /dev/null
    fi

    # Key bindings
    if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
        source $key_bindings
    fi

    return 0
}

function indicate_error() {
    print "[oh-my-zsh] fzf plugin: Cannot find fzf installation directory.\n"\
          "Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc" >&2
}

# Check for debian package first, because it easy to short cut
# Indicate to user that fzf installation not found if nothing worked
setup_using_debian_package || setup_using_base_dir || indicate_error
99
100

unset -f setup_using_debian_package setup_using_base_dir indicate_error
101
102
103
104
105
106
107
108
109
110

if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
    if (( $+commands[rg] )); then
        export FZF_DEFAULT_COMMAND='rg --files --hidden'
    elif (( $+commands[fd] )); then
        export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
    elif (( $+commands[ag] )); then
        export FZF_DEFAULT_COMMAND='ag -l --hidden -g ""'
    fi
fi