Skip to content

Commit

Permalink
users/16932: implement $UNDO_CHANGE_NO and argument to undo widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Stephenson committed Mar 29, 2012
1 parent 8bb9818 commit 13a45d7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
8 changes: 7 additions & 1 deletion ChangeLog
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,9 @@
2012-03-29 Peter Stephenson <p.w.stephenson@ntlworld.com>

* users/16932: Doc/Zsh/zle.yo, Functions/Zle/replace-string:
Src/Zle/zle.h, Src/Zle/zle_params.c, Src/Zle/zle_utils.c:
implement $UNDO_CHANGE_NO and argument to undo widget.

2012-03-25 Mikael Magnusson <mikachu@gmail.com> 2012-03-25 Mikael Magnusson <mikachu@gmail.com>


* Jesper Nygårds: 30378: Completion/Unix/Command/.distfiles, * Jesper Nygårds: 30378: Completion/Unix/Command/.distfiles,
Expand Down Expand Up @@ -16125,5 +16131,5 @@


***************************************************** *****************************************************
* This is used by the shell to define $ZSH_PATCHLEVEL * This is used by the shell to define $ZSH_PATCHLEVEL
* $Revision: 1.5615 $ * $Revision: 1.5616 $
***************************************************** *****************************************************
11 changes: 10 additions & 1 deletion Doc/Zsh/zle.yo
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -877,6 +877,12 @@ any predisplay string should be highlighted in bold.
Note that the effect of tt(region_highlight) is not saved and disappears Note that the effect of tt(region_highlight) is not saved and disappears
as soon as the line is accepted. as soon as the line is accepted.
) )
vindex(UNDO_CHANGE_NO)
item(tt(UNDO_CHANGE_NO) (integer))
A number representing the state of the undo history. The only use
of this is passing as an argument to the tt(undo) widget in order to
undo back to the recorded point. Read-only.
)
vindex(WIDGET) vindex(WIDGET)
item(tt(WIDGET) (scalar))( item(tt(WIDGET) (scalar))(
The name of the widget currently being executed; read-only. The name of the widget currently being executed; read-only.
Expand Down Expand Up @@ -2164,7 +2170,10 @@ command is typed. By default it beeps.
) )
tindex(undo) tindex(undo)
item(tt(undo) (^_ ^Xu ^X^U) (unbound) (unbound))( item(tt(undo) (^_ ^Xu ^X^U) (unbound) (unbound))(
Incrementally undo the last text modification. Incrementally undo the last text modification. When called from a
user-defined widget, takes an optional argument indicating a previous state
of the undo history as returned by the tt(UNDO_CHANGE_NO) variable;
modifications are undone until that state is reached.
) )
tindex(redo) tindex(redo)
item(tt(redo))( item(tt(redo))(
Expand Down
9 changes: 9 additions & 0 deletions Functions/Zle/replace-string
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ setopt extendedglob
autoload -Uz read-from-minibuffer replace-string-again autoload -Uz read-from-minibuffer replace-string-again


local p1 p2 local p1 p2
integer changeno=$UNDO_CHANGE_NO

{


if [[ -n $_replace_string_src ]]; then if [[ -n $_replace_string_src ]]; then
p1="[$_replace_string_src -> $_replace_string_rep]"$'\n' p1="[$_replace_string_src -> $_replace_string_rep]"$'\n'
Expand All @@ -30,4 +33,10 @@ if [[ -n $REPLY ]]; then
typeset -g _replace_string_rep=$REPLY typeset -g _replace_string_rep=$REPLY
fi fi


} always {
# Undo back to the original line; we don't want the
# undo history of editing the strings left.
zle undo $changeno
}

replace-string-again $curwidget replace-string-again $curwidget
1 change: 1 addition & 0 deletions Src/Zle/zle.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ struct change {
ZLE_STRING_T ins; /* characters to insert */ ZLE_STRING_T ins; /* characters to insert */
int insl; /* no. of characters in ins */ int insl; /* no. of characters in ins */
int old_cs, new_cs; /* old and new cursor positions */ int old_cs, new_cs; /* old and new cursor positions */
zlong changeno; /* unique number of this change */
}; };


#define CH_NEXT (1<<0) /* next structure is also part of this change */ #define CH_NEXT (1<<0) /* next structure is also part of this change */
Expand Down
4 changes: 4 additions & 0 deletions Src/Zle/zle_params.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ static const struct gsu_integer pending_gsu =
{ get_pending, NULL, zleunsetfn }; { get_pending, NULL, zleunsetfn };
static const struct gsu_integer region_active_gsu = static const struct gsu_integer region_active_gsu =
{ get_region_active, set_region_active, zleunsetfn }; { get_region_active, set_region_active, zleunsetfn };
static const struct gsu_integer undo_change_no_gsu =
{ get_undo_current_change, NULL, zleunsetfn };


static const struct gsu_array killring_gsu = static const struct gsu_array killring_gsu =
{ get_killring, set_killring, unset_killring }; { get_killring, set_killring, unset_killring };
Expand Down Expand Up @@ -133,6 +135,8 @@ static struct zleparam {
{ "RBUFFER", PM_SCALAR, GSU(rbuffer_gsu), NULL }, { "RBUFFER", PM_SCALAR, GSU(rbuffer_gsu), NULL },
{ "REGION_ACTIVE", PM_INTEGER, GSU(region_active_gsu), NULL}, { "REGION_ACTIVE", PM_INTEGER, GSU(region_active_gsu), NULL},
{ "region_highlight", PM_ARRAY, GSU(region_highlight_gsu), NULL }, { "region_highlight", PM_ARRAY, GSU(region_highlight_gsu), NULL },
{ "UNDO_CHANGE_NO", PM_INTEGER | PM_READONLY, GSU(undo_change_no_gsu),
NULL },
{ "WIDGET", PM_SCALAR | PM_READONLY, GSU(widget_gsu), NULL }, { "WIDGET", PM_SCALAR | PM_READONLY, GSU(widget_gsu), NULL },
{ "WIDGETFUNC", PM_SCALAR | PM_READONLY, GSU(widgetfunc_gsu), NULL }, { "WIDGETFUNC", PM_SCALAR | PM_READONLY, GSU(widgetfunc_gsu), NULL },
{ "WIDGETSTYLE", PM_SCALAR | PM_READONLY, GSU(widgetstyle_gsu), NULL }, { "WIDGETSTYLE", PM_SCALAR | PM_READONLY, GSU(widgetstyle_gsu), NULL },
Expand Down
30 changes: 28 additions & 2 deletions Src/Zle/zle_utils.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1359,6 +1359,10 @@ static struct change *changes, *curchange;


static struct change *nextchanges, *endnextchanges; static struct change *nextchanges, *endnextchanges;


/* incremented to provide a unique change number */

static zlong undo_changeno;

/**/ /**/
void void
initundo(void) initundo(void)
Expand All @@ -1368,6 +1372,7 @@ initundo(void)
curchange->prev = curchange->next = NULL; curchange->prev = curchange->next = NULL;
curchange->del = curchange->ins = NULL; curchange->del = curchange->ins = NULL;
curchange->dell = curchange->insl = 0; curchange->dell = curchange->insl = 0;
curchange->changeno = undo_changeno = 0;
lastline = zalloc((lastlinesz = linesz) * ZLE_CHAR_SIZE); lastline = zalloc((lastlinesz = linesz) * ZLE_CHAR_SIZE);
ZS_memcpy(lastline, zleline, (lastll = zlell)); ZS_memcpy(lastline, zleline, (lastll = zlell));
lastcs = zlecs; lastcs = zlecs;
Expand Down Expand Up @@ -1492,6 +1497,7 @@ mkundoent(void)
ch->flags = 0; ch->flags = 0;
ch->prev = NULL; ch->prev = NULL;
} }
ch->changeno = ++undo_changeno;
endnextchanges = ch; endnextchanges = ch;
} }


Expand All @@ -1512,8 +1518,15 @@ setlastline(void)


/**/ /**/
int int
undo(UNUSED(char **args)) undo(char **args)
{ {
zlong last_change = (zlong)0;

if (*args)
{
last_change = zstrtol(*args, NULL, 0);
}

handleundo(); handleundo();
do { do {
if(!curchange->prev) if(!curchange->prev)
Expand All @@ -1522,7 +1535,8 @@ undo(UNUSED(char **args))
curchange = curchange->prev; curchange = curchange->prev;
else else
break; break;
} while(curchange->flags & CH_PREV); } while (*args ? curchange->changeno != last_change :
(curchange->flags & CH_PREV));
setlastline(); setlastline();
return 0; return 0;
} }
Expand Down Expand Up @@ -1637,3 +1651,15 @@ zlecallhook(char *name, char *arg)
errflag = saverrflag; errflag = saverrflag;
retflag = savretflag; retflag = savretflag;
} }

/*
* Return the number corresponding to the last change made.
*/

/**/
zlong
get_undo_current_change(UNUSED(Param pm))
{
return undo_changeno;
}

0 comments on commit 13a45d7

Please sign in to comment.