zsh-workers
 help / color / mirror / code / Atom feed
* Minor details of command_not_found_handler
@ 2020-08-28  5:41 Bart Schaefer
  2020-08-28  8:21 ` Roman Perepelitsa
  0 siblings, 1 reply; 2+ messages in thread
From: Bart Schaefer @ 2020-08-28  5:41 UTC (permalink / raw)
  To: Zsh hackers list

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.

Except that does not really mimic the behavior.  Interactive shells print e.g.:

zsh: command not found: notacommand

and non-interactive shells add a line number:

zsh:1: command not found: notacommand

This almost does it:

  print -u2 -r -- "${functrace[1]:-$ZSH_ARGZERO}: command not found: $1"
  return 127

The nit being that $functrace[1] for a login shell contains "-zsh"
rather than "zsh", and of course includes the line number whether or
not the shell is interactive.

One thing that the doc fails to mention is that, even when
function_argzero is set, $0 inside command_not_found_handler is the
same as $1 (i.e., the name of the not-found command).  However, %N for
prompt expansion is "command_not_found_handler".


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Minor details of command_not_found_handler
  2020-08-28  5:41 Minor details of command_not_found_handler Bart Schaefer
@ 2020-08-28  8:21 ` Roman Perepelitsa
  0 siblings, 0 replies; 2+ messages in thread
From: Roman Perepelitsa @ 2020-08-28  8:21 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On Fri, Aug 28, 2020 at 7:41 AM Bart Schaefer <schaefer@brasslantern.com> 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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-28  8:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-28  5:41 Minor details of command_not_found_handler Bart Schaefer
2020-08-28  8:21 ` Roman Perepelitsa

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).