!Eve Advanced, Version 1 Build
!
!       A rehash of DEC's 3 build procedures.
!
![Start of Dec's documentation]
!       This file doesn't contain any of the hacks, rather it defines a
!       few basic building blocks and three procedures comprising an
!       "include" feature and then executes a buffer which includes the
!       various pacakages that make up "EVE plus". The three procedures
!       are used thusly:
!
!       eveplus_start_build;            ! Set-up to gather TPU$LOCAL_INITs
!
!       eveplus_include("...");         ! Include the first file
!       eveplus_include("...");         ! Include the next file
!               :
!       eveplus_include("...");         ! Include the last file
!
!       eveplus_complete_build;         ! Compile the accumulated local init
!
![End of Dec's documentation]
!
!       eveplus_log_build;              ! Logs the build into a file
!
!       Some differences.  Now the package does not depend on finding the
!       kernel first; this means you don't necessarily have to use the
!       EVEplus kernel routines or be burdened with them needlessly.  This
!       also makes following the TPU code much easier.  Now the package
!       can be used to develop your own routines and key def's.  It does
!       not save anything for you automatically!  If you like what you got,
!       you must save the section file by and or in your build script file.
!       If you're a real masochist, you could append
!       a build script to the end of this file and use it as your tpuini.
!

procedure eveplus_start_build           ! Make init buffer & check environment

local   old_buffer,                             ! Buffer when we were entered
        input_file_name,
        buffer_file_name;

        old_buffer := current_buffer;           ! Remember where we were

input_file_name:=get_info(current_buffer,"file_name");
if length(input_file_name)=0 then               !need to get ini buffer
        input_file_name:=get_info(command_line,"command_file");
endif;

erase(message_buffer);
message("EVE BUILD");
message("       File:  "+input_file_name);
message("       Time:  "+fao("!%D",0));
position(old_buffer);

!    eve_buffer("messages");                    !use map and unmap instead
    map(info_window,message_buffer);
    update (current_window);
    eveplus_local_init := create_buffer("eveplus_local_init");
    set(no_write, eveplus_local_init);          ! Create an RO buffer

    eveplus_local_init_name := "tpu$local_init";

    position(eveplus_local_init);               ! Put in our minimal local_init
    copy_text("procedure ");                    !  procedure which updates EVE's
    copy_text(eveplus_local_init_name);         !  version number by adding a
    split_line;                                 !  "+" to the end of it.
    copy_text("    eve$x_version := eve$x_version + '+';");
    split_line;
    copy_text("    eveplus_g_shift_key := ctrl_y_key; ");
    split_line;
    copy_text("    eveplus_building := FALSE;");
    split_line;
    copy_text("endprocedure");
    split_line;

    eveplus_building := TRUE;

    position(old_buffer);                       ! Go back

endprocedure

procedure eveplus_include(file_name)    ! Include a .TPU file

local   temp_buffer,                    ! Buffer to compile in
        compiled_code,                  ! The compiled code
        expanded_file,                  ! Full name after defaulting etc.
        master_file,                    ! Name of the file associated with
                                        !  the current buffer - for defaulting
        init_code,                      ! The tpu$local_init from this file
        old_buffer,                     ! Buffer when we were entered
        temp,                           ! Junk
        end_pattern,                    ! "endprocedure" pattern
        white_space,                    ! White-space spanning pattern
        the_pattern;                    ! Pattern that matches the whole
                                        !  tpu$local_init procedure

    master_file := get_info(current_buffer, "file_name");
    expanded_file := file_parse(file_name, ".TPU;", master_file);

    temp := file_search("");                    ! Reset file_search

    if (file_search(expanded_file) = "") then   ! Does it exist?
        message(" ");
        message("Can't find: " + expanded_file);! Nope.
    else
        message(" ");
        message("Including "+expanded_file);
        old_buffer := current_buffer;           ! Remeber where we were
        temp_buffer := create_buffer(expanded_file,
                                     expanded_file);
        set(no_write, temp_buffer);
        position(temp_buffer);                  ! Go to the init buffer

        end_pattern := line_begin &             ! Build a pattern that'll match
                       "endprocedure";          !  a tpu$local_init definition
        white_space := " " + ascii(9);          !  with the procedure and
        the_pattern := line_begin &             !  endprocedure both at the
                       "procedure" &            !  left margin.
                       spanl(white_space) &
                       eveplus_local_init_name &
                       end_pattern;

        init_code := eveplus_search_quietly(the_pattern,
                                    FORWARD);   ! Find the init code
        if (init_code <> 0) then                ! and copy it
            position(eveplus_local_init);       !  to the init buffer
            split_line;                         !  after a blank line.
            copy_text(init_code);
        endif;

        set(informational, on);                 ! Compile the buffer
        compiled_code := compile(temp_buffer);  !  (including the
        if (compiled_code <> 0) then            !  tpu$local_init
            execute(compiled_code);             !  procedure!)
        endif;
        set(informational, off);

        position(old_buffer);                   ! Go back and clean up
        erase(temp_buffer);
        delete(temp_buffer);
    endif;
    temp := file_search("");                    ! Keep from affecting
                                                !  other file_searches
endprocedure

!
!       Procedure to combine the collected tpu$local_init procedures and
!       compile them.
!

procedure eveplus_complete_build                ! Compile all the local inits

local   old_buffer,                             ! Buffer when we were entered
        proc_pattern,                           ! "procedure" pattern
        white_space,                            ! White-space spanning pattern
        the_junk,
        the_pattern;                            ! Pattern that matches the extra
                                                ! endprocedure/procedure pairs

    old_buffer := current_buffer;               ! Remember where we were
    white_space := " " + ascii(9);              ! Create a pattern to
    proc_pattern := line_begin &                !  match an endprocedure
                    "procedure" &               !  followed by a
                                                !  procedure tpu$local_init
                    spanl(white_space) &        ! NOTE:
                    eveplus_local_init_name;    !  Any lines between the
    the_pattern := line_begin &                 !  end of one procedure
                   "endprocedure" &             !  and the beginning of
                   proc_pattern;                !  the next will go away

    position(beginning_of(eveplus_local_init));

    loop                                        ! Search and destroy
        the_junk := eveplus_search_quietly(the_pattern,
                                   FORWARD);    !  the extraneous
        exitif (the_junk = 0);                  !  statements
        erase(the_junk);
    endloop;

    position(beginning_of(eveplus_local_init));

    loop                                        ! Remove any blank lines
        the_junk := eveplus_search_quietly(line_end & line_end, FORWARD);
        exitif (the_junk = 0);
        position(beginning_of(the_junk));
        erase(the_junk);
        move_horizontal(1);
        exitif (mark(none) = end_of(current_buffer));
        move_horizontal(-1);
        split_line;
        move_vertical(-1);
    endloop;

    compile(eveplus_local_init);                ! And compile what's left.
    position(old_buffer);                       ! Go back

!    erase(eveplus_local_init);                  ! And clean up.
!    delete(eveplus_local_init);

    execute(eveplus_local_init_name);           ! Execute the local init
    message("");
    message("Build completed");

    unmap(info_window);
    update(message_window);

endprocedure


!
! Writes the message buffer (which has been accumulating log messages) to
! a file with the same name as the build file, with an extension of .LOG
! for future reference (date and time of build, list of routines, any error
! messages).
!
procedure eveplus_log_build
local input_file_name,dot_seen,valid_file_name,x,log_file_name,buffer_file_name;
input_file_name:=get_info(current_buffer,"file_name");
x:=length(input_file_name);
if x=0 then                                             !need to get ini buffer
        input_file_name:=get_info(command_line,"command_file");
        x:=length(input_file_name);
endif;
dot_seen:=0;
valid_file_name:=1;
loop
        if x=0 then valid_file_name:=0; exitif 1; endif;
        if substr(input_file_name,x,1)="." then dot_seen:=1; endif;
        exitif dot_seen;
        x:=x-1;
endloop;
if not(valid_file_name) then
        message("Could not generate log file; invalid file name for buffer");
        return(0);
endif;
log_file_name:=substr(input_file_name,1,x-1)+".LOG";
message(write_file(message_buffer,log_file_name)+" written");
endprocedure


procedure eveplus_search_quietly(target, dir)   ! Search w/o "String not found"

on_error
    return(0);
endon_error;

    return(search(target, dir));

endprocedure;

save("$abel:build.tpu$section");
quit;
