/******************************************************************************

    MODUL
	edit.c

    DESCRIPTION
	Everything for editing like DEL, BS, INDENT, DELINE, INSLINE, ...

    NOTES

    BUGS

    TODO

    EXAMPLES

    SEE ALSO

    INDEX

    HISTORY
	14. Nov 1992	ada created

******************************************************************************/

/**************************************
		Includes
**************************************/
#include "defs.h"
#include "clipboard.h"


/**************************************
	    Globale Variable
**************************************/


/**************************************
      Interne Defines & Strukturen
**************************************/


/**************************************
	    Interne Variable
**************************************/


/**************************************
	   Interne Prototypes
**************************************/
Prototype void do_bs	     (void);
Prototype void do_del	     (void);
Prototype void do_remeol     (void);
Prototype void do_split      (void);
Prototype int  do_join	     (void);
Prototype void do_reformat   (int);
Prototype void do_insertmode (void);
Prototype void do_insline    (void);
Prototype void do_deline     (void);
Prototype void do_tlate      (void);
Prototype void do_justify    (void);
Prototype void do_unjustify  (void);
Prototype void do_indent     (void);
Prototype void do_inslines   (void);
Prototype void do_delete     (void);
Prototype void do_delines    (void);
Prototype void do_undeline   (void);
Prototype void do_space      (void);
Prototype void do_clipins    (void);
Prototype void do_return     (void);


void do_bs (void)
{
    ED	* ep  = Ep;
    RP	* rp  = ep->Win->RPort;
    short i,
	  col = ep->Column;

    if (col) {
	movmem(Current + col, Current + col - 1, Clen - col + 1);

	col --;
	ep->Column --;
	Clen --;

	if (col < ep->Topcolumn) {
	    text_sync ();
	} else {
	    i = ep->Line;

	    if (is_inblock (i, ep->Topcolumn + Columns -1) & BP_INSIDE) {
		SetBPen (rp, BLOCK_BPEN);
		SetWrMsk (rp, BLOCK_MASK);
	    }

	    i -= ep->Topline;

	    ScrollRaster(rp, Xsize, 0,
		COL(col - ep->Topcolumn),
		ROW(i),
		Xpixs,
		ROW(i + 1) - 1
	    );

	    if (Clen >= ep->Topcolumn + Columns) {
		setpen(ep->Line);

		Move (rp, COLT(Columns-1), ROWT(i));
		Text (rp, Current + ep->Topcolumn + Columns - 1, 1);
	    } else
		SetBPen (rp, TEXT_BPEN);

	    SetWrMsk (rp, -1);
	} /* if (col < ep->Topcolumn) */

	if (Comlinemode == 0 && ep->Wordwrap)
	    do_reformat(0);
    } else {
	Abortcommand = 1;
    }
} /* do_bs */


void do_del (void)
{
    ED	* ep  = Ep;
    RP	* rp  = ep->Win->RPort;
    short i,
	  col = ep->Column;

    if (Current[col]) {
	movmem(Current + col + 1, Current + col, Clen - col);

	Clen --;

	i = ep->Line;

	if (is_inblock (i, ep->Topcolumn + Columns - 1) & BP_INSIDE) {
	    SetBPen (rp, BLOCK_BPEN);
	    SetWrMsk (rp, BLOCK_MASK);
	}

	i -= ep->Topline;

	ScrollRaster(rp, Xsize, 0,
	    COL(col - ep->Topcolumn),
	    ROW(i),
	    Xpixs,
	    ROW(i + 1) - 1
	);

	if (Clen >= ep->Topcolumn + Columns) {
	    setpen(ep->Line);

	    Move (rp, COLT(Columns-1), ROWT(i));
	    Text (rp, Current+ep->Topcolumn+Columns-1, 1);
	} else
	    SetBPen (rp, TEXT_BPEN);

	SetWrMsk (rp, -1);

	if (Comlinemode == 0 && ep->Wordwrap)
	    do_reformat(0);
    }
} /* do_del */


void do_remeol (void)
{
    Current[Clen = Ep->Column] = 0;

    text_sync ();
    text_redisplaycurrline ();
} /* do_remeol */


