!++
! FILENAME: CHAR_MANIP.TPU 
! FUNCTION: This file contains procedures which will allow the user to embed
!           various characters into the text file as well as manipulate the
!           text.
! AUTHOR:   Steven K. Shapiro, (C) Copyright SKS Enterprises, Austin TX. 
!                                  All Rights Reserved.
!
!           The format, structure and contents of this file are the sole
!           property  of Steven K. Shapiro  and are  copyrighted to  SKS
!           Enterprises, Austin Texas.
!
!           The information may be freely distributed, used and modified
!           provided  that the  information in this  header block is not
!           changed, altered, disturbed or modified in any way.
!
! DATE:     25-AUG-1987 Original.
! HISTORY:  current.
! CONTENTS:
!           eve_put_crlf            
!           eve_put_lf              
!           eve_put_ff              
!           eve_put_esc             
!           eve_put_cr              
!           eve_put_bs              
!           eve_raa                 
!           eve_faa                 
!           eve_specins (number_parameter)
!           eve_inspecial
!           eve_insertchars (numchrs)
!
!23456789A123456789B123456789C123456789D123456789E123456789F123456789G123456789H
!--
!*----------------------------------------------------------------------------*!

procedure char_manip_module_ident 

  local file_date,
        module_vers;

  file_date := "-<(  1-DEC-1988 15:01:37.22 )>-";
  module_vers := substr(file_date,5,2) +
                 substr(file_date,8,3) +
                 substr(file_date,14,2) +
                 substr(file_date,17,5) ;

  return module_vers;

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_put_crlf

    copy_text("
");

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_put_lf

    copy_text("
");

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_put_ff

    copy_text("");

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_put_esc

    copy_text("");

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_put_cr

    copy_text("");

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_put_bs

    copy_text("");

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_raa
!
! This search and replace procedure will allow the user to replace unprintable
! ASCII characters with printable strings.  The case-sensitivity of search is
! same as for the find command.  If case-insensitive, replacements
! are done to match case of current occurrence.
!

local   number,
        target,                 ! Target to be replaced.
        replacement,            ! String target is to be replaced with.
        this_buffer,            ! Current buffer
        this_mode,              ! Keyword for current mode
        lowercase_target,       ! Lowercase version of target string
        lowercase_replacement,  ! Lowercase version of replacement string
        uppercase_target,       ! Uppercase version of target string
        uppercase_replacement,  ! Uppercase version of replacement string
        capital_target,         ! Capitalized version of target string
        capital_replacement,    ! Capitalized version of replacement string
        how_exact,              ! Keyword to indicate case-sensitivity
        replace_range,          ! Range of current occurrence
        highlight_range,        ! Reverse-video version of replace_range
        replace_action,         ! String reply to prompt
        action_length,          ! Length of replace_action
        asking,                 ! True unless "all" option has been chosen
        replace_count,          ! count of replace's to do
        this_occurrence,        ! String of replace_range
        occurrences;            ! Number of replacements made so far

    this_buffer := current_buffer;
    this_mode := get_info (current_buffer, "mode");
    set (insert, this_buffer);
    asking := 1;
    replace_count := 0;

    if not (eve$prompt_number ("", number,
                               "Number: ", "No number given"))
    then
      return;
    endif;

    target := ASCII(number);

    replacement := read_line ("New string: "); ! empty string is ok here

    lowercase_target := target;
    if get_info (lowercase_target, "type") = string
    then
        change_case (lowercase_target, lower);
    endif;
    lowercase_replacement := replacement;
    change_case (lowercase_replacement, lower);
    if (lowercase_target = target) and (lowercase_replacement = replacement)
    then
        how_exact := no_exact;
        uppercase_target := target;
        if get_info (uppercase_target, "type") = string
        then
            change_case (uppercase_target, upper);
        endif;
        capital_target := target;
        if get_info (capital_target, "type") = string
        then
            eve$capitalize_string (capital_target);
        endif;
        uppercase_replacement := replacement;
        change_case (uppercase_replacement, upper);
        capital_replacement := replacement;
        eve$capitalize_string (capital_replacement);
    else
        how_exact := exact;

    endif;

    loop
        replace_range := eve$find (target, 1);
        exitif replace_range = 0;
        highlight_range :=
            create_range (beginning_of (replace_range),
                      end_of (replace_range), eve$x_highlighting);
        position (beginning_of (replace_range));
        update (current_window);
        loop
            if asking
            then
                replace_action := read_line
                ("Replace? Type - a number, yes, no, all, last, or quit: ");
                change_case (replace_action, lower);
                replace_count := int(replace_action);
            else
                replace_action := "yes";
            endif;
            action_length := length (replace_action);
            if (replace_action = substr ("yes", 1, action_length)) or
                (replace_action = substr ("all", 1, action_length)) or
                (replace_action = substr ("last", 1, action_length)) or
                (replace_count <> 0) or (action_length = 0)
            then
                highlight_range := 0;
                this_occurrence := erase_character (length (replace_range));
                if how_exact = exact
                then
                    copy_text (replacement);
                else
                    ! Make sure non-alphabetic target is replaced by lowercase
                    if this_occurrence = lowercase_target
                    then
                        copy_text (lowercase_replacement);
                    else
                        if this_occurrence = uppercase_target
                        then
                            copy_text (uppercase_replacement);
                        else
                            if this_occurrence = capital_target
                            then
                                copy_text (capital_replacement);
                            else
                                copy_text (lowercase_replacement);
                            endif;
                        endif;
                    endif;
                endif;
                if current_direction = reverse
                then
                    move_horizontal (- length (replacement));
                endif;
                occurrences := occurrences + 1;
                update (current_window);
                if (replace_action = substr ("all", 1, action_length)) and
                    (action_length > 0)
                then
                    asking := 0;
                    message ("Replacing all occurrences...");
                    set (screen_update, off);
                endif;
                if (replace_count <> 0) and (replace_action <> "yes")
                then
                    asking := 0;
                    message ("Replacing "+ replace_action +" occurrences...");
                    set (screen_update, off);
                endif;
                exitif (replace_count <> 0) and (occurrences = replace_count);
                exitif 1;
            else
                if (replace_action = substr ("no", 1, action_length)) or
                    (replace_action = substr ("quit", 1, action_length))
                then
                    highlight_range := 0;
                    if current_direction = forward
                    then
                        position (end_of (replace_range));
                        move_horizontal (1);
                    endif;
                    update (current_window);
                    exitif 1;
                endif;
            endif;
        endloop;

        exitif (replace_count <> 0) and (occurrences = replace_count);

        exitif (action_length > 0) and
                ((replace_action = substr ("quit", 1, action_length)) or
                (replace_action = substr ("last", 1, action_length)));

    endloop;

    set (screen_update, on);
    message (fao ("Replaced !SL occurrence!%S", occurrences));
    set (this_mode, this_buffer);

endprocedure;

!*---------------------------------------------------------------------------*!
!
procedure eve_faa
!
! This search procedure will allow the user to find unprintable ASCII
! characters. The case-sensitivity of the search is same as for the find
! command.
!
local   number,
        target,                 ! Target to be replaced.
        replace_range,          ! Range of current occurrence
        this_buffer,            ! Current buffer
        this_mode,              ! Keyword for current mode
        highlight_range;        ! Reverse-video version of replace_range

    this_buffer := current_buffer;
    this_mode := get_info (current_buffer, "mode");
    set (insert, this_buffer);

    if not (eve$prompt_number ("", number,
                               "Number: ", "No number given"))
    then
      return;
    endif;

    if current_direction = reverse
    then
      move_horizontal (-1);
    else
      move_horizontal (1);
    endif;

    target := ASCII(number);

    replace_range := eve$find (target, 1);

    if replace_range = 0
    then
      message ("No matching character found");
    else
      position(replace_range);
      message ("Matching character found");
    endif;

    set (screen_update, on);
    set (this_mode, this_buffer);

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_specins (number_parameter)

! Insert a character which cannot be inserted from the keyboard
! into the current buffer.
!
! Parameters:
!
!	number_paramter		decimal representation of the character

local	number;

    if not (eve$prompt_number (number_parameter, number,
				"Number: ", "No number given"))
    then
	return;
    endif;

    copy_text (ascii (number));

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_inspecial
!
! Insert all of the characters (00 - FF) into text file.
!
local	number;

loop
  exitif number = 256;
  copy_text (ascii (number));
  number := number + 1;
  split_line;
endloop;

endprocedure;

!*----------------------------------------------------------------------------*!

procedure eve_insertchars (numchrs)

local loop_count;

loop_count := numchrs;

loop
  exitif loop_count = 0;
  evedt_insert_text(' ');
  loop_count := loop_count - 1;
endloop;

endprocedure

