home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epmmac.zip / KWHELP.E < prev    next >
Text File  |  1994-03-17  |  9KB  |  237 lines

  1. /********************************************************************/
  2. /* Modified 12/03/93 to include the following changes:              */
  3. /*                                                                  */
  4. /* - check filetype on keyword lookup - Fortran is case insensitive */
  5. /* - when building the help file index, use only indices with       */
  6. /*   EXTENSIONS = '*' or that match the filetype                    */
  7. /* - re-build the helpfile index if filetype has changed since      */
  8. /*   the last time it was built                                     */
  9. /* - do not terminate if one of the help indexes is not found       */
  10. /* - successive tries to match identifier with wildcards is         */
  11. /*   terminated at 1 character + '*' rather than just '*'           */
  12. /*                                                                  */
  13. /********************************************************************/
  14.  
  15. /* format of index file:
  16.      (Win*, view winhelp.inf ~)
  17.      (printf, view edchelp.inf printf)
  18. */
  19.  
  20. const
  21.    FORTRAN_TYPES = 'FXC F F77 F90 FOR FORTRAN'
  22.    GENERAL_NOCASE_TYPES = 'CMD SYS BAT'
  23.  
  24. defc kwhelp = call pHelp_C_identifier()
  25.  
  26. /***********************************************/
  27. /* pHelp_C_identifier()                        */
  28. /***********************************************/
  29. defproc pHelp_C_identifier
  30.    universal savetype, helpindex_id
  31.    ft = filetype()
  32. ;  if savetype = '' then              /* initialize file type so we know when it changes */
  33. ;     savetype = ft
  34. ;  endif
  35.  
  36.    if not find_token(startcol, endcol) then   /* only look for keywords if cursor is on a word */
  37.       return
  38.    endif
  39.  
  40.    call pGet_Identifier(identifier, startcol, endcol, ft)        /* locate the keyword in question */
  41.    if identifier = '' then
  42.       sayerror 'Unable to identify help subject from cursor position in source file'
  43.       return
  44.    endif
  45.  
  46.    getfileid CurrentFile           /* save the id of the current file */
  47.    if helpindex_id then            /* If helpfile is already built ... */
  48.       display -2                      /* then make sure it is still available */
  49.       rc = 0
  50.       activatefile helpindex_id
  51.       display 2
  52.       if rc then  -- File's gone?
  53.          helpindex_id = 0
  54.       else                            /* If helpfile index is already built ... */
  55.          if (ft <> savetype) then     /* then make sure the file extension has not changed */
  56.             savetype = ft                 /* if it has ... reset the file type */
  57.             'quit'
  58.             activatefile CurrentFile
  59.             helpindex_id = 0              /* and mark the helpfile index as unbuilt */
  60.          endif
  61.       endif
  62.    endif
  63.    if not helpindex_id then -- if the helpfile index is not built then build it
  64.       call pBuild_Helpfile(ft)
  65.       if rc then
  66.          sayerror 'Unable to build help file'
  67.          return
  68.       endif
  69.    endif
  70.  
  71.    top; .col = 1
  72.  
  73.    /* search for keyword match */
  74.    display -2
  75.    getsearch savesearch
  76.    /* Alter search criteria based on filetype */
  77.    if wordpos(ft, FORTRAN_TYPES GENERAL_NOCASE_TYPES) then
  78.       case_aware = 'c'      -- Add 'ignore case' parameter for Fortran
  79.    else
  80.       case_aware = ''
  81.    end
  82.    'xcom /('identifier',/' case_aware  -- search for a match...
  83.    if rc then
  84.       do i = length(identifier) to 1 by -1
  85.          'xcom /('leftstr(identifier, i)'*,/' case_aware
  86.          if not rc then
  87.             leave
  88.          endif
  89.       enddo
  90.    endif
  91.    setsearch savesearch
  92.    display 2
  93.  
  94.    if rc then
  95.       sayerror 'Unable to find an entry for 'identifier' in 'helpindex_id.userstring'.'
  96.    else
  97.       parse value substr(textline(.line), .col) with ',' line ')'
  98.       /* Substitute all occurrances of '~' with the original identifier */
  99.       loop
  100.          i = pos('~', line)
  101.          if not i then
  102.             leave
  103.          endif
  104.          line = leftstr(line, i-1)||identifier||substr(line, i+1)
  105.       endloop
  106.  
  107.       /* Execute keyword help command */
  108.       if upcase(word(line,1))='VIEW' then
  109.          sayerror 'Invoking View ...'
  110.       endif
  111.       'dos' line  -- execute the command
  112.    endif
  113.    activatefile CurrentFile
  114.  
  115. /***********************************************/
  116. /* pGet_Identifier()                                                   */
  117. /***********************************************/
  118. defproc pGet_Identifier(var id, startcol, endcol, ft)
  119.  
  120.    getline line
  121.    if wordpos(ft, FORTRAN_TYPES) then        /* Fortran doesn't need to mess w/ C classes */
  122.       id = substr(line, startcol, (endcol-startcol)+1)
  123.       return
  124.    endif
  125. ;; is_class = 0; colon_pos = 0
  126.    if substr(line, endcol+1, 2) = '::' then  -- Class?
  127.       ch = upcase(substr(line, endcol+3, 1))
  128.       if (ch>='A' & ch<='Z') | ch='_' then
  129.          curcol = .col
  130.          .col = endcol+3
  131.          call find_token(junk, endcol)
  132.          .col = curcol
  133. ;;       is_class = 1
  134.       endif
  135.    elseif startcol>3 then
  136.       if substr(line, startcol-2, 2) = '::' then  -- Class?
  137.          ch = upcase(substr(line, startcol-3, 1))
  138.          if (ch>='A' & ch<='Z') | (ch>='0' & ch<='9') | ch='_' then
  139.             curcol = .col
  140.             .col = startcol-3
  141.             call find_token(startcol, junk)
  142.             .col = curcol
  143. ;;          is_class = 2
  144.          endif
  145.       endif
  146.    endif
  147.    id = substr(line, startcol, (endcol-startcol)+1)
  148.  
  149. /***********************************************/
  150. /* pBuild_Helpfile()                                                   */
  151. /***********************************************/
  152. defproc pBuild_Helpfile(ft)
  153.    universal helpindex_id, savetype
  154.    rc = 0
  155.  
  156.    helplist = Get_Env('HELPNDX')
  157.    if helplist='' then
  158.       compile if defined(KEYWORD_HELP_INDEX_FILE)
  159.                     helplist = KEYWORD_HELP_INDEX_FILE
  160.       compile else
  161.                     helplist = 'epmkwhlp.ndx'
  162.       compile endif
  163.    endif
  164.    savelist = helplist
  165.  
  166.    do while helplist<>''
  167.       parse value helplist with helpindex '+' helplist
  168.  
  169.       /* look for the help index file in current dir, EPMPATH, DPATH, and EPM.EXE's dir: */
  170.       findfile destfilename, helpindex, '','D'
  171.  
  172.       if rc then
  173.          /* If that fails, try the standard path. */
  174.          findfile destfilename, helpindex, 'PATH'
  175.          if rc then
  176.             sayerror  'Help index 'helpindex' not found'
  177.             rc = 0
  178.             /* return -- changed this so that error is informational, not severe */
  179.             destfilename = ''
  180.          endif
  181.       endif
  182.  
  183.       if destfilename <> '' then
  184. compile if EVERSION >='5.50'
  185.          if pos(' ',destfilename) then
  186.             destfilename = '"'destfilename'"'
  187.          endif
  188. compile endif
  189.          if helpindex_id then
  190.             bottom
  191.             last = .last
  192.             'get' destfilename
  193.             line = upcase(textline(last+1))
  194.  
  195.             if word(line,1)='EXTENSIONS:' & wordpos(ft, line) then
  196.                /* Give priority to this helpfile by moving it to the top */
  197.                call psave_mark(savemark)
  198.                call pset_mark(last+1, .last, 1, MAXCOL, 'LINE', helpindex_id)
  199.                0
  200.                move_mark
  201.                call prestore_mark(savemark)
  202.             else
  203.                if word(line,1)='EXTENSIONS:' & not wordpos('*', line) then
  204.                   /* This helpfile is not relevant to the file being edited, so remove it */
  205.                   call psave_mark(savemark)
  206.                   call pset_mark(last+1, .last, 1, MAXCOL, 'LINE', helpindex_id)
  207.                   delete_mark
  208.                   call prestore_mark(savemark)
  209.                endif
  210.             endif
  211.  
  212.          else       /* Need to add first .NDX file to the editor ring */
  213.             'xcom e /d' destfilename
  214.             if rc = 0 then
  215.                line = upcase(textline(1))
  216.                if word(line,1)='EXTENSIONS:' & (wordpos(ft, line) | wordpos('*', line)) then        /* only read in 'relevant' files */
  217.                   getfileid helpindex_id -- read in the file
  218.                   .visible = 0
  219.                else
  220.                   /* This helpfile is not relevant to the file being edited, so remove it */
  221.                   'quit'
  222.                endif
  223.             else
  224.                sayerror 'Error reading helpfile ' destfilename
  225.                rc = 8
  226.             endif
  227.          endif -- helpindex_id
  228.       endif -- destfilename <> ''
  229.    enddo
  230.  
  231.    if helpindex_id then            /* If helpfile is already built ... */
  232.       helpindex_id.userstring = savelist
  233.    endif
  234.    savetype = ft
  235.    return rc
  236.  
  237.