zoxide (4370B)
1 # shellcheck shell=bash 2 3 # ============================================================================= 4 # 5 # Utility functions for zoxide. 6 # 7 8 # pwd based on the value of _ZO_RESOLVE_SYMLINKS. 9 function __zoxide_pwd() { 10 \builtin pwd -L 11 } 12 13 # cd + custom logic based on the value of _ZO_ECHO. 14 function __zoxide_cd() { 15 # shellcheck disable=SC2164 16 \builtin cd -- "$@" 17 } 18 19 # ============================================================================= 20 # 21 # Hook configuration for zoxide. 22 # 23 24 # Hook to add new entries to the database. 25 function __zoxide_hook() { 26 # shellcheck disable=SC2312 27 \command zoxide add -- "$(__zoxide_pwd)" 28 } 29 30 # Initialize hook. 31 \builtin typeset -ga precmd_functions 32 \builtin typeset -ga chpwd_functions 33 # shellcheck disable=SC2034,SC2296 34 precmd_functions=("${(@)precmd_functions:#__zoxide_hook}") 35 # shellcheck disable=SC2034,SC2296 36 chpwd_functions=("${(@)chpwd_functions:#__zoxide_hook}") 37 chpwd_functions+=(__zoxide_hook) 38 39 # Report common issues. 40 function __zoxide_doctor() { 41 [[ ${_ZO_DOCTOR:-1} -ne 0 ]] || return 0 42 [[ ${chpwd_functions[(Ie)__zoxide_hook]:-} -eq 0 ]] || return 0 43 44 _ZO_DOCTOR=0 45 \builtin printf '%s\n' \ 46 'zoxide: detected a possible configuration issue.' \ 47 'Please ensure that zoxide is initialized right at the end of your shell configuration file (usually ~/.zshrc).' \ 48 '' \ 49 'If the issue persists, consider filing an issue at:' \ 50 'https://github.com/ajeetdsouza/zoxide/issues' \ 51 '' \ 52 'Disable this message by setting _ZO_DOCTOR=0.' \ 53 '' >&2 54 } 55 56 # ============================================================================= 57 # 58 # When using zoxide with --no-cmd, alias these internal functions as desired. 59 # 60 61 # Jump to a directory using only keywords. 62 function __zoxide_z() { 63 __zoxide_doctor 64 if [[ "$#" -eq 0 ]]; then 65 __zoxide_cd ~ 66 elif [[ "$#" -eq 1 ]] && { [[ -d "$1" ]] || [[ "$1" = '-' ]] || [[ "$1" =~ ^[-+][0-9]$ ]]; }; then 67 __zoxide_cd "$1" 68 elif [[ "$#" -eq 2 ]] && [[ "$1" = "--" ]]; then 69 __zoxide_cd "$2" 70 else 71 \builtin local result 72 # shellcheck disable=SC2312 73 result="$(\command zoxide query --exclude "$(__zoxide_pwd)" -- "$@")" && __zoxide_cd "${result}" 74 fi 75 } 76 77 # Jump to a directory using interactive search. 78 function __zoxide_zi() { 79 __zoxide_doctor 80 \builtin local result 81 result="$(\command zoxide query --interactive -- "$@")" && __zoxide_cd "${result}" 82 } 83 84 # ============================================================================= 85 # 86 # Commands for zoxide. Disable these using --no-cmd. 87 # 88 89 function z() { 90 __zoxide_z "$@" 91 } 92 93 function zi() { 94 __zoxide_zi "$@" 95 } 96 97 # Completions. 98 if [[ -o zle ]]; then 99 __zoxide_result='' 100 101 function __zoxide_z_complete() { 102 # Only show completions when the cursor is at the end of the line. 103 # shellcheck disable=SC2154 104 [[ "${#words[@]}" -eq "${CURRENT}" ]] || return 0 105 106 if [[ "${#words[@]}" -eq 2 ]]; then 107 # Show completions for local directories. 108 _cd -/ 109 110 elif [[ "${words[-1]}" == '' ]]; then 111 # Show completions for Space-Tab. 112 # shellcheck disable=SC2086 113 __zoxide_result="$(\command zoxide query --exclude "$(__zoxide_pwd || \builtin true)" --interactive -- ${words[2,-1]})" || __zoxide_result='' 114 115 # Set a result to ensure completion doesn't re-run 116 compadd -Q "" 117 118 # Bind '\e[0n' to helper function. 119 \builtin bindkey '\e[0n' '__zoxide_z_complete_helper' 120 # Sends query device status code, which results in a '\e[0n' being sent to console input. 121 \builtin printf '\e[5n' 122 123 # Report that the completion was successful, so that we don't fall back 124 # to another completion function. 125 return 0 126 fi 127 } 128 129 function __zoxide_z_complete_helper() { 130 if [[ -n "${__zoxide_result}" ]]; then 131 # shellcheck disable=SC2034,SC2296 132 BUFFER="z ${(q-)__zoxide_result}" 133 __zoxide_result='' 134 \builtin zle reset-prompt 135 \builtin zle accept-line 136 else 137 \builtin zle reset-prompt 138 fi 139 } 140 \builtin zle -N __zoxide_z_complete_helper 141 142 [[ "${+functions[compdef]}" -ne 0 ]] && \compdef __zoxide_z_complete z 143 fi