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