void do_split()              /* split line in two at cursor pos */
{
    ubyte buf[MAXLINELEN];
    ED	* ep		   = Ep;
    RP	* rp		   = ep->Win->RPort;
    char  onLastLine;

    strcpy ((char *)buf, (char *)Current+ep->Column);
    Current[Clen = ep->Column] = '\0';

    text_sync ();

    SetAPen (rp, TEXT_BPEN);

    if (!Nsu)
	RectFill (rp, COL(0), ROW(ep->Line-ep->Topline),
	       Xpixs, ROW(ep->Line-ep->Topline+1)-1);

    SetAPen (rp, TEXT_FPEN);

    text_displayseg (ep->Line - ep->Topline, 1);

    onLastLine = (ep->Line == ep->Lines-1);
    do_downadd ();

    if (!onLastLine)
	do_insline ();

    strcpy ((char *)Current, (char *)buf);
    Clen = strlen ((char *)Current);

    text_sync ();
    text_displayseg (ep->Line - ep->Topline, 1);

    do_up ();
} /* do_split() */


int do_join (void)
{
    int   i = Clen, j;
    ED * ep = Ep;

    /* Check if there is a line below and the length of both lines */
    if (ep->Line + 1 < ep->Lines &&
		strlen((char *)ep->List[ep->Line+1])+i <= MAXLINELEN-2) {
	/* Add a space */
	if (i && Current[i-1] != ' ')
	    Current[i++] = ' ';

	/* Copy the line */
	strcpy((char *)Current+i, (char *)ep->List[ep->Line+1]);

	/* Find first nonspace */
	for (j = i; Current[j] == ' '; ++j);
	/* Find last space */
	for (; i >= 0 && Current[i] == ' '; --i);

	/* Too many spaces ? */
	if (j > i+2)
	    movmem (Current+j, Current+i+2, strlen((char *)Current+j)+1);

	/* new length */
	Clen = strlen ((char *)Current);

	/* redisplay */
	text_sync ();
	text_displayseg (ep->Line - ep->Topline, 1);

	i = text_lineno ();

	do_down ();
	do_deline ();

	if (i != text_lineno ())
	    do_up ();

	return(1);
    }

    return(0);
} /* do_join */


/*
 * n == -1  :	force reformat entire paragraph
 * n ==  0  :	only until line equalizes (from text_write())
 *
 * What is a paragraph?   A paragraph ends whenever the left justification
 * gets larger, or on a blank line.
 */

