b54a6dab9cc9db9196aae5d8bc53ac3956855ecf

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(command-not-found): clean up and reorganize logic

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/command-not-found/command-not-found.plugin.zsh b/plugins/command-not-found/command-not-found.plugin.zsh
  2index 10f3a627709be3a9c40d90c1604e52fbe1b08908..8fdc8a6471d87c184d3fb6b14d2cecc4575b3b01 100644
  3--- a/plugins/command-not-found/command-not-found.plugin.zsh
  4+++ b/plugins/command-not-found/command-not-found.plugin.zsh
  5@@ -1,55 +1,55 @@
  6-# Uses the command-not-found package zsh support
  7-# as seen in https://www.porcheron.info/command-not-found-for-zsh/
  8-# this is installed in Ubuntu
  9+## Platforms with a built-in command-not-found handler init file
 10+
 11+for file (
 12+  # Arch Linux. Must have pkgfile installed: https://wiki.archlinux.org/index.php/Pkgfile#Command_not_found
 13+  /usr/share/doc/pkgfile/command-not-found.zsh
 14+  # macOS (M1 and classic Homebrew): https://github.com/Homebrew/homebrew-command-not-found
 15+  /opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh
 16+  /usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh
 17+); do
 18+  if [[ -r "$file" ]]; then
 19+    source "$file"
 20+    unset file
 21+    return 0
 22+  fi
 23+done
 24+unset file
 25 
 26-if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
 27-    function command_not_found_handler {
 28-        # check because c-n-f could've been removed in the meantime
 29-        if [ -x /usr/lib/command-not-found ]; then
 30-            /usr/lib/command-not-found -- "$1"
 31-            return $?
 32-        elif [ -x /usr/share/command-not-found/command-not-found ]; then
 33-            /usr/share/command-not-found/command-not-found -- "$1"
 34-            return $?
 35-        else
 36-            printf "zsh: command not found: %s\n" "$1" >&2
 37-            return 127
 38-        fi
 39-        return 0
 40-    }
 41-fi
 42 
 43-# Arch Linux command-not-found support, you must have package pkgfile installed
 44-# https://wiki.archlinux.org/index.php/Pkgfile#.22Command_not_found.22_hook
 45-[[ -e /usr/share/doc/pkgfile/command-not-found.zsh ]] && source /usr/share/doc/pkgfile/command-not-found.zsh
 46+## Platforms with manual command_not_found_handler() setup
 47 
 48-# Fedora command-not-found support
 49-if [ -f /usr/libexec/pk-command-not-found ]; then
 50-    command_not_found_handler() {
 51-        runcnf=1
 52-        retval=127
 53-        [ ! -S /var/run/dbus/system_bus_socket ] && runcnf=0
 54-        [ ! -x /usr/libexec/packagekitd ] && runcnf=0
 55-        if [ $runcnf -eq 1 ]; then
 56-            /usr/libexec/pk-command-not-found $@
 57-            retval=$?
 58-        fi
 59-        return $retval
 60-    }
 61+# Debian and derivatives: https://launchpad.net/ubuntu/+source/command-not-found
 62+if [[ -x /usr/lib/command-not-found || -x /usr/share/command-not-found/command-not-found ]]; then
 63+  command_not_found_handler() {
 64+    if [[ -x /usr/lib/command-not-found ]]; then
 65+      /usr/lib/command-not-found -- "$1"
 66+      return $?
 67+    elif [[ -x /usr/share/command-not-found/command-not-found ]]; then
 68+      /usr/share/command-not-found/command-not-found -- "$1"
 69+      return $?
 70+    else
 71+      printf "zsh: command not found: %s\n" "$1" >&2
 72+      return 127
 73+    fi
 74+  }
 75 fi
 76 
 77-# macOS command-not-found support
 78-# https://github.com/Homebrew/homebrew-command-not-found
 79-HB_CNF_HANDLER_SUFFIX="Library/Taps/homebrew/homebrew-command-not-found/handler.sh"
 80-if [[ -s "/opt/homebrew/$HB_CNF_HANDLER_SUFFIX" ]]; then
 81-      source "/opt/homebrew/$HB_CNF_HANDLER_SUFFIX"
 82-elif [[ -s "/usr/local/Homebrew/$HB_CNF_HANDLER_SUFFIX" ]]; then
 83-      source "/usr/local/Homebrew/$HB_CNF_HANDLER_SUFFIX"
 84+# Fedora: https://fedoraproject.org/wiki/Features/PackageKitCommandNotFound
 85+if [[ -x /usr/libexec/pk-command-not-found ]]; then
 86+  command_not_found_handler() {
 87+    if [[ -S /var/run/dbus/system_bus_socket && -x /usr/libexec/packagekitd ]]; then
 88+      /usr/libexec/pk-command-not-found -- "$@"
 89+      return $?
 90+    fi
 91+
 92+    printf "zsh: command not found: %s\n" "$1" >&2
 93+    return 127
 94+  }
 95 fi
 96 
 97-# NixOS command-not-found support
 98-if [ -x /run/current-system/sw/bin/command-not-found ]; then
 99-    command_not_found_handler() {
100-        /run/current-system/sw/bin/command-not-found $@
101-    }
102+# NixOS: https://github.com/NixOS/nixpkgs/tree/master/nixos/modules/programs/command-not-found
103+if [[ -x /run/current-system/sw/bin/command-not-found ]]; then
104+  command_not_found_handler() {