1 1.8 nia /* $NetBSD: undo.c,v 1.8 2021/11/02 08:04:20 nia Exp $ */ 2 1.2 cgd 3 1.1 alm /* undo.c: This file contains the undo routines for the ed line editor */ 4 1.1 alm /*- 5 1.1 alm * Copyright (c) 1993 Andrew Moore, Talke Studio. 6 1.1 alm * All rights reserved. 7 1.1 alm * 8 1.1 alm * Redistribution and use in source and binary forms, with or without 9 1.1 alm * modification, are permitted provided that the following conditions 10 1.1 alm * are met: 11 1.1 alm * 1. Redistributions of source code must retain the above copyright 12 1.1 alm * notice, this list of conditions and the following disclaimer. 13 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 alm * notice, this list of conditions and the following disclaimer in the 15 1.1 alm * documentation and/or other materials provided with the distribution. 16 1.1 alm * 17 1.1 alm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 1.1 alm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 1.1 alm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 1.1 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 1.1 alm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 1.1 alm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 1.1 alm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 1.1 alm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 1.1 alm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 1.1 alm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 1.1 alm * SUCH DAMAGE. 28 1.1 alm */ 29 1.1 alm 30 1.3 thorpej #include <sys/cdefs.h> 31 1.1 alm #ifndef lint 32 1.2 cgd #if 0 33 1.1 alm static char *rcsid = "@(#)undo.c,v 1.1 1994/02/01 00:34:44 alm Exp"; 34 1.2 cgd #else 35 1.8 nia __RCSID("$NetBSD: undo.c,v 1.8 2021/11/02 08:04:20 nia Exp $"); 36 1.2 cgd #endif 37 1.1 alm #endif /* not lint */ 38 1.1 alm 39 1.1 alm #include "ed.h" 40 1.1 alm 41 1.1 alm 42 1.1 alm #define USIZE 100 /* undo stack size */ 43 1.1 alm undo_t *ustack = NULL; /* undo stack */ 44 1.1 alm long usize = 0; /* stack size variable */ 45 1.1 alm long u_p = 0; /* undo stack pointer */ 46 1.1 alm 47 1.5 msaitoh /* push_undo_stack: return pointer to initialized undo node */ 48 1.1 alm undo_t * 49 1.4 xtraeme push_undo_stack(int type, long from, long to) 50 1.1 alm { 51 1.8 nia int err; 52 1.1 alm 53 1.1 alm if (u_p < usize || 54 1.8 nia (err = reallocarr(&ustack, usize += USIZE, sizeof(undo_t))) == 0) { 55 1.1 alm ustack[u_p].type = type; 56 1.1 alm ustack[u_p].t = get_addressed_line_node(to); 57 1.1 alm ustack[u_p].h = get_addressed_line_node(from); 58 1.1 alm return ustack + u_p++; 59 1.1 alm } 60 1.1 alm /* out of memory - release undo stack */ 61 1.8 nia fprintf(stderr, "%s\n", strerror(err)); 62 1.6 dholland seterrmsg("out of memory"); 63 1.1 alm clear_undo_stack(); 64 1.1 alm free(ustack); 65 1.1 alm ustack = NULL; 66 1.1 alm usize = 0; 67 1.1 alm return NULL; 68 1.1 alm } 69 1.1 alm 70 1.1 alm 71 1.1 alm /* USWAP: swap undo nodes */ 72 1.1 alm #define USWAP(x,y) { \ 73 1.1 alm undo_t utmp; \ 74 1.1 alm utmp = x, x = y, y = utmp; \ 75 1.1 alm } 76 1.1 alm 77 1.1 alm 78 1.1 alm long u_current_addr = -1; /* if >= 0, undo enabled */ 79 1.1 alm long u_addr_last = -1; /* if >= 0, undo enabled */ 80 1.1 alm 81 1.1 alm /* pop_undo_stack: undo last change to the editor buffer */ 82 1.1 alm int 83 1.4 xtraeme pop_undo_stack(void) 84 1.1 alm { 85 1.1 alm long n; 86 1.1 alm long o_current_addr = current_addr; 87 1.1 alm long o_addr_last = addr_last; 88 1.1 alm 89 1.1 alm if (u_current_addr == -1 || u_addr_last == -1) { 90 1.6 dholland seterrmsg("nothing to undo"); 91 1.1 alm return ERR; 92 1.1 alm } else if (u_p) 93 1.1 alm modified = 1; 94 1.1 alm get_addressed_line_node(0); /* this get_addressed_line_node last! */ 95 1.1 alm SPL1(); 96 1.1 alm for (n = u_p; n-- > 0;) { 97 1.1 alm switch(ustack[n].type) { 98 1.1 alm case UADD: 99 1.1 alm REQUE(ustack[n].h->q_back, ustack[n].t->q_forw); 100 1.1 alm break; 101 1.1 alm case UDEL: 102 1.1 alm REQUE(ustack[n].h->q_back, ustack[n].h); 103 1.1 alm REQUE(ustack[n].t, ustack[n].t->q_forw); 104 1.1 alm break; 105 1.1 alm case UMOV: 106 1.1 alm case VMOV: 107 1.1 alm REQUE(ustack[n - 1].h, ustack[n].h->q_forw); 108 1.1 alm REQUE(ustack[n].t->q_back, ustack[n - 1].t); 109 1.1 alm REQUE(ustack[n].h, ustack[n].t); 110 1.1 alm n--; 111 1.1 alm break; 112 1.1 alm default: 113 1.1 alm /*NOTREACHED*/ 114 1.1 alm ; 115 1.1 alm } 116 1.1 alm ustack[n].type ^= 1; 117 1.1 alm } 118 1.1 alm /* reverse undo stack order */ 119 1.1 alm for (n = u_p; n-- > (u_p + 1)/ 2;) 120 1.1 alm USWAP(ustack[n], ustack[u_p - 1 - n]); 121 1.1 alm if (isglobal) 122 1.1 alm clear_active_list(); 123 1.1 alm current_addr = u_current_addr, u_current_addr = o_current_addr; 124 1.1 alm addr_last = u_addr_last, u_addr_last = o_addr_last; 125 1.1 alm SPL0(); 126 1.1 alm return 0; 127 1.1 alm } 128 1.1 alm 129 1.1 alm 130 1.1 alm /* clear_undo_stack: clear the undo stack */ 131 1.1 alm void 132 1.4 xtraeme clear_undo_stack(void) 133 1.1 alm { 134 1.1 alm line_t *lp, *ep, *tl; 135 1.1 alm 136 1.1 alm while (u_p--) 137 1.1 alm if (ustack[u_p].type == UDEL) { 138 1.1 alm ep = ustack[u_p].t->q_forw; 139 1.1 alm for (lp = ustack[u_p].h; lp != ep; lp = tl) { 140 1.1 alm unmark_line_node(lp); 141 1.1 alm tl = lp->q_forw; 142 1.1 alm free(lp); 143 1.1 alm } 144 1.1 alm } 145 1.1 alm u_p = 0; 146 1.1 alm u_current_addr = current_addr; 147 1.1 alm u_addr_last = addr_last; 148 1.1 alm } 149