diff options
author | Vincent Labrecque <vincent@cvs.openbsd.org> | 2003-10-21 22:48:08 +0000 |
---|---|---|
committer | Vincent Labrecque <vincent@cvs.openbsd.org> | 2003-10-21 22:48:08 +0000 |
commit | dd71fb5736254f2ce55f82746e8bb3d536f035fd (patch) | |
tree | d2f2c3ac88ca665a83b900ddd230e8e78c1935d6 /usr.bin | |
parent | 153ebf77723e989776c848774d7fb0010d25d51e (diff) |
make undo records per MGWIN, not per BUFFER...
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/mg/buffer.c | 12 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 36 | ||||
-rw-r--r-- | usr.bin/mg/undo.c | 34 | ||||
-rw-r--r-- | usr.bin/mg/window.c | 74 |
4 files changed, 92 insertions, 64 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c index 54ff40af09e..5ce140ffbfd 100644 --- a/usr.bin/mg/buffer.c +++ b/usr.bin/mg/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.32 2003/08/15 23:23:18 vincent Exp $ */ +/* $OpenBSD: buffer.c,v 1.33 2003/10/21 22:48:07 vincent Exp $ */ /* * Buffer handling. @@ -112,7 +112,6 @@ killbuffer(int f, int n) MGWIN *wp; int s; char bufn[NBUFN]; - struct undo_rec *rec, *next; if ((s = eread("Kill buffer: (default %s) ", bufn, NBUFN, EFNEW | EFBUF, curbp->b_bname)) == ABORT) @@ -171,12 +170,6 @@ killbuffer(int f, int n) bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb; bp1 = bp1->b_bufp; } - rec = LIST_FIRST(&bp->b_undo); - while (rec != NULL) { - next = LIST_NEXT(rec, next); - free_undo_record(rec); - rec = next; - } free((char *)bp->b_bname); /* Release name block */ free(bp); /* Release buffer block */ return TRUE; @@ -458,9 +451,6 @@ bfind(const char *bname, int cflag) bp->b_nwnd = 0; bp->b_linep = lp; bp->b_nmodes = defb_nmodes; - LIST_INIT(&bp->b_undo); - bp->b_undoptr = NULL; - memset(&bp->b_undopos, 0, sizeof bp->b_undopos); 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 1f1815b24a2..6bf7e5059b0 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.48 2003/08/15 23:23:18 vincent Exp $ */ +/* $OpenBSD: def.h,v 1.49 2003/10/21 22:48:07 vincent Exp $ */ #include <sys/queue.h> @@ -106,6 +106,20 @@ typedef int (*PF)(int, int); /* generally useful type */ #define KFORW 1 #define KBACK 2 + +/* + * This structure holds the starting position + * (as a line/offset pair) and the number of characters in a + * region of a buffer. This makes passing the specification + * of a region around a little bit easier. + */ +typedef struct { + struct LINE *r_linep; /* Origin LINE address. */ + int r_offset; /* Origin LINE offset. */ + RSIZE r_size; /* Length in characters. */ +} REGION; + + /* * All text is kept in circularly linked * lists of "LINE" structures. These begin at the @@ -187,6 +201,10 @@ typedef struct MGWIN { char w_ntrows; /* # of rows of text in window */ char w_force; /* If NZ, forcing row. */ char w_flag; /* Flags. */ + LIST_HEAD(, undo_rec) w_undo; /* Undo actions list */ + REGION w_undopos; /* Where we were during the last + undo action */ + struct undo_rec *w_undoptr; } MGWIN; #define w_wndp w_list.l_p.l_wp #define w_name w_list.l_name @@ -209,17 +227,6 @@ typedef struct MGWIN { struct undo_rec; /* - * This structure holds the starting position - * (as a line/offset pair) and the number of characters in a - * region of a buffer. This makes passing the specification - * of a region around a little bit easier. - */ -typedef struct { - struct LINE *r_linep; /* Origin LINE address. */ - int r_offset; /* Origin LINE offset. */ - RSIZE r_size; /* Length in characters. */ -} REGION; -/* * Text is kept in buffers. A buffer header, described * below, exists for every buffer in the system. The buffers are * kept in a big list, so that commands that search for a buffer by @@ -243,12 +250,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 */ - REGION b_undopos; /* Where we were during the last - undo action */ - struct undo_rec *b_undoptr; } BUFFER; - #define b_bufp b_list.l_p.x_bp #define b_bname b_list.l_name diff --git a/usr.bin/mg/undo.c b/usr.bin/mg/undo.c index 5d10f21f19b..7768a720205 100644 --- a/usr.bin/mg/undo.c +++ b/usr.bin/mg/undo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: undo.c,v 1.18 2003/06/26 23:04:10 vincent Exp $ */ +/* $OpenBSD: undo.c,v 1.19 2003/10/21 22:48:07 vincent Exp $ */ /* * Copyright (c) 2002 Vincent Labrecque * All rights reserved. @@ -158,7 +158,7 @@ drop_oldest_undo_record(void) { struct undo_rec *rec; - rec = LIST_END(&curbp->b_undo); + rec = LIST_END(&curwp->w_undo); if (rec != NULL) { undo_free_num--; LIST_REMOVE(rec, next); @@ -173,7 +173,7 @@ last_was_boundary(void) { struct undo_rec *rec; - if ((rec = LIST_FIRST(&curbp->b_undo)) != NULL && + if ((rec = LIST_FIRST(&curwp->w_undo)) != NULL && (rec->type == BOUNDARY)) return (1); return (0); @@ -198,7 +198,7 @@ undo_add_boundary(void) rec = new_undo_record(); rec->type = BOUNDARY; - LIST_INSERT_HEAD(&curbp->b_undo, rec, next); + LIST_INSERT_HEAD(&curwp->w_undo, rec, next); return (TRUE); } @@ -228,7 +228,7 @@ undo_add_custom(int asocial, if (!last_was_boundary()) undo_add_boundary(); - LIST_INSERT_HEAD(&curbp->b_undo, rec, next); + LIST_INSERT_HEAD(&curwp->w_undo, rec, next); undo_add_boundary(); if (asocial) /* Add a second one */ undo_add_boundary(); @@ -254,7 +254,7 @@ undo_add_insert(LINE *lp, int offset, int size) /* * We try to reuse the last undo record to `compress' things. */ - rec = LIST_FIRST(&curbp->b_undo); + rec = LIST_FIRST(&curwp->w_undo); if (rec != NULL) { /* this will be hit like, 80% of the time... */ if (rec->type == BOUNDARY) @@ -279,7 +279,7 @@ undo_add_insert(LINE *lp, int offset, int size) if (!last_was_boundary()) undo_add_boundary(); - LIST_INSERT_HEAD(&curbp->b_undo, rec, next); + LIST_INSERT_HEAD(&curwp->w_undo, rec, next); undo_add_boundary(); return (TRUE); @@ -306,7 +306,7 @@ undo_add_delete(LINE *lp, int offset, int size) if (offset == llength(lp)) /* if it's a newline... */ undo_add_boundary(); - else if ((rec = LIST_FIRST(&curbp->b_undo)) != NULL) { + else if ((rec = LIST_FIRST(&curwp->w_undo)) != NULL) { /* * Separate this command from the previous one if we're not * just before the previous record... @@ -331,7 +331,7 @@ undo_add_delete(LINE *lp, int offset, int size) region_get_data(®, rec->content, reg.r_size); - LIST_INSERT_HEAD(&curbp->b_undo, rec, next); + LIST_INSERT_HEAD(&curwp->w_undo, rec, next); undo_add_boundary(); return (TRUE); @@ -384,7 +384,7 @@ undo_dump(int f, int n) } num = 0; - for (rec = LIST_FIRST(&curbp->b_undo); rec != NULL; + for (rec = LIST_FIRST(&curwp->w_undo); rec != NULL; rec = LIST_NEXT(rec, next)) { num++; snprintf(buf, sizeof buf, @@ -451,13 +451,13 @@ undo(int f, int n) LINE *lp; int offset; - ptr = curbp->b_undoptr; + ptr = curwp->w_undoptr; /* if we moved, make ptr point back to the top of the list */ - if ((curbp->b_undopos.r_linep != curwp->w_dotp) || - (curbp->b_undopos.r_offset != curwp->w_doto) || + if ((curwp->w_undopos.r_linep != curwp->w_dotp) || + (curwp->w_undopos.r_offset != curwp->w_doto) || (ptr == NULL)) - ptr = LIST_FIRST(&curbp->b_undo); + ptr = LIST_FIRST(&curwp->w_undo); rval = TRUE; while (n--) { @@ -536,9 +536,9 @@ undo(int f, int n) * Record where we are. (we have to save our new position at the end * since we change the dot when undoing....) */ - curbp->b_undoptr = ptr; - curbp->b_undopos.r_linep = curwp->w_dotp; - curbp->b_undopos.r_offset = curwp->w_doto; + curwp->w_undoptr = ptr; + curwp->w_undopos.r_linep = curwp->w_dotp; + curwp->w_undopos.r_offset = curwp->w_doto; return (rval); } diff --git a/usr.bin/mg/window.c b/usr.bin/mg/window.c index 3240ece1405..20fcf71113b 100644 --- a/usr.bin/mg/window.c +++ b/usr.bin/mg/window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window.c,v 1.11 2003/05/20 03:08:55 cloder Exp $ */ +/* $OpenBSD: window.c,v 1.12 2003/10/21 22:48:07 vincent Exp $ */ /* * Window handling. @@ -6,6 +6,44 @@ #include "def.h" +MGWIN * +new_window(BUFFER *bp) +{ + MGWIN *wp; + + wp = malloc(sizeof(MGWIN)); + if (wp == NULL) + return (NULL); + + wp->w_bufp = bp; + wp->w_dotp = NULL; + wp->w_doto = 0; + wp->w_markp = NULL; + wp->w_marko = 0; + wp->w_flag = 0; + wp->w_force = 0; + bp->b_nwnd++; + LIST_INIT(&wp->w_undo); + wp->w_undoptr = NULL; + memset(&wp->w_undopos, 0, sizeof wp->w_undopos); + + return (wp); +} + +void +free_window(MGWIN *wp) +{ + struct undo_rec *rec, *next; + + rec = LIST_FIRST(&wp->w_undo); + while (rec != NULL) { + next = LIST_NEXT(rec, next); + free_undo_record(rec); + rec = next; + } + free(wp); +} + /* * Reposition dot in the current window to line "n". If the argument is * positive, it is that line. If it is negative it is that line from the @@ -135,7 +173,7 @@ onlywind(int f, int n) wp->w_bufp->b_markp = wp->w_markp; wp->w_bufp->b_marko = wp->w_marko; } - free((char *)wp); + free_window(wp); } while (curwp->w_wndp != NULL) { wp = curwp->w_wndp; @@ -146,7 +184,7 @@ onlywind(int f, int n) wp->w_bufp->b_markp = wp->w_markp; wp->w_bufp->b_marko = wp->w_marko; } - free((char *)wp); + free_window(wp); } lp = curwp->w_linep; i = curwp->w_toprow; @@ -180,28 +218,26 @@ splitwind(int f, int n) ewprintf("Cannot split a %d line window", curwp->w_ntrows); return (FALSE); } - if ((wp = malloc(sizeof(MGWIN))) == NULL) { - ewprintf("Can't get %d", sizeof(MGWIN)); + wp = new_window(curbp); + if (wp == NULL) { + ewprintf("Unable to create a window"); return (FALSE); } - /* displayed twice */ - ++curbp->b_nwnd; - wp->w_bufp = curbp; + /* use the current dot and mark */ wp->w_dotp = curwp->w_dotp; wp->w_doto = curwp->w_doto; wp->w_markp = curwp->w_markp; wp->w_marko = curwp->w_marko; - wp->w_flag = 0; - wp->w_force = 0; - ntru = (curwp->w_ntrows - 1) / 2; /* Upper size */ - ntrl = (curwp->w_ntrows - 1) - ntru; /* Lower size */ - lp = curwp->w_linep; - ntrd = 0; - while (lp != curwp->w_dotp) { - ++ntrd; - lp = lforw(lp); - } + + /* figure out which half of the screen we're in */ + ntru = (curwp->w_ntrows - 1) / 2; /* Upper size */ + ntrl = (curwp->w_ntrows - 1) - ntru; /* Lower size */ + + for (lp = curwp->w_linep, ntrd = 0; lp != curwp->w_dotp; + lp = lforw(lp)) + ntrd++; + lp = curwp->w_linep; /* old is upper window */ @@ -385,7 +421,7 @@ delwind(int f, int n) nwp->w_wndp = wp->w_wndp; break; } - free((char *)wp); + free_window(wp); return TRUE; } |