home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / less5 / part03 / position.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-22  |  2.2 KB  |  110 lines

  1. /*
  2.  * Routines dealing with the "position" table.
  3.  * This is a table which tells the position (in the input file) of the
  4.  * first char on each currently displayed line.
  5.  *
  6.  * {{ The position table is scrolled by moving all the entries.
  7.  *    Would be better to have a circular table 
  8.  *    and just change a couple of pointers. }}
  9.  */
  10.  
  11. #include "less.h"
  12. #include "position.h"
  13.  
  14. #define    NPOS    100        /* {{ sc_height must be less than NPOS }} */
  15. static POSITION table[NPOS];    /* The position table */
  16.  
  17. extern int sc_width, sc_height;
  18.  
  19. /*
  20.  * Return the starting file position of a line displayed on the screen.
  21.  * The line may be specified as a line number relative to the top
  22.  * of the screen, but is usually one of these special cases:
  23.  *    the top (first) line on the screen
  24.  *    the second line on the screen
  25.  *    the bottom line on the screen
  26.  *    the line after the bottom line on the screen
  27.  */
  28.     public POSITION
  29. position(where)
  30.     int where;
  31. {
  32.     switch (where)
  33.     {
  34.     case BOTTOM:
  35.         where = sc_height - 2;
  36.         break;
  37.     case BOTTOM_PLUS_ONE:
  38.         where = sc_height - 1;
  39.         break;
  40.     case MIDDLE:
  41.         where = sc_height / 2;
  42.     }
  43.     return (table[where]);
  44. }
  45.  
  46. /*
  47.  * Add a new file position to the bottom of the position table.
  48.  */
  49.     public void
  50. add_forw_pos(pos)
  51.     POSITION pos;
  52. {
  53.     register int i;
  54.  
  55.     /*
  56.      * Scroll the position table up.
  57.      */
  58.     for (i = 1;  i < sc_height;  i++)
  59.         table[i-1] = table[i];
  60.     table[sc_height - 1] = pos;
  61. }
  62.  
  63. /*
  64.  * Add a new file position to the top of the position table.
  65.  */
  66.     public void
  67. add_back_pos(pos)
  68.     POSITION pos;
  69. {
  70.     register int i;
  71.  
  72.     /*
  73.      * Scroll the position table down.
  74.      */
  75.     for (i = sc_height - 1;  i > 0;  i--)
  76.         table[i] = table[i-1];
  77.     table[0] = pos;
  78. }
  79.  
  80. /*
  81.  * Initialize the position table, done whenever we clear the screen.
  82.  */
  83.     public void
  84. pos_clear()
  85. {
  86.     register int i;
  87.  
  88.     for (i = 0;  i < sc_height;  i++)
  89.         table[i] = NULL_POSITION;
  90. }
  91.  
  92. /*
  93.  * See if the byte at a specified position is currently on the screen.
  94.  * Check the position table to see if the position falls within its range.
  95.  * Return the position table entry if found, -1 if not.
  96.  */
  97.     public int
  98. onscreen(pos)
  99.     POSITION pos;
  100. {
  101.     register int i;
  102.  
  103.     if (pos < table[0])
  104.         return (-1);
  105.     for (i = 1;  i < sc_height;  i++)
  106.         if (pos < table[i])
  107.             return (i-1);
  108.     return (-1);
  109. }
  110.