home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / util / vim-2.0.lha / Vim-2.0 / src / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-15  |  3.4 KB  |  163 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMproved
  4.  *
  5.  * Code Contributions By:    Bram Moolenaar            mool@oce.nl
  6.  *                            Tim Thompson            twitch!tjt
  7.  *                            Tony Andrews            onecom!wldrdg!tony 
  8.  *                            G. R. (Fred) Walter        watmath!watcgl!grwalter 
  9.  */
  10.  
  11. /*
  12.  * help.c: display help from the vim.hlp file
  13.  */
  14.  
  15. #include "vim.h"
  16. #include "globals.h"
  17. #include "proto.h"
  18. #include "param.h"
  19.  
  20. static long helpfilepos;        /* position in help file */
  21. static FILE *helpfd;            /* file descriptor of help file */
  22.  
  23. #define MAXSCREENS 52            /* one screen for a-z and A-Z */
  24.  
  25.     void
  26. help()
  27. {
  28.     int        c;
  29.     int        eof;
  30.     int        screens;
  31.     int        i;
  32.     long    filepos[MAXSCREENS];    /* seek position for each screen */
  33.     int        screennr;            /* screen number; index == 0, 'c' == 1, 'd' == 2, etc */
  34. #ifdef MSDOS
  35.     char    *fnamep;
  36. #endif
  37.  
  38. /*
  39.  * try to open the file specified by the "helpfile" option
  40.  */
  41.     if ((helpfd = fopen(p_hf, READBIN)) == NULL)
  42.     {
  43. #ifdef MSDOS
  44.     /*
  45.      * for MSDOS: try the DOS search path
  46.      */
  47.         fnamep = searchpath("vim.hlp");
  48.         if (fnamep == NULL || (helpfd = fopen(fnamep, READBIN)) == NULL)
  49.         {
  50.             smsg("Sorry, help file \"%s\" and \"vim.hlp\" not found", p_hf);
  51.             return;
  52.         }
  53. #else
  54.         smsg("Sorry, help file \"%s\" not found", p_hf);
  55.         return;
  56. #endif
  57.     }
  58.     helpfilepos = 0;
  59.     screennr = 0;
  60.     for (i = 0; i < MAXSCREENS; ++i)
  61.         filepos[i] = 0;
  62.     State = HELP;
  63.     for (;;)
  64.     {
  65.         screens = redrawhelp();                /* show one or more screens */
  66.         eof = (screens < 0);
  67.         if (!eof && screennr + screens < MAXSCREENS)
  68.             filepos[screennr + screens] = ftell(helpfd);
  69.  
  70.         if ((c = vgetc()) == '\n' || c == '\r' || c == Ctrl('C') || c == ESC)
  71.             break;
  72.  
  73.         if (c == ' ' ||
  74. #ifdef MSDOS
  75.                 (c == K_NUL && vpeekc() == 'Q') ||    /* page down */
  76. #endif
  77.                 c == Ctrl('F'))                        /* one screen forwards */
  78.         {
  79.             if (screennr < MAXSCREENS && !eof)
  80.                 ++screennr;
  81.         }
  82.         else if (c == 'a')                    /* go to first screen */
  83.             screennr = 0;
  84.         else if (c == 'b' ||
  85. #ifdef MSDOS
  86.                 (c == K_NUL && vpeekc() == 'I') ||    /* page up */
  87. #endif
  88.                 c == Ctrl('B'))                    /* go one screen backwards */
  89.         {
  90.             if (screennr > 0)
  91.                 --screennr;
  92.         }
  93.         else if (isalpha(c))                /* go to specified screen */
  94.         {
  95.             if (isupper(c))
  96.                 c = c - 'A' + 'z' + 1;        /* 'A' comes after 'z' */
  97.             screennr = c - 'b';
  98.         }
  99. #ifdef MSDOS
  100.         if (c == K_NUL)
  101.             c = vgetc();
  102. #endif
  103.         for (i = screennr; i > 0; --i)
  104.             if (filepos[i])
  105.                 break;
  106.         fseek(helpfd, filepos[i], 0);
  107.         while (i < screennr)
  108.         {
  109.             while ((c = getc(helpfd)) != '\f' && c != -1)
  110.                 ;
  111.             if (c == -1)
  112.                 break;
  113.             filepos[++i] = ftell(helpfd);    /* store the position just after the '\f' */
  114.         }
  115.         screennr = i;                        /* required when end of file reached */
  116.         helpfilepos = filepos[screennr];
  117.     }
  118.     State = NORMAL;
  119.     script_winsize_pp();
  120.     fclose(helpfd);
  121.     updateScreen(CLEAR);
  122. }
  123.  
  124.     int
  125. redrawhelp()
  126. {
  127.     int nextc;
  128.     int col;
  129.     int    line = 0;
  130.     int    screens = 1;
  131.  
  132.     fseek(helpfd, helpfilepos, 0);
  133.     outstr(T_ED);
  134.     windgoto(0,0);
  135.     while ((nextc = getc(helpfd)) != -1 && (nextc != '\f' || line < Rows - 24))
  136.     {
  137.         if (nextc == Ctrl('B'))            /* begin of invert */
  138.             outstr(T_TI);
  139.         else if (nextc == Ctrl('E'))    /* end of invert */
  140.             outstr(T_TP);
  141.         else if (nextc == '\f')            /* start of next screen */
  142.         {
  143.             ++screens;
  144.             outchar('\n');
  145.             ++line;
  146.         }
  147.         else
  148.         {
  149.             outchar((char)nextc);
  150.             if (nextc == '\n')
  151.                 ++line;
  152.         }
  153.     }
  154.     windgoto(0, (int)(Columns - strlen(Version) - 1));
  155.     outstrn(Version);
  156.     col = (int)Columns - 52;
  157.     if (col < 0)
  158.         col = 0;
  159.     windgoto((int)Rows - 1, col);
  160.     outstrn("<space = next; return = quit; a = index; b = back>");
  161.     return (nextc == -1 ? -1 : screens);
  162. }
  163.