void do_reformat(int n)
{
    char * str;
    ED	 * ep = Ep;
    RP	 * rp = ep->Win->RPort;
    int    nlok;   /* Next Line ok ? */
    int    lnsc;   /* LastNS of Current */
    int    fnst;   /* FirstNS of next Line */
    int    fnsc;    /* FirstNS of Current */
    int    column  = ep->Column;
    int    srow    = ep->Line;
    int    crow    = srow;
    int    erow    = srow;
    short  dins    = 0; 	/* relative insert lines/delete lines	*/
    char   moded   = 0; 	/* any modifications done at all?	*/
    char   checked = 0; 	/* for cursor positioning.		*/

    if (ep->Margin == 0)
	ep->Margin = 75;

    ++Nsu;
    for (;;) {
	str = (char *)ep->List[ep->Line+1];
	fnst = 0;
	fnsc = firstns((char *)Current);
	nlok = (ep->Line + 1 < ep->Lines && fnsc >= (fnst=firstns(str)));
	if (ep->WWCol >= 0)
	    fnst = fnsc = ep->WWCol;
	if (nlok && str[0] == 0)
	    nlok = 0;
	lnsc = lastns((char *)Current);
	if (lnsc < ep->Margin) {    /* space at end of line for marg-lnsc-2 letter word   */
	    if (nlok == 0)        /* but no more data to joinup   */
		break;		  /* done */
	    if (ep->Margin - lnsc - 2 >= wordlen(str+fnst)) {
		ep->Column = 0;
		Clen = lastns((char *)Current);
		if (Current[Clen])
		    ++Clen;
		moded = 1;
		--dins;
		if (do_join())
		    continue;
		++dins;

		error ("reformat:\nMargin too big");
		break;
	    }
	    if (n == 0)        /* if couldn't mod line, and text_write, don't update any more */
		break;
	    do_down ();
	    erow = ep->Line;
	    continue;
	}
				/* no space, need to split	*/
				/* find start of prev word	*/
	for (;;) {
	    int i = lnsc;
	    while (i && Current[i] != ' ')
		--i;
	    lnsc = i;
	    if (i >= ep->Margin) {
		while (i && Current[i] == ' ')
		    --i;
		if (i < ep->Margin)
		    break;
		lnsc = i;
	    } else
		break;
	}

	/* Word too long */
	if (wordlen((char *)Current+lnsc) >= ep->Margin) {
	    error ("reformat:\nWord too long");
	    break;
	}

	if (lnsc) {             /* ok to split at word          */
	    ++lnsc;
	    ++dins;
	    ep->Column = lnsc;
	    do_split (); /* Split at point LNSC          */
	    do_down ();          /* must insert proper amount?   */
	    {
		int indent = (nlok == 0) ? fnsc : fnst;
		if (ep->WWCol >= 0)
		    indent = ep->WWCol;
		if (!checked) {
		    checked = 1;
		    if (lnsc <= column) {   /* if split before cursor   */
			column = column - ep->Column + indent;
			++crow;
		    }
		}
		if (Clen + indent < 253) {
		    movmem(Current, Current + indent,
			    strlen((char *)Current)+1);
		    setmem(Current, indent, ' ');
		    Clen += indent;
		}
	    }
	    erow = ep->Line;
	    continue;
	}

	if (n == 0)
	    break;

	do_down ();
    }

    if (column < 0 || column > 200)
	column = 0;
    if (srow >= ep->Lines) {
	srow = ep->Lines - 1;
	goto ra;
    }
    if (dins || srow < ep->Topline || srow >= ep->Topline + Rows) {
ra:
	text_sync ();
	--Nsu;
	ep->Line = crow;
	ep->Column = column;
	text_load ();
	if (!text_sync())
	    text_redisplay ();
    } else {
	text_sync ();

	Nsu --;

	ep->Line = crow;
	ep->Column = column;

	text_load ();

	if (erow != srow) {
	    if (!text_sync ()) {
		erow ++;

		if (erow - ep->Topline > Rows)
		    erow = ep->Topline + Rows;

		SetAPen(rp, TEXT_BPEN);

		RectFill(rp, COL(0), ROW(srow - ep->Topline),
		    Xpixs, ROW(erow - ep->Topline)-1);

		SetAPen(rp, TEXT_FPEN);

		text_displayseg(srow - ep->Topline, erow - srow);
	    }
	} else {
	    text_sync ();
	    if (moded)
		text_redisplaycurrline ();
	}
    }
    if (column > Clen) {
	setmem(Current+Clen, column - Clen, ' ');
	Current[column] = 0;
    }
    ep->Column = column;
} /* do_reformat(int */


void do_insline()
{
    ubyte *ptr;
    ED *ep = Ep;

    ep->Modified = 1;

    text_sync ();

    if (makeroom (32)) {
	ptr = allocline (1);

	bmovl(ep->List+ep->Line, ep->List+ep->Line+1,ep->Lines-ep->Line);
	ep->List[ep->Line] = ptr;

	ep->Lines ++;

	if (ActualBlock.ep == ep) {
	    if (ep->Line < ActualBlock.start_line)
		ActualBlock.start_line ++;
	    if (ep->Line <= ActualBlock.end_line)
		ActualBlock.end_line ++;
	}
    } else {
	nomemory ();
    }

    text_load ();

    if (Nsu == 0) {
	scroll_display (0, -1, 0, ep->Line, MAXLINELEN, ep->Topline+Rows);
	text_displayseg (ep->Line - ep->Topline, 1);
    }
} /* do_insline() */


void do_deline()
{
    int delline, eline;
    ED *ep = Ep;

    strcpy((char *)Deline, (char *)Current);
    if (ep->Lines > 1) {
	ep->Modified = 1;
	text_sync ();

	freeline(ep->List[ep->Line]);
	bmovl(ep->List+ep->Line+1, ep->List+ep->Line,ep->Lines-ep->Line-1);
	if (ActualBlock.ep == ep) {
	    if (ep->Line < ActualBlock.start_line)
		ActualBlock.start_line --;
	    if (ep->Line <= ActualBlock.end_line)
		ActualBlock.end_line --;
	}
	delline = ep->Line;
	if (ep->Line >= --ep->Lines) {
	    --ep->Line;
	    text_load ();
	    if (ep->Line < ep->Topline) {
		if (Nsu == 0) {
		    ep->Topline = ep->Line - (Rows>>1);
		    if (ep->Topline < 0)
			ep->Topline = 0;
		    text_redisplay ();
		}
		return;
	    }
	}
	text_load ();
	if (Nsu == 0) {
	    eline = ep->Lines +1;
	    scroll_display (0, 1, 0, delline, MAXLINELEN, eline);
	    text_displayseg (Rows-1, 1);
	}
    } else {
	do_firstcolumn ();
	do_remeol ();
	ep->Modified = 0;
    }
} /* do_deline */


