zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Dave Yost <Dave@Yost.com>, zsh-workers@zsh.org
Subject: Re: function to replace the command line text
Date: Fri, 18 Apr 2014 20:27:39 -0700	[thread overview]
Message-ID: <140418202739.ZM24101@torch.brasslantern.com> (raw)
In-Reply-To: <CAE5DDCE-D495-4546-ACDA-8605B8644FDC@Yost.com>

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).


  parent reply	other threads:[~2014-04-19  3:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-18 19:49 Dave Yost
2014-04-18 21:55 ` Frank Terbeck
2014-04-19  3:27 ` Bart Schaefer [this message]
2014-04-24 20:34 ` zsh script to run prepared shell commands for a demo Dave Yost

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=140418202739.ZM24101@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=Dave@Yost.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).