home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/autosave.pev 1.27 27 Aug 1990 11:23:18 ericj $
-
- ##############################################################################
- #
- # Sage Software - POLYTRON Division
- # 1700 NW 167th Place
- # Beaverton, OR 97006
- #
- # Copyright 1990, Sage Software, Inc.
- #
- # Permission is hereby granted for licensed users of Sage Professional
- # Editor and PolyAwk to copy and modify this source code for their own
- # personal use. These derivative works may be distributed only to other
- # licensed Sage Professional Editor and PolyAwk users. All other usage
- # is prohibited without express written permission from Sage Software.
- #
- ##############################################################################
-
- #### $Workfile: autosave.pel $: Support for automatically saving buffers
-
- global autosave_time = 15 # every 15 seconds #PUBLIC #INT
- global autosave_force_time = 0 # number of seconds before forcing an autosave
-
- local autosave_on = FALSE # initially no autosave
- local autosave_array
- local accumulative_force_time # accumulative time since last forced autosave
- local accumulative_save_time # accumulative time since last autosave
-
-
- local autosave_files_id
- local autosave_exit_id
- local autosave_kbd_id
-
- local AUTOSAVE_EXT_CHAR = "!" # extension character for autosave files
-
-
- ## autosave()
- #
- # Invoke or toggle autosaving.
- # atime (if specified) is the number of seconds between autosaves
- # ftime (if specified) is the number of seconds between forced saves
- #
- # If atime is not specified, autosave_time is used.
- #
- global function autosave( atime, ftime ) { #PUBLIC #VOID
- local prev_autosave = autosave_on;
-
- #
- # determine new setting
- #
- if( argcount() < 1 )
- autosave_on = !autosave_on
- else {
- if ( (autosave_on = 0+atime) )
- autosave_time = autosave_on;
- }
-
- accumulative_save_time = 0
- accumulative_force_time = 0;
-
-
- if ( autosave_on ) {
- idle_threshold = 1; # always set to 1 and we'll figure
- # out later whether autosave_time
- # has been exceeded
-
- if (argcount() == 2)
- autosave_force_time = 0+ftime;
-
- if (!prev_autosave) {
- autosave_files_id = function_id( "autosave_files" );
- autosave_exit_id = function_id( "autosave_exit" );
- autosave_kbd_id = function_id( "autosave_kbd_hit" );
- attach_event_handler( EVENT_IDLE_THRESHOLD, autosave_files_id )
- attach_event_handler( EVENT_EXIT_EDITOR, autosave_exit_id )
- attach_event_handler( EVENT_KEYPRESS, autosave_kbd_id )
- }
- message( "Autosave is on." )
- } else {
- if (prev_autosave) {
- delete_event( EVENT_IDLE_THRESHOLD, autosave_files_id)
- delete_event( EVENT_KEYPRESS, autosave_kbd_id)
- }
- message( "Autosave is off." )
- }
- }
-
-
- #
- # autosave_files()
- #
- # write all of the modified buffers to a new ".__!" file.
- #
- global function autosave_files()
- {
- local old_curnt_buffer = current_buffer
- local asv_name
- local fname;
- local forcesave = 0;
- local diff;
- local cw = current_window;
- local prev_msg = "";
- local wx0, wy0;
-
- accumulative_save_time += 1;
- accumulative_force_time += 1;
-
- if (autosave_force_time \
- && (accumulative_force_time >= autosave_force_time)) {
- forcesave = 1;
- accumulative_force_time = 0;
- } else if (accumulative_save_time < autosave_time) {
- return;
- }
-
- accumulative_save_time = 0;
-
- if (dialog_window){
- current_window = dialog_window;
- wx0 = window_cursor_x;
- wy0 = window_cursor_y;
- window_cursor_x = window_cursor_y = 0;
- prev_msg = read_window( window_text_width * window_text_height );
- }
-
- do {
- #
- # if a key is pressed, stop autosaving and return
- # to process the key.
- #
- if (!forcesave && keyboard_input_pending) {
- break;
- }
- if (!(and(buffer_flags, BUFFER_SYSTEM))) {
- #
- # see if the buffer has changed
- #
- if ( and( buffer_flags, BUFFER_AUTOSAVE_MODIFIED) ) {
- if (buffer_filename) {
- asv_name = fname = buffer_filename;
-
- asv_name = create_semaphore_fname( asv_name, AUTOSAVE_EXT_CHAR );
-
- if (asv_name in autosave_array)
- # delete the file first so no backups
- # will be made
- unlink( asv_name )
- else
- # new filename entry
- autosave_array[ asv_name ] = asv_name;
-
- # clear the autosave bit so we will know next time
- # whether any more changes have occurred
- buffer_flags = and(buffer_flags, not(BUFFER_AUTOSAVE_MODIFIED))
-
- message( "Autosaving '" fname "'." );
- write_buffer( asv_name )
- message( "'"fname"' saved." )
- }
- }
- }
- next_buffer( "", 1)
- } while (old_curnt_buffer != current_buffer)
-
- if (prev_msg){
- window_cursor_x = 0;
- window_cursor_y = 0;
- write_window( prev_msg );
- window_cursor_x = wx0;
- window_cursor_y = wy0;
- }
-
- assign_current_window( cw );
- current_buffer = old_curnt_buffer;
- display_update();
- }
-
- function autosave_exit() {
- local i;
-
- for (i in autosave_array) {
- unlink( autosave_array[ i ] ); # delete the file
- delete( autosave_array[ i ] ); # delete the array element
- }
- }
-
-
- function autosave_kbd_hit(){
- accumulative_save_time = 0;
- }
-