diff options
author | Vincent Labrecque <vincent@cvs.openbsd.org> | 2002-02-26 00:45:46 +0000 |
---|---|---|
committer | Vincent Labrecque <vincent@cvs.openbsd.org> | 2002-02-26 00:45:46 +0000 |
commit | 2ace209c9029dcccdc73c46e40e0d848fd1f037a (patch) | |
tree | f1cab78fc4fe1db82206f5c29f6bf6e4ba680086 /usr.bin | |
parent | 8a01f656c9da504eda625918b5c5ecc532d1f5cf (diff) |
keep undo records in the BUFFER structures insteda of having a huge list.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/mg/buffer.c | 8 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 9 | ||||
-rw-r--r-- | usr.bin/mg/undo.c | 44 |
3 files changed, 24 insertions, 37 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c index f1500f6a28a..fecc33fab11 100644 --- a/usr.bin/mg/buffer.c +++ b/usr.bin/mg/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.23 2002/02/21 00:04:10 dhartmei Exp $ */ +/* $OpenBSD: buffer.c,v 1.24 2002/02/26 00:45:45 vincent Exp $ */ /* * Buffer handling. @@ -99,6 +99,7 @@ killbuffer(f, n) MGWIN *wp; int s; char bufn[NBUFN]; + struct undo_rec *rec; if ((s = eread("Kill buffer: (default %s) ", bufn, NBUFN, EFNEW | EFBUF, curbp->b_bname)) == ABORT) @@ -157,6 +158,10 @@ killbuffer(f, n) bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb; bp1 = bp1->b_bufp; } + while ((rec = LIST_FIRST(&bp->b_undo)) != NULL) { + free_undo_record(rec); + LIST_REMOVE(rec, next); + } free(bp->b_bname); /* Release name block */ free(bp); /* Release buffer block */ return TRUE; @@ -450,6 +455,7 @@ bfind(bname, cflag) bp->b_nwnd = 0; bp->b_linep = lp; bp->b_nmodes = defb_nmodes; + LIST_INIT(&bp->b_undo); i = 0; do { bp->b_modes[i] = defb_modes[i]; diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index 177c6d16bb5..3f8f8895680 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.34 2002/02/21 15:27:29 deraadt Exp $ */ +/* $OpenBSD: def.h,v 1.35 2002/02/26 00:45:45 vincent Exp $ */ #include <sys/queue.h> @@ -206,6 +206,8 @@ typedef struct MGWIN { #define WFHARD 0x08 /* Better to a full display. */ #define WFMODE 0x10 /* Update mode line. */ +struct undo_rec; + /* * Text is kept in buffers. A buffer header, described * below, exists for every buffer in the system. The buffers are @@ -230,6 +232,7 @@ typedef struct BUFFER { char b_flag; /* Flags */ char b_fname[NFILEN];/* File name */ struct fileinfo b_fi; /* File attributes */ + LIST_HEAD(, undo_rec) b_undo; /* Undo actions list */ } BUFFER; #define b_bufp b_list.l_p.x_bp #define b_bname b_list.l_name @@ -258,7 +261,6 @@ typedef struct { */ struct undo_rec { LIST_ENTRY(undo_rec) next; - BUFFER *buf; enum { INSERT = 1, DELETE, @@ -270,8 +272,6 @@ struct undo_rec { int size; char *content; }; - -LIST_HEAD(undo_list, undo_rec); /* * Prototypes. @@ -572,6 +572,7 @@ int cntnonmatchlines(int, int); #endif /* REGEX */ /* undo.c X */ +void free_undo_record(struct undo_rec *); int undo_init(void); int undo_enable(int); int undo_add_custom(int, LINE *, int, void *, int); diff --git a/usr.bin/mg/undo.c b/usr.bin/mg/undo.c index 8617b698d3d..5bfecff4e89 100644 --- a/usr.bin/mg/undo.c +++ b/usr.bin/mg/undo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: undo.c,v 1.6 2002/02/21 17:36:12 vincent Exp $ */ +/* $OpenBSD: undo.c,v 1.7 2002/02/26 00:45:45 vincent Exp $ */ /* * Copyright (c) 2002 Vincent Labrecque <vincent@openbsd.org> * All rights reserved. @@ -29,16 +29,13 @@ #include <sys/queue.h> -#define MAX_LIST_RECORDS 32 #define MAX_FREE_RECORDS 32 /* * Local variables */ -static struct undo_list undo_list; -static int undo_list_num; -static struct undo_list undo_free; -static int undo_free_num; +static LIST_HEAD(, undo_rec) undo_free; +static int undo_free_num; /* * Global variables @@ -59,7 +56,6 @@ int undoaction; /* Are we called indirectly from undo()? */ static int find_offset(LINE *, int); static int find_linep(int, LINE **, int *); static struct undo_rec *new_undo_record(void); -static void free_undo_record(struct undo_rec *); static int drop_oldest_undo_record(void); static int @@ -108,11 +104,6 @@ new_undo_record(void) { struct undo_rec *rec; - while (undo_list_num >= MAX_LIST_RECORDS) { - drop_oldest_undo_record(); - undo_list_num--; - } - undo_list_num++; rec = LIST_FIRST(&undo_free); if (rec != NULL) LIST_REMOVE(rec, next); /* Remove it from the free-list */ @@ -125,7 +116,7 @@ new_undo_record(void) return rec; } -static void +void free_undo_record(struct undo_rec *rec) { if (rec->content != NULL) { @@ -165,7 +156,6 @@ int undo_init(void) { LIST_INIT(&undo_free); - LIST_INIT(&undo_list); return TRUE; } @@ -195,14 +185,13 @@ undo_add_custom(int type, LINE *lp, int offset, void *content, int size) return TRUE; rec = new_undo_record(); rec->pos = find_offset(lp, offset); - rec->buf = curbp; rec->type = type; rec->content = content; rec->region.r_linep = lp; rec->region.r_offset = offset; rec->region.r_size = size; - LIST_INSERT_HEAD(&undo_list, rec, next); + LIST_INSERT_HEAD(&curbp->b_undo, rec, next); return TRUE; } @@ -216,10 +205,9 @@ undo_add_boundary(void) return TRUE; rec = new_undo_record(); - rec->buf = curbp; rec->type = BOUNDARY; - LIST_INSERT_HEAD(&undo_list, rec, next); + LIST_INSERT_HEAD(&curbp->b_undo, rec, next); return TRUE; } @@ -240,10 +228,9 @@ undo_add_insert(LINE *lp, int offset, int size) /* * We try to reuse the last undo record to `compress' things. */ - rec = LIST_FIRST(&undo_list); + rec = LIST_FIRST(&curbp->b_undo); if ((rec != NULL) && (rec->type == INSERT) && - (rec->buf == curbp) && (rec->region.r_linep == lp)) { int dist; @@ -260,11 +247,10 @@ undo_add_insert(LINE *lp, int offset, int size) */ rec = new_undo_record(); rec->pos = find_offset(lp, offset); - rec->buf = curbp; rec->type = INSERT; memmove(&rec->region, ®, sizeof(REGION)); rec->content = NULL; - LIST_INSERT_HEAD(&undo_list, rec, next); + LIST_INSERT_HEAD(&curbp->b_undo, rec, next); return TRUE; } @@ -295,11 +281,10 @@ undo_add_delete(LINE *lp, int offset, int size) /* * Again, try to reuse last undo record, if we can */ - rec = LIST_FIRST(&undo_list); + rec = LIST_FIRST(&curbp->b_undo); if (!skip && (rec != NULL) && (rec->type == DELETE) && - (rec->buf == curbp) && (rec->region.r_linep == reg.r_linep)) { char *newbuf; int newlen; @@ -340,7 +325,6 @@ undo_add_delete(LINE *lp, int offset, int size) rec = new_undo_record(); rec->pos = pos; - rec->buf = curbp; rec->type = DELETE; memmove(&rec->region, ®, sizeof(REGION)); do { @@ -352,7 +336,7 @@ undo_add_delete(LINE *lp, int offset, int size) region_get_data(®, rec->content, reg.r_size); - LIST_INSERT_HEAD(&undo_list, rec, next); + LIST_INSERT_HEAD(&curbp->b_undo, rec, next); return TRUE; } @@ -376,7 +360,6 @@ undo_add_change(LINE *lp, int offset, int size) rec = new_undo_record(); rec->pos = find_offset(lp, offset); - rec->buf = curbp; rec->type = CHANGE; memmove(&rec->region, ®, sizeof reg); @@ -389,7 +372,7 @@ undo_add_change(LINE *lp, int offset, int size) region_get_data(®, rec->content, size); - LIST_INSERT_HEAD(&undo_list, rec, next); + LIST_INSERT_HEAD(&curbp->b_undo, rec, next); return TRUE; } @@ -409,14 +392,11 @@ undo(int f, int n) undoaction++; while (n > 0) { - rec = LIST_FIRST(&undo_list); + rec = LIST_FIRST(&curbp->b_undo); if (rec == NULL) { ewprintf("Nothing to undo!"); return FALSE; } - if (rec->buf != curbp) - popbuf(rec->buf); - LIST_REMOVE(rec, next); if (rec->type == BOUNDARY) { continue; |