home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / p2demo21.exe / PEL / STATE.PEL < prev    next >
Text File  |  1995-04-05  |  40KB  |  1,319 lines

  1. # $Header:   P:\source\wmacros\state.pev   1.143   05 Apr 1995 16:32:54   NOBLE  $
  2.  
  3. ##############################################################################
  4. #
  5. #       Compuware Corporation
  6. #         31440 Northwestern Highway
  7. #           Farmington Hills, Michigan 48334-2564
  8. #
  9. #   This source code listing contains information that is
  10. #   proprietary to Compuware Corporation and may not be copied
  11. #   duplicated, translated, transmitted, stored, retrieved
  12. #   or in any manner or by any method conveyed or disclosed
  13. #   to a third party or parties without express written
  14. #   permission from Compuware Corporation.
  15. #
  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 
  28.  
  29. global SAVE_SETTINGS  = 0x0001
  30. global SAVE_COMMANDS  = 0x0002
  31. global SAVE_SEARCH    = 0x0004
  32. global SAVE_REPLACE   = 0x0008
  33. global SAVE_EDITFILE  = 0x0010
  34. global SAVE_COMPILE   = 0x0020
  35. global SAVE_DIRECTORY = 0x0040
  36.  
  37. global SAVE_DEFAULT   = SAVE_SETTINGS + SAVE_COMMANDS + SAVE_SEARCH + SAVE_REPLACE + SAVE_EDITFILE + SAVE_COMPILE
  38.  
  39. global save_flags     = SAVE_DEFAULT
  40.  
  41. global editor_running
  42.  
  43. local config_buffer_ref_count = 0
  44. local the_config_buffer       = 0
  45.  
  46. local  startup_dir = ""    # last directory if 'save directory on exit' was set. See set_startup_dir().
  47.  
  48. #local DO_FAST_CONFIG = FALSE
  49. local DO_FAST_CONFIG = TRUE
  50.  
  51. ## read startup configuration and state information saved in a cfg file
  52. #
  53. global function read_config_file()           #NO LONGER PUBLIC #INT
  54. {
  55.    local config_array[]
  56.    local config_buffer
  57.    local old_buffer     = current_buffer
  58.    local try_old_state  = FALSE
  59.    local comline_count  = 0
  60.  
  61.    # install an event to write state information upon exit
  62.    attach_event_handler( EVENT.EXIT_EDITOR, function_id( "write_state_file" ) )
  63.  
  64.    # load up the config file and make it the current buffer
  65.    # If the file exists, the function will return a good value, and we
  66.    # can then process the various sections 
  67.    #
  68.    if ( get_config_buffer() )
  69.    {
  70.       # save config_buffer id in case one of the sections changes the 
  71.       # current_buffer
  72.       #
  73.       config_buffer  = current_buffer
  74.       current_buffer = old_buffer
  75.  
  76.       read_config_section( "$SAVE_FLAGS$", config_buffer )
  77.  
  78.       config_array = read_config_section( "$SETTINGS$", config_buffer, DO_FAST_CONFIG )
  79.       do_fast_config( "$SETTINGS$", config_array )
  80.  
  81.       read_config_section( "$BUFFER1_SETTINGS$", config_buffer )
  82.       read_config_section( "$BUFFER2_SETTINGS$", config_buffer )
  83.       read_config_section( "$BUFFER3_SETTINGS$", config_buffer )
  84.  
  85.       read_config_section( "$WINDOW1_SETTINGS$", config_buffer )
  86.       read_config_section( "$WINDOW2_SETTINGS$", config_buffer )
  87.       read_config_section( "$WINDOW3_SETTINGS$", config_buffer )
  88.  
  89.       config_array = read_config_section( "$COLOR_SETTINGS$", config_buffer, DO_FAST_CONFIG )
  90.       process_color_config( config_array )
  91.  
  92.       read_config_section( "$KEY_ASSIGNMENTS$", config_buffer )
  93.  
  94.       # read user settings last so there values override everything else
  95.       read_config_section( "$USER_SETTINGS$", config_buffer )
  96.  
  97.       comline_count = count_command_line_files()
  98.  
  99.       #  This pair of lines reads the entire section into the array, and then
  100.       #  processes the commands as STATE information.  The same technique is
  101.       #  employed below for other sections;
  102.       if ( !comline_count )
  103.       {
  104.          #  Only read the BUFFER_STATE if no files specified on the comand line;
  105.          config_array = read_config_section( "$BUFFER_STATE$", config_buffer, DO_FAST_CONFIG )
  106.  
  107.          if ( !config_array )        #  If no BUFFER_STATE section, try reading the old style STATE section;
  108.             try_old_state = TRUE
  109.          else
  110.            do_fast_config( "$STATE$", config_array )
  111.       }
  112.  
  113.       if(!comline_count || !(comline_count && temp_create_new_bufwin) )
  114.       {
  115.          #  Only read the WINDOW_STATE section if no files on the command line or
  116.          #  files on the command line with one buffer per window mode turned off;
  117.          config_array = read_config_section( "$WINDOW_STATE$", config_buffer, DO_FAST_CONFIG )
  118.          if(config_array)
  119.            do_fast_config( "$STATE$", config_array )
  120.       }
  121.  
  122.       if(comline_count && temp_create_new_bufwin)
  123.       {
  124.          #  If files were passed on the command line and we are in one buffer
  125.          #  per window mode, we want the editor information, but not the window
  126.          #  information.  In this case, we pass a third parameter to do_fast_config
  127.          #  that does not process the window information.
  128.          config_array = read_config_section( "$WINDOW_STATE$", config_buffer, DO_FAST_CONFIG )
  129.          if(config_array)
  130.            do_fast_config( "$STATE$", config_array, TRUE )
  131.  
  132.          create_new_bufwin = 1;
  133.          temp_create_new_bufwin = -1;
  134.       }
  135.  
  136.       if( !config_array && try_old_state )
  137.          read_config_section( "$STATE$", config_buffer)
  138.  
  139.       if ( temp_create_new_bufwin != -1 )
  140.          create_new_bufwin = temp_create_new_bufwin
  141.  
  142.       display_redraw()
  143.  
  144.       # fix the menu in case there are key assignments in config file that matter
  145.       # by calling the corresponding menu function for the current emulation
  146.       if ( emulation_mode )
  147.          execute_function( emulation_mode"_menu")
  148.  
  149.       # cleanup by removing config_buffer
  150.       delete_config_buffer()
  151.    }
  152. }
  153.  
  154. local old_buffer
  155.  
  156. global function read_config_error()
  157. {
  158.    delete_event( EVENT.ERROR, "read_config_error" )
  159.  
  160.    # don't delete config buffer here because we want the user
  161.    # to see where the error occured.
  162.    config_buffer_ref_count--
  163.    if ( config_buffer_ref_count <= 0)
  164.    {
  165.       if (the_config_buffer)
  166.       {
  167.          the_config_buffer = 0
  168.       }
  169.       config_buffer_ref_count = 0
  170.    }
  171. }
  172.  
  173. # read a section from the config file
  174. global function read_config_section( section_name, config_buffer, \
  175.                                      no_execute,   include_comments )
  176. {
  177.    local fid
  178.    local cmd
  179.    local data
  180.    local index           = 1
  181.    local free_buffer     = FALSE
  182.    local re_section_name = ""
  183.  
  184.    attach_event_handler( EVENT.ERROR, "read_config_error" )
  185.  
  186.    old_buffer = current_buffer
  187.  
  188.    # if no config_buffer is specified then get it otherwise use the
  189.    # config_buffer passed in.
  190.    if (argcount() < 2 || !config_buffer)
  191.    {
  192.       if (get_config_buffer())
  193.       {
  194.          config_buffer = current_buffer
  195.          free_buffer   = TRUE
  196.       }
  197.       else
  198.          return
  199.    }
  200.    else
  201.       current_buffer = config_buffer
  202.  
  203.    goto_buffer_top()
  204.  
  205.    re_section_name = section_name
  206.    gsub( "\\$", "\\$", re_section_name )
  207.  
  208.    # look for the section name
  209.    if ( search( "^" re_section_name, SEARCH_FWD_REGEX) )
  210.    {
  211.       # go past section name
  212.       if ( down(1) )
  213.       {
  214.          cmd = ltrim( trim(read_buffer()) )
  215.  
  216.          # go until end of buffer or next section
  217.          while (substr(cmd,1,1) != "$")
  218.          {
  219.             # comments must have a "#" in col 1
  220.             if (cmd != "" && substr(cmd,1,1) != "#" )
  221.             {
  222.                # if the commands are not to be executed, stuff them into the
  223.                # return array
  224.                #
  225.                if (argcount() >= 3 && no_execute)
  226.                {
  227.                   data[index++] = cmd
  228.                }
  229.                # or new buffer flags with ones from edit_file so we don't
  230.                # lose the read only stuff
  231.                #
  232.                else if ( "buffer_flags" == substr(cmd, 1, 12) )
  233.                {
  234.                   current_buffer = old_buffer
  235.                   buffer_flags   = or(buffer_flags, substr(cmd, 14) );
  236.                   current_buffer = config_buffer
  237.                }
  238.                # execute config command
  239.                #
  240.                else if ((fid = quoted_function_id(cmd)))
  241.                {
  242.                   save_position()
  243.                   current_buffer = old_buffer
  244.                   execute_function(fid)
  245.                   old_buffer     = current_buffer
  246.                   current_buffer = config_buffer
  247.                   restore_position(TRUE)
  248.                }
  249.                else
  250.                   warning( "Config: \"%s\" is invalid, line %d.", cmd, current_line )
  251.             }
  252.             else if (argcount() >= 4 && include_comments && cmd != "")
  253.             {
  254.                data[index++] = cmd
  255.             }
  256.  
  257.             if ( !down(1) )
  258.                break
  259.  
  260.             cmd = ltrim( trim(read_buffer()) )
  261.          }
  262.       }
  263.    }
  264.  
  265.    if ( free_buffer )
  266.       delete_config_buffer()
  267.  
  268.    current_buffer = old_buffer
  269.  
  270.    delete_event( EVENT.ERROR, "read_config_error" )
  271.  
  272.    if (argcount() >= 3 && no_execute)
  273.       return data
  274. }
  275.  
  276. global function buffer_state_config()
  277. {
  278.    local firstBuf       = 0
  279.    local buf            = 0
  280.    local flg            = 0
  281.    local lastBuffer     = ""
  282.    local data[]
  283.    local i = 1
  284.  
  285.    # append state information
  286.    data[ i++ ] = sprintf( "# %s - Buffer State Information\t%s\n", editor_title, ctime() )
  287.  
  288.    buf = firstBuf = current_buffer
  289.  
  290.    # ensure that firstBuf is not a system buffer
  291.    if ( and( buffer_flags, BUFFER_SYSTEM ) )
  292.       firstBuf = prev_buffer()
  293.  
  294.    do
  295.    {
  296.       if (buffer_filename && !and(buffer_flags, BUFFER_SCRATCH) )
  297.       {
  298.          # CFV don't save the read-only bit 
  299.          #
  300.          flg = and( buffer_flags, not( BUFFER_READ_ONLY + BUFFER_MODIFIED + BUFFER_AUTOSAVE_MODIFIED + BUFFER_IN_VIRTUAL_SPACE) )
  301.  
  302.          # the new config method is more complex in that you
  303.          # must be sure to insert to the correct buffer!
  304.  
  305.          data[ i++ ] = sprintf(  "BUFFER \"%s\", %d, \"%s\", %d, %d, \"%s\", %d\n",
  306.                                  buffer_filename,
  307.                                  flg,                  
  308.                                  buffer_name,          
  309.                                  current_line,         
  310.                                  current_column,       
  311.                                  buffer_tabs,          
  312.                                  auto_indent_mode )
  313.       }
  314.    } while ( prev_buffer() != firstBuf )
  315.  
  316.    data[ i++ ] = sprintf( "\n" )
  317.  
  318.    current_buffer = firstBuf
  319.  
  320.    return data
  321. }
  322.  
  323. global function window_state_config()
  324. {
  325.    local width          = 0
  326.    local height         = 0
  327.    local x_pos          = 0
  328.    local y_pos          = 0
  329.    local win_x          = 0
  330.    local win_y          = 0
  331.    local editwin_state  = 0
  332.    local window_number  = 1
  333.    local MIN_WIDTH      = 10
  334.    local MIN_HEIGHT     = 10
  335.    local firstWin       = 0
  336.    local noStartWin     = TRUE
  337.    local fmt            = ""
  338.    local lastBuffer     = ""
  339.    local data[]
  340.    local i = 1
  341.  
  342.    # append state information
  343.    data[ i++ ] = sprintf( "# %s - State Information\t%s\n", editor_title, ctime() )
  344.  
  345.    # save the current directory
  346.    if ( and( save_flags, SAVE_DIRECTORY ) )
  347.       data[ i++ ] = sprintf( "set_startup_dir %s\n", getcwd())
  348.  
  349.    # save editwin's normal position and size
  350.    if ( query_editwin_property(EWC_MAXIMIZE) || query_editwin_property(EWC_MINIMIZE) )
  351.    {
  352.       width    = query_editwin_property( EWC_RESTORE_WIDTH  )
  353.       height   = query_editwin_property( EWC_RESTORE_HEIGHT )
  354.       x_pos    = query_editwin_property( EWC_RESTORE_X_POS  )
  355.       y_pos    = query_editwin_property( EWC_RESTORE_Y_POS  )
  356.    }
  357.    else
  358.    {
  359.       width    = query_editwin_property( EWC_WIDTH  )
  360.       height   = query_editwin_property( EWC_HEIGHT )
  361.       x_pos    = query_editwin_property( EWC_X_POS  )
  362.       y_pos    = query_editwin_property( EWC_Y_POS  )
  363.    }
  364.  
  365.    data[ i++ ] = sprintf( "EDITOR " max(width, MIN_WIDTH) ", " max(height, MIN_HEIGHT) "\n" )
  366.  
  367.    lastBuffer = buffer_name
  368.    firstWin   = current_window;
  369.    if ( firstWin != 0 )
  370.    {
  371.       do
  372.       {
  373.          next_window( "", 1, 1 );  #  We include system windows, but we don't process them?
  374.          # don't save information on system windows
  375.          if ( !and( window_flags, WINDOW_SYSTEM ) && ( !create_new_bufwin || (create_new_bufwin && !and(buffer_flags, BUFFER_SCRATCH) ) ) )
  376.          {
  377.             if (noStartWin)
  378.             {
  379. #              data[ i++ ] = sprintf( "delete_default_window\n" );# make a least one window
  380. #              data[ i++ ] = sprintf( "set_editwin_property %d\n", EWC_NORMAL)
  381.                noStartWin = 0;
  382.             }
  383.       
  384.             if ( query_editwin_property(EWC_MINIMIZE, current_window) ||
  385.                  query_editwin_property(EWC_MAXIMIZE, current_window) )
  386.             {
  387.                win_x    = query_editwin_property( EWC_RESTORE_X_POS,    current_window )
  388.                win_y    = query_editwin_property( EWC_RESTORE_Y_POS,    current_window )
  389.                width    = query_editwin_property( EWC_RESTORE_WIDTH,    current_window )
  390.                height   = query_editwin_property( EWC_RESTORE_HEIGHT,   current_window )
  391.             }
  392.             else
  393.             {
  394.                win_x    = window_x0
  395.                win_y    = window_y0
  396.       
  397.                width    = window_width
  398.                height   = window_height
  399.             }
  400.       
  401.             data[ i++ ] = sprintf( "WINDOW %d, %d, %d, %d, %d, \"%s\", \"%s\", \"%s\"\n",
  402.                                    win_x,         
  403.                                    win_y,         
  404.                                    width,   
  405.                                    height,  
  406.                                    window_flags,
  407.                                    window_name,
  408.                                    linenumber_format,
  409.                                    buffer_name )
  410.  
  411.             data[ i++ ] = sprintf(  "WIN_VISIBLES \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"\n",
  412.                                     visible_end_buffer,
  413.                                     visible_spaces,
  414.                                     visible_tabs,
  415.                                     visible_newlines,
  416.                                     visible_virtual_lines,
  417.                                     visible_virtual_spaces )
  418.  
  419.             data[ i++ ] = sprintf(  "WIN_COLORS %d, %d, %d, %d, %d, %d\n",
  420.                                     color_window_fg,
  421.                                     color_window_bg,
  422.                                     color_linenumbers_fg,
  423.                                     color_linenumbers_bg,
  424.                                     color_linecommands_fg,
  425.                                     color_linecommands_bg )
  426.                                     
  427.             # scroll settings
  428.             data[ i++ ] = sprintf(  "WIN_CONTEXT %d, %d, %d, %d, %d, %d, %d\n",
  429.                                     scroll_context_top,
  430.                                     scroll_context_bottom,
  431.                                     scroll_context_right,
  432.                                     scroll_context_left,
  433.                                     scroll_unit_horizontal,
  434.                                     scroll_unit_vertical,
  435.                                     scroll_means_pan )
  436.  
  437.             # font information
  438.             store_font("window_" window_number, "Fonts")
  439.             data[ i++ ] = sprintf(  "load_font \"window_%d\" \"Fonts\"\n",
  440.                                     window_number )
  441.  
  442.             if ( mdi_mode == 2 )
  443.                data[i++] = sprintf( "toggle_status_bar 1 %d\n", status_bar_flags )
  444.  
  445.             if(and(window_flags, WINDOW_ASCII_DUMP))
  446.             {
  447.                data[i++] = sprintf("save_hex_state %d \"%s\"\n",
  448.                                    old_hex_info[current_window].status,
  449.                                    old_hex_info[current_window].format "");
  450.             }
  451.  
  452.             window_number++
  453.          }
  454.       } while (firstWin != current_window);
  455.    }
  456.  
  457.    if ( query_editwin_property(EWC_MAXIMIZE) )                                                            
  458.       editwin_state = EWC_MAXIMIZE
  459.    else if ( query_editwin_property(EWC_NORMAL) )                                                            
  460.       editwin_state = EWC_NORMAL
  461.    else
  462.       editwin_state = EWC_MINIMIZE
  463.  
  464.    # save whether or not the editor was maximized/normal/minimized
  465.    data[ i++ ] = sprintf( "EDITOR %d, %d, %d\n", editwin_state, x_pos, y_pos )
  466.    data[ i++ ] = sprintf( "\n" )
  467.  
  468.    return data
  469. }
  470.  
  471. global function save_flags_config()
  472. {
  473.    local data[]
  474.    local i = 1
  475.  
  476.    data[ i++ ] = sprintf( "save_state=%d\n", save_state )
  477.    data[ i++ ] = sprintf( "save_flags=%d\n", save_flags )
  478.    data[ i++ ] = sprintf( "\n" )
  479.  
  480.    return data
  481. }
  482.  
  483. # This functions saves those sections that are considered to be part of
  484. # the SETTINGS of the editor
  485. #
  486. global function save_settings_data( save_settings_now, config_buffer )
  487. {
  488.    local old_buffer  = current_buffer
  489.    local free_buffer = FALSE
  490.  
  491.    # if no config_buffer is specified then get it otherwise use the
  492.    # config_buffer passed in.
  493.    if (argcount() < 2 || !config_buffer)
  494.    {
  495.       if (get_config_buffer())
  496.       {
  497.          config_buffer  = current_buffer
  498.          current_buffer = old_buffer
  499.          free_buffer    = TRUE
  500.       }
  501.       else
  502.          return
  503.    }
  504.  
  505.    # this function is in both save_settings_data() and 
  506.    # save_state_data() so that the flags are always updated
  507.    #
  508.    update_config_section( "$SAVE_FLAGS$", save_flags_config(),
  509.                           config_buffer )
  510.  
  511.    if ( and( save_flags, SAVE_SETTINGS ) || save_settings_now )
  512.    {
  513.       update_config_section( "$SETTINGS$", settings_config(),
  514.                               config_buffer )
  515.  
  516.       update_config_section( "$BUFFER1_SETTINGS$", buffer1_settings_config(),
  517.                              config_buffer );
  518.       update_config_section( "$BUFFER2_SETTINGS$", buffer2_settings_config(),
  519.                              config_buffer );
  520.       update_config_section( "$BUFFER3_SETTINGS$", buffer3_settings_config(),
  521.                              config_buffer );
  522.  
  523.       update_config_section( "$WINDOW1_SETTINGS$", window1_settings_config(),
  524.                              config_buffer );
  525.       update_config_section( "$WINDOW2_SETTINGS$", window2_settings_config(),
  526.                              config_buffer );
  527.       update_config_section( "$WINDOW3_SETTINGS$", window3_settings_config(),
  528.                              config_buffer );
  529.  
  530.       update_config_section( "$PRINT_SETTINGS$", save_print_settings(),
  531.                              config_buffer )
  532.  
  533.       update_config_section( "$ISPF$",
  534.                              optional_function( "save_ispf_settings" ),
  535.                              config_buffer )
  536.    }
  537.  
  538.    if ( free_buffer )
  539.       delete_config_buffer(TRUE)
  540.  
  541.    current_buffer = old_buffer
  542. }
  543.  
  544. # This functions saves those sections that are considered to be part of
  545. # the STATE of the editor
  546. #
  547. global function save_state_data( save_state_now, config_buffer )
  548. {
  549.    local old_buffer  = current_buffer
  550.    local free_buffer = FALSE
  551.  
  552.    # if no config_buffer is specified then get it otherwise use the
  553.    # config_buffer passed in.
  554.    if (argcount() < 2 || !config_buffer)
  555.    {
  556.       if (get_config_buffer())
  557.       {
  558.          config_buffer  = current_buffer
  559.          current_buffer = old_buffer
  560.          free_buffer    = TRUE
  561.       }
  562.       else
  563.          return
  564.    }
  565.  
  566.    # this function is in both save_settings_data() and 
  567.    # save_state_data() so that the flags are always updated
  568.    #
  569.    update_config_section( "$SAVE_FLAGS$", save_flags_config(),
  570.                           config_buffer )
  571.  
  572.    if ( save_state || save_state_now )
  573.    {
  574.       update_config_section( "$BUFFER_STATE$", buffer_state_config(), config_buffer )
  575.       update_config_section( "$WINDOW_STATE$", window_state_config(), config_buffer )
  576.    }
  577.  
  578.    if ( and( save_flags, SAVE_COMMANDS ) )
  579.       update_config_section( "$XMACRO_HISTORY$", history_config("XMACRO"),
  580.                              config_buffer )
  581.  
  582.    if ( and( save_flags, SAVE_SEARCH ) )
  583.    {
  584.       update_config_section( "$SEARCH_HISTORY$", history_config("SEARCH"),
  585.                              config_buffer )
  586.  
  587.       update_config_section( "$SRCHFILES_HISTORY$",
  588.                              history_config("SRCHFILES"), config_buffer )
  589.    }
  590.  
  591.    if ( and( save_flags, SAVE_REPLACE ) )
  592.       update_config_section( "$REPLACE_HISTORY$",
  593.                              history_config("REPLACE"), config_buffer )
  594.  
  595.    if ( and( save_flags, SAVE_EDITFILE ) )
  596.       update_config_section( "$EDITFILE_HISTORY$",
  597.                              history_config("EDITFILE"), config_buffer )
  598.  
  599.    if ( and( save_flags, SAVE_COMPILE ) )
  600.       update_config_section( "$COMPILE_HISTORY$",
  601.                              history_config("COMPILE"), config_buffer )
  602.  
  603.    if ( free_buffer )
  604.       delete_config_buffer(TRUE)
  605.  
  606.    current_buffer = old_buffer
  607. }
  608.  
  609. # faster version of its predecessor
  610. global function update_config_section( section_name, data_array, config_buffer )
  611. {
  612.    local re_section_name      = ""
  613.    local in_target_section    = FALSE
  614.    local free_buffer          = FALSE
  615.    local old_buffer           = current_buffer
  616.    local data, section_begin
  617.  
  618.    # ensure we are getting something to add
  619.    #
  620.    if ( !data_array )
  621.       return
  622.  
  623.    # if no config_buffer is specified then get it otherwise use the
  624.    # config_buffer passed in.
  625.    if (argcount() < 3 || !config_buffer)
  626.    {
  627.       if (get_config_buffer())
  628.       {
  629.          config_buffer = current_buffer
  630.          free_buffer   = TRUE
  631.       }
  632.       else
  633.          return
  634.    }
  635.  
  636.    current_buffer = config_buffer
  637.    goto_buffer_top()
  638.  
  639.    # look for the section name
  640.    re_section_name = section_name
  641.    gsub( "\\$", "\\$", re_section_name )
  642.  
  643.    if ( search( "^" re_section_name, SEARCH_FWD_REGEX) )
  644.    {
  645.       in_target_section = TRUE
  646.  
  647.       # go to the first line of the section
  648.       if ( down(1) )
  649.       {
  650.          section_begin = current_line
  651.          begin_selection( LINE_SELECTION )
  652.  
  653.          # look for the next section
  654.          if ( search("^\\$[a-zA-Z0-9_]+\\$", SEARCH_FWD_REGEX) )
  655.             up( 1 )
  656.          # if no other section, go to eob
  657.          else
  658.             goto_buffer_bottom()
  659.  
  660.          # delete the body of our current section
  661.          end_selection()
  662.  
  663.          # only delete chars if it is at least one line long!
  664.          if ( current_line >= section_begin )
  665.             delete_chars()
  666.          else
  667.          {
  668.             remove_selection()
  669.             down( 1 )
  670.          }
  671.       }
  672.       # else if at the end of the buffer, start on a new line
  673.       else
  674.       {
  675.          goto_eol()
  676.          insert_newline()
  677.       }
  678.       goto_bol()
  679.    }
  680.    # if it doesn't exist, go to $STATE$; if $STATE$ doesn't exist, go to eob
  681.    else # if ( !search("^\\$STATE\\$", SEARCH_FWD_REGEX) )
  682.    {
  683.       goto_buffer_bottom()
  684.       # if we're not on a blank line, make a new one
  685.       if ( current_line_length )
  686.       {
  687.          goto_eol()
  688.          insert_newline()
  689.       }
  690.  
  691.    }
  692.  
  693.    # if we did not find the section specified, create it
  694.    if ( !in_target_section )
  695.    {
  696.       insert_string( "%s\n", section_name )
  697.    }
  698.  
  699.    for ( data in data_array )
  700.    {
  701.       # replace any hardcoded 0x13 and 0x9 with \n and \t.
  702. #     sub( "$", "\\\\n", data_array[ data ] )
  703.       gsub( "\t", "\\\\t", data_array[ data ] )
  704.  
  705.       # if we're not on a blank line, make a new one
  706.       if ( current_line_length )
  707.       {
  708.          goto_bol()
  709.          insert_newline()
  710.          up()
  711.       }
  712.  
  713.       insert_string( data_array[ data ] )
  714.    }
  715.  
  716.    if ( free_buffer )
  717.       delete_config_buffer(TRUE)
  718.  
  719.    current_buffer = old_buffer
  720. }
  721.  
  722. global function get_config_buffer()
  723. {
  724.    local rc 
  725.    local prevEol
  726.    local cfgName
  727.    local old_message_level = message_level;
  728.  
  729.    if (config_buffer_ref_count && the_config_buffer)
  730.    {
  731.       rc             = TRUE
  732.       current_buffer = the_config_buffer
  733.       config_buffer_ref_count++
  734.    }
  735.    else
  736.    {
  737.       # search cpe path for the config file
  738.       if ( !(cfgName = locate_cpe_file(CPE_FILE_CONFIG)) )
  739.          cfgName = editor_path( CPE_FILE_CONFIG )
  740.  
  741.       prevEol = default_buffer_eol_string
  742.       default_buffer_eol_string = "\r\n"
  743.  
  744.       current_buffer = create_buffer( "Config File"  , 
  745.                                       cfgName        , 
  746.                                       BUFFER_SYSTEM  + 
  747.                                       BUFFER_NO_UNDO + 
  748.                                       BUFFER_REAL_SPACE_ONLY )
  749.  
  750.       #  Have to be sure to turn insert mode on, otherwise config may get messed up;
  751.       message_level = 1;
  752.       toggle_buffer_flags(BUFFER_OVERTYPE_MODE, 0);
  753.       message_level = old_message_level;
  754.  
  755.       # restore the default end of line sequence
  756.       default_buffer_eol_string = prevEol
  757.  
  758.       rc = ( buffer_size > 0 )
  759.  
  760.       # initialize an empty config file
  761.       if ( buffer_size == 0 )
  762.       {
  763.          insert_string( "# %s - Configuration File\n", editor_title )
  764.          insert_string( "$USER_SETTINGS$ \n\n# Place user settings here\n\n" )
  765.          write_buffer()
  766.       }
  767.       config_buffer_ref_count = 1
  768.       the_config_buffer       = current_buffer
  769.    }
  770.    return( rc )
  771. }
  772.  
  773. global function delete_config_buffer(forUpdate)
  774. {
  775.    local old_buffer
  776.  
  777.    config_buffer_ref_count--
  778.    if ( config_buffer_ref_count <= 0)
  779.    {
  780.       if (the_config_buffer)
  781.       {
  782.          old_buffer = current_buffer
  783.  
  784.          if (argcount() && forUpdate)
  785.          {
  786.             current_buffer = the_config_buffer
  787.             if (buffer_is_modified())
  788.                write_buffer()
  789.          }
  790.  
  791.          delete_buffer(the_config_buffer)
  792.  
  793.          if (old_buffer != the_config_buffer)
  794.             current_buffer = old_buffer
  795.  
  796.          the_config_buffer = 0
  797.       }
  798.       config_buffer_ref_count = 0
  799.    }
  800. }
  801.  
  802. # This function is attached to the EVENT.EXIT_EDITOR and is used to save 
  803. # the various sections of the config file
  804. #
  805. global function write_state_file()
  806. {
  807.    local config_buffer
  808.    local old_buffer     = current_buffer
  809.  
  810.    if ( get_config_buffer() )
  811.    {
  812.       # save config_buffer id in case one of the sections changes the 
  813.       # current_buffer
  814.       #
  815.       config_buffer  = current_buffer
  816.       current_buffer = old_buffer
  817.  
  818.       save_state_data(0, config_buffer)
  819.       save_settings_data(0, config_buffer)
  820.  
  821.       delete_config_buffer(TRUE)
  822.       current_buffer = old_buffer
  823.    }
  824. }
  825.  
  826. #
  827. # attach_win_to_buf()
  828. #
  829. # provide a means of attaching the current window to the current buffer 
  830. # from within an execute_function call.
  831. #
  832. global function attach_win_to_buf()
  833. {
  834.    attach_window_buffer( current_window, current_buffer );
  835.    update_current_view()
  836. }
  837.  
  838. global function delete_default_window()
  839. {
  840.    local cw = current_window;
  841.    local wf = window_flags;
  842.  
  843.    next_window();
  844.  
  845.    while( !and( window_flags, WINDOW_SYSTEM ) && current_window)
  846.    {
  847.       delete_window();
  848.       next_window();
  849.    }
  850.  
  851.    if (and( wf, WINDOW_SYSTEM) && cw)
  852.       assign_current_window( cw );
  853. }
  854.  
  855. global function editor_start()
  856. {
  857.    if (  !query_editwin_property( EWC_MINIMIZE ) )
  858.    {
  859.       if ( !query_editwin_property( EWC_MAXIMIZE ) )
  860.          set_editwin_property( EWC_NORMAL )
  861.  
  862.       set_editwin_property( EWC_TO_TOP )
  863.    }
  864.  
  865.    if ( mdi_mode == 2 )
  866.       current_window = current_window
  867.  
  868.    delete_event(EVENT.EDITOR_RUNNING,   function_id("editor_start"))
  869.    
  870.    editor_running = TRUE
  871. }
  872.  
  873. # DWM 9/28/94
  874. # Instead of calling all of these functions from the config file,
  875. # just create a function that does all of the work.
  876. global function setup_buffer( filespec, bflags, bname, line, col, tabs, a_i )
  877. {
  878.    local mask;
  879.  
  880.    if ( !is_floppy_drive(filespec) || filemode(filespec) != -1 )
  881.    {
  882.       mask = BUFFER_OVERTYPE_MODE + BUFFER_SNAP_TO_EOL +        \
  883.              BUFFER_REAL_SPACE_ONLY + BUFFER_EXPAND_TABS +      \
  884.              BUFFER_TABS_TO_SPACES + BUFFER_WP_CARRY_COMMENTS + \
  885.              BUFFER_WP_ENABLED + BUFFER_WP_WRAP_CONTINUOUS
  886.  
  887.       edit_file(filespec )
  888.       buffer_flags = set_flag_bits(buffer_flags, mask, bflags )
  889.       buffer_name = bname     
  890.       current_line = line
  891.       current_column = col
  892.       buffer_tabs = tabs
  893.       toggle_auto_indent( a_i )
  894.    }
  895. }
  896.  
  897. global function setup_window( x, y, width, height, flags, wname, lformat, buffer )
  898. {
  899.    local old_name = buffer_name
  900.  
  901.    if ( mdi_mode == 2 )
  902.    {
  903.       current_window = create_window( x, y, width, height, flags )
  904.       attach_menu_to_win( current_window )
  905.    }
  906.    else
  907.       current_window = create_window( x, y, width, height, flags )
  908.  
  909.    next_buffer( buffer )
  910.  
  911.    if ( buffer_name == old_name && and(buffer_flags, BUFFER_SYSTEM) )
  912.    {
  913.       next_buffer()
  914.    }
  915.  
  916.    attach_window_buffer( current_window, current_buffer )
  917.    update_current_caption()
  918.  
  919.    if ( wname )
  920.       window_name = wname
  921.  
  922.    if (and(flags, WINDOW_ASCII_DUMP))
  923.       begin_dump_mode()
  924.  
  925.    linenumber_format = lformat
  926. }
  927.  
  928. global function setup_window_colors( fg, bg, l_fg, l_bg, lc_fg, lc_bg )
  929. {
  930.    color_window_fg         = fg
  931.    color_window_bg         = bg
  932.    
  933.    color_linenumbers_fg    = l_fg
  934.    color_linenumbers_bg    = l_bg
  935.    
  936.    color_linecommands_fg   = lc_fg
  937.    color_linecommands_bg   = lc_bg
  938. }
  939.  
  940. global function setup_window_context( top, bottom, right, left, shorz, svert, smp )
  941. {
  942.    scroll_context_top      = top
  943.    scroll_context_bottom   = bottom
  944.    scroll_context_right    = right
  945.    scroll_context_left     = left
  946.    scroll_unit_horizontal  = shorz
  947.    scroll_unit_vertical    = svert
  948.    scroll_means_pan        = smp
  949. }
  950.  
  951. global function setup_window_visibles( veob, vs, vt, vn, vvl, vvs )
  952. {
  953.    visible_end_buffer = veob
  954.  
  955.    if ( vs )
  956.       visible_spaces = vs
  957.  
  958.    visible_tabs            = vt
  959.    visible_newlines        = vn
  960.    visible_virtual_lines   = vvl
  961.  
  962.    if ( vvs )
  963.       visible_virtual_spaces  = vvs
  964. }
  965.  
  966. global function move_ed_offscreen( width, height )
  967. {
  968.    set_editwin_property( EWC_X_POS, 5000  )
  969.    set_editwin_property( EWC_Y_POS, 5000  )
  970.  
  971.    set_editwin_property( EWC_WIDTH, width )
  972.    set_editwin_property( EWC_HEIGHT, height  )
  973.    set_editwin_property( EWC_NORMAL )
  974. }
  975.  
  976. global function setup_editwin( mode, x, y )
  977. {
  978.    set_editwin_property( mode )
  979.  
  980.    if ( mode == EWC_NORMAL )
  981.    {
  982.       set_editwin_property( EWC_X_POS, x )
  983.       set_editwin_property( EWC_Y_POS, y )
  984.    }
  985.    else
  986.    {
  987.       set_editwin_property( EWC_RESTORE_X_POS, x )
  988.       set_editwin_property( EWC_RESTORE_Y_POS, y )
  989.    }
  990. }
  991.  
  992. local function unquote( str )
  993. {
  994.    local no_quotes
  995.    
  996.    # first trim whitespace, then "'s
  997.    no_quotes = trim( ltrim(str) )
  998.    no_quotes = trim( ltrim(no_quotes, "\""), "\"" )
  999.  
  1000.    return( unquote_string(no_quotes) )
  1001. }
  1002.  
  1003. local function process_setting_command( count, cmd, args, arg_list )
  1004. {
  1005.    local ret_val = TRUE
  1006.  
  1007.    # $SETTINGS$ settings
  1008.    if ( cmd == "BRIEF" )
  1009.       optional_function("brief")
  1010.    else if ( cmd == "CUA" )
  1011.       optional_function("cua")
  1012.    else if ( cmd == "EMACS" )
  1013.       optional_function("emacs")
  1014.    else if ( cmd == "VI" )
  1015.       optional_function("vi")
  1016.    else if ( cmd == "ISPF" )
  1017.       optional_function("ispf")
  1018.    else if ( cmd == "NATIVE" )
  1019.       optional_function("native")
  1020.    else if ( cmd == "DEF_TABS" )
  1021.    {
  1022.       if ( count != 3 )
  1023.          warning( "DEF_TABS requires 3 arguments in cpe.cfg" )
  1024.       else
  1025.       {
  1026.          default_buffer_tabs     = unquote( arg_list[ 1  ] )
  1027.          default_wp_right_margin = arg_list[ 2  ]
  1028.          default_wp_left_margin  = arg_list[ 3  ]
  1029.       }
  1030.    }
  1031.    else if ( cmd == "WORD_DELIM" )
  1032.    {
  1033.       if ( !args )
  1034.          warning( "WORD_DELIM requires 1 argument in cpe.cfg" )
  1035.       else
  1036.          word_delimiter = unquote( args )
  1037.    }
  1038.    else if ( cmd == "SRCH_REPL" )
  1039.    {
  1040.       if ( count != 3 )
  1041.          warning( "SRCH_REPL requires 3 arguments in cpe.cfg" )
  1042.       else
  1043.       {
  1044.          search_flags         = arg_list[ 1  ]
  1045.          search_pattern       = unquote( arg_list[ 2  ] )
  1046.          replace_pattern      = unquote( arg_list[ 3  ] )
  1047.       }
  1048.          
  1049.    }
  1050.    else if ( cmd == "BACKUP" )
  1051.    {
  1052.       if ( count != 2 )
  1053.          warning( "BACKUP requires 2 arguments in cpe.cfg" )
  1054.       else
  1055.       {
  1056.          backup_files_enabled = arg_list[ 1  ]
  1057.          backup_directory     = unquote( arg_list[ 2  ] )
  1058.       }
  1059.    }
  1060.    else if ( cmd == "DEF_COLS" )
  1061.    {
  1062.       if ( count != 12 )
  1063.          warning( "DEF_COLS requires 12 arguments in cpe.cfg" )
  1064.       else
  1065.       {
  1066.          color_help_bg     = arg_list[ 1  ]
  1067.          color_help_fg     = arg_list[ 2  ]
  1068.          color_message_bg  = arg_list[ 3  ]
  1069.          color_message_fg  = arg_list[ 4  ]
  1070.          color_notify_bg   = arg_list[ 5  ]
  1071.          color_notify_fg   = arg_list[ 6  ]
  1072.          color_warning_bg  = arg_list[ 7  ]
  1073.          color_warning_fg  = arg_list[ 8  ]
  1074.          color_error_bg    = arg_list[ 9  ]
  1075.          color_error_fg    = arg_list[ 10 ]
  1076.          color_bookmark_bg = arg_list[ 11 ]
  1077.          color_bookmark_fg = arg_list[ 12 ]
  1078.       }
  1079.    }
  1080.    else if ( cmd == "CURSORS" )
  1081.    {
  1082.       if ( count != 4 )
  1083.          warning( "CURSORS requires 4 arguments in cpe.cfg" )
  1084.       else
  1085.       {
  1086.          cursor_normal            = arg_list[ 1  ]
  1087.          cursor_overtype          = arg_list[ 2  ]
  1088.          cursor_virtual           = arg_list[ 3  ]
  1089.          cursor_virtual_overtype  = arg_list[ 4  ]
  1090.       }
  1091.    }
  1092.    else if ( cmd == "STAT" )
  1093.    {
  1094.       if ( count != 2 )
  1095.          warning( "STAT requires 2 arguments in cpe.cfg" )
  1096.       else
  1097.       {
  1098.          status_bar_flags  = arg_list[ 1  ]
  1099.  
  1100.          toggle_status_bar( arg_list[2] )
  1101.          #toggle_toolbar( arg_list[3] )
  1102.       }
  1103.    }
  1104.    else if ( cmd == "SET_VCS" )
  1105.    {
  1106.       if ( count != 3 )
  1107.          warning( "SET_VCS requires 3 arguments in cpe.cfg" )
  1108.       else
  1109.       {
  1110.          pvcs( arg_list[1] )
  1111.          get_command = unquote( arg_list[2] )
  1112.          put_command = unquote( arg_list[3] )
  1113.       }
  1114.    }
  1115.    else if ( cmd == "SET_MDI" )
  1116.    {
  1117.       if ( count != 2 )
  1118.          warning( cmd " requires 2 arguments in cpe.cfg" )
  1119.       else
  1120.       {
  1121.          mdi_mode = arg_list[1]
  1122.          modify_mdi_menu( arg_list[1] )
  1123.          temp_create_new_bufwin = arg_list[2]+0;
  1124.          if ( mdi_mode == 2 )
  1125.          {
  1126.             create_container_list();
  1127.          }
  1128.       }
  1129.    }
  1130.    else if ( cmd == "TAGS_PATH" )
  1131.    {
  1132.       if ( !args )
  1133.          warning( "TAGS_PATH requires 1 arguments in cpe.cfg" )
  1134.       else
  1135.          tags_path = args
  1136.    }
  1137.    else
  1138.       ret_val = FALSE
  1139.  
  1140.    return ret_val
  1141. }
  1142.  
  1143. local function process_state_command( count, cmd, args, arg_list, noWindows )
  1144. {
  1145.    local ret_val = TRUE
  1146.  
  1147.    # $STATE$ settings
  1148.    if ( cmd == "EDITOR" )
  1149.    {
  1150.       if ( count == 2 )
  1151.          move_ed_offscreen( arg_list[1], arg_list[2] )
  1152.       else if ( count == 3 )
  1153.       {
  1154.          if ( start_editor_minimized )
  1155.          {
  1156.             arg_list[1] = EWC_MINIMIZE
  1157.          }
  1158.  
  1159.          setup_editwin( arg_list[1], arg_list[2], arg_list[3] )
  1160.       }
  1161.       else
  1162.          warning( "EDITOR requires 2 or 3 arguments in cpe.cfg" )
  1163.    }
  1164.    else if ( cmd == "BUFFER" )
  1165.    {
  1166.       if ( count != 7 )
  1167.          warning( "BUFFER requires 7 arguments in cpe.cfg" )
  1168.       else
  1169.          setup_buffer(  unquote(arg_list[1]), arg_list[2], unquote(arg_list[3]), 
  1170.                         arg_list[4], arg_list[5], unquote(arg_list[6]), arg_list[7] )
  1171.    }
  1172.    else if( cmd == "WINDOW" )
  1173.    {
  1174.       if(noWindows)
  1175.          nop();
  1176.       else if ( count != 8 )
  1177.          warning( "WINDOW requires 8 arguments in cpe.cfg" )
  1178.       else
  1179.          setup_window(  arg_list[1], arg_list[2], arg_list[3], 
  1180.                         arg_list[4], arg_list[5], unquote(arg_list[6]), 
  1181.                         unquote(arg_list[7]), unquote(arg_list[8]) )
  1182.    }
  1183.    else if ( cmd == "WIN_COLORS" )
  1184.    {
  1185.       if(noWindows)
  1186.          nop();
  1187.       else if ( count != 6 )
  1188.          warning( "COLORS requires 6 arguments in cpe.cfg" )
  1189.       else
  1190.          setup_window_colors(    arg_list[1], arg_list[2], arg_list[3], 
  1191.                                  arg_list[4], arg_list[5], arg_list[6] )
  1192.    }
  1193.    else if ( cmd == "WIN_CONTEXT" )
  1194.    {
  1195.       if(noWindows)
  1196.          nop();
  1197.       else if ( count != 7 )
  1198.          warning( "WIN_CONTEXT requires 7 arguments in cpe.cfg" )
  1199.       else
  1200.          setup_window_context(   arg_list[1], arg_list[2], arg_list[3], 
  1201.                                  arg_list[4], arg_list[5], arg_list[6], arg_list[7] )
  1202.    }
  1203.    else if ( cmd == "WIN_VISIBLES" )
  1204.    {
  1205.       if(noWindows)
  1206.          nop();
  1207.       else if ( count != 6 )
  1208.          warning( "WIN_VISIBLES requires 6 arguments in cpe.cfg" )
  1209.       else
  1210.          setup_window_visibles(   unquote(arg_list[1]), unquote(arg_list[2]), unquote(arg_list[3]), 
  1211.                                  unquote(arg_list[4]), unquote(arg_list[5]), unquote(arg_list[6]) )
  1212.    }
  1213.    else 
  1214.       ret_val = FALSE
  1215.  
  1216.    return ret_val
  1217. }
  1218.  
  1219. local function process_color_config( array )
  1220. {
  1221.    if ( array )
  1222.    {
  1223.       default_color_window_fg          = array[ 1 ]
  1224.       default_color_window_bg          = array[ 2 ]
  1225.       default_color_linenumbers_fg     = array[ 3 ]
  1226.       default_color_linenumbers_bg     = array[ 4 ]
  1227.       default_color_linecommands_fg    = array[ 5 ]
  1228.       default_color_linecommands_bg    = array[ 6 ]
  1229.       default_color_message_fg         = array[ 7 ]
  1230.       default_color_message_bg         = array[ 8 ]
  1231.       default_color_notify_fg          = array[ 9 ]
  1232.       default_color_notify_bg          = array[ 10 ]
  1233.       default_color_warning_fg         = array[ 11 ]
  1234.       default_color_warning_bg         = array[ 12 ]
  1235.       default_color_error_fg           = array[ 13 ]
  1236.       default_color_error_bg           = array[ 14 ]
  1237.       default_color_bookmark_fg        = array[ 15 ]
  1238.       default_color_bookmark_bg        = array[ 16 ]
  1239.    }
  1240. }
  1241.  
  1242. local function do_fast_config( section_name, data_array, noWindows )
  1243. {
  1244.    local index
  1245.    local cmd
  1246.    local cmdstr
  1247.    local str
  1248.    local args
  1249.    local count
  1250.    local arg_list[]
  1251.    local first_space
  1252.    local fid
  1253.    local success
  1254.  
  1255.    if ( section_name == "$STATE$" )
  1256.    {
  1257.       delete_default_window()
  1258.    }
  1259.  
  1260.    for ( index in data_array )
  1261.    {
  1262.       str = data_array[index]
  1263.  
  1264.       first_space = cindex( str, " " )
  1265.  
  1266.       if ( first_space )
  1267.          cmdstr   = prefix( str, first_space - 1 )
  1268.       else
  1269.          cmdstr   = str
  1270.  
  1271.       cmd = cmdstr
  1272.  
  1273.       args  = suffix( str, length(str) - first_space )
  1274.  
  1275.       count = split( args, arg_list, "," )
  1276.  
  1277.       if ( section_name == "$STATE$" )
  1278.          success = process_state_command( count, cmd, args, arg_list, noWindows )
  1279.  
  1280.       else if ( section_name == "$SETTINGS$" )
  1281.          success = process_setting_command( count, cmd, args, arg_list )
  1282.  
  1283.       else
  1284.       {
  1285.          warning( section_name "--invalid section name" )
  1286.          return
  1287.       }
  1288.  
  1289.       if ( !success )
  1290.       {
  1291.          if ( (fid = quoted_function_id(str)) )
  1292.             execute_function(fid)
  1293.          else
  1294.             warning( str "--invalid configuration command" )
  1295.       }
  1296.    }    
  1297. }
  1298.  
  1299.  
  1300. # function called on startup if the 'save directory on exit' option was set.
  1301. global function set_startup_dir(path)
  1302. {
  1303.    if (!editor_running && argcount() > 0)
  1304.    {
  1305.       # We are processing the config file, so save the parameter and set event
  1306.       # to reinvoke this function when startup is complete.
  1307.       startup_dir = path
  1308.       attach_event_handler(EVENT.EDITOR_RUNNING, "set_startup_dir")
  1309.    }
  1310.    else if (editor_running)
  1311.    {
  1312.       delete_event(EVENT.EDITOR_RUNNING, "set_startup_dir")
  1313.       if (argcount() < 1)
  1314.          path = startup_dir   # use the path saved above
  1315.       chdir(path)
  1316.    }
  1317. }
  1318.  
  1319.