1 /* $NetBSD: v_undo.c,v 1.2 2013/11/22 15:52:06 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 #ifndef lint 14 static const char sccsid[] = "Id: v_undo.c,v 10.6 2001/06/25 15:19:36 skimo Exp (Berkeley) Date: 2001/06/25 15:19:36 "; 15 #endif /* not lint */ 16 17 #include <sys/types.h> 18 #include <sys/queue.h> 19 #include <sys/time.h> 20 21 #include <bitstring.h> 22 #include <errno.h> 23 #include <limits.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include "../common/common.h" 29 #include "vi.h" 30 31 /* 32 * v_Undo -- U 33 * Undo changes to this line. 34 * 35 * PUBLIC: int v_Undo __P((SCR *, VICMD *)); 36 */ 37 int 38 v_Undo(SCR *sp, VICMD *vp) 39 { 40 /* 41 * Historically, U reset the cursor to the first column in the line 42 * (not the first non-blank). This seems a bit non-intuitive, but, 43 * considering that we may have undone multiple changes, anything 44 * else (including the cursor position stored in the logging records) 45 * is going to appear random. 46 */ 47 vp->m_final.cno = 0; 48 49 /* 50 * !!! 51 * Set up the flags so that an immediately subsequent 'u' will roll 52 * forward, instead of backward. In historic vi, a 'u' following a 53 * 'U' redid all of the changes to the line. Given that the user has 54 * explicitly discarded those changes by entering 'U', it seems likely 55 * that the user wants something between the original and end forms of 56 * the line, so starting to replay the changes seems the best way to 57 * get to there. 58 */ 59 F_SET(sp->ep, F_UNDO); 60 sp->ep->lundo = BACKWARD; 61 62 return (log_setline(sp)); 63 } 64 65 /* 66 * v_undo -- u 67 * Undo the last change. 68 * 69 * PUBLIC: int v_undo __P((SCR *, VICMD *)); 70 */ 71 int 72 v_undo(SCR *sp, VICMD *vp) 73 { 74 EXF *ep; 75 76 /* Set the command count. */ 77 VIP(sp)->u_ccnt = sp->ccnt; 78 79 /* 80 * !!! 81 * In historic vi, 'u' toggled between "undo" and "redo", i.e. 'u' 82 * undid the last undo. However, if there has been a change since 83 * the last undo/redo, we always do an undo. To make this work when 84 * the user can undo multiple operations, we leave the old semantic 85 * unchanged, but make '.' after a 'u' do another undo/redo operation. 86 * This has two problems. 87 * 88 * The first is that 'u' didn't set '.' in historic vi. So, if a 89 * user made a change, realized it was in the wrong place, does a 90 * 'u' to undo it, moves to the right place and then does '.', the 91 * change was reapplied. To make this work, we only apply the '.' 92 * to the undo command if it's the command immediately following an 93 * undo command. See vi/vi.c:getcmd() for the details. 94 * 95 * The second is that the traditional way to view the numbered cut 96 * buffers in vi was to enter the commands "1pu.u.u.u. which will 97 * no longer work because the '.' immediately follows the 'u' command. 98 * Since we provide a much better method of viewing buffers, and 99 * nobody can think of a better way of adding in multiple undo, this 100 * remains broken. 101 * 102 * !!! 103 * There is change to historic practice for the final cursor position 104 * in this implementation. In historic vi, if an undo was isolated to 105 * a single line, the cursor moved to the start of the change, and 106 * then, subsequent 'u' commands would not move it again. (It has been 107 * pointed out that users used multiple undo commands to get the cursor 108 * to the start of the changed text.) Nvi toggles between the cursor 109 * position before and after the change was made. One final issue is 110 * that historic vi only did this if the user had not moved off of the 111 * line before entering the undo command; otherwise, vi would move the 112 * cursor to the most attractive position on the changed line. 113 * 114 * It would be difficult to match historic practice in this area. You 115 * not only have to know that the changes were isolated to one line, 116 * but whether it was the first or second undo command as well. And, 117 * to completely match historic practice, we'd have to track users line 118 * changes, too. This isn't worth the effort. 119 */ 120 ep = sp->ep; 121 if (!F_ISSET(ep, F_UNDO)) { 122 F_SET(ep, F_UNDO); 123 ep->lundo = BACKWARD; 124 } else if (!F_ISSET(vp, VC_ISDOT)) 125 ep->lundo = ep->lundo == BACKWARD ? FORWARD : BACKWARD; 126 127 switch (ep->lundo) { 128 case BACKWARD: 129 return (log_backward(sp, &vp->m_final)); 130 case FORWARD: 131 return (log_forward(sp, &vp->m_final)); 132 default: 133 abort(); 134 } 135 /* NOTREACHED */ 136 } 137