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