void do_tlate (void)
{
    ubyte * ptr = av[1];
    ED	  * ep	= Ep;
    ubyte   c	= Current[ep->Column];

    if (c == 0)
	c = ' ';

    switch (*ptr) {
	case '+':
	    c += strtol ((char *)ptr+1, NULL, 0);
	    break;

	case '-':
	    c -= strtol ((char *)ptr+1, NULL, 0);
	    break;

	case '"':
	    c = ptr[1];
	    break;

	case 'u':
	case 'U':
	    c = toupper (c);
	    break;

	case 'l':
	case 'L':
	    c = tolower (c);
	    break;

	default:
	    c = strtol ((char *)ptr, NULL, 0);
    }

    if (c) {
	if (Current[ep->Column] == 0) {
	    Clen = ep->Column + 1;
	    Current[Clen] = 0;
	}

	Current[ep->Column] = c;

	if (!Nsu) {
	    movetocursor ();
	    setpen (ep->Line);
	    Text (ep->Win->RPort, Current+ep->Column, 1);
	    SetWrMsk (ep->Win->RPort, -1);
	}
    }
} /* do_tlate */


void do_justify (void)
{
    ED *ep = Ep;
    short firstnb, lastnb, i, n, fill, c, sp;
    short changed = FALSE;

    switch(av[1][0]) {
	case 'c':
	break;

	case 'f':
	    firstnb = firstns((char *)Current);
	    lastnb = lastns((char *)Current);
	    if (firstnb < lastnb && ep->Margin < MAXLINELEN-1) {
		n = 0;
		i = firstnb;
		while (i <= lastnb) {
		    while ((c = Current[i]) && c != ' ')
			i++;
		    if (i <= lastnb) {
			n++;
			while (Current[i] == ' ')
			    i++;
		    }
		}
		fill = ep->Margin - lastnb - 1;
		i = firstnb;
		Current[lastnb + 1] = 0;
		if (n > 0 && fill > 0)
		    changed = TRUE;
		while (n > 0 && fill > 0 && Current[i]) {
		    while ((c = Current[i]) && c != ' ')
			i++;
		    sp = fill / n;
		    movmem (&Current[i], &Current[i + sp],
			    strlen((char *)&Current[i]) + 1);
		    memset ((char *)&Current[i], ' ', sp);
		    while (Current[i] == ' ')
			i++;
		    fill -= sp;
		    n--;
		}
	    }
	break;

	default:
	break;
    }

    if (changed) {
	text_sync();
	text_redisplaycurrline();
    }
} /* do_justify */


void do_unjustify (void)
{
    short i, j, waswhite = FALSE;
    ubyte c;

    for (i = 0; Current[i] == ' '; i++);

    for (j = i; Current[i]; i++) {
	c = Current[j] = Current[i];

	if (c != ' ' || !waswhite)
	    j++;

	waswhite = (c == ' ');
    }

    Current[j] = 0;

    if (i != j) {
	text_sync();
	text_redisplaycurrline();
    }
} /* do_unjustify */


