diff options
author | Kjell Wooding <kjell@cvs.openbsd.org> | 2008-09-15 16:13:36 +0000 |
---|---|---|
committer | Kjell Wooding <kjell@cvs.openbsd.org> | 2008-09-15 16:13:36 +0000 |
commit | 46afcbd52cae880b9a7eb20df81241e1f99a64cb (patch) | |
tree | e7f8293c9d96e318bdccb005f328c5824f50f1d8 | |
parent | 0b98546c7b94e0858d8daad1b96091ba91734f4b (diff) |
Expose the undo commands as proper mg functions.
This should have no functional change on undo, but it does facilitate
testing undo behavior.
-rw-r--r-- | usr.bin/mg/cmode.c | 8 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 8 | ||||
-rw-r--r-- | usr.bin/mg/file.c | 12 | ||||
-rw-r--r-- | usr.bin/mg/funmap.c | 5 | ||||
-rw-r--r-- | usr.bin/mg/line.c | 16 | ||||
-rw-r--r-- | usr.bin/mg/mg.1 | 13 | ||||
-rw-r--r-- | usr.bin/mg/paragraph.c | 8 | ||||
-rw-r--r-- | usr.bin/mg/random.c | 8 | ||||
-rw-r--r-- | usr.bin/mg/undo.c | 79 | ||||
-rw-r--r-- | usr.bin/mg/yank.c | 8 |
10 files changed, 94 insertions, 71 deletions
diff --git a/usr.bin/mg/cmode.c b/usr.bin/mg/cmode.c index e728a8eccf2..ed9598011f1 100644 --- a/usr.bin/mg/cmode.c +++ b/usr.bin/mg/cmode.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmode.c,v 1.4 2008/06/14 08:39:30 kjell Exp $ */ +/* $OpenBSD: cmode.c,v 1.5 2008/09/15 16:13:35 kjell Exp $ */ /* * This file is in the public domain. * @@ -164,8 +164,7 @@ cc_indent(int f, int n) if (n < 0) return (FALSE); - undo_add_boundary(); - undo_boundary_enable(FALSE); + undo_boundary_enable(FFRAND, 0); if (cc_strip_trailp) deltrailwhite(FFRAND, 1); @@ -188,8 +187,7 @@ cc_indent(int f, int n) else ret = indent(FFOTHARG, pi + ci); - undo_boundary_enable(TRUE); - undo_add_boundary(); + undo_boundary_enable(FFRAND, 1); return (ret); } diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index 542a07c084e..aee87b081bc 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.107 2008/09/15 16:11:35 kjell Exp $ */ +/* $OpenBSD: def.h,v 1.108 2008/09/15 16:13:35 kjell Exp $ */ /* This file is in the public domain. */ @@ -609,12 +609,12 @@ int cntnonmatchlines(int, int); void free_undo_record(struct undo_rec *); int undo_dump(int, int); int undo_enabled(void); -int undo_enable(int); -void undo_add_boundary(void); +int undo_enable(int, int); +int undo_add_boundary(int, int); void undo_add_modified(void); int undo_add_insert(struct line *, int, int); int undo_add_delete(struct line *, int, int); -void undo_boundary_enable(int); +int undo_boundary_enable(int, int); int undo_add_change(struct line *, int, int); int undo(int, int); diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c index 6f3f87f7d78..9c5aac36b9b 100644 --- a/usr.bin/mg/file.c +++ b/usr.bin/mg/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.68 2008/09/15 16:11:35 kjell Exp $ */ +/* $OpenBSD: file.c,v 1.69 2008/09/15 16:13:35 kjell Exp $ */ /* This file is in the public domain. */ @@ -293,7 +293,7 @@ insertfile(char *fname, char *newname, int replacebuf) char *dp; if (replacebuf == TRUE) - x = undo_enable(FALSE); + x = undo_enable(FFRAND, 0); else x = undo_enabled(); @@ -335,7 +335,7 @@ insertfile(char *fname, char *newname, int replacebuf) killbuffer(bp); if ((bp = dired_(fname)) == NULL) return (FALSE); - undo_enable(x); + undo_enable(FFRAND, x); curbp = bp; return (showbuffer(bp, curwp, WFFULL | WFMODE)); } else { @@ -351,10 +351,10 @@ insertfile(char *fname, char *newname, int replacebuf) * We will delete this newline after insertion. * Disable undo, as we create the undo record manually. */ - x2 = undo_enable(FALSE); + x2 = undo_enable(FFRAND, 0); (void)lnewline(); olp = lback(curwp->w_dotp); - undo_enable(x2); + undo_enable(FFRAND, x2); nline = 0; siz = 0; @@ -477,7 +477,7 @@ out: lp2 = NULL; } bp->b_lines += nline; cleanup: - undo_enable(x); + undo_enable(FFRAND, x); /* return FALSE if error */ return (s != FIOERR); diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c index 056658b5224..d620743ebb1 100644 --- a/usr.bin/mg/funmap.c +++ b/usr.bin/mg/funmap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: funmap.c,v 1.31 2008/06/14 08:46:30 kjell Exp $ */ +/* $OpenBSD: funmap.c,v 1.32 2008/09/15 16:13:35 kjell Exp $ */ /* This file is in the public domain */ @@ -181,6 +181,9 @@ static struct funmap functnames[] = { {togglereadonly, "toggle-read-only" }, {twiddle, "transpose-chars",}, {undo, "undo", }, + {undo_enable, "undo-enable", }, + {undo_boundary_enable, "undo-boundary-toggle", }, + {undo_add_boundary, "undo-boundary", }, {undo_dump, "undo-list", }, {universal_argument, "universal-argument",}, {upperregion, "upcase-region",}, diff --git a/usr.bin/mg/line.c b/usr.bin/mg/line.c index 4d0565e0afe..840c3adf3d9 100644 --- a/usr.bin/mg/line.c +++ b/usr.bin/mg/line.c @@ -1,4 +1,4 @@ -/* $OpenBSD: line.c,v 1.45 2008/09/15 16:11:35 kjell Exp $ */ +/* $OpenBSD: line.c,v 1.46 2008/09/15 16:13:35 kjell Exp $ */ /* This file is in the public domain. */ @@ -352,9 +352,9 @@ lnewline_at(struct line *lp1, int doto) for (wp = wheadp; wp != NULL; wp = wp->w_wndp) if (wp->w_linep == lp1) wp->w_linep = lp2; - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); undo_add_insert(lp2, 0, 1); - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); return (TRUE); } @@ -382,9 +382,9 @@ lnewline_at(struct line *lp1, int doto) wp->w_marko -= doto; } } - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); undo_add_insert(lp1, llength(lp1), 1); - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); return (TRUE); } @@ -596,8 +596,7 @@ lreplace(RSIZE plen, char *st) ewprintf("Buffer is read only"); return (FALSE); } - undo_add_boundary(); - undo_boundary_enable(FALSE); + undo_boundary_enable(FFRAND, 0); (void)backchar(FFARG | FFRAND, (int)plen); (void)ldelete(plen, KNONE); @@ -606,8 +605,7 @@ lreplace(RSIZE plen, char *st) region_put_data(st, rlen); lchange(WFFULL); - undo_boundary_enable(TRUE); - undo_add_boundary(); + undo_boundary_enable(FFRAND, 1); return (TRUE); } diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1 index d7f01e7f463..9c40640420e 100644 --- a/usr.bin/mg/mg.1 +++ b/usr.bin/mg/mg.1 @@ -1,7 +1,7 @@ -.\" $OpenBSD: mg.1,v 1.40 2008/06/29 08:28:01 sobrado Exp $ +.\" $OpenBSD: mg.1,v 1.41 2008/09/15 16:13:35 kjell Exp $ .\" This file is in the public domain. .\" -.Dd $Mdocdate: June 29 2008 $ +.Dd $Mdocdate: September 15 2008 $ .Dt MG 1 .Os .Sh NAME @@ -736,6 +736,15 @@ make this work. Undo the most recent action. If invoked again without an intervening command, move the undo pointer to the previous action and undo it. +.It undo-boundary +Add an undo boundary. +This is not usually done interactively. +.It undo-boundary-toggle +Toggle whether undo boundaries are generated. +Undo boundaries are often disabled before operations that should +be considered atomically undoable. +.It undo-enable +Toggle whether undo information is kept. .It undo-list Show the undo records for the current buffer in a new buffer. .It universal-argument diff --git a/usr.bin/mg/paragraph.c b/usr.bin/mg/paragraph.c index a4378977a22..0ef46bd38d5 100644 --- a/usr.bin/mg/paragraph.c +++ b/usr.bin/mg/paragraph.c @@ -1,4 +1,4 @@ -/* $OpenBSD: paragraph.c,v 1.16 2008/06/12 21:36:47 kjell Exp $ */ +/* $OpenBSD: paragraph.c,v 1.17 2008/09/15 16:13:35 kjell Exp $ */ /* This file is in the public domain. */ @@ -130,8 +130,7 @@ fillpara(int f, int n) struct line *eopline; /* pointer to line just past EOP */ char wbuf[MAXWORD]; /* buffer for current word */ - undo_add_boundary(); - undo_boundary_enable(FALSE); + undo_boundary_enable(FFRAND, 0); /* record the pointer to the line just past the EOP */ (void)gotoeop(FFRAND, 1); @@ -237,8 +236,7 @@ fillpara(int f, int n) (void)backchar(FFRAND, 1); retval = TRUE; cleanup: - undo_boundary_enable(TRUE); - undo_add_boundary(); + undo_boundary_enable(FFRAND, 1); return (retval); } diff --git a/usr.bin/mg/random.c b/usr.bin/mg/random.c index e53a5489d4e..8a34e495e75 100644 --- a/usr.bin/mg/random.c +++ b/usr.bin/mg/random.c @@ -1,4 +1,4 @@ -/* $OpenBSD: random.c,v 1.25 2008/06/11 23:18:33 kjell Exp $ */ +/* $OpenBSD: random.c,v 1.26 2008/09/15 16:13:35 kjell Exp $ */ /* This file is in the public domain. */ @@ -119,8 +119,7 @@ twiddle(int f, int n) dotp = curwp->w_dotp; doto = curwp->w_doto; - undo_add_boundary(); - undo_boundary_enable(FALSE); + undo_boundary_enable(FFRAND, 0); if (doto == llength(dotp)) { if (--doto <= 0) return (FALSE); @@ -136,8 +135,7 @@ twiddle(int f, int n) linsert(1, cr); if (fudge != TRUE) (void)backchar(FFRAND, 1); - undo_boundary_enable(TRUE); - undo_add_boundary(); + undo_boundary_enable(FFRAND, 1); lchange(WFEDIT); return (TRUE); } diff --git a/usr.bin/mg/undo.c b/usr.bin/mg/undo.c index f4206a86a74..fe8b53bfeed 100644 --- a/usr.bin/mg/undo.c +++ b/usr.bin/mg/undo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: undo.c,v 1.46 2008/06/14 07:38:53 kjell Exp $ */ +/* $OpenBSD: undo.c,v 1.47 2008/09/15 16:13:35 kjell Exp $ */ /* * This file is in the public domain */ @@ -159,56 +159,77 @@ undo_enabled(void) } /* - * undo_enable(TRUE/FALSE) will enable / disable the undo mechanism. - * Returns TRUE if previously enabled, FALSE otherwise. + * undo_enable: toggle undo_enable. + * Returns the previous value of the flag. */ int -undo_enable(int on) +undo_enable(int f, int n) { int pon = undo_enable_flag; - undo_enable_flag = on; + if (f & (FFARG | FFRAND)) + undo_enable_flag = n > 0; + else + undo_enable_flag = !undo_enable_flag; + + if (!(f & FFRAND)) + ewprintf("Undo %sabled", undo_enable_flag ? "en" : "dis"); + return (pon); } /* * If undo is enabled, then: - * undo_boundary_enable(FALSE) stops recording undo boundaries - * between actions. - * undo_boundary_enable(TRUE) enables undo boundaries. + * Toggle undo boundary recording. + * If called with an argument, (n > 0) => enable. Otherwise disable. + * In either case, add an undo boundary * If undo is disabled, this function has no effect. */ - -void -undo_boundary_enable(int flag) +int +undo_boundary_enable(int f, int n) { - if (undo_enable_flag == TRUE) - boundary_flag = flag; + int bon = boundary_flag; + + if (!undo_enable_flag) + return (FALSE); + + undo_add_boundary(FFRAND, 1); + + if (f & (FFARG | FFRAND)) + boundary_flag = n > 0; + else + boundary_flag = !boundary_flag; + + if (!(f & FFRAND)) + ewprintf("Undo boundaries %sabled", + boundary_flag ? "en" : "dis"); + + return (bon); } /* * Record an undo boundary, unless boundary_flag == FALSE. * Does nothing if previous undo entry is already a boundary or 'modified' flag. */ -void -undo_add_boundary(void) +int +undo_add_boundary(int f, int n) { struct undo_rec *rec; int last; if (boundary_flag == FALSE) - return; + return (FALSE); last = lastrectype(); if (last == BOUNDARY || last == MODIFIED) - return; + return (TRUE); rec = new_undo_record(); rec->type = BOUNDARY; LIST_INSERT_HEAD(&curbp->b_undo, rec, next); - return; + return (TRUE); } /* @@ -234,7 +255,7 @@ undo_add_insert(struct line *lp, int offset, int size) struct undo_rec *rec; int pos; - if (undo_enable_flag == FALSE) + if (!undo_enable_flag) return (TRUE); reg.r_linep = lp; reg.r_offset = offset; @@ -262,7 +283,7 @@ undo_add_insert(struct line *lp, int offset, int size) memmove(&rec->region, ®, sizeof(struct region)); rec->content = NULL; - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); LIST_INSERT_HEAD(&curbp->b_undo, rec, next); @@ -279,7 +300,7 @@ undo_add_delete(struct line *lp, int offset, int size) struct undo_rec *rec; int pos; - if (undo_enable_flag == FALSE) + if (!undo_enable_flag) return (TRUE); reg.r_linep = lp; @@ -289,7 +310,7 @@ undo_add_delete(struct line *lp, int offset, int size) pos = find_dot(lp, offset); if (offset == llength(lp)) /* if it's a newline... */ - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); else if ((rec = LIST_FIRST(&curbp->b_undo)) != NULL) { /* * Separate this command from the previous one if we're not @@ -297,7 +318,7 @@ undo_add_delete(struct line *lp, int offset, int size) */ if (rec->type == DELETE) { if (rec->pos - rec->region.r_size != pos) - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); } } rec = new_undo_record(); @@ -315,7 +336,7 @@ undo_add_delete(struct line *lp, int offset, int size) region_get_data(®, rec->content, reg.r_size); if (lastrectype() != DELETE) - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); LIST_INSERT_HEAD(&curbp->b_undo, rec, next); @@ -328,14 +349,14 @@ undo_add_delete(struct line *lp, int offset, int size) int undo_add_change(struct line *lp, int offset, int size) { - if (undo_enable_flag == FALSE) + if (!undo_enable_flag) return (TRUE); - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); boundary_flag = FALSE; undo_add_delete(lp, offset, size); undo_add_insert(lp, offset, size); boundary_flag = TRUE; - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); return (TRUE); } @@ -484,7 +505,7 @@ undo(int f, int n) * finished the current action... */ - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); save = boundary_flag; boundary_flag = FALSE; @@ -541,7 +562,7 @@ undo(int f, int n) } while (ptr != NULL && !done); boundary_flag = save; - undo_add_boundary(); + undo_add_boundary(FFRAND, 1); ewprintf("Undo!"); } diff --git a/usr.bin/mg/yank.c b/usr.bin/mg/yank.c index 42dfa1ecfb5..c178347a72e 100644 --- a/usr.bin/mg/yank.c +++ b/usr.bin/mg/yank.c @@ -1,4 +1,4 @@ -/* $OpenBSD: yank.c,v 1.6 2006/11/19 16:51:19 deraadt Exp $ */ +/* $OpenBSD: yank.c,v 1.7 2008/09/15 16:13:35 kjell Exp $ */ /* This file is in the public domain. */ @@ -222,8 +222,7 @@ yank(int f, int n) /* newline counting */ nline = 0; - undo_add_boundary(); - undo_boundary_enable(FALSE); + undo_boundary_enable(FFRAND, 0); while (n--) { /* mark around last yank */ isetmark(); @@ -251,8 +250,7 @@ yank(int f, int n) curwp->w_linep = lp; curwp->w_flag |= WFFULL; } - undo_boundary_enable(TRUE); - undo_add_boundary(); + undo_boundary_enable(FFRAND, 1); return (TRUE); } |