home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / EDWRAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  2.6 KB  |  87 lines

  1. /**
  2. *
  3. * Name        EDWRAP -- Perform word wrapping on an edit buffer.
  4. *
  5. * Synopsis    edwrap(pedit_buffer)
  6. *
  7. *        ED_BUFFER *pedit_buffer  edit buffer structure.
  8. *
  9. * Description    EDWRAP accepts a pointer to an edit buffer and
  10. *        performs word wrapping on it.  A word is defined as a
  11. *        series of non-whitespace characters surrounded by
  12. *        whitespace.  Any word which would touch the right edge
  13. *        of the field will have blanks inserted in front of it
  14. *        to force it onto the next line.  If the word is longer
  15. *        than one line long, it is broken.  Note that word
  16. *        wrapping may force data off the end of the buffer, in
  17. *        which case it is lost.
  18. *
  19. * Returns    nothing.
  20. *
  21. * Version    6.00 (C)Copyright Blaise Computing Inc.  1989
  22. *
  23. **/
  24. #include <bedit.h>
  25.  
  26. void edwrap(pedit_buffer)
  27. ED_BUFFER *pedit_buffer;
  28. {
  29.     int current_row;
  30.     int last_char_on_row;
  31.     int current_index;
  32.  
  33.     /* If the displayed field is only one row, there's nothing to do. */
  34.     if (pedit_buffer->dimensions.h == 1)
  35.     return;
  36.  
  37.     for (current_row = 0; current_row < (pedit_buffer->dimensions.h - 1);
  38.      current_row++)
  39.     {
  40.     last_char_on_row = (pedit_buffer->dimensions.w *
  41.                (current_row + 1)) - 1;
  42.     if (last_char_on_row >= pedit_buffer->buffer_size)
  43.         return;
  44.  
  45.         /* Find the last non-space on the current line.     */
  46.     for (current_index = last_char_on_row;
  47.          (current_index > last_char_on_row -
  48.                   pedit_buffer->dimensions.w) &&
  49.          (!isspace(pedit_buffer->pbuffer[current_index]));
  50.          current_index--)
  51.         ;
  52.  
  53.         /* Check to see if we should wrap here.         */
  54.     if (!utrange(current_index, last_char_on_row -
  55.              pedit_buffer->dimensions.w + 1,
  56.              last_char_on_row - 1)           &&
  57.         !isspace(pedit_buffer->pbuffer[current_index + 1]))
  58.     {
  59.         current_index++;
  60.  
  61.         memmove(&(pedit_buffer->pbuffer[last_char_on_row + 1]),
  62.             &pedit_buffer->pbuffer[current_index],
  63.             pedit_buffer->buffer_size - last_char_on_row - 1);
  64.         memset(&pedit_buffer->pbuffer[current_index], ' ',
  65.            (last_char_on_row - current_index) + 1);
  66.  
  67.         /* Update current end of data, if necessary.        */
  68.         if (pedit_buffer->data_end >= current_index)
  69.         {
  70.         pedit_buffer->data_end += (last_char_on_row -
  71.                        current_index) + 1;
  72.         utuplim(pedit_buffer->data_end,
  73.             pedit_buffer->buffer_size);
  74.         }
  75.  
  76.         /* Update current edit position, if necessary.        */
  77.         if (pedit_buffer->cursor_pos >= current_index)
  78.         {
  79.         pedit_buffer->cursor_pos += (last_char_on_row -
  80.                          current_index) + 1;
  81.         utuplim(pedit_buffer->cursor_pos,
  82.             pedit_buffer->buffer_size - 1);
  83.         }
  84.     }
  85.     }
  86. }
  87.