#include <sys/queue.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include "def.h"
int doindent(int);
int
ntabstop(int col, int tabw)
{
return (((col + tabw) / tabw) * tabw);
}
int
showcpos(int f, int n)
{
struct line *clp;
char *msg;
long nchar, cchar;
int nline, row;
int cline, cbyte;
int ratio;
clp = bfirstlp(curbp);
msg = "Char:";
cchar = 0;
cline = 0;
cbyte = 0;
nchar = 0;
nline = 0;
for (;;) {
++nline;
if (clp == curwp->w_dotp) {
cline = nline;
cchar = nchar + curwp->w_doto;
if (curwp->w_doto == llength(clp))
cbyte = *curbp->b_nlchr;
else
cbyte = lgetc(clp, curwp->w_doto);
}
nchar += llength(clp);
clp = lforw(clp);
if (clp == curbp->b_headp) {
if (cbyte == *curbp->b_nlchr &&
cline == curbp->b_lines) {
cbyte = EOF;
msg = "(EOB)";
}
break;
}
nchar++;
}
row = curwp->w_toprow + 1;
clp = curwp->w_linep;
while (clp != curbp->b_headp && clp != curwp->w_dotp) {
++row;
clp = lforw(clp);
}
ratio = nchar ? (100L * cchar) / nchar : 100;
ewprintf("%s %c (0%o) point=%ld(%d%%) line=%d row=%d col=%d" \
" (blines=%d rlines=%d l_size=%d)", msg,
cbyte, cbyte, cchar, ratio, cline, row, getcolpos(curwp),
curbp->b_lines, nline, clp->l_size);
return (TRUE);
}
int
getcolpos(struct mgwin *wp)
{
int col, i, c;
char tmp[5];
col = 0;
for (i = 0; i < wp->w_doto; ++i) {
c = lgetc(wp->w_dotp, i);
if (c == '\t') {
col = ntabstop(col, wp->w_bufp->b_tabw);
} else if (ISCTRL(c) != FALSE)
col += 2;
else if (isprint(c)) {
col++;
} else {
col += snprintf(tmp, sizeof(tmp), "\\%o", c);
}
}
return (col);
}
int
twiddle(int f, int n)
{
struct line *dotp;
int doto, cr;
if (n == 0)
return (TRUE);
dotp = curwp->w_dotp;
doto = curwp->w_doto;
if (doto == 0 && lback(dotp) == curbp->b_headp) {
dobeep();
ewprintf("Beginning of buffer");
return(FALSE);
}
if (doto == llength(dotp) && lforw(dotp) == curbp->b_headp) {
dobeep();
return(FALSE);
}
undo_boundary_enable(FFRAND, 0);
if (doto == 0 && doto == llength(dotp)) {
(void)forwline(FFRAND, 1);
curwp->w_doto = 0;
} else {
if (doto == 0) {
cr = lgetc(dotp, doto);
(void)backdel(FFRAND, 1);
(void)forwchar(FFRAND, 1);
lnewline();
linsert(1, cr);
(void)backdel(FFRAND, 1);
} else {
cr = lgetc(dotp, doto - 1);
(void)backdel(FFRAND, 1);
(void)forwchar(FFRAND, 1);
linsert(1, cr);
}
}
undo_boundary_enable(FFRAND, 1);
lchange(WFEDIT);
return (TRUE);
}
int
openline(int f, int n)
{
int i, s;
if (n < 0)
return (FALSE);
if (n == 0)
return (TRUE);
undo_boundary_enable(FFRAND, 0);
i = n;
do {
s = lnewline();
} while (s == TRUE && --i);
if (s == TRUE)
s = backchar(f | FFRAND, n);
undo_boundary_enable(FFRAND, 1);
return (s);
}
int
enewline(int f, int n)
{
int s;
if (n < 0)
return (FALSE);
while (n--) {
if ((s = lnewline()) != TRUE)
return (s);
}
return (TRUE);
}
int
deblank(int f, int n)
{
struct line *lp1, *lp2;
RSIZE nld;
lp1 = curwp->w_dotp;
while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_headp)
lp1 = lp2;
lp2 = lp1;
nld = (RSIZE)0;
while ((lp2 = lforw(lp2)) != curbp->b_headp && llength(lp2) == 0)
++nld;
if (nld == 0)
return (TRUE);
curwp->w_dotp = lforw(lp1);
curwp->w_doto = 0;
return (ldelete((RSIZE)nld, KNONE));
}
int
justone(int f, int n)
{
undo_boundary_enable(FFRAND, 0);
(void)delwhite(f, n);
linsert(1, ' ');
undo_boundary_enable(FFRAND, 1);
return (TRUE);
}
int
delwhite(int f, int n)
{
int col, s;
col = curwp->w_doto;
while (col < llength(curwp->w_dotp) &&
(isspace(lgetc(curwp->w_dotp, col))))
++col;
do {
if (curwp->w_doto == 0) {
s = FALSE;
break;
}
if ((s = backchar(FFRAND, 1)) != TRUE)
break;
} while (isspace(lgetc(curwp->w_dotp, curwp->w_doto)));
if (s == TRUE)
(void)forwchar(FFRAND, 1);
(void)ldelete((RSIZE)(col - curwp->w_doto), KNONE);
return (TRUE);
}
int
delleadwhite(int f, int n)
{
int soff, ls;
struct line *slp;
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);
}
int
deltrailwhite(int f, int n)
{
int soff;
soff = curwp->w_doto;
gotoeol(FFRAND, 1);
delwhite(FFRAND, 1);
if (soff < curwp->w_doto)
curwp->w_doto = soff;
return (TRUE);
}
int
doindent(int cols)
{
int n;
if (curbp->b_flag & BFNOTAB)
return (linsert(cols, ' '));
if ((n = cols / curbp->b_tabw) != 0 && linsert(n, '\t') == FALSE)
return (FALSE);
if ((n = cols % curbp->b_tabw) != 0 && linsert(n, ' ') == FALSE)
return (FALSE);
return (TRUE);
}
int
lfindent(int f, int n)
{
int c, i, nicol;
int s = TRUE;
if (n < 0)
return (FALSE);
undo_boundary_enable(FFRAND, 0);
while (n--) {
nicol = 0;
for (i = 0; i < llength(curwp->w_dotp); ++i) {
c = lgetc(curwp->w_dotp, i);
if (c != ' ' && c != '\t')
break;
if (c == '\t')
nicol = ntabstop(nicol, curwp->w_bufp->b_tabw);
else
++nicol;
}
(void)delwhite(FFRAND, 1);
if (lnewline() == FALSE || doindent(nicol) == FALSE) {
s = FALSE;
break;
}
}
undo_boundary_enable(FFRAND, 1);
return (s);
}
int
indent(int f, int n)
{
int soff;
if (n < 0)
return (FALSE);
delleadwhite(FFRAND, 1);
if (!(f & FFARG))
return (TRUE);
soff = curwp->w_doto;
(void)gotobol(FFRAND, 1);
if (doindent(n) == FALSE)
return (FALSE);
forwchar(FFRAND, soff);
return (TRUE);
}
int
forwdel(int f, int n)
{
if (n < 0)
return (backdel(f | FFRAND, -n));
if (f & FFARG) {
if ((lastflag & CFKILL) == 0)
kdelete();
thisflag |= CFKILL;
}
return (ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE));
}
int
backdel(int f, int n)
{
int s;
if (n < 0)
return (forwdel(f | FFRAND, -n));
if (f & FFARG) {
if ((lastflag & CFKILL) == 0)
kdelete();
thisflag |= CFKILL;
}
if ((s = backchar(f | FFRAND, n)) == TRUE)
s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE);
return (s);
}
int
space_to_tabstop(int f, int n)
{
int col, target;
if (n < 0)
return (FALSE);
if (n == 0)
return (TRUE);
col = target = getcolpos(curwp);
while (n-- > 0)
target = ntabstop(target, curbp->b_tabw);
return (linsert(target - col, ' '));
}
int
backtoindent(int f, int n)
{
gotobol(FFRAND, 1);
while (curwp->w_doto < llength(curwp->w_dotp) &&
(isspace(lgetc(curwp->w_dotp, curwp->w_doto))))
++curwp->w_doto;
return (TRUE);
}
int
joinline(int f, int n)
{
int doto;
undo_boundary_enable(FFRAND, 0);
if (f & FFARG) {
gotoeol(FFRAND, 1);
forwdel(FFRAND, 1);
} else {
gotobol(FFRAND, 1);
backdel(FFRAND, 1);
}
delwhite(FFRAND, 1);
if ((doto = curwp->w_doto) > 0) {
linsert(1, ' ');
curwp->w_doto = doto;
}
undo_boundary_enable(FFRAND, 1);
return (TRUE);
}