Skip to content

Commit

Permalink
Re-enabled undo.
Browse files Browse the repository at this point in the history
Renamed umark to bdelmrk and moved unmark into zedit proper.
  • Loading branch information
smaclennan committed Mar 2, 2015
1 parent ecc7cf7 commit 56812b9
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 49 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CC = cc
# returns -g, else it returns the default (-O2).
D = -O2
CFLAGS += -DHAVE_MARKS -DHAVE_GLOBAL_MARKS -DUNSIGNED_BYTES
CFLAGS += -DUNDO
CFLAGS += -Wall $(D:1=-g) $(ZLIBINC) $(ASPELLINC)

MAKEFLAGS += --no-print-directory
Expand Down
4 changes: 3 additions & 1 deletion bcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ static void bfini(void)
while (Bufflist)
/* bdelbuff will update Bufflist */
cdelbuff(Bufflist);

cdelbuff(Paw);
}

static void binit(void)
Expand Down Expand Up @@ -335,7 +337,7 @@ bool cdelbuff(struct zbuff *tbuff)
if (tbuff->fname)
free(tbuff->fname);
uncomment(tbuff);
undo_clear(tbuff);
undo_clear(tbuff->buff);

bdelbuff(tbuff->buff);
free(tbuff);
Expand Down
29 changes: 25 additions & 4 deletions buff.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
#include "mark.h"
#endif

#ifdef UNDO
void undo_add(int size, bool clumped);
void undo_del(int size);
void undo_clear(struct buff *buff);
#endif

static void dummy_bsetmod(struct buff *buff) {}
void (*bsetmod)(struct buff *buff) = dummy_bsetmod;

Expand Down Expand Up @@ -85,8 +91,8 @@ bool binsert(struct buff *buff, Byte byte)
curplen(buff)++;
buff->bmodf = true;

#if UNDO
undo_add(1);
#ifdef UNDO
undo_add(1, false);
#endif

#ifdef HAVE_MARKS
Expand All @@ -113,7 +119,7 @@ void bdelete(struct buff *buff, int quantity)
if (quan < 0)
quan = 0; /* May need to switch pages */

#if UNDO
#ifdef UNDO
undo_del(quan);
#endif

Expand Down Expand Up @@ -335,7 +341,7 @@ void bempty(struct buff *buff)
}
#endif

#if UNDO
#ifdef UNDO
undo_clear(buff);
#endif
bsetmod(buff);
Expand Down Expand Up @@ -389,6 +395,9 @@ static int bappendpage(struct buff *buff, Byte *data, int size)
size -= n;
data += n;
appended += n;
#ifdef UNDO
undo_add(n, false);
#endif
}

/* Put the rest in new pages */
Expand All @@ -402,6 +411,9 @@ static int bappendpage(struct buff *buff, Byte *data, int size)
memcpy(buff->curcptr, data, n);
curplen(buff) = n;
makeoffset(buff, n);
#ifdef UNDO
undo_add(n, appended != 0);
#endif
size -= n;
data += n;
appended += n;
Expand Down Expand Up @@ -443,6 +455,9 @@ int bindata(struct buff *buff, Byte *data, int size)
buff->curcptr += size;
buff->curchar += size;
curplen(buff) += size;
#ifdef UNDO
undo_add(size, false);
#endif
return size;
}

Expand All @@ -456,6 +471,9 @@ int bindata(struct buff *buff, Byte *data, int size)
memcpy(buff->curcptr, data, n);
data += n;
size -= n;
#ifdef UNDO
undo_add(n, copied > 0);
#endif
copied += n;
buff->curcptr += n;
buff->curchar += n;
Expand All @@ -470,6 +488,9 @@ int bindata(struct buff *buff, Byte *data, int size)
memcpy(npage->pdata, data, n);
data += n;
size -= n;
#ifdef UNDO
undo_add(n, copied > 0);
#endif
copied += n;

makecur(buff, npage, n);
Expand Down
1 change: 0 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#define GPM_MOUSE 1 /* Requires libgpm */
#endif

#define UNDO 1 /* EXPERIMENTAL, but portable! */
#define SHOW_REGION 1

/* DOPIPES: Output in real time and you can continue editing.
Expand Down
2 changes: 1 addition & 1 deletion delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void Zyank(void)
}
btostart(Killbuff);
#if UNDO
undo_add(bcopyrgn(tmark, Bbuff));
undo_add(bcopyrgn(tmark, Bbuff), false);
#else
bcopyrgn(tmark, Bbuff);
#endif
Expand Down
17 changes: 16 additions & 1 deletion display.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,24 @@ static Byte tline[COLMAX + 1];
static struct mark Scrnmarks[ROWMAX + 3]; /* Screen marks - one per line */
static bool Scrnmodf[ROWMAX];

static struct mark *mhead;

/* Keeping just one mark around is a HUGE win for a trivial amount of code. */
static struct mark *freeumark;

static void (*printchar)(Byte ichar) = tprntchar;

static void mfini(void)
{
if (mhead->next)
mhead->next->prev = NULL;
else
Marklist = NULL;

while (Marklist)
unmark(Marklist);
}

