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

  1. # $Header:   P:/source/ppee/macros/state.pev   1.42   21 Aug 1990 09:22:02   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:   state.pel  $: config/state file reading and writing
  20.  
  21. ###
  22. ###  Editor Configuration File
  23. ###
  24.  
  25. ## global variable to enable (1) or disable (0) state files
  26. #
  27. global    save_state = 0                        #PUBLIC #INT
  28. global    initialize_config_file    # used only first time editor is invoked
  29.  
  30. local    state_offset
  31.  
  32. ## read configuration and state information saved in a cfg file
  33. #
  34. global function read_config_file() {            #NO LONGER PUBLIC #INT
  35.  
  36.     local    cfgName
  37.     local    cf
  38.     local    cmd
  39.     local    pos
  40.     local    buf
  41.     local    fid
  42.     local    prevEol = default_buffer_eol_string
  43.  
  44.     # locate the config file in the current directory or SAGEEDIT path
  45.     if ( !(cfgName = locate_sageedit_file( SAGEEDIT_FILE_CONFIG ))) {
  46.         return
  47.     }
  48.  
  49.     # create a system buffer for the config file
  50.     default_buffer_eol_string = "\r\n"
  51.     cf = create_buffer( \
  52.             "", \
  53.             cfgName, \
  54.             BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY )
  55.  
  56.     # restore the default end of line sequence
  57.     default_buffer_eol_string = prevEol
  58.  
  59.     if ( cf ) {
  60.         current_buffer = buf = cf
  61. #        goto_buffer_top()
  62.  
  63.         do {
  64.             if ( (cmd = read_buffer()) != "" ) {
  65.                 # comments must have a "#" in col 1
  66.                 if ( substr(cmd,1,1) == "#" ) {
  67.                     continue
  68.                 }
  69.                 if ( (cmd = trim( cmd )) ) {
  70.                     if ( cmd == "$STATE$" ) {
  71.                         state_offset = buffer_offset
  72.                         if ( !(0+save_state) || ARGC != 1 )
  73.                             break
  74.                     } else {
  75.                         current_buffer = buf
  76.  
  77.                         # execute config command
  78.                         if ((fid = quoted_function_id(cmd))) {
  79.                             execute_function( fid )
  80.                         } else {
  81.                             warning( "config: \"%s\" is invalid", cmd )
  82.                         }
  83.  
  84.                         buf = current_buffer
  85.                         current_buffer = cf
  86.                     }
  87.                 }
  88.             }
  89.         } while ( down( 1 ) )
  90.  
  91.         # delete the temporary system buffer and make the last buffer
  92.         # in the list the current buffer.  (process_command_line will
  93.         # do a next_buffer() to make the first buffer current.)
  94.  
  95.         delete_buffer( cf )
  96.     }
  97.  
  98.     # reset the state file the first time the editor is invoked
  99.     if ( initialize_config_file == TRUE ) {
  100.         initialize_config_file = FALSE
  101.         resetStateFIle( cfgName )
  102.         setup()
  103.     }
  104.  
  105.     # install an event to write state information upon exit
  106.     attach_event_handler( EVENT_EXIT_EDITOR,    \
  107.             function_id( "write_state_file" ) )
  108.  
  109.     return (cf != 0)
  110. }
  111.  
  112. # empty the state file
  113. local function resetStateFIle( cfgName ) {
  114.     local    lfil
  115.  
  116.     if ( (lfil = fopen( cfgName, 1 )) >= 0 ) {
  117.         fprintf( lfil, "# Sage Professional Editor - Configuration File\n" )
  118.         fclose( lfil )
  119.     }
  120. }
  121.  
  122.  
  123. ## event handler to save the current editor state to the config file
  124. #
  125. global function write_state_file() {
  126.  
  127.     local    cfgName
  128.     local    cf
  129.     local    firstBuf
  130.     local    buf
  131.     local    fmt
  132.     local    flg
  133.     local    newline_required
  134.     local    noStartWin = 1
  135.     local    firstWin
  136.     local    icon, icon_x, icon_y
  137.     local    prevEol = default_buffer_eol_string
  138.  
  139.     if ( !(0+save_state) ) {
  140.         return 0
  141.     }
  142.  
  143.  
  144.     # search sageedit path for the config file
  145.     if ( !(cfgName = locate_sageedit_file(SAGEEDIT_FILE_CONFIG))) {
  146.         cfgName = editor_path( SAGEEDIT_FILE_CONFIG )
  147.     }
  148.  
  149.     if ((cf = fopen( cfgName, 2 )) < 0) {
  150.         return 0
  151.     }
  152.  
  153. # locate $STATE$ keyword
  154.  
  155.     fseek( cf, state_offset, 0 )
  156.     if ( fgetc( cf ) != ord("$") ) {
  157.  
  158.         # If we reach here, the config file must be re-initialized.
  159.         # This happens whenever no config file exists, or when the
  160.         # config file is modified after the call to read_config_file.
  161.  
  162.         # open config file as a system buffer
  163.         fclose( cf )
  164.         default_buffer_eol_string = "\r\n"
  165.         current_buffer = create_buffer( \
  166.                 "Config File", \
  167.                 cfgName, \
  168.                 BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY )
  169.  
  170.         # restore the default end of line sequence
  171.         default_buffer_eol_string = prevEol
  172.  
  173.         # find the seek position of $STATE$
  174.         if ( !search( /^\$STATE\$[ \t]*$/, SEARCH_FWD_REGEX ) ) {
  175.             goto_buffer_bottom()
  176.             if ( current_line_length ) {
  177.                 newline_required = 1
  178.             }
  179.         }
  180.         state_offset = buffer_offset
  181.  
  182.         # re-open config file as a writable file
  183.         delete_buffer()
  184.         if ((cf = fopen( cfgName, 2 )) < 0)
  185.             return 0;
  186.  
  187.         fseek( cf, state_offset, 0 )
  188.  
  189.         # ensure $STATE$ appears in column 1
  190.         if ( newline_required ) {
  191.             fprintf( cf, "\n$" )
  192.         } else {
  193.             fprintf( cf, "$" )
  194.         }
  195.     }
  196.  
  197.  
  198. # append state information
  199.  
  200.     fprintf( cf, "STATE$\n" )
  201.     fprintf( cf, "#Sage Professional Editor - State Information\t\t%s\n", ctime() )
  202.     fprintf( cf, "search_flags=%d\n",    search_flags )
  203.     if ( search_pattern != "" )
  204.         fprintf( cf, "search_pattern=%s\n", search_pattern )
  205.     if ( search_replacement != "" )
  206.         fprintf( cf, "search_replacement=%s\n", search_replacement)
  207.     if ( display_height > 25 )
  208.         fprintf( cf, "toggle_display 1\n" )
  209.  
  210.     buf = firstBuf = current_buffer
  211.  
  212.     # ensure that firstBuf is not a system buffer
  213.     if ( and( buffer_flags, BUFFER_SYSTEM ) ) {
  214.         firstBuf = next_buffer()
  215.     }
  216.  
  217.     fmt =    "edit_file %s\n"            \
  218.         "buffer_name=%s\n"            \
  219.         "buffer_flags=%d\n"            \
  220.         "current_line=%d\n"            \
  221.         "current_column=%d\n"            \
  222.         "buffer_tabs=%s\n"
  223.  
  224.     do {
  225.         if (buffer_filename) {
  226.             flg = and( buffer_flags,        \
  227.                     not(BUFFER_MODIFIED    \
  228.                         +BUFFER_AUTOSAVE_MODIFIED \
  229.                         +BUFFER_IN_VIRTUAL_SPACE) \
  230.                 )                \
  231.  
  232.             fprintf( cf, fmt, buffer_filename,    \
  233.                     buffer_name,        \
  234.                     flg,            \
  235.                     current_line,        \
  236.                     current_column,        \
  237.                     buffer_tabs )
  238.         }
  239.     } while ( next_buffer() != firstBuf )
  240.  
  241.  
  242.     firstWin = current_window;
  243.  
  244.     do {
  245.         next_window( "", 1 );
  246.         # don't save information on system windows
  247.         if ( !and( window_flags, WINDOW_SYSTEM ) ) {
  248.             if (noStartWin) {
  249.                 fprintf( cf, "delete_default_window\n" );# make a least one window
  250.                 noStartWin = 0;
  251.             }
  252.  
  253.             icon = and( window_flags, WINDOW_ZOOM )
  254.             if ( icon != WINDOW_NORMAL ) {
  255.                 # window is an icon or expanded
  256.                 if ( icon == WINDOW_COLLAPSED ) {
  257.                     icon_x = window_x0
  258.                     icon_y = window_y0
  259.                 }
  260.                 restore_window()
  261.             }
  262.  
  263.             fprintf( cf, "create_window %d %d %d %d %d\n",
  264.                 window_x0,
  265.                 window_y0,
  266.                 window_width,
  267.                 window_height,
  268.                 window_flags );
  269.  
  270.             fprintf( cf, "color_text=%d\n", color_text );
  271.             fprintf( cf, "color_border=%d\n", color_border );
  272.             fprintf( cf, "next_window\n" );
  273.             fprintf( cf, "next_buffer %s\n", buffer_name );
  274.             fprintf( cf, "attach_win_to_buf\n" );
  275.  
  276.             if ( icon != WINDOW_NORMAL ) {
  277.                 if ( icon == WINDOW_COLLAPSED ) {
  278.                     # make window an icon
  279.                     fprintf( cf, "collapse_window\n" )
  280.                     fprintf( cf, "frame_window %d %d 14 1\n",
  281.                         icon_x, icon_y )
  282.                 } else {
  283.                     # make window expanded
  284.                     fprintf( cf, "expand_window\n" )
  285.                 }
  286.             }
  287.         }
  288.     } while (firstWin != current_window);
  289.  
  290. #    fprintf( cf, "next_buffer %s\n", buffer_name )
  291.  
  292.     current_buffer = buf
  293.  
  294.     fwrite( cf, 0, 0 );            # truncate the file
  295.     fclose( cf )
  296.  
  297.     return 1
  298. }
  299.  
  300. #
  301. # attach_win_to_buf()
  302. #
  303. # provide a means of attaching the current window to the current buffer 
  304. # from within an execute_function call.
  305. #
  306. global function attach_win_to_buf(){
  307.     attach_window_buffer( current_window, current_buffer );
  308. }
  309.  
  310. global function delete_default_window(){
  311.     local cw = current_window;
  312.     local wf = window_flags;
  313.  
  314.     next_window();
  315.  
  316.     while( !and( window_flags, WINDOW_SYSTEM ) && current_window) {
  317.         delete_window();
  318.         next_window();
  319.     }
  320.  
  321.     if (and( wf, WINDOW_SYSTEM) && cw)
  322.         assign_current_window( cw );
  323. }