fzf.plugin.zsh 4.88 KB
Newer Older
1
function setup_using_base_dir() {
2
    local fzf_base fzf_shell fzfdirs dir
3
4
5
6
7
8

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

    if [[ -z "${fzf_base}" ]]; then
        fzfdirs=(
          "${HOME}/.fzf"
9
          "${HOME}/.nix-profile/share/fzf"
10
11
          "/usr/local/opt/fzf"
          "/usr/share/fzf"
12
          "/usr/local/share/examples/fzf"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
        )
        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

30
31
32
    if [[ ! -d "${fzf_base}" ]]; then
        return 1
    fi
33

34
35
36
37
38
39
    # Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
    if [[ ! -d "${fzf_base}/shell" ]]; then
        fzf_shell="${fzf_base}"
    else
        fzf_shell="${fzf_base}/shell"
    fi
40

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

46
47
48
49
50
51
52
53
    # Auto-completion
    if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
        source "${fzf_shell}/completion.zsh" 2> /dev/null
    fi

    # Key bindings
    if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
        source "${fzf_shell}/key-bindings.zsh"
54
55
56
57
58
    fi
}


function setup_using_debian_package() {
59
60
    if (( ! $+commands[dpkg] )) || ! dpkg -s fzf &>/dev/null; then
        # Either not a debian based distro, or no fzf installed
61
62
63
64
65
66
        return 1
    fi

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

67
68
69
70
    # 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"

71
72
73
    local key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"

    # Auto-completion
74
75
    if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
        source $completions 2> /dev/null
76
77
78
79
80
81
82
83
84
85
    fi

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

    return 0
}

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function setup_using_opensuse_package() {
    # OpenSUSE installs fzf in /usr/bin/fzf
    # If the command is not found, the package isn't installed
    (( $+commands[fzf] )) || return 1

    # The fzf-zsh-completion package installs the auto-completion in
    local completions="/usr/share/zsh/site-functions/_fzf"
    # The fzf-zsh-completion package installs the key-bindings file in
    local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"

    # If these are not found: (1) maybe we're not on OpenSUSE, or
    # (2) maybe the fzf-zsh-completion package isn't installed.
    if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
        return 1
    fi

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

    # Key bindings
    if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
        source "$key_bindings" 2>/dev/null
    fi

    return 0
}

function setup_using_openbsd_package() {
    # openBSD installs fzf in /usr/local/bin/fzf
    if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
        return 1
    fi

    # The fzf package installs the auto-completion in
    local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
    # The fzf package installs the key-bindings file in
    local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"

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

    # Key bindings
    if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
        source "$key_bindings" 2>/dev/null
    fi

    return 0
}

139
function indicate_error() {
140
141
142
143
    cat >&2 <<EOF
[oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc
EOF
144
145
146
}

# Indicate to user that fzf installation not found if nothing worked
147
148
149
150
151
setup_using_openbsd_package \
    || setup_using_debian_package \
    || setup_using_opensuse_package \
    || setup_using_base_dir \
    || indicate_error
152

153
unset -f setup_using_opensuse_package setup_using_debian_package setup_using_base_dir indicate_error
154
155
156

if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
    if (( $+commands[rg] )); then
157
        export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
158
159
160
    elif (( $+commands[fd] )); then
        export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
    elif (( $+commands[ag] )); then
161
        export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
162
163
    fi
fi