!+
! PRINT_BUFFER.TPU
!-
!
! The 3 following procedures copies the current buffer to another buffer,
! translates control characters to readable characters and writes the
! new buffer. It then submits the file to the specified print que (default
! sys$print). The first two procedures are taken from this note file 
! and modified a bit. The last procedure calls the other two and creates
! the subprocess/writes the file/prints the file. 
!

!
! This procedure translates control characters to readable characters.
!
procedure eve$translate_controls (char)

! The backwards questions mark is the placeholder for control characters
! from ASCII(0) thru ASCII(31) on the VT2xx series of terminals
CASE char FROM ' ' TO ''

    [' '] : COPY_TEXT ('<NUL>');
    [''] : COPY_TEXT ('<SOH>');
    [''] : COPY_TEXT ('<STX>');
    [''] : COPY_TEXT ('<ETX>');
    [''] : COPY_TEXT ('<EOT>');
    [''] : COPY_TEXT ('<ENQ>');
    [''] : COPY_TEXT ('<ACK>');
    [''] : COPY_TEXT ('<BEL>');
    [''] : COPY_TEXT ('<BS>');
    [''] : COPY_TEXT ('<SO>');
    [''] : COPY_TEXT ('<SI>');
    [''] : COPY_TEXT ('<DLE>');
    [''] : COPY_TEXT ('<DC1>');
    [''] : COPY_TEXT ('<DC2>');
    [''] : COPY_TEXT ('<DC3>');
    [''] : COPY_TEXT ('<DC4>');
    [''] : COPY_TEXT ('<NAK>');
    [''] : COPY_TEXT ('<SYN>');
    [''] : COPY_TEXT ('<ETB>');
    [''] : COPY_TEXT ('<CAN>');
    [''] : COPY_TEXT ('<EM>');
    [''] : COPY_TEXT ('<SUB>');
    [''] : COPY_TEXT ('<ESC>');
    [''] : COPY_TEXT ('<FS>');
    [''] : COPY_TEXT ('<GS>');
    [''] : COPY_TEXT ('<RS>');
    [''] : COPY_TEXT ('<US>');
    [INRANGE, OUTRANGE] : COPY_TEXT (char);

endcase;
endprocedure

! This procedure controls the outer loop search for the special
! control characters that we want to view
!
procedure eve$search_controls (this_buffer)
local
    control_char_pat,
    control_char,
    char_to_translate;

! When the search fails we know that we have either hit the end of
! the buffer or there were no more special characters found.
on_error
   position (translate_buffer);
   return;
endon_error;

if get_info(translate_buffer,"type") = UNSPECIFIED then
    translate_buffer := create_buffer ('translation');
    set (no_write, translate_buffer);
endif;
control_char_pat := any (' ');

position (translate_buffer);
erase (translate_buffer);
copy_text (this_buffer);	! Make a copy of the original buffer
position (beginning_of (translate_buffer));

loop	! Find all occurrences
    control_char := search (control_char_pat, forward);
    position (control_char);
    char_to_translate := current_character;	! Save the character
    erase (control_char);			! then erase it
    eve$translate_controls (char_to_translate);	! Substitute the new text
endloop;

endprocedure

!
! Procedure to print the current buffer.
!
procedure eve_print_buffer
local this_position,
 this_buffer,
 buffer_name,
 file_name,
 print_command,
 print_process;
on_error
	if error = tpu$_createfail then
	     message("Subprocess could not be created");
	     return;
	endif;
endon_error;

set(informational,off);
set(success,off);
this_position := mark(none);
this_buffer := current_buffer;

eve$search_controls(this_buffer);		! Translate control characters.

! Get the output file from the original buffer and use it to write the
! translated buffer.

buffer_name := get_info(this_buffer,"name");
file_name := get_info(this_buffer,"file_name"); 
if file_name = "" then
	file_name := read_line
    (fao("Enter a file name to write buffer !AS or press RETURN to cancel: ",
    buffer_name));
	if file_name = "" then
	    set(informational,on);
	    set(success,on);	
            return;
      	endif;
endif;

if ( index(file_name,";") <> 0 )
   then file_name := substr(file_name,1,index(file_name,";") - 1);
endif;

! Set the output file on the original buffer. Consistent with eve_write_file.

set(output_file,this_buffer,file_name);	
set(output_file,translate_buffer,file_name);
write_file(translate_buffer);
print_command := read_line("Print command: ");
if print_command = "" then 
	print_command := "PRINT";
endif;
print_command := print_command + " ";
message(fao("Printing !AS with command !AS",file_name,print_command));
print_process := create_process(message_buffer,"$set noon");
send(print_command + file_name, print_process);

delete(print_process);
set(informational,on);
set(success,on);
update(message_window);

position(this_position);

endprocedure
