xref: /freebsd/contrib/nvi/ex/ex_undo.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_undo.c,v 10.7 2001/06/25 15:19:21 skimo Exp $";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 
20 #include <bitstring.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "../common/common.h"
26 
27 /*
28  * ex_undo -- u
29  *	Undo the last change.
30  *
31  * PUBLIC: int ex_undo(SCR *, EXCMD *);
32  */
33 int
34 ex_undo(SCR *sp, EXCMD *cmdp)
35 {
36 	EXF *ep;
37 	MARK m;
38 
39 	/*
40 	 * !!!
41 	 * Historic undo always set the previous context mark.
42 	 */
43 	m.lno = sp->lno;
44 	m.cno = sp->cno;
45 	if (mark_set(sp, ABSMARK1, &m, 1))
46 		return (1);
47 
48 	/*
49 	 * !!!
50 	 * Multiple undo isn't available in ex, as there's no '.' command.
51 	 * Whether 'u' is undo or redo is toggled each time, unless there
52 	 * was a change since the last undo, in which case it's an undo.
53 	 */
54 	ep = sp->ep;
55 	if (!F_ISSET(ep, F_UNDO)) {
56 		F_SET(ep, F_UNDO);
57 		ep->lundo = FORWARD;
58 	}
59 	switch (ep->lundo) {
60 	case BACKWARD:
61 		if (log_forward(sp, &m))
62 			return (1);
63 		ep->lundo = FORWARD;
64 		break;
65 	case FORWARD:
66 		if (log_backward(sp, &m))
67 			return (1);
68 		ep->lundo = BACKWARD;
69 		break;
70 	case NOTSET:
71 		abort();
72 	}
73 	sp->lno = m.lno;
74 	sp->cno = m.cno;
75 	return (0);
76 }
77