void display_init(void)
{
int cnt;
Expand All @@ -59,7 +72,9 @@ void display_init(void)
Sendp = false;

/* init the mark list */
minit(Send);
Marklist = Send;
mhead = Send;
atexit(mfini);
}

/* True if user mark moved */
Expand Down
2 changes: 1 addition & 1 deletion file.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void Zrevert_file(void)
return;

/* Lose the undo history */
undo_clear(Curbuff);
undo_clear(Curbuff->buff);

offset = blocation(Bbuff);
zreadfile(Curbuff->fname);
Expand Down
27 changes: 1 addition & 26 deletions mark.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,10 @@

#ifdef HAVE_GLOBAL_MARKS
struct mark *Marklist; /* the marks list tail */
static struct mark *mhead;
#endif

int NumMarks;

#ifdef HAVE_GLOBAL_MARKS
static void mfini(void)
{
if (mhead) {
if (mhead->next)
mhead->next->prev = NULL;
else
Marklist = NULL;
}

while (Marklist)
unmark(Marklist);
}
#endif

void minit(struct mark *preallocated)
{
#ifdef HAVE_GLOBAL_MARKS
Marklist = preallocated;
mhead = preallocated;
atexit(mfini);
#endif
}

/* Create a mark at the current point and add it to the list. */
struct mark *bcremrk(struct buff *buff)
{
Expand All @@ -62,7 +37,7 @@ struct mark *bcremrk(struct buff *buff)
}

/* Free up the given mark and remove it from the list. */
void unmark(struct mark *mptr)
void bdelmark(struct mark *mptr)
{
if (mptr) {
#ifdef HAVE_GLOBAL_MARKS
Expand Down
2 changes: 1 addition & 1 deletion mark.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void minit(struct mark *preallocated);
extern int NumMarks; /* stats */

struct mark *bcremrk(struct buff *);
void unmark(struct mark *);
void bdelmark(struct mark *);

#define bisatmrk(b, m) (((b)->curpage == (m)->mpage) && ((b)->curchar == (m)->moffset))
bool bisaftermrk(struct buff *, struct mark *);
Expand Down
9 changes: 5 additions & 4 deletions proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ struct mark *zcreatemrk(void);
int zreadfile(char *fname);
bool zwritefile(char *fname);
void set_shell_mark(void);
void unmark(struct mark *);

int batoi(void);

Expand Down Expand Up @@ -282,13 +283,13 @@ void resetcomments(void);
void uncomment(struct zbuff *buff);
void cprntchar(Byte ch);

#if UNDO
#ifdef UNDO
extern unsigned long undo_total;
void undo_add(int size);
void undo_add(int size, bool clumped);
void undo_del(int size);
void undo_clear(struct zbuff *buff);
void undo_clear(struct buff *buff);
#else
#define undo_add(size)
#define undo_add(size, clumped)
#define undo_del(size)
#define undo_clear(buff)
#endif
15 changes: 9 additions & 6 deletions undo.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ static inline bool clump(void)

/* Exports */

void undo_add(int size)
void undo_add(int size, bool clumped)
{
if (no_undo(Curbuff))
return;

struct undo *undo = (struct undo *)Curbuff->undo_tail;

if (undo && is_insert(undo) && clump()) {
if (undo && is_insert(undo) && (clumped || clump())) {
/* clump with last undo */
undo->size += size;
undo->offset += size;
Expand Down Expand Up @@ -174,18 +174,21 @@ void undo_del(int size)
memcpy(undo->data, Curcptr, size);
}

void undo_clear(struct zbuff *buff)
/* Must be a struct buff since it is called from the buff.c code */
void undo_clear(struct buff *buff)
{
while (buff->undo_tail)
free_undo(&buff->undo_tail);
struct zbuff *tbuff = cfindzbuff(buff);
if (tbuff)
while (tbuff->undo_tail)
free_undo(&tbuff->undo_tail);
}

void Zundo(void)
{
struct undo *undo;
int i;

if (Curbuff->undo_tail) {
if (!Curbuff->undo_tail) {
tbell();
return;
}
Expand Down
21 changes: 18 additions & 3 deletions z.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ int main(int argc, char **argv)

display_init();

/* create the needed buffers */
delinit();

/* PAW must not be on the Bufflist */
Expand Down Expand Up @@ -553,10 +552,26 @@ void Zstats(void)
_putpaw(PawStr);
}

/* Keep around one mark */
struct mark *freemark;

struct mark *zcreatemrk(void)
{
struct mark *mrk = bcremrk(Bbuff);
if (!mrk)
struct mark *mrk = freemark;

if (mrk) {
freemark = NULL;
bmrktopnt(Bbuff, mrk);
} else if (!(mrk = bcremrk(Bbuff)))
longjmp(zenv, -1); /* ABORT */

return mrk;
}

void unmark(struct mark *mrk)
{
if (freemark)
bdelmark(mrk);
else
freemark = mrk;
}

0 comments on commit 56812b9

Please sign in to comment.