command-not-found.plugin.zsh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/zsh
  2. ## Platforms with a built-in command-not-found handler init file
  3. # if this line will print bash at least once, so script is trying run in bash
  4. # fix is add shebang string (first in this file)
  5. #printf 'This is totally %s.\n' ${BASH_VERSION:+bash} ${ZSH_VERSION:+zsh}
  6. for file (
  7. # Arch Linux. Must have pkgfile installed: https://wiki.archlinux.org/index.php/Pkgfile#Command_not_found
  8. /usr/share/doc/pkgfile/command-not-found.zsh
  9. # macOS (M1 and classic Homebrew): https://github.com/Homebrew/homebrew-command-not-found
  10. /opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh
  11. /usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh
  12. ); do
  13. if [[ -r "$file" ]]; then
  14. source "$file"
  15. unset file
  16. return 0
  17. fi
  18. done
  19. unset file
  20. ## Platforms with manual command_not_found_handler() setup
  21. # Debian and derivatives: https://launchpad.net/ubuntu/+source/command-not-found
  22. if [[ -x /usr/lib/command-not-found || -x /usr/share/command-not-found/command-not-found ]]; then
  23. command_not_found_handler() {
  24. if [[ -x /usr/lib/command-not-found ]]; then
  25. /usr/lib/command-not-found -- "$1"
  26. return $?
  27. elif [[ -x /usr/share/command-not-found/command-not-found ]]; then
  28. /usr/share/command-not-found/command-not-found -- "$1"
  29. return $?
  30. else
  31. printf "zsh: command not found: %s\n" "$1" >&2
  32. return 127
  33. fi
  34. }
  35. fi
  36. # Fedora: https://fedoraproject.org/wiki/Features/PackageKitCommandNotFound
  37. if [[ -x /usr/libexec/pk-command-not-found ]]; then
  38. command_not_found_handler() {
  39. if [[ -S /var/run/dbus/system_bus_socket && -x /usr/libexec/packagekitd ]]; then
  40. /usr/libexec/pk-command-not-found "$@"
  41. return $?
  42. fi
  43. printf "zsh: command not found: %s\n" "$1" >&2
  44. return 127
  45. }
  46. fi
  47. # NixOS: https://github.com/NixOS/nixpkgs/tree/master/nixos/modules/programs/command-not-found
  48. if [[ -x /run/current-system/sw/bin/command-not-found ]]; then
  49. command_not_found_handler() {
  50. /run/current-system/sw/bin/command-not-found "$@"
  51. }
  52. fi
  53. # Termux: https://github.com/termux/command-not-found
  54. if [[ -x /data/data/com.termux/files/usr/libexec/termux/command-not-found ]]; then
  55. command_not_found_handler() {
  56. /data/data/com.termux/files/usr/libexec/termux/command-not-found "$1"
  57. }
  58. fi
  59. # SUSE and derivates: https://www.unix.com/man-page/suse/1/command-not-found/
  60. if [[ -x /usr/bin/command-not-found ]]; then
  61. command_not_found_handler() {
  62. /usr/bin/command-not-found "$1"
  63. }
  64. fi