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

  1. # $Header:   P:/source/ppee/macros/autosave.pev   1.27   27 Aug 1990 11:23:18   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:   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.  
  30. local    autosave_files_id
  31. local    autosave_exit_id
  32. local    autosave_kbd_id
  33.  
  34. local AUTOSAVE_EXT_CHAR = "!"    # extension character for autosave files
  35.  
  36.  
  37. ## autosave()
  38. #
  39. #  Invoke or toggle autosaving.
  40. #  atime (if specified) is the number of seconds between autosaves
  41. #  ftime (if specified) is the number of seconds between forced saves
  42. #
  43. #  If atime is not specified, autosave_time is used.
  44. #
  45. global function autosave( atime, ftime ) {            #PUBLIC #VOID
  46.     local    prev_autosave = autosave_on;
  47.  
  48.     #
  49.     # determine new setting
  50.     #
  51.     if( argcount() < 1 )
  52.         autosave_on = !autosave_on
  53.     else {
  54.         if ( (autosave_on = 0+atime) )
  55.             autosave_time = autosave_on;
  56.     }
  57.  
  58.     accumulative_save_time = 0
  59.     accumulative_force_time = 0;
  60.  
  61.  
  62.     if ( autosave_on ) {
  63.         idle_threshold = 1;    # always set to 1 and we'll figure
  64.                     # out later whether autosave_time
  65.                     # has been exceeded
  66.  
  67.         if (argcount() == 2)
  68.             autosave_force_time = 0+ftime;
  69.  
  70.         if (!prev_autosave) {
  71.             autosave_files_id = function_id( "autosave_files" );
  72.             autosave_exit_id  = function_id( "autosave_exit" );
  73.             autosave_kbd_id   = function_id( "autosave_kbd_hit" );
  74.             attach_event_handler( EVENT_IDLE_THRESHOLD, autosave_files_id )
  75.             attach_event_handler( EVENT_EXIT_EDITOR, autosave_exit_id )
  76.             attach_event_handler( EVENT_KEYPRESS, autosave_kbd_id )
  77.         }
  78.         message( "Autosave is on." )
  79.     } else {
  80.         if (prev_autosave) {
  81.             delete_event( EVENT_IDLE_THRESHOLD, autosave_files_id)
  82.             delete_event( EVENT_KEYPRESS, autosave_kbd_id)
  83.         }
  84.         message( "Autosave is off." )
  85.     }
  86. }
  87.  
  88.  
  89. #
  90. # autosave_files()
  91. #
  92. # write all of the modified buffers to a new ".__!" file.
  93. #
  94. global function autosave_files()
  95. {
  96.     local    old_curnt_buffer = current_buffer
  97.     local    asv_name
  98.     local    fname;
  99.     local   forcesave = 0;
  100.     local   diff;
  101.     local    cw = current_window;
  102.     local   prev_msg = "";
  103.     local   wx0, wy0;
  104.  
  105.     accumulative_save_time += 1;
  106.     accumulative_force_time += 1;
  107.  
  108.     if (autosave_force_time \
  109.         && (accumulative_force_time >= autosave_force_time)) {
  110.         forcesave = 1;
  111.         accumulative_force_time = 0;
  112.     } else if (accumulative_save_time < autosave_time) {
  113.         return;
  114.     }
  115.  
  116.     accumulative_save_time = 0;
  117.  
  118.     if (dialog_window){
  119.         current_window = dialog_window;
  120.         wx0 = window_cursor_x;
  121.         wy0 = window_cursor_y;
  122.         window_cursor_x = window_cursor_y = 0;
  123.         prev_msg = read_window( window_text_width * window_text_height );
  124.     }
  125.  
  126.     do {
  127.         #
  128.         # if a key is pressed, stop autosaving and return
  129.         # to process the key.
  130.         #
  131.         if (!forcesave && keyboard_input_pending) {
  132.             break;
  133.         }
  134.         if (!(and(buffer_flags, BUFFER_SYSTEM))) {
  135.             #
  136.             # see if the buffer has changed
  137.             #
  138.             if ( and( buffer_flags, BUFFER_AUTOSAVE_MODIFIED) ) {
  139.                 if (buffer_filename) {
  140.                     asv_name = fname = buffer_filename;
  141.  
  142.                     asv_name = create_semaphore_fname( asv_name, AUTOSAVE_EXT_CHAR );
  143.  
  144.                     if (asv_name in autosave_array)
  145.                         # delete the file first so no backups
  146.                         # will be made
  147.                         unlink( asv_name )
  148.                     else
  149.                         # new filename entry 
  150.                         autosave_array[ asv_name ] = asv_name;
  151.     
  152.                     # clear the autosave bit so we will know next time
  153.                     # whether any more changes have occurred
  154.                     buffer_flags = and(buffer_flags, not(BUFFER_AUTOSAVE_MODIFIED))
  155.  
  156.                     message( "Autosaving '" fname "'." );
  157.                     write_buffer( asv_name )
  158.                     message( "'"fname"' saved." )
  159.                 }
  160.             }
  161.         }
  162.         next_buffer( "", 1)
  163.     } while (old_curnt_buffer != current_buffer)
  164.  
  165.     if (prev_msg){
  166.         window_cursor_x = 0;
  167.         window_cursor_y = 0;
  168.         write_window( prev_msg );
  169.         window_cursor_x = wx0;
  170.         window_cursor_y = wy0;
  171.     }
  172.  
  173.     assign_current_window( cw );
  174.     current_buffer = old_curnt_buffer;
  175.     display_update();
  176. }
  177.  
  178. function autosave_exit() {
  179.     local    i;
  180.  
  181.     for (i in autosave_array) {
  182.         unlink( autosave_array[ i ] );    # delete the file
  183.         delete( autosave_array[ i ] );    # delete the array element
  184.     }
  185. }
  186.  
  187.  
  188. function autosave_kbd_hit(){
  189.     accumulative_save_time = 0;
  190. }
  191.