diff options
author | 2008-06-11 23:18:33 +0000 | |
---|---|---|
committer | 2008-06-11 23:18:33 +0000 | |
commit | 0e12bd5ddf594a07497916bd2b35bd2ac61dad6d (patch) | |
tree | c0c9d8d245ac4446b85c465f10784594e17e101f | |
parent | Explicit flush stdout after printing the pty name when -p is used. (diff) | |
download | wireguard-openbsd-0e12bd5ddf594a07497916bd2b35bd2ac61dad6d.tar.xz wireguard-openbsd-0e12bd5ddf594a07497916bd2b35bd2ac61dad6d.zip |
Add delete-leading-space, delete-trailing-space,
indent-current-line utility functions for stripping leading/trailing
whitespace, and setting a fixed indent respectively.
-rw-r--r-- | usr.bin/mg/def.h | 5 | ||||
-rw-r--r-- | usr.bin/mg/funmap.c | 7 | ||||
-rw-r--r-- | usr.bin/mg/keymap.c | 6 | ||||
-rw-r--r-- | usr.bin/mg/random.c | 95 | ||||
-rw-r--r-- | usr.bin/mg/util.c | 95 |
5 files changed, 192 insertions, 16 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index 808ca15948f..0aa9de7b4b2 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.101 2008/06/10 23:23:53 kjell Exp $ */ +/* $OpenBSD: def.h,v 1.102 2008/06/11 23:18:33 kjell Exp $ */ /* This file is in the public domain. */ @@ -489,6 +489,9 @@ int newline(int, int); int deblank(int, int); int justone(int, int); int delwhite(int, int); +int delleadwhite(int, int); +int deltrailwhite(int, int); +int lfindent(int, int); int indent(int, int); int forwdel(int, int); int backdel(int, int); diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c index c641c735a52..4c9e68fb461 100644 --- a/usr.bin/mg/funmap.c +++ b/usr.bin/mg/funmap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: funmap.c,v 1.29 2008/06/10 23:23:53 kjell Exp $ */ +/* $OpenBSD: funmap.c,v 1.30 2008/06/11 23:18:33 kjell Exp $ */ /* This file is in the public domain */ @@ -53,6 +53,8 @@ static struct funmap functnames[] = { {deblank, "delete-blank-lines",}, {forwdel, "delete-char",}, {delwhite, "delete-horizontal-space",}, + {delleadwhite, "delete-leading-space",}, + {deltrailwhite, "delete-trailing-space",}, #ifdef REGEX {delmatchlines, "delete-matching-lines",}, {delnonmatchlines, "delete-non-matching-lines",}, @@ -119,7 +121,8 @@ static struct funmap functnames[] = { {do_meta, "meta-key-mode",}, /* better name, anyone? */ {negative_argument, "negative-argument",}, {newline, "newline",}, - {indent, "newline-and-indent",}, + {lfindent, "newline-and-indent",}, + {indent, "indent-current-line",}, {forwline, "next-line",}, #ifdef NOTAB {notabmode, "no-tab-mode",}, diff --git a/usr.bin/mg/keymap.c b/usr.bin/mg/keymap.c index 89e73aa51e3..a1a2b879154 100644 --- a/usr.bin/mg/keymap.c +++ b/usr.bin/mg/keymap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: keymap.c,v 1.39 2006/08/18 00:22:56 kjell Exp $ */ +/* $OpenBSD: keymap.c,v 1.40 2008/06/11 23:18:33 kjell Exp $ */ /* This file is in the public domain. */ @@ -302,7 +302,7 @@ static PF fund_at[] = { /* ^I is selfinsert */ static PF fund_CJ[] = { - indent, /* ^J */ + lfindent, /* ^J */ killline, /* ^K */ reposition, /* ^L */ newline, /* ^M */ @@ -385,7 +385,7 @@ static PF indent_lf[] = { newline, /* ^J */ rescan, /* ^K */ rescan, /* ^L */ - indent /* ^M */ + lfindent /* ^M */ }; static struct KEYMAPE (1 + IMAPEXT) indntmap = { diff --git a/usr.bin/mg/random.c b/usr.bin/mg/random.c index 68e4b7f1a1e..e53a5489d4e 100644 --- a/usr.bin/mg/random.c +++ b/usr.bin/mg/random.c @@ -1,4 +1,4 @@ -/* $OpenBSD: random.c,v 1.24 2007/02/08 21:40:38 kjell Exp $ */ +/* $OpenBSD: random.c,v 1.25 2008/06/11 23:18:33 kjell Exp $ */ /* This file is in the public domain. */ @@ -236,12 +236,12 @@ justone(int f, int n) int delwhite(int f, int n) { - int col, c, s; + int col, s; col = curwp->w_doto; while (col < llength(curwp->w_dotp) && - ((c = lgetc(curwp->w_dotp, col)) == ' ' || c == '\t')) + (isspace(lgetc(curwp->w_dotp, col)))) ++col; do { if (curwp->w_doto == 0) { @@ -250,7 +250,7 @@ delwhite(int f, int n) } if ((s = backchar(FFRAND, 1)) != TRUE) break; - } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ' || c == '\t'); + } while (isspace(lgetc(curwp->w_dotp, curwp->w_doto))); if (s == TRUE) (void)forwchar(FFRAND, 1); @@ -259,6 +259,55 @@ delwhite(int f, int n) } /* + * Delete any leading whitespace on the current line + */ +int +delleadwhite(int f, int n) +{ + int soff, ls; + struct line *slp; + + /* Save current position */ + slp = curwp->w_dotp; + soff = curwp->w_doto; + + for (ls = 0; ls < llength(slp); ls++) + if (!isspace(lgetc(slp, ls))) + break; + gotobol(FFRAND, 1); + forwdel(FFRAND, ls); + soff -= ls; + if (soff < 0) + soff = 0; + forwchar(FFRAND, soff); + + return (TRUE); +} + +/* + * Delete any trailing whitespace on the current line + */ +int +deltrailwhite(int f, int n) +{ + int soff; + + /* Save current position */ + soff = curwp->w_doto; + + gotoeol(FFRAND, 1); + delwhite(FFRAND, 1); + + /* restore original position, if possible */ + if (soff < curwp->w_doto) + curwp->w_doto = soff; + + return (TRUE); +} + + + +/* * Insert a newline, then enough tabs and spaces to duplicate the indentation * of the previous line. Assumes tabs are every eight characters. Quite * simple. Figure out the indentation of the current line. Insert a newline @@ -268,7 +317,7 @@ delwhite(int f, int n) */ /* ARGSUSED */ int -indent(int f, int n) +lfindent(int f, int n) { int c, i, nicol; @@ -297,6 +346,42 @@ indent(int f, int n) } /* + * Indent the current line. Delete existing leading whitespace, + * and use tabs/spaces to achieve correct indentation. Try + * to leave dot where it started. + */ +int +indent(int f, int n) +{ + int soff, i; + + if (n < 0) + return (FALSE); + + delleadwhite(FFRAND, 1); + + /* If not invoked with a numerical argument, done */ + if (!(f & FFARG)) + return (TRUE); + + /* insert appropriate whitespace */ + soff = curwp->w_doto; + (void)gotobol(FFRAND, 1); + if ( +#ifdef NOTAB + curbp->b_flag & BFNOTAB) ? linsert(n, ' ') == FALSE : +#endif /* NOTAB */ + (((i = n / 8) != 0 && linsert(i, '\t') == FALSE) || + ((i = n % 8) != 0 && linsert(i, ' ') == FALSE))) + return (FALSE); + + forwchar(FFRAND, soff); + + return (TRUE); +} + + +/* * Delete forward. This is real easy, because the basic delete routine does * all of the work. Watches for negative arguments, and does the right thing. * If any argument is present, it kills rather than deletes, to prevent loss diff --git a/usr.bin/mg/util.c b/usr.bin/mg/util.c index b507f73bed3..0c100804bf9 100644 --- a/usr.bin/mg/util.c +++ b/usr.bin/mg/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.24 2007/02/08 21:40:38 kjell Exp $ */ +/* $OpenBSD: util.c,v 1.25 2008/06/11 23:18:33 kjell Exp $ */ /* This file is in the public domain. */ @@ -236,12 +236,12 @@ justone(int f, int n) int delwhite(int f, int n) { - int col, c, s; + int col, s; col = curwp->w_doto; while (col < llength(curwp->w_dotp) && - ((c = lgetc(curwp->w_dotp, col)) == ' ' || c == '\t')) + (isspace(lgetc(curwp->w_dotp, col)))) ++col; do { if (curwp->w_doto == 0) { @@ -250,7 +250,7 @@ delwhite(int f, int n) } if ((s = backchar(FFRAND, 1)) != TRUE) break; - } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ' || c == '\t'); + } while (isspace(lgetc(curwp->w_dotp, curwp->w_doto))); if (s == TRUE) (void)forwchar(FFRAND, 1); @@ -259,6 +259,55 @@ delwhite(int f, int n) } /* + * Delete any leading whitespace on the current line + */ +int +delleadwhite(int f, int n) +{ + int soff, ls; + struct line *slp; + + /* Save current position */ + slp = curwp->w_dotp; + soff = curwp->w_doto; + + for (ls = 0; ls < llength(slp); ls++) + if (!isspace(lgetc(slp, ls))) + break; + gotobol(FFRAND, 1); + forwdel(FFRAND, ls); + soff -= ls; + if (soff < 0) + soff = 0; + forwchar(FFRAND, soff); + + return (TRUE); +} + +/* + * Delete any trailing whitespace on the current line + */ +int +deltrailwhite(int f, int n) +{ + int soff; + + /* Save current position */ + soff = curwp->w_doto; + + gotoeol(FFRAND, 1); + delwhite(FFRAND, 1); + + /* restore original position, if possible */ + if (soff < curwp->w_doto) + curwp->w_doto = soff; + + return (TRUE); +} + + + +/* * Insert a newline, then enough tabs and spaces to duplicate the indentation * of the previous line. Assumes tabs are every eight characters. Quite * simple. Figure out the indentation of the current line. Insert a newline @@ -268,7 +317,7 @@ delwhite(int f, int n) */ /* ARGSUSED */ int -indent(int f, int n) +lfindent(int f, int n) { int c, i, nicol; @@ -297,6 +346,42 @@ indent(int f, int n) } /* + * Indent the current line. Delete existing leading whitespace, + * and use tabs/spaces to achieve correct indentation. Try + * to leave dot where it started. + */ +int +indent(int f, int n) +{ + int soff, i; + + if (n < 0) + return (FALSE); + + delleadwhite(FFRAND, 1); + + /* If not invoked with a numerical argument, done */ + if (!(f & FFARG)) + return (TRUE); + + /* insert appropriate whitespace */ + soff = curwp->w_doto; + (void)gotobol(FFRAND, 1); + if ( +#ifdef NOTAB + curbp->b_flag & BFNOTAB) ? linsert(n, ' ') == FALSE : +#endif /* NOTAB */ + (((i = n / 8) != 0 && linsert(i, '\t') == FALSE) || + ((i = n % 8) != 0 && linsert(i, ' ') == FALSE))) + return (FALSE); + + forwchar(FFRAND, soff); + + return (TRUE); +} + + +/* * Delete forward. This is real easy, because the basic delete routine does * all of the work. Watches for negative arguments, and does the right thing. * If any argument is present, it kills rather than deletes, to prevent loss |