home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/state.pev 1.42 21 Aug 1990 09:22:02 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: state.pel $: config/state file reading and writing
-
- ###
- ### Editor Configuration File
- ###
-
- ## global variable to enable (1) or disable (0) state files
- #
- global save_state = 0 #PUBLIC #INT
- global initialize_config_file # used only first time editor is invoked
-
- local state_offset
-
- ## read configuration and state information saved in a cfg file
- #
- global function read_config_file() { #NO LONGER PUBLIC #INT
-
- local cfgName
- local cf
- local cmd
- local pos
- local buf
- local fid
- local prevEol = default_buffer_eol_string
-
- # locate the config file in the current directory or SAGEEDIT path
- if ( !(cfgName = locate_sageedit_file( SAGEEDIT_FILE_CONFIG ))) {
- return
- }
-
- # create a system buffer for the config file
- default_buffer_eol_string = "\r\n"
- cf = create_buffer( \
- "", \
- cfgName, \
- BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY )
-
- # restore the default end of line sequence
- default_buffer_eol_string = prevEol
-
- if ( cf ) {
- current_buffer = buf = cf
- # goto_buffer_top()
-
- do {
- if ( (cmd = read_buffer()) != "" ) {
- # comments must have a "#" in col 1
- if ( substr(cmd,1,1) == "#" ) {
- continue
- }
- if ( (cmd = trim( cmd )) ) {
- if ( cmd == "$STATE$" ) {
- state_offset = buffer_offset
- if ( !(0+save_state) || ARGC != 1 )
- break
- } else {
- current_buffer = buf
-
- # execute config command
- if ((fid = quoted_function_id(cmd))) {
- execute_function( fid )
- } else {
- warning( "config: \"%s\" is invalid", cmd )
- }
-
- buf = current_buffer
- current_buffer = cf
- }
- }
- }
- } while ( down( 1 ) )
-
- # delete the temporary system buffer and make the last buffer
- # in the list the current buffer. (process_command_line will
- # do a next_buffer() to make the first buffer current.)
-
- delete_buffer( cf )
- }
-
- # reset the state file the first time the editor is invoked
- if ( initialize_config_file == TRUE ) {
- initialize_config_file = FALSE
- resetStateFIle( cfgName )
- setup()
- }
-
- # install an event to write state information upon exit
- attach_event_handler( EVENT_EXIT_EDITOR, \
- function_id( "write_state_file" ) )
-
- return (cf != 0)
- }
-
- # empty the state file
- local function resetStateFIle( cfgName ) {
- local lfil
-
- if ( (lfil = fopen( cfgName, 1 )) >= 0 ) {
- fprintf( lfil, "# Sage Professional Editor - Configuration File\n" )
- fclose( lfil )
- }
- }
-
-
- ## event handler to save the current editor state to the config file
- #
- global function write_state_file() {
-
- local cfgName
- local cf
- local firstBuf
- local buf
- local fmt
- local flg
- local newline_required
- local noStartWin = 1
- local firstWin
- local icon, icon_x, icon_y
- local prevEol = default_buffer_eol_string
-
- if ( !(0+save_state) ) {
- return 0
- }
-
-
- # search sageedit path for the config file
- if ( !(cfgName = locate_sageedit_file(SAGEEDIT_FILE_CONFIG))) {
- cfgName = editor_path( SAGEEDIT_FILE_CONFIG )
- }
-
- if ((cf = fopen( cfgName, 2 )) < 0) {
- return 0
- }
-
- # locate $STATE$ keyword
-
- fseek( cf, state_offset, 0 )
- if ( fgetc( cf ) != ord("$") ) {
-
- # If we reach here, the config file must be re-initialized.
- # This happens whenever no config file exists, or when the
- # config file is modified after the call to read_config_file.
-
- # open config file as a system buffer
- fclose( cf )
- default_buffer_eol_string = "\r\n"
- current_buffer = create_buffer( \
- "Config File", \
- cfgName, \
- BUFFER_SYSTEM + BUFFER_NO_UNDO + BUFFER_REAL_SPACE_ONLY )
-
- # restore the default end of line sequence
- default_buffer_eol_string = prevEol
-
- # find the seek position of $STATE$
- if ( !search( /^\$STATE\$[ \t]*$/, SEARCH_FWD_REGEX ) ) {
- goto_buffer_bottom()
- if ( current_line_length ) {
- newline_required = 1
- }
- }
- state_offset = buffer_offset
-
- # re-open config file as a writable file
- delete_buffer()
- if ((cf = fopen( cfgName, 2 )) < 0)
- return 0;
-
- fseek( cf, state_offset, 0 )
-
- # ensure $STATE$ appears in column 1
- if ( newline_required ) {
- fprintf( cf, "\n$" )
- } else {
- fprintf( cf, "$" )
- }
- }
-
-
- # append state information
-
- fprintf( cf, "STATE$\n" )
- fprintf( cf, "#Sage Professional Editor - State Information\t\t%s\n", ctime() )
- fprintf( cf, "search_flags=%d\n", search_flags )
- if ( search_pattern != "" )
- fprintf( cf, "search_pattern=%s\n", search_pattern )
- if ( search_replacement != "" )
- fprintf( cf, "search_replacement=%s\n", search_replacement)
- if ( display_height > 25 )
- fprintf( cf, "toggle_display 1\n" )
-
- buf = firstBuf = current_buffer
-
- # ensure that firstBuf is not a system buffer
- if ( and( buffer_flags, BUFFER_SYSTEM ) ) {
- firstBuf = next_buffer()
- }
-
- fmt = "edit_file %s\n" \
- "buffer_name=%s\n" \
- "buffer_flags=%d\n" \
- "current_line=%d\n" \
- "current_column=%d\n" \
- "buffer_tabs=%s\n"
-
- do {
- if (buffer_filename) {
- flg = and( buffer_flags, \
- not(BUFFER_MODIFIED \
- +BUFFER_AUTOSAVE_MODIFIED \
- +BUFFER_IN_VIRTUAL_SPACE) \
- ) \
-
- fprintf( cf, fmt, buffer_filename, \
- buffer_name, \
- flg, \
- current_line, \
- current_column, \
- buffer_tabs )
- }
- } while ( next_buffer() != firstBuf )
-
-
- firstWin = current_window;
-
- do {
- next_window( "", 1 );
- # don't save information on system windows
- if ( !and( window_flags, WINDOW_SYSTEM ) ) {
- if (noStartWin) {
- fprintf( cf, "delete_default_window\n" );# make a least one window
- noStartWin = 0;
- }
-
- icon = and( window_flags, WINDOW_ZOOM )
- if ( icon != WINDOW_NORMAL ) {
- # window is an icon or expanded
- if ( icon == WINDOW_COLLAPSED ) {
- icon_x = window_x0
- icon_y = window_y0
- }
- restore_window()
- }
-
- fprintf( cf, "create_window %d %d %d %d %d\n",
- window_x0,
- window_y0,
- window_width,
- window_height,
- window_flags );
-
- fprintf( cf, "color_text=%d\n", color_text );
- fprintf( cf, "color_border=%d\n", color_border );
- fprintf( cf, "next_window\n" );
- fprintf( cf, "next_buffer %s\n", buffer_name );
- fprintf( cf, "attach_win_to_buf\n" );
-
- if ( icon != WINDOW_NORMAL ) {
- if ( icon == WINDOW_COLLAPSED ) {
- # make window an icon
- fprintf( cf, "collapse_window\n" )
- fprintf( cf, "frame_window %d %d 14 1\n",
- icon_x, icon_y )
- } else {
- # make window expanded
- fprintf( cf, "expand_window\n" )
- }
- }
- }
- } while (firstWin != current_window);
-
- # fprintf( cf, "next_buffer %s\n", buffer_name )
-
- current_buffer = buf
-
- fwrite( cf, 0, 0 ); # truncate the file
- fclose( cf )
-
- return 1
- }
-
- #
- # attach_win_to_buf()
- #
- # provide a means of attaching the current window to the current buffer
- # from within an execute_function call.
- #
- global function attach_win_to_buf(){
- attach_window_buffer( current_window, current_buffer );
- }
-
- global function delete_default_window(){
- local cw = current_window;
- local wf = window_flags;
-
- next_window();
-
- while( !and( window_flags, WINDOW_SYSTEM ) && current_window) {
- delete_window();
- next_window();
- }
-
- if (and( wf, WINDOW_SYSTEM) && cw)
- assign_current_window( cw );
- }