Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Minor details of command_not_found_handler



On Fri, Aug 28, 2020 at 7:41 AM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> Doc says:
>        ... If  the  function wishes to mimic the behaviour of the shell
>        when the command is not found, it should print the message `command not
>        found:  cmd'  to  standard  error and return status 127.

FWIW, here's how I hook up GNU command-not-found in interactive Zsh:

  function command_not_found_handler() {
    emulate -L zsh
    if (( $#functrace >= 2 )); then
      print -ru2 -- "$functrace[1]: command not found: $1"
    else
      local msg="$(/usr/lib/command-not-found --no-failure-msg -- $1 2>&1)"
      if [[ -n $msg ]]; then
        print -ru2 -- ${msg#$'\n'}
      else
        print -ru2 -- "zsh: command not found: $1"
      fi
    fi
    return 127
  }

The check for (( $#functrace >= 2 )) is there in order to invoke the
incredibly slow /usr/lib/command-not-found only when mistyping a
command on the command line. It doesn't do exactly that but it's close
enough.

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author