xref: /minix3/external/bsd/nvi/dist/ex/ex_undo.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_undo.c,v 1.3 2014/01/26 21:43:45 christos Exp $	*/
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_undo.c,v 10.7 2001/06/25 15:19:21 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:21 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_undo.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <limits.h>
2784d9c625SLionel Sambuc #include <stdio.h>
2884d9c625SLionel Sambuc #include <stdlib.h>
2984d9c625SLionel Sambuc 
3084d9c625SLionel Sambuc #include "../common/common.h"
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc /*
3384d9c625SLionel Sambuc  * ex_undo -- u
3484d9c625SLionel Sambuc  *	Undo the last change.
3584d9c625SLionel Sambuc  *
3684d9c625SLionel Sambuc  * PUBLIC: int ex_undo __P((SCR *, EXCMD *));
3784d9c625SLionel Sambuc  */
3884d9c625SLionel Sambuc int
ex_undo(SCR * sp,EXCMD * cmdp)3984d9c625SLionel Sambuc ex_undo(SCR *sp, EXCMD *cmdp)
4084d9c625SLionel Sambuc {
4184d9c625SLionel Sambuc 	EXF *ep;
4284d9c625SLionel Sambuc 	MARK m;
4384d9c625SLionel Sambuc 
4484d9c625SLionel Sambuc 	/*
4584d9c625SLionel Sambuc 	 * !!!
4684d9c625SLionel Sambuc 	 * Historic undo always set the previous context mark.
4784d9c625SLionel Sambuc 	 */
4884d9c625SLionel Sambuc 	m.lno = sp->lno;
4984d9c625SLionel Sambuc 	m.cno = sp->cno;
5084d9c625SLionel Sambuc 	if (mark_set(sp, ABSMARK1, &m, 1))
5184d9c625SLionel Sambuc 		return (1);
5284d9c625SLionel Sambuc 
5384d9c625SLionel Sambuc 	/*
5484d9c625SLionel Sambuc 	 * !!!
5584d9c625SLionel Sambuc 	 * Multiple undo isn't available in ex, as there's no '.' command.
5684d9c625SLionel Sambuc 	 * Whether 'u' is undo or redo is toggled each time, unless there
5784d9c625SLionel Sambuc 	 * was a change since the last undo, in which case it's an undo.
5884d9c625SLionel Sambuc 	 */
5984d9c625SLionel Sambuc 	ep = sp->ep;
6084d9c625SLionel Sambuc 	if (!F_ISSET(ep, F_UNDO)) {
6184d9c625SLionel Sambuc 		F_SET(ep, F_UNDO);
6284d9c625SLionel Sambuc 		ep->lundo = FORWARD;
6384d9c625SLionel Sambuc 	}
6484d9c625SLionel Sambuc 	switch (ep->lundo) {
6584d9c625SLionel Sambuc 	case BACKWARD:
6684d9c625SLionel Sambuc 		if (log_forward(sp, &m))
6784d9c625SLionel Sambuc 			return (1);
6884d9c625SLionel Sambuc 		ep->lundo = FORWARD;
6984d9c625SLionel Sambuc 		break;
7084d9c625SLionel Sambuc 	case FORWARD:
7184d9c625SLionel Sambuc 		if (log_backward(sp, &m))
7284d9c625SLionel Sambuc 			return (1);
7384d9c625SLionel Sambuc 		ep->lundo = BACKWARD;
7484d9c625SLionel Sambuc 		break;
7584d9c625SLionel Sambuc 	case NOTSET:
7684d9c625SLionel Sambuc 		abort();
7784d9c625SLionel Sambuc 	}
7884d9c625SLionel Sambuc 	sp->lno = m.lno;
7984d9c625SLionel Sambuc 	sp->cno = m.cno;
8084d9c625SLionel Sambuc 	return (0);
8184d9c625SLionel Sambuc }
82