void do_indent (void)
{
    int    start,
	   end,
	   indent,
	   dir,
	   align,
	   len,
	   curr_indent,
	   leading,
	   trailing,
	   column,
	   t;
    char * ptr;
    RP	 * rp;
    LINE * linelist;

     /* decode range */
     ptr = av[1];

     start = end = -1;

     while (*ptr) {
	  if (*ptr == '.') {
	       start = end = Ep->Line;
	       break;
	  } else if (isdigit(*ptr)) {
	       if (start == -1)
		    start = atoi (ptr);
	       else end = atoi (ptr);

	       while (isdigit(*ptr)) ptr ++;

	       ptr --;
	  } else if (*ptr == '$') {
	       if (start == -1)
		    start = get_pong (*ptr - '0');
	       else end = get_pong (*ptr - '0');
	  } else if (*ptr == 'b') {
	       if (!block_ok()) {
		    error ("indent block:\nNo block specified");
		    return ;
	       }

	       if (!ptr[1]) {
		    start = ActualBlock.start_line;
		    end = ActualBlock.end_line;
	       } else {
		    ptr ++;

		    if (*ptr == 's')
			 indent = ActualBlock.start_line;
		    else indent = ActualBlock.end_line;

		    if (start == -1)
			 start = indent;
		    else end = indent;
	       }
	  } else if (*ptr == 't') {
	       start = 1;
	       end = Ep->Lines;
	  } else if (*ptr == '_') {
	       if (start == -1)
		    start = Ep->Lines;
	       else end = Ep->Lines;
	  }

	  ptr ++;
     } /* while (*ptr) */

     if (start == -1) {
	  error ("indent:\ncannot evaluate start-line");
	  return ;
     }

     if (end == -1) end = start;

     if (start > end) {
	  start ^= end;       /* Swap ! 8-O */
	  end ^= start;
	  start ^= end;
     }

     /* Now find out how to indent */

     ptr = av[2];

     dir = 1;		 /* indent, i.e. insert spaces */
     align = 0; 	 /* just insert `indent' spaces */
     indent = -1;
     column = 0;

     while (*ptr) {
	  if (*ptr == '-')
	       dir = -1;	   /* outdent line, i.e. delete spaces */
	  else if (*ptr == '.')
	       align = 1;	   /* align line to a multiple of `indent' */
	  else if (*ptr == 't')
	       indent = Ep->Tabstop;
	  else if (isdigit(*ptr)) {
	       indent = atoi (ptr);

	       while (isdigit(*ptr)) ptr++;
	       ptr --;
	  } else if (*ptr == 'c')
	       column = Ep->Column;

	  ptr ++;
     } /* while (*ptr) */

     if (indent == -1) {
	  error ("indent:\nCannot evaluate indent-amount");
	  return ;
     }

     text_sync ();

     /* Note : If the line contains less spaces than indent specifies and
	the user want to outdent, the line is left-aligned, i.e. no chars
	but spaces are deleted ! This is also true for indent, i.e. no
	chars are moved over the right border (255 chars). */

     linelist = Ep->List + start;

     for (t=start; t <= end; t ++, linelist ++) {
	  strcpy (Current, linelist[0]);
	  Clen = strlen (Current);

	  if (Clen <= column) continue;

	  ptr = Current + column;

	  while (*ptr == ' ') ptr ++;

	  if (!*ptr) continue;          /* Empty line */

	  leading  = (int)ptr - (int)Current;
	  trailing = MAXLINELEN-1 - Clen;

	  /* Should I align the line to a multiple of indent or just
	     insert/delete indent spaces ? */
	  if (align) {
	       /* use appropriate difference */
	       if (dir > 0)
		    curr_indent = indent - (leading % indent);
	       else {
		    curr_indent = leading % indent;

		    if (!curr_indent)
			  curr_indent = indent;
	       }
	  } else curr_indent = indent;

	  len = strlen (ptr) + 1;

	  if (dir > 0) {
	       if (trailing < curr_indent)
		    curr_indent = trailing;

	       if (!curr_indent) continue;

	       movmem (ptr, ptr+curr_indent, len);
	       setmem (ptr, curr_indent, ' ');
	  } else {
	       if (leading < curr_indent)
		    curr_indent = leading;

	       if (!curr_indent) continue;

	       movmem (ptr, ptr-curr_indent, len);
	  }

	  Clen = strlen (Current);
	  len  = strlen (linelist[0]);

#ifdef DEBUG
    printf ("Old len %d  New Len %d     BlenOld %d  BlenNew %d\n",
	    len, Clen, ((len + 8) & ~7), ((Clen + 8) & ~7));
#endif

	  /* Find out if we have to allocate new memory */
	  /* Clen is total length in bytes while len is only strlen() */
	  /* TODO: Ain't work yet */
	  /*if ((((Clen + 8) & ~7) != ((len + 8) & ~7))) {*/
	       ptr = allocline (Clen + 1);

	       if (!ptr) {
		    nomemory ();
		    Abortcommand = 1;
		    break;
	       }

	       freeline (linelist[0]);
	       linelist[0] = ptr;
	  /*}*/

	  strcpy (ptr, Current);
     } /* for (t = start; t <= end; t ++, linelist ++) */

     Ep->Modified = 1;

     text_load ();

     /* Don't display if there is nothing to display */
     if (end < Ep->Topline)
	  return;

     t = end - start +1;
     start -= Ep->Topline;

     if (start >= Rows)
	  return;

     if (start < 0)
	  start = 0;

     if (t+start > Rows)
	  t = Rows - start;

     rp = Ep->Win->RPort;

     SetAPen (rp, Ep->BGPen);
     RectFill (rp, COL(0), ROW(start), Xpixs, ROW(start+t) -1);

     if (!text_sync ())
	  text_displayseg (start, t);
} /* do_indent */


void do_inslines (void)
{
     int lines = atoi (av[1]);

     if (lines <= 0) return;

     while (lines) {
	  do_insline ();
	  lines --;
     }

} /* do_inslines */


void do_delete (void)
{
    char * ptr, * hptr;

    hptr = ptr = Current + Ep->Column;

    if (*hptr == ' ')
	while (*hptr == ' ') hptr ++;
    else while (*hptr != ' ') hptr ++;

    strcpy (ptr, hptr);

    text_sync ();
    text_redisplaycurrline ();
} /* do_delete */


void do_delines (void)
{
     int t;

     t = atoi (av[1]);

     for ( ; t >= 0; t--)
	  do_deline ();
} /* do_delines */


/*
 *  Commands submitted by Markus Wenzel
 */

void do_undeline (void)
{
   do_insline ();
   text_load ();

   strcpy((char *)Current, (char *)Deline);

   text_sync ();
   text_displayseg (Ep->Line - Ep->Topline, 1);
} /* do_undeline */


void do_clipins (void)
{
    struct IOClipReq * ior;
    struct cbbuf * buf;

    if (!(ior = CBOpen (0)) )
	return;

    if (CBQueryFTXT (ior)) {
	buf = CBReadCHRS (ior);

	if (buf) {
	    text_write ((ubyte *)buf->mem);
	    CBFreeBuf (buf);
	}

	CBReadDone (ior);
    }

    CBClose (ior);
} /* do_clipins */


void do_space (void)
{
    ED * ep = Ep;
    int  insmode = ep->Insertmode;

    ep->Insertmode = 1;
    text_write (" ");
    ep->Insertmode = insmode;
} /* do_space */


void do_return (void)
{
    char   buf[MAXLINELEN];
    char * partial;

    if (Comlinemode) {
	strcpy (buf, (char *)Current);

	partial = Partial;
	Partial = NULL;

	escapecomlinemode ();

	if (partial) {
	    if (do_command (buf))
		do_command (partial);

	    free(partial);
	} else
	    do_command (buf);
    } else {
	if (Ep->Autoindent) {   /* If user want's autoindent */
	    short  indent;  /* how much */
	    short  line;    /* Line-no. */
	    char * ptr;     /* pointer to Ep->List[line] */

	    text_sync ();   /* store actual line */

	    /* Start with current line */
	    indent = 0;
	    ptr = (char *)Current;

	    /* Find 1. nonspace ... */
	    while (isspace (*ptr)) {
		ptr ++;
		indent ++;
	    }

	    /* ... but not '\0'. In this case the line is empty and
	       indent is therefore 0 */
	    if (!*ptr)
		indent = 0;

	    /* Try on, if indent is zero, line is empty and there are more
	       lines on top */
	    if (!indent && !*ptr && Ep->Line) {
		line = Ep->Line - 1;

		/* Until 1. line */
		while (line) {
		    /* Try every line */
		    ptr = (char *)Ep->List[line];
		    indent = 0;

		    while (isspace (*ptr)) {
			ptr ++;
			indent ++;
		    }

		    if (indent || *ptr)
			break;

		    line --;
		}
	    }

	    /* Not at last position in line ? then we have to split and
	       indent the rest */
	    if (Ep->Column != Clen) {
		/* Split line, move to next line but shouldn't show this */
		do_split ();
		do_down ();

		/* Indent new line */
		if (indent > Ep->Column)
		    indent -= indent - Ep->Column;

		if (indent > 0) {
		    movmem (Current, Current+indent, Clen+1);
		    setmem (Current, indent, ' ');
		    Clen += indent;
		    Ep->Column = indent;
		} else
		    Ep->Column = 0;

		Current[Clen] = 0;

		/* and position cursor */
		text_sync ();

		/* Show result */
		text_redisplaycurrline ();
	    } else {
		if (Ep->Line != Ep->Lines-1) {
		    do_down ();
		    do_insline ();

		    Ep->Column = indent;
		    text_sync ();
		} else {
		    do_split ();
		    Ep->Column = indent;
		    text_sync ();
		    do_down ();
		}
	    }
	} else {
	    Ep->Column = 0;
	    text_sync ();
	    do_downadd ();
	}
    } /* !Comlinemode */
} /* do_return */


/******************************************************************************
*****  ENDE edit.c
******************************************************************************/
