summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorKjell Wooding <kjell@cvs.openbsd.org>2008-06-11 23:18:34 +0000
committerKjell Wooding <kjell@cvs.openbsd.org>2008-06-11 23:18:34 +0000
commitd1436874c326a3e1a5ac8a746fe2651ee54493b1 (patch)
tree0e96bc5996e377e917ad8738e43d8b592f715e41 /usr.bin
parente221eb36662c2d31e07375381aa17400624fe363 (diff)
Add delete-leading-space, delete-trailing-space,
indent-current-line utility functions for stripping leading/trailing whitespace, and setting a fixed indent respectively.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/mg/util.c95
1 files changed, 90 insertions, 5 deletions
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