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

Re: mailcap configuration in zsh can't open .bk files



On Mon, 02 Sep 2013 09:28:26 +0800
vinurs <haiyuan.vinurs@xxxxxxxxx> wrote:
> It's not intelligent deciding filetype by suffix only, I'd like to write
> functions to open flles using 'file --mime-type'

You're on your own here -- the zsh system isn't going to do this --- and
it looks like you've got problems.  The shell allows you to define a
command to handle other commands that aren't found, but it doesn't allow
you define a command to handle other errors, in particular "permission
denied", which is what you'd get if you tried to "execute" a
non-executable file that wasn't aliased by one means or another.

Your best bet might be to do it in ZLE as a zle-line-finish hook.  It's
going to be a bit of a guess, but if you determine that the word in
command position is a file but isn't executable, then you've got the
basis for something workable.  You'd then need to insert the handler
back in the command line at the start of BUFFER.

I haven't tried the following, so there may be trivial bugs, but you
ought to be able to whip it into shape with enough effort.


zle-line-finish() {
  local REPLY REPLY2 handler
  local -a reply
  autoload -Uz split-shell-arguments
  split-shell-arguments

  local fname=$reply[1]

  # Tests here could be a bit more stringent
  if [[ -n $fname && -f $fname && ! -x $fname ]]; then
    # Should replace the case with a look-up similar to zsh-mime-handler
    # but based on type not suffix.
    case ${$(file --mime-type $fname)##*:} in
      # HERE: match types, e.g.
      (application/pdf)
      handler=okular
      ;;
    esac
  fi

  # HERE: cleverness for backgrounding, etc, also similar to
  # zsh-mime-handler.
  if [[ -n $handler ]]; then
    BUFFER="$handler $BUFFER"
  fi
}
zle -N zle-line-finish


pws



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