xref: /openbsd/usr.bin/vi/ex/ex_undo.c (revision 09467b48)
1 /*	$OpenBSD: ex_undo.c,v 1.6 2014/11/12 04:28:41 bentley 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
31 ex_undo(SCR *sp, EXCMD *cmdp)
32 {
33 	EXF *ep;
34 	MARK m;
35 
36 	/*
37 	 * !!!
38 	 * Historic undo always set the previous context mark.
39 	 */
40 	m.lno = sp->lno;
41 	m.cno = sp->cno;
42 	if (mark_set(sp, ABSMARK1, &m, 1))
43 		return (1);
44 
45 	/*
46 	 * !!!
47 	 * Multiple undo isn't available in ex, as there's no '.' command.
48 	 * Whether 'u' is undo or redo is toggled each time, unless there
49 	 * was a change since the last undo, in which case it's an undo.
50 	 */
51 	ep = sp->ep;
52 	if (!F_ISSET(ep, F_UNDO)) {
53 		F_SET(ep, F_UNDO);
54 		ep->lundo = FORWARD;
55 	}
56 	switch (ep->lundo) {
57 	case BACKWARD:
58 		if (log_forward(sp, &m))
59 			return (1);
60 		ep->lundo = FORWARD;
61 		break;
62 	case FORWARD:
63 		if (log_backward(sp, &m))
64 			return (1);
65 		ep->lundo = BACKWARD;
66 		break;
67 	case NOTSET:
68 		abort();
69 	}
70 	sp->lno = m.lno;
71 	sp->cno = m.cno;
72 	return (0);
73 }
74