home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / p2demo21.exe / PEL / AUTOSAVE.PEL < prev    next >
Text File  |  1995-02-10  |  6KB  |  203 lines

  1. # $Header:   P:\source\wmacros\autosave.pev   1.10   10 Feb 1995 12:30:28   PFHJXM0  $
  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:   autosave.pel  $: Support for automatically saving buffers
  20.  
  21. global  autosave_time   = 15    # every 15 seconds              #PUBLIC #INT
  22. global  autosave_force_time = 0 # number of seconds before forcing an autosave
  23.  
  24. local   autosave_on     = FALSE # initially no autosave
  25. local   autosave_array
  26. local   accumulative_force_time # accumulative time since last forced autosave
  27. local   accumulative_save_time  # accumulative time since last autosave
  28.  
  29. local   autosave_files_id
  30. local   autosave_exit_id
  31. local   autosave_kbd_id
  32.  
  33. local AUTOSAVE_EXT_CHAR = "!"   # extension character for autosave files
  34.  
  35. ## autosave_active()
  36. #
  37. #  returns a true value if autosave is activated, otherwise a false value
  38. #
  39. global function autosave_active()
  40. {
  41.    return( autosave_on )
  42. }
  43.  
  44.  
  45.  
  46. ## autosave()
  47. #
  48. #  Invoke or toggle autosaving.
  49. #  atime (if specified) is the number of seconds between autosaves
  50. #  ftime (if specified) is the number of seconds between forced saves
  51. #
  52. #  If atime is not specified, autosave_time is used.
  53. #
  54. global function autosave( atime, ftime )
  55. {
  56.    local   prev_autosave = autosave_on;
  57.  
  58.    #
  59.    # determine new setting
  60.    #
  61.    if( argcount() < 1 )
  62.       autosave_on = !autosave_on
  63.    else
  64.    {
  65.       if ( (autosave_on = 0+atime) )
  66.          autosave_time = autosave_on
  67.    }
  68.  
  69.    accumulative_save_time = 0
  70.    accumulative_force_time = 0;
  71.  
  72.    if ( autosave_on )
  73.    {
  74.       idle_threshold = 1;     # always set to 1 and we'll figure
  75.                               # out later whether autosave_time
  76.                               # has been exceeded
  77.  
  78.       if (argcount() == 2)
  79.          autosave_force_time = 0+ftime;
  80.  
  81.       if (!prev_autosave)
  82.       {
  83.          autosave_files_id = function_id( "autosave_files" );
  84.          autosave_exit_id  = function_id( "autosave_exit" );
  85.          autosave_kbd_id   = function_id( "autosave_kbd_hit" );
  86.          attach_event_handler( EVENT.IDLE_THRESHOLD, autosave_files_id )
  87.          attach_event_handler( EVENT.EXIT_EDITOR, autosave_exit_id )
  88.          attach_event_handler( EVENT.KEYPRESS, autosave_kbd_id )
  89.       }
  90.       message( "Autosave is on." )
  91.    }
  92.    else
  93.    {
  94.       if (prev_autosave)
  95.       {
  96.          delete_event( EVENT.IDLE_THRESHOLD, autosave_files_id)
  97.          delete_event( EVENT.KEYPRESS, autosave_kbd_id)
  98.       }
  99.       message( "Autosave is off." )
  100.    }
  101. }
  102.  
  103.  
  104. #
  105. # autosave_files()
  106. #
  107. # write all of the modified buffers to a new ".__!" file.
  108. #
  109. global function autosave_files()
  110. {
  111.    local   old_curnt_buffer = current_buffer
  112.    local   asv_name
  113.    local   fname;
  114.    local   forcesave = 0;
  115.    local   diff;
  116.  
  117.    accumulative_save_time += 1;
  118.    accumulative_force_time += 1;
  119.  
  120.    if (autosave_force_time && (accumulative_force_time >= autosave_force_time))
  121.    {
  122.       forcesave = 1;
  123.       accumulative_force_time = 0;
  124.    }
  125.    else if (accumulative_save_time < autosave_time)
  126.    {
  127.       return;
  128.    }
  129.  
  130.    accumulative_save_time = 0;
  131.  
  132.    do {
  133.       if (!(and(buffer_flags, BUFFER_SYSTEM))) 
  134.       {
  135.          #
  136.          # see if the buffer has changed
  137.          #
  138.          if ( and( buffer_flags, BUFFER_AUTOSAVE_MODIFIED) )
  139.          {
  140.             if (buffer_filename)
  141.             {
  142.                asv_name = fname = buffer_filename;
  143.    
  144.                asv_name = create_semaphore_fname( asv_name, AUTOSAVE_EXT_CHAR );
  145.    
  146.                if (asv_name in autosave_array)
  147.                   # delete the file first so no backups
  148.                   # will be made
  149.                   unlink( asv_name )
  150.                else
  151.                   # new filename entry 
  152.                   autosave_array[ asv_name ] = asv_name;
  153.            
  154.                # clear the autosave bit so we will know next time
  155.                # whether any more changes have occurred
  156.                buffer_flags = and(buffer_flags, not(BUFFER_AUTOSAVE_MODIFIED))
  157.    
  158.                message( "Autosaving '" fname "'." );
  159.                write_buffer( asv_name )
  160.                #message( "'"fname"' saved." )
  161.                message( "Temp file '"asv_name"' autosaved." )
  162.             }
  163.          }
  164.       }
  165.       next_buffer( "", 1, 1) # must include system buffers in case we started in one
  166.    } while (old_curnt_buffer != current_buffer)
  167.  
  168.    current_buffer = old_curnt_buffer;
  169. }
  170.  
  171. function autosave_exit()
  172. {
  173.    local   i;
  174.  
  175.    if ( !really_close )
  176.       return 0
  177.  
  178.    for (i in autosave_array)
  179.    {
  180.       unlink( autosave_array[ i ] );  # delete the file
  181.       delete( autosave_array[ i ] );  # delete the array element
  182.    }
  183. }
  184.  
  185.  
  186. function autosave_kbd_hit()
  187. {
  188.    accumulative_save_time = 0;
  189. }
  190.  
  191. function autosave_settings( settings_index, settings_data )
  192. {
  193.    local new_array;
  194.  
  195.    if ( autosave_active() )
  196.       settings_data[ settings_index++ ] = sprintf( "autosave %d\n", autosave_time )
  197.    
  198.    new_array.array = settings_data
  199.    new_array.index = settings_index
  200.    return new_array
  201. }
  202.  
  203.