home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / editline.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  6KB  |  183 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    editline.c (Editor Line)
  6.  * Purpose:    Manipulate the popup editor character string
  7.  * Subroutine:    insert_chars()            returns: void
  8.  * Subroutine:    delete_chars_backward()        returns: void
  9.  * Subroutine:    delete_chars_forward()        returns: void
  10.  * Subroutine:    next_word_end()            returns: int
  11.  * Subroutine:    last_word_end()            returns: int
  12.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  13.  *        You may do anything you like with this file except remove
  14.  *        this copyright.  The Smithsonian Astrophysical Observatory
  15.  *        makes no representations about the suitability of this
  16.  *        software for any purpose.  It is provided "as is" without
  17.  *        express or implied warranty.
  18.  * Modified:    {0} Michael VanHilst    initial version          4 July 1989
  19.  *        {n} <who> -- <does what> -- <when>
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <X11/Xlib.h>
  24. #include "hfiles/edit.h"
  25.  
  26. /*
  27.  * Subroutine:    insert_chars
  28.  * Purpose:    Insert new characters into the edit line
  29.  * Returns:    Number of characters inserted
  30.  */
  31. void insert_chars ( edit, str, len )
  32.      EditStruct *edit;
  33.      char *str;
  34.      int len;
  35. {
  36.   int i, j;
  37.   void redraw_after_insert();
  38.  
  39.   /* check for end of buffer */
  40.   if( (edit->char_cnt + len) > edit->max_char_cnt ) {
  41.     (void)fprintf(stderr, "Line buffer full!\n");
  42.     if( edit->char_cnt < edit->max_char_cnt ) {
  43.       len = edit->max_char_cnt - edit->char_cnt;
  44.     } else
  45.       return;
  46.   }
  47.   /* move later characters forward */
  48.   for( i = edit->char_cnt, j = i + len;
  49.        i >= edit->active_position; i--, j-- ) {
  50.     edit->charsz[j] = edit->charsz[i];
  51.     edit->string[j] = edit->string[i];
  52.   }
  53.   /* insert new characters */
  54.   for( j = 0, ++i; j < len; j++, i++ ) {
  55.     edit->string[i] = str[j];
  56.     edit->charsz[i] = XTextWidth(edit->fontstruct, &str[j], 1);
  57.     edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  58.   }
  59.   edit->char_cnt += len;
  60.   edit->active_position += len;
  61.   /* update running lengths */
  62.   for( i--; i <= edit->char_cnt; i++ )
  63.     edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  64.   redraw_after_insert(edit, len);
  65. }
  66.  
  67. /*
  68.  * Subroutine:    delete_chars_backward
  69.  * Purpose:    Delete characters before the active position
  70.  */
  71. void delete_chars_backward ( edit, len )
  72.      EditStruct *edit;
  73.      int len;
  74. {
  75.   int i, j;
  76.   void redraw_after_delete();
  77.  
  78.   /* delete before beginning of line? */
  79.   if( len > edit->active_position ) {
  80.     (void)fprintf(stderr, "Beginning of line!\n");
  81.     if( edit->active_position > 0 )
  82.       len = edit->active_position;
  83.     else
  84.       return;
  85.   }
  86.   /* move later characters backward */
  87.   for( i = edit->active_position, j = i - len;
  88.        i <= edit->char_cnt; i++, j++ ) {
  89.     edit->charsz[j] = edit->charsz[i];
  90.     edit->string[j] = edit->string[i];
  91.   }
  92.   edit->char_cnt -= len;
  93.   edit->active_position -= len;
  94.   /* update running lengths */
  95.   for( i=edit->active_position; i<=edit->char_cnt; i++ )
  96.     edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];
  97.   redraw_after_delete(edit);
  98. }
  99.  
  100. /*
  101.  * Subroutine:    delete_chars_forward
  102.  * Purpose:    Delete characters from the active position forward
  103.  */
  104. void delete_chars_forward ( edit, len )
  105.      EditStruct *edit;
  106.      int len;
  107. {
  108.   int i, j;
  109.   void redraw_after_delete();
  110.  
  111.   /* delete beyond end of line? */
  112.   if( (edit->active_position + len) > edit->char_cnt ) {
  113.     if( edit->active_position < edit->char_cnt )
  114.       len = edit->char_cnt - edit->active_position;
  115.     else
  116.       return;
  117.   }
  118.   /* move later characters backward */
  119.   for( j = edit->active_position, i = j + len;
  120.        i <= edit->char_cnt; i++, j++ ) {
  121.     edit->charsz[j] = edit->charsz[i];
  122.     edit->string[j] = edit->string[i];
  123.   }
  124.   edit->char_cnt -= len;
  125.   /* update running lengths */
  126.   for( i=edit->active_position; i<=edit->char_cnt; i++ )
  127.     edit->pixlen[i+1] = edit->pixlen[i] + edit->charsz[i];    
  128.   redraw_after_delete(edit);
  129. }
  130.  
  131. /*
  132.  * Subroutine:    next_word_end
  133.  * Returns:    The distance from the active position to the next space
  134.  *        following a character (or the end of the line)
  135.  */
  136. int next_word_end ( edit )
  137.      EditStruct *edit;
  138. {
  139.   int i;
  140.   for( i=edit->active_position;
  141.        (i < edit->char_cnt) && ((edit->string[i] == ' ') ||
  142.                   (edit->string[i] == '/') ||
  143.                   (edit->string[i] == ',') ||
  144.                   (edit->string[i] == '.') ||
  145.                   (edit->string[i] == ';') ||
  146.                   (edit->string[i] == ':')); i++ );
  147.   /* string[char_cnt]==' ', so loop will stop there, if nowhere else */
  148.   if( i < edit->char_cnt ) {
  149.     while( (edit->string[i] != ' ') && (edit->string[i] != '/') &&
  150.        (edit->string[i] != ',') && (edit->string[i] != '.') &&
  151.        (edit->string[i] != ';') && (edit->string[i] != ':') )
  152.       i++;
  153.   }
  154.   return( i - edit->active_position );
  155. }
  156.  
  157. /*
  158.  * Subroutine:    last_word_start
  159.  * Returns:    The distance from the active position to the first character
  160.  *        following a space found in a backward search (or the start of
  161.  *        the line)
  162.  */
  163. int last_word_start ( edit )
  164.      EditStruct *edit;
  165. {
  166.   int i;
  167.   if( edit->active_position == 0 )
  168.     return( 0 );
  169.   for( i = edit->active_position - 1;
  170.        (i >= 0) && ((edit->string[i] == ' ') || (edit->string[i] == '/') ||
  171.             (edit->string[i] == ',') || (edit->string[i] == '.') ||
  172.             (edit->string[i] == ';') || (edit->string[i] == ':'));
  173.       i-- );
  174.   if( i >= 0 ) {
  175.     while( (i >= 0) &&
  176.        ((edit->string[i] != ' ') && (edit->string[i] != '/') &&
  177.         (edit->string[i] != ',') && (edit->string[i] != '.') &&
  178.         (edit->string[i] != ';') && (edit->string[i] != ':')) )
  179.       i--;
  180.   }
  181.   return( (i+1) - edit->active_position );
  182. }
  183.