Exemple #1
0
/* ARGSUSED */
int
fillpara(int f, int n)
{
    int	 c;		/* current char during scan		*/
    int	 wordlen;	/* length of current word		*/
    int	 clength;	/* position on line during fill		*/
    int	 i;		/* index during word copy		*/
    int	 eopflag;	/* Are we at the End-Of-Paragraph?	*/
    int	 firstflag;	/* first word? (needs no space)		*/
    int	 newlength;	/* tentative new line length		*/
    int	 eolflag;	/* was at end of line			*/
    int	 retval;	/* return value				*/
    struct line	*eopline;	/* pointer to line just past EOP	*/
    char	 wbuf[MAXWORD];	/* buffer for current word		*/

    if (n == 0)
        return (TRUE);

    undo_boundary_enable(FFRAND, 0);

    /* record the pointer to the line just past the EOP */
    (void)gotoeop(FFRAND, 1);
    if (curwp->w_doto != 0) {
        /* paragraph ends at end of buffer */
        (void)lnewline();
        eopline = lforw(curwp->w_dotp);
    } else
        eopline = curwp->w_dotp;

    /* and back top the beginning of the paragraph */
    (void)gotobop(FFRAND, 1);

    /* initialize various info */
    while (inword() == 0 && forwchar(FFRAND, 1));

    clength = curwp->w_doto;
    wordlen = 0;

    /* scan through lines, filling words */
    firstflag = TRUE;
    eopflag = FALSE;
    while (!eopflag) {

        /* get the next character in the paragraph */
        if ((eolflag = (curwp->w_doto == llength(curwp->w_dotp)))) {
            c = ' ';
            if (lforw(curwp->w_dotp) == eopline)
                eopflag = TRUE;
        } else
            c = lgetc(curwp->w_dotp, curwp->w_doto);

        /* and then delete it */
        if (ldelete((RSIZE) 1, KNONE) == FALSE && !eopflag) {
            retval = FALSE;
            goto cleanup;
        }

        /* if not a separator, just add it in */
        if (c != ' ' && c != '\t') {
            if (wordlen < MAXWORD - 1)
                wbuf[wordlen++] = c;
            else {
                /*
                 * You lose chars beyond MAXWORD if the word
                 * is too long. I'm too lazy to fix it now; it
                 * just silently truncated the word before,
                 * so I get to feel smug.
                 */
                ewprintf("Word too long!");
            }
        } else if (wordlen) {

            /* calculate tentative new length with word added */
            newlength = clength + 1 + wordlen;

            /*
             * if at end of line or at doublespace and previous
             * character was one of '.','?','!' doublespace here.
             * behave the same way if a ')' is preceded by a
             * [.?!] and followed by a doublespace.
             */
            if (dblspace && (!eopflag && ((eolflag ||
                                           curwp->w_doto == llength(curwp->w_dotp) ||
                                           (c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' '
                                           || c == '\t') && (ISEOSP(wbuf[wordlen - 1]) ||
                                                   (wbuf[wordlen - 1] == ')' && wordlen >= 2 &&
                                                    ISEOSP(wbuf[wordlen - 2])))) &&
                             wordlen < MAXWORD - 1))
                wbuf[wordlen++] = ' ';

            /* at a word break with a word waiting */
            if (newlength <= fillcol) {
                /* add word to current line */
                if (!firstflag) {
                    (void)linsert(1, ' ');
                    ++clength;
                }
                firstflag = FALSE;
            } else {
                if (curwp->w_doto > 0 &&
                        lgetc(curwp->w_dotp, curwp->w_doto - 1) == ' ') {
                    curwp->w_doto -= 1;
                    (void)ldelete((RSIZE) 1, KNONE);
                }
                /* start a new line */
                (void)lnewline();
                clength = 0;
            }

            /* and add the word in in either case */
            for (i = 0; i < wordlen; i++) {
                (void)linsert(1, wbuf[i]);
                ++clength;
            }
            wordlen = 0;
        }
    }
    /* and add a last newline for the end of our new paragraph */
    (void)lnewline();

    /*
     * We really should wind up where we started, (which is hard to keep
     * track of) but I think the end of the last line is better than the
     * beginning of the blank line.
     */
    (void)backchar(FFRAND, 1);
    retval = TRUE;
cleanup:
    undo_boundary_enable(FFRAND, 1);
    return (retval);
}
Exemple #2
0
/*
 * Fill the current paragraph according to the current fill column.
 */
int
fillpara (int f, int n, int k)
{
  register int c;		/* current char durring scan    */
  register int wordlen;		/* length of current word       */
  register int clength;		/* position on line during fill */
  register int eopflag;		/* Are we at the End-Of-Paragraph? */
  int firstflag;		/* first word? (needs no space) */
  int newlength;		/* tentative new line length    */
  int eolflag;			/* was at end of line           */
  LINE *eopline;		/* pointer to line just past EOP */
  char wbuf[MAXWORD];		/* buffer for current word      */

  /* Record the pointer to the line just past the
   * end of the paragraph.
   */
  gotoeop (FALSE, 1, KRANDOM);
  eopline = curwp->w_dot.p;
  if (lforw (eopline) == curbp->b_linep)
    eopline = curbp->b_linep;

  /* Move to the begining of the paragraph.
   */
  gotobop (FALSE, 1, KRANDOM);

  /* Skip to the start of the first word in the paragraph,
   * and set our current column position.
   */
  while (!inword ())
    if (forwchar (FALSE, 1, KRANDOM) == FALSE)
      break;
  clength = getcolpos () - 1;
  wordlen = 0;

  /* scan through lines, filling words
   */
  firstflag = TRUE;
  eopflag = FALSE;
  while (!eopflag)
    {
      /* get the next character in the paragraph
       */
      if ((eolflag = (curwp->w_dot.o == llength (curwp->w_dot.p))) == TRUE)
	{
	  c = ' ';
	  if (lforw (curwp->w_dot.p) == eopline)
	    eopflag = TRUE;
	}
      else
	c = lgetc (curwp->w_dot.p, curwp->w_dot.o);

      /* and then delete it
       */
      if (ldelete (1, FALSE) == FALSE)
	return (FALSE);

      /* if not a separator, just add it in
       */
      if (c != ' ' && c != '\t')
	{
	  if (wordlen < MAXWORD - 1)
	    wbuf[wordlen++] = c;
	  else
	    {
	      /* You lose chars beyond MAXWORD if the word
	       * is to long. I'm to lazy to fix it now; it
	       * just silently truncated the word before, so
	       * I get to feel smug.
	       */
	      eprintf ("Word too long!");
	    }
	}
      else if (wordlen)
	{
	  /* calculate tenatitive new length with word added
	   */
	  newlength = clength + 1 + wordlen;

	  /* if at end of line or at doublespace and previous
	   * character was one of '.','?','!' doublespace here.
	   */
	  if ((eolflag || curwp->w_dot.o == llength (curwp->w_dot.p)
	       || (c = lgetc (curwp->w_dot.p, curwp->w_dot.o)) == ' '
	       || c == '\t')
	      && ISEOSP (wbuf[wordlen - 1]) && wordlen < MAXWORD - 1)
	    wbuf[wordlen++] = ' ';

	  /* at a word break with a word waiting
	   */
	  if (newlength <= fillcol)
	    {
	      /* add word to current line */
	      if (!firstflag)
		{
		  linsert (1, ' ', NULLPTR);
		  ++clength;
		}
	      firstflag = FALSE;
	    }
	  else
	    {
	      if (curwp->w_dot.o > 0 &&
		  lgetc (curwp->w_dot.p, curwp->w_dot.o - 1) == ' ')
		{
		  curwp->w_dot.o -= 1;
		  ldelete (1, FALSE);
		}
	      /* start a new line */
	      lnewline ();
	      clength = 0;
	    }

	  /* and add the word in in either case */
	  linsert (wordlen, 0, wbuf);
	  clength += wordlen;
	  wordlen = 0;
	}
    }
  /* and add a last newline for the end of our new paragraph */
  lnewline ();

  /* we realy should wind up where we started, (which is hard to keep
   * track of) but I think the end of the last line is better than the
   * begining of the blank line.
   */
  backchar (FALSE, 1, KRANDOM);
  return (TRUE);
}