#! /usr/bin/zsh (source) # ZSH code for CommandNotFound support. Sourced from zshrc, but in a separate # file so it can be updated without going through the UpdateSettings process. # Defined as separate functions to ease the use of other precmd() functions in # ~/.zshrc. Behaviour can be disabled with `unset -f CommandNotFound_preexec`. function CommandNotFound_preexec() { setopt localoptions shwordsplit # Trim back to the command itself, chopping off everything until # a space, pipe, redirect, semicolon or ampersand. command="${1%%[|>< ;&]*}" # Our zshrc defines autocd by default, so ignore directory names [ -d "$command" ] && command= # If you define this variable to point to a tool that works like # "kfmclient exec", you can automatically launch a file with its # appropriate application just by typing its name. A little hacky, but # effective. if [ "$OPEN_COMMAND" -a -e "$command" ] then if ! whence -- "$command" >& /dev/null then # If it succeeds, suppress ZSH's default "command not found" message. $OPEN_COMMAND "$command" && exec 2>/dev/null command= fi fi # When there's an environment variable or function definition, a path, # or a shell expansion, just forget about it. They probably know what # they're doing. [ "$command" != "${command%%[\$~=(\\\"\'/]*}" ] && command="" } function CommandNotFound_precmd() { [ -z "$command" ] && [ "$OPEN_COMMAND" ] && exec 2>&1 # Restore stderr if [ $? -ne 0 ] && [ -n "$command" ] then whence -- "$command" >& /dev/null || CommandNotFound $command unset command fi } # This initialises the list of precmd/preexec functions so the standard # function names transparently work. It ensures the entries aren't # duplicated if this file is sourced more than once. if [ "$precmd_functions" ] then precmd_functions=( "${precmd_functions[@]:#CommandNotFound_precmd}" CommandNotFound_precmd ) else precmd_functions=( CommandNotFound_precmd ) fi if [ "$preexec_functions" ] then preexec_functions=( "${preexec_functions[@]:#CommandNotFound_preexec}" CommandNotFound_preexec ) else preexec_functions=(CommandNotFound_preexec ) fi