home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / LIBRARY.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  12.1 KB  |  507 lines

  1. # $Header:   P:/source/ppee/macros/library.pev   1.35   27 Aug 1990 12:41:06   ericj  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   library.pel  $: On line reference manual
  20.  
  21. local    libraryBuffer;        # buffer containing reference material in file
  22. local    libraryListBuffer;    # buffer containing list of reference names
  23. local    libraryDispBuffer;    # buffer containing reference information
  24. local    lastLibraryLine = 1    # line displayed during last call to library()
  25.  
  26. local    srchfwdregex = 0x35;    # search_flags
  27. #local    srchbwdregex = 0x31;    # search_flags
  28. local    srchfwdregexwrap = 0x3D    # search_flags
  29.  
  30. local    saveWindow;
  31. local    saveBuffer;
  32. local    libraryListWindow;    # window containing list of reference names
  33. local    libraryDispWindow;    # window containing reference information
  34. local    indexArray[];
  35. local    tmpArray[];
  36.  
  37. local    windowHeight;
  38.  
  39. local     libraryInitialized = 0;
  40. local    libraryResident = 0;
  41. local    libraryId;        # function id of libraryWindowHandler()
  42.  
  43. local    save_last_window;
  44.  
  45.  
  46. local function library_init() {
  47.     local    buffOff;
  48.     local    len;
  49.     local    str;
  50.     local    cb = current_buffer;
  51.     local    save_eol_str = default_buffer_eol_string;
  52.     local    size;
  53.     local    txtfile
  54.  
  55.  
  56.     message( "Initializing Library System..." );
  57.  
  58.     init_menu_colors();
  59.  
  60.     # search sageedit path for the on-line manual
  61.     if ( !(txtfile = locate_sageedit_file(SAGEEDIT_FILE_REFMAN))) {
  62.         txtfile = editor_path( SAGEEDIT_FILE_REFMAN )
  63.     }
  64.  
  65.     default_buffer_eol_string = "\010";
  66.     current_buffer = 
  67.     libraryBuffer = create_buffer( "", txtfile,
  68.             BUFFER_READ_ONLY + BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY)
  69.     buffer_flags = and( buffer_flags, not(BUFFER_READ_ONLY) );
  70.     size = buffer_size;
  71.     current_buffer = cb;
  72.     if (!size) {
  73.         # couldn't find the help information
  74.         default_buffer_eol_string = save_eol_str;
  75.         notify( "Cannot find help information in: %s", txtfile );
  76.         delete_buffer( libraryBuffer );
  77.         return FALSE;
  78.     }
  79.  
  80.  
  81.     default_buffer_eol_string = "\r";
  82.     libraryListBuffer = create_buffer( \
  83.             "Library", \
  84.             "", \
  85.             BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY )
  86.  
  87.     default_buffer_eol_string = "\010";
  88.     libraryDispBuffer = create_buffer( \
  89.             "Library", \
  90.             "", \
  91.             BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY )
  92.     default_buffer_eol_string = save_eol_str;
  93.  
  94.     #
  95.     # read the offset of the indices and library text from the first two
  96.     # lines in the library file
  97.     #
  98.     current_buffer = libraryListBuffer;
  99.     transfer( libraryBuffer, 1, 1, 1, 8000 );
  100.     goto_buffer_bottom();
  101.     backspace();
  102.  
  103.     current_buffer = libraryBuffer;
  104.     delete_line();
  105.     split( read_buffer(), indexArray, "\r" );
  106.     delete_line();
  107.  
  108.     current_buffer = cb;
  109.     message( "" );
  110.  
  111.     libraryInitialized = 1;
  112.     return TRUE;
  113. }
  114.  
  115.  
  116.  
  117. global function library( item ) {
  118.     local    x0 = 3
  119.     local    y0 = 3
  120.     local    libraryListWindowWidth = 22
  121.     local   is_diag = 0+dialog_window
  122.     local   cb;
  123.  
  124.     if (!is_diag)
  125.         toggle_dialog( 1 );
  126.  
  127.  
  128.     if (libraryResident) {
  129.         delete_event( EVENT_NEW_CURNT_WINDOW, libraryId );
  130.         libraryResident = 0;
  131.     }
  132.  
  133.     saveWindow = current_window;
  134.     saveBuffer = current_buffer;
  135.  
  136.     windowHeight = display_height - y0 -y0;
  137.  
  138.     if (!libraryInitialized)
  139.         if (!library_init()) {
  140.             toggle_dialog( is_diag ) 
  141.             return;
  142.         }
  143.  
  144.  
  145.     if (item) {
  146.         cb = current_buffer;
  147.         current_buffer = libraryListBuffer;
  148.         if ( search( " " item, or(srchfwdregex, SEARCH_WRAPS) ) ){
  149.             lastLibraryLine = current_line;
  150.             current_buffer = cb;
  151.         } else {
  152.             current_buffer = cb;
  153.             toggle_dialog( is_diag );
  154.             beep();
  155.             return;
  156.         }
  157.     }
  158.  
  159.  
  160.     if (libraryDispWindow) {
  161.         raise_window( libraryDispWindow );
  162.     } else {
  163.         #
  164.         # create the display window which will contain the library text
  165.         #
  166.         current_window = 
  167.         libraryDispWindow = create_factory_window( x0+libraryListWindowWidth, y0, display_width - x0 - libraryListWindowWidth - x0,
  168.             windowHeight, 
  169.             WINDOW_SYSTEM + WINDOW_SB_BELOW + WINDOW_SB_RIGHT + WINDOW_BORDER );
  170.     
  171.         initLibraryAttributes( 1 );
  172.         attach_window_buffer( libraryDispWindow, libraryDispBuffer );
  173.     }
  174.  
  175.  
  176.     if (libraryListWindow) {
  177.         raise_window( libraryListWindow );
  178.     } else {
  179.         #
  180.         # create the list window which will contain the list of available
  181.         # library items
  182.         #
  183.         current_window = 
  184.         libraryListWindow = create_factory_window( x0, y0, libraryListWindowWidth, windowHeight,
  185.             WINDOW_SYSTEM + WINDOW_SB_BELOW + WINDOW_SB_RIGHT + WINDOW_BORDER );
  186.         attach_window_buffer( libraryListWindow, libraryListBuffer );
  187.         initLibraryAttributes( 2 );
  188.     }
  189.  
  190.     current_window = libraryListWindow;
  191.     current_buffer = libraryListBuffer;
  192.     goto_buffer_top();
  193.  
  194.     current_line = lastLibraryLine;
  195.  
  196.     menu_search();
  197.  
  198.     save_last_window = current_window;
  199.  
  200.     toggle_dialog( is_diag ) 
  201. }
  202.  
  203.  
  204. #
  205. # initialize the attribute and window names for the library windows
  206. # win == 1 setting display window, win == 2 setting list window
  207. #
  208. local function initLibraryAttributes( win ) {
  209.  
  210.     color_border = BAR_COLOR;
  211.     color_text   = BAR_COLOR;
  212.     color_highlight = HBAR_COLOR
  213.     visible_virtual_lines  = "";
  214.     visible_virtual_spaces = "";
  215.     visible_end_buffer     = "";
  216.  
  217. }
  218.  
  219.  
  220. local function showLibraryItem( lineNum, clear_anchor ) {
  221.     local    cw = current_window;
  222.  
  223.     #
  224.     # locate the item in the database
  225.     #
  226.  
  227.     split( indexArray[ lineNum ], tmpArray, " " );
  228.  
  229.     current_window = libraryDispWindow;
  230.  
  231.     if (clear_anchor)
  232.         raise_anchor();
  233.  
  234.     goto_buffer_top();
  235.     drop_anchor();
  236.     goto_buffer_bottom();
  237.     delete_chars();
  238.  
  239.     transfer( libraryBuffer, atoi( tmpArray[1] ), 1, atoi( tmpArray[2]), 8000 );
  240.     buffer_tabs = "6"
  241.     down()
  242.     insert_string( "────────────────────────────────────────────────\n" );
  243.     goto_buffer_top();
  244.  
  245.     if (cw)
  246.         current_window = cw;
  247. }
  248.  
  249. local function menu_search() {
  250.     local    ch;
  251.     local    xy_win
  252.     local    mpos;
  253.  
  254.     #
  255.     # reset the position to the top of the list and highlight the
  256.     # first entry
  257.     #
  258.     drop_anchor( LINE_SELECTION )
  259.     updateItem( 1 )
  260.  
  261.     do {
  262.         display_update();        # update the screen
  263.  
  264.         ch = getchar();
  265.  
  266.         if (ch == 0) {
  267.             ch = getchar();        # expanded code, get rest of key
  268.             if (ch == SCANCODE_DOWN) {
  269.                 menu_down(1)
  270.             } else if (ch == SCANCODE_UP) {
  271.                 menu_up(1)
  272.             } else if (ch == SCANCODE_PGUP) {
  273.                 menu_up( window_height-1 )
  274.             } else if (ch == SCANCODE_PGDN) {
  275.                 menu_down( window_height-1 )
  276.             } else if ((ch == 0x8d) || (ch == SCANCODE_LEFT)) {
  277.                 libraryDisplayUp();
  278.                 continue;
  279.             } else if ((ch == 0x91) || (ch == SCANCODE_RIGHT)) {
  280.                 libraryDisplayDn();
  281.                 continue;
  282.             } else if ((ch == SCANCODE_LEFT_PRESS) || \
  283.                     (ch == SCANCODE_RIGHT_PRESS) || \
  284.                     (ch == SCANCODE_LEFT_CLICK)) {
  285.                 xy_win = window_containing( mouse_event_x, mouse_event_y );
  286.                 if ( (xy_win == libraryListWindow) ||
  287.                     (xy_win == libraryDispWindow) ) {
  288.                     mpos = mouse_position(1);
  289.                     if ( !and(mpos,MOUSE_W) ){
  290.                         if (!((current_window == libraryListWindow) && \
  291.                             (ch == SCANCODE_RIGHT_PRESS))) {
  292.                             assign_current_window( xy_win );
  293.                             setHighlightedScrolling( current_window == libraryListWindow );
  294.                             if (ch == SCANCODE_RIGHT_PRESS)
  295.                                 right_press( 1 );
  296.                             else            # LEFT PRESS or CLICK
  297.                                 left_press( 1 );
  298.  
  299.                             display_update();
  300.                             setHighlightedScrolling( 0 );
  301.                         }
  302.                     }
  303.                     current_window = libraryListWindow;
  304.                     display_update();
  305.                 } else if (xy_win) {
  306.                     raise_anchor();
  307.                     assign_current_window( xy_win );
  308.                     installLibraryHandler();
  309.                     ungetkey( shiftl(ch,8) );
  310.                     return;
  311.                 }
  312.  
  313.                 if ( !region_type() )
  314.                     drop_anchor( LINE_SELECTION );
  315.             }
  316.         } else {
  317.             if (ch == ASCII_ESC) {
  318.                 prev_window();
  319.                 esc();
  320.                 return;
  321.             } else if (ch == ASCII_CR) {
  322.                 current_window = libraryDispWindow;
  323.                 #
  324.                 # copy any marked block to scrap
  325.                 # and return
  326.                 #
  327.                 if (region_type()) {
  328.                     copy_to_scrap();
  329.                     raise_anchor();
  330.                 }
  331.                 current_window = libraryListWindow;
  332.                 prev_window()
  333.                 installLibraryHandler();
  334.                 return;
  335.             } else {
  336.                 menu_search_i( ch );
  337.             }
  338.         }
  339.         updateItem()
  340.     } while (TRUE);
  341. }
  342.  
  343.  
  344. global function libraryWindowHandler(){
  345.     if ( and(mouse_buttons, LEFT_BUTTON) || 
  346.             (current_key == LEFT_CLICK) ||
  347.             (current_key == LEFT_PRESS) ) {
  348.         if ((current_window == libraryListWindow) ||
  349.             (current_window == libraryDispWindow)) {
  350.             library();
  351.  
  352.             assign_current_window( save_last_window );
  353.     
  354.             # see if the save_last_window was deleted
  355.             if ((current_window == libraryListWindow) ||
  356.                 (current_window == libraryDispWindow))
  357.                 prev_window();
  358.             # flush_keyboard();
  359.         }
  360.     } else if ( !and(window_flags, WINDOW_SYSTEM) )
  361.         save_last_window = current_window;
  362. }
  363.  
  364. local function installLibraryHandler(){
  365.     libraryResident = 1;
  366.     libraryId = function_id( "libraryWindowHandler" );
  367.     attach_event_handler( EVENT_NEW_CURNT_WINDOW, libraryId );
  368. }
  369.  
  370. local function libraryDisplayDn(){
  371.     scrollDispWindow( 2 );
  372. }
  373.  
  374. local function libraryDisplayUp(){
  375.     scrollDispWindow( -2 );
  376. }
  377.  
  378. local function scrollDispWindow( inc ){
  379.     current_window = libraryDispWindow;
  380.     scroll_vertical( inc, 1 );
  381.     current_window = libraryListWindow;
  382.     # flush_keyboard();
  383. }
  384.  
  385.  
  386. #
  387. # process an escape key pressed from within the dir menu
  388. #
  389. local function esc() {
  390.     raise_anchor();
  391.     assign_current_window( saveWindow );
  392.     current_buffer = saveBuffer;
  393.     delete_window( libraryListWindow )
  394.     delete_window( libraryDispWindow )
  395.     libraryListWindow = 0;
  396.     libraryDispWindow = 0;
  397.     current_key = 0;
  398.     message( "" );
  399. }
  400.  
  401.  
  402. #
  403. # process a down key press from within the dir menu
  404. #
  405. local function menu_down( lines ) {
  406.  
  407.     if ( down( lines )) {
  408.         #
  409.         # redisplay the highlight on the new selection
  410.         #
  411.         raise_anchor()
  412.         drop_anchor( LINE_SELECTION )
  413.     }
  414. }
  415.  
  416.  
  417. #
  418. # process an up key press from within the dir menu
  419. #
  420. local function menu_up( lines ){
  421.  
  422.     if ( up( lines )) {
  423.         #
  424.         # redisplay the highlight on the new selection
  425.         #
  426.         raise_anchor()
  427.         drop_anchor( LINE_SELECTION )
  428.     }
  429. }
  430.  
  431.  
  432. local function menu_search_i( ch ) {
  433.     local    pattern = "",
  434.         char     = "",
  435.         level    = 0
  436.  
  437.     display_update();
  438.  
  439.     while (TRUE) {
  440.  
  441.         if ((ch == 0) || (ch == ASCII_ESC) || (ch == ASCII_CR)) {
  442.             break;
  443.         } else if (ch == ASCII_BACKSPACE) {
  444.             #
  445.             # remove the last char in the search string and
  446.             # restore the previous position
  447.             #
  448.             pattern = substr( pattern, 0, length( pattern ) - 1)
  449.             if (level) {
  450.                 raise_anchor()
  451.                 restore_position( TRUE );
  452.                 drop_anchor( LINE_SELECTION );
  453.                 level--;
  454.             }
  455.         } else {
  456.             #
  457.             # It is a normal ascii character.
  458.             # Let's search for the new pattern.  If we find it,
  459.             # save the previous position and advance to the
  460.             # newly found string.
  461.             #
  462.             char = chr( ch );
  463.             raise_anchor();
  464.             save_position();
  465.             if (search( " " pattern char, srchfwdregexwrap )){
  466.                 pattern = pattern char
  467.                 level++;
  468.             } else {
  469.                 beep();
  470.                 restore_position( FALSE );
  471.             }
  472.             drop_anchor( LINE_SELECTION );
  473.             message( "Library-Search for: %s\137", pattern)
  474.         }
  475.         updateItem( 1 )
  476.  
  477.         display_update()
  478.  
  479.         # display the prompt message with a fake cursor
  480.         message( "Library-Search for: %s\334", pattern)
  481.  
  482.         # get the next ascii character from the keyboard
  483.         # ignore extended character which have an ascii code of 0
  484.         ch = getchar()
  485.     }
  486.     
  487.     message( "" );
  488.  
  489.     while (level--)
  490.         restore_position( FALSE );
  491.  
  492.     display_update();
  493.     ungetkey( ch );
  494. }
  495.  
  496. local function updateItem( force_update ) {
  497.     local    cb = current_buffer;
  498.  
  499.     if (keyboard_input_pending == 0) {
  500.         if (force_update || (lastLibraryLine != current_line)) {
  501.             showLibraryItem( current_line, lastLibraryLine != current_line );
  502.             lastLibraryLine = current_line;
  503.             # flush_keyboard();
  504.         }
  505.     }
  506. }
  507.