Modifying behavior of "again"

Is there any way to change what the again/g command does? Specifically, I’m looking to make it re-run the last valid action the player tried rather than an erroneous one.

I was thinking of doing something like this:

[code]There is a room.

The previous action is an indexed text that varies.

Directly-run is a truth state that varies.

Before reading a command:
now directly-run is true.

First before doing something when directly-run is true:
now the previous action is the player’s command;
now directly-run is false.

After reading a command:
if the player’s command matches “again” and the previous action is not empty:
replace the matched text with “[previous action]”.

Test me with “z / blah / again”[/code]

However I get a runtime error, and then it retries the mistaken command anyway.

Here’s a way:

[code]There is a room.

The previous action is a stored action that varies.

Before doing anything:
now the previous action is the current action.

After reading a command:
if the player’s command matches “again/g”, try the previous action instead.

Test me with “z / blah / r / again / i / g / x / g / g”[/code]

This works well, thank you. Just a couple tweaks and it’s exactly what I’m looking for.