xref: /freebsd/contrib/nvi/ex/ex_undo.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <limits.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include "../common/common.h"
22 
23 /*
24  * ex_undo -- u
25  *	Undo the last change.
26  *
27  * PUBLIC: int ex_undo(SCR *, EXCMD *);
28  */
29 int
30 ex_undo(SCR *sp, EXCMD *cmdp)
31 {
32 	EXF *ep;
33 	MARK m;
34 
35 	/*
36 	 * !!!
37 	 * Historic undo always set the previous context mark.
38 	 */
39 	m.lno = sp->lno;
40 	m.cno = sp->cno;
41 	if (mark_set(sp, ABSMARK1, &m, 1))
42 		return (1);
43 
44 	/*
45 	 * !!!
46 	 * Multiple undo isn't available in ex, as there's no '.' command.
47 	 * Whether 'u' is undo or redo is toggled each time, unless there
48 	 * was a change since the last undo, in which case it's an undo.
49 	 */
50 	ep = sp->ep;
51 	if (!F_ISSET(ep, F_UNDO)) {
52 		F_SET(ep, F_UNDO);
53 		ep->lundo = FORWARD;
54 	}
55 	switch (ep->lundo) {
56 	case BACKWARD:
57 		if (log_forward(sp, &m))
58 			return (1);
59 		ep->lundo = FORWARD;
60 		break;
61 	case FORWARD:
62 		if (log_backward(sp, &m))
63 			return (1);
64 		ep->lundo = BACKWARD;
65 		break;
66 	case NOTSET:
67 		abort();
68 	}
69 	sp->lno = m.lno;
70 	sp->cno = m.cno;
71 	return (0);
72 }
73