mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-04-28 07:31:14 -03:00
Call "fish_command_not_found" if a command wasn't found
Previously, when a command wasn't found, fish would emit the
"fish_command_not_found" *event*.
This was annoying as it was hard to override (the code ended up
checking for a function called `__fish_command_not_found_handler`
anyway!), the setup was ugly,
and it's useless - there is no use case for multiple command-not-found handlers.
Instead, let's just call a function `fish_command_not_found` if it
exists, or print the default message otherwise.
The event is completely removed, but because a missing event is not an error
(MEISNAE in C++-speak) this isn't an issue.
Note that, for backwards-compatibility, we still keep the default
handler function around even tho the new one is hard-coded in C++.
Also, if we detect a previous handler, the new handler just calls it.
This way, the backwards-compatible way to install a custom handler is:
```fish
function __fish_command_not_found_handler --on-event fish_command_not_found
# do a little dance, make a little love, get down tonight
end
```
and the new hotness is
```fish
function fish_command_not_found
# do the thing
end
```
Fixes #7293.
This commit is contained in:
@@ -258,63 +258,9 @@ function __fish_config_interactive -d "Initializations that should be performed
|
||||
__update_cwd_osc # Run once because we might have already inherited a PWD from an old tab
|
||||
end
|
||||
|
||||
### Command-not-found handlers
|
||||
# This can be overridden by defining a new __fish_command_not_found_handler function
|
||||
if not type -q __fish_command_not_found_handler
|
||||
# Read the OS/Distro from /etc/os-release.
|
||||
# This has a "ID=" line that defines the exact distribution,
|
||||
# and an "ID_LIKE=" line that defines what it is derived from or otherwise like.
|
||||
# For our purposes, we use both.
|
||||
set -l os
|
||||
if test -r /etc/os-release
|
||||
set os (string match -r '^ID(?:_LIKE)?\s*=.*' < /etc/os-release | \
|
||||
string replace -r '^ID(?:_LIKE)?\s*=(.*)' '$1' | string trim -c '\'"' | string split " ")
|
||||
end
|
||||
|
||||
# First check if we are on OpenSUSE since SUSE's handler has no options
|
||||
# but the same name and path as Ubuntu's.
|
||||
if contains -- suse $os || contains -- sles $os && type -q command-not-found
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
/usr/bin/command-not-found $argv[1]
|
||||
end
|
||||
# Check for Fedora's handler
|
||||
else if test -f /usr/libexec/pk-command-not-found
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
/usr/libexec/pk-command-not-found $argv[1]
|
||||
end
|
||||
# Check in /usr/lib, this is where modern Ubuntus place this command
|
||||
else if test -f /usr/lib/command-not-found
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
/usr/lib/command-not-found -- $argv[1]
|
||||
end
|
||||
# Check for NixOS handler
|
||||
else if test -f /run/current-system/sw/bin/command-not-found
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
/run/current-system/sw/bin/command-not-found $argv
|
||||
end
|
||||
# Ubuntu Feisty places this command in the regular path instead
|
||||
else if type -q command-not-found
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
command-not-found -- $argv[1]
|
||||
end
|
||||
# pkgfile is an optional, but official, package on Arch Linux
|
||||
# it ships with example handlers for bash and zsh, so we'll follow that format
|
||||
else if type -p -q pkgfile
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
set -l __packages (pkgfile --binaries --verbose -- $argv[1] 2>/dev/null)
|
||||
if test $status -eq 0
|
||||
printf "%s may be found in the following packages:\n" "$argv[1]"
|
||||
printf " %s\n" $__packages
|
||||
else
|
||||
__fish_default_command_not_found_handler $argv[1]
|
||||
end
|
||||
end
|
||||
# Use standard fish command not found handler otherwise
|
||||
else
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
__fish_default_command_not_found_handler $argv[1]
|
||||
end
|
||||
end
|
||||
# For backwards-compatibility - the event doesn't exist anymore so it's harmless.
|
||||
function __fish_command_not_found_handler --on-event fish_command_not_found
|
||||
fish_command_not_found $argv
|
||||
end
|
||||
|
||||
# Bump this whenever some code below needs to run once when upgrading to a new version.
|
||||
|
||||
Reference in New Issue
Block a user