-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCommandNotFound
57 lines (54 loc) · 2.17 KB
/
CommandNotFound
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /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