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

Re: function to replace the command line text



On Apr 18, 12:49pm, Dave Yost wrote:
}
} Is there a builtin function that replaces the command line with the
} function's output?

As Frank demonstrated, the most basic "function" for this is to assign
to the special parameter $BUFFER from inside a user-defined ZLE widget.

However ...

} I want to use that function to build a function I can use to run a
} demo consisting of a sequence of commands.
}  * Make an array of strings
}  * For each step of the demo
}     * Type a command or a keyboard shortcut that grabs the next string
}       from the array and places it on the command line
}     * Hit Enter to execute the command

There are a bunch of ways to do this.  If you just want to execute the
commands one after another, you could:

 - Store the commands in a file, one per line;
 - Load the file as history with "fc -R" or "fc -p";
 - bindkey ^M accept-and-infer-next-history;
 - scroll back (up-history) to the first command;
 - each subseqent enter executes the command and advances to the next.

This has the advantage that you can up-history to redo any command.  If
that isn't likely to be necessary you can:

 - push the commands onto the buffer stack, in reverse order, with
   one "print -z" per command;
 - press enter to load the first command;
 - each subseqent enter executes the command and pops the next.

Here you'd have to use the push-line widget and then up-history if you
want to redo a command.

If you want to be able to go off-script to execute some other commands
and then resume the demo, then you'll need something like what Frank
outlined, with a key binding to produce the next step when ready.  If
you hit the "next step" key more than once, though, you'll skip over
a command step and have no way to get back to it, unless you also
create another binding that steps down through the array.

Here's a variation of Frank's FOO widget that lets you select which
step of the demo you want, and by default goes on to the next:

FOO() {
  if (( NUMERIC > 0 ))
  then (( j=NUMERIC ))
  elif (( NUMERIC < 0 ))
  then (( NUMERIC > -j ? (j+=NUMERIC) : (j=1) ))
  fi
  BUFFER=${foo[j++]}; CURSOR=${#BUFFER}
}

You press ESC 2 ^L, for example, to select the second step, or to go
backward, ESC - ESC 1 ^L (repeats the last step).



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