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

Re: first adventures



On Oct 29,  4:31pm, Ray Andrews wrote:
>
> I have a real issue, and chewing on it seems like a good first
> motivation to dive into the internals. But I think my questions have
> been focused enough that, were it possible to do as I'm seeking: break
> up the command line (in the .histfile format as we've discussed) into
> separate individual commands (by semicolons and/or however else that
> is done), and be able to pass that 'literal' command string to the
> command to which it applies:

The problem with asking focused questions is that they feed on one's
preconceived notions of what needs to be done, rather than revealing
what has already BEEN done ...

torch% setopt DEBUG_BEFORE_CMD

(That's the default, but just in case.)

torch% TRAPDEBUG() { print -u2 "XX $ZSH_DEBUG_CMD XX" }
torch% print $path * > /dev/null
XX print $path * > /dev/null XX
torch% 

This only gets you as far as what the grammar calls a "sublist", so a
pipeline will get put into $ZSH_DEBUG_CMD in its entirety, but a new
trap is run after each semicolon:

torch% print $path * > /dev/null ; print foo | grep bar
XX print $path * > /dev/null XX
XX print foo | grep bar XX
torch% print $(echo foo | grep bar)
XX print $(echo foo | grep bar) XX
XX echo foo | grep bar XX

torch% 

Also for structured commands (loops, if-then) the DEBUG trap is called
once for the entire structure and then again for each sublist inside:

torch% repeat 3
repeat> do print foo
repeat> done
XX repeat 3
do
        print foo
done XX
XX print foo XX
foo
XX print foo XX
foo
XX print foo XX
foo
torch% 

This means you can work around the pipeline thing by using braces:

torch% { print $path * > /dev/null } && { print foo } | { grep bar }
XX {
        print $path * > /dev/null
} && {
        print foo
} | {
        grep bar
} XX
XX print $path * > /dev/null XX
XX print foo XX
XX grep bar XX
torch% 

The commands that appear inside the debug trap are not themselves
debugged, so you can examine other state there to decide for example
whether you want to copy $ZSH_DEBUG_CMD into another variable that
your wrapper can then examine.  (( $#functrace == 1 )) is probably
a good test for whether you are at the shell prompt, e.g.:

typeset -g THIS_TOPLEVEL_CMD
TRAPDEBUG() {
  (( $#functrace == 1 )) && THIS_TOPLEVEL_CMD="$ZSH_DEBUG_CMD"
}

Then your ell function can look at $THIS_TOPLEVEL_CMD like you want.

One final word:

>          echo "$the-big-long-ls-command" >> $HOME/.histfile
>          fc -R

Ouch; please don't do that.

	print -S "$THIS_TOPLEVEL_CMD"



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