procedure convert_esc_to_text
    local
        found_esc;
        position (beginning_of (current_buffer));   ! Position cursor at top
    loop
        found_esc := search (ascii(27), forward);   ! Look for escape char
        exitif found_esc = 0;                       ! Exit loop if none
        position(found_esc);                        ! Position cursor at escape
        erase(found_esc);                           ! Delete the escape
        copy_text('<ESC>');                         ! Insert text as substitute
        move_horizontal(-1);                        ! Back up 1 char before
    endloop;        ! searching again
endprocedure;

procedure unconvert_esc_to_text
    local
    found_esc;
    position (beginning_of (current_buffer));    ! Position cursor at top
    loop
        found_esc := search ('<ESC>', forward);  ! Look for escape char
        exitif found_esc = 0;                    ! Exit loop if none
        position(found_esc);                     ! Position cursor at escape
        erase(found_esc);                        ! Delete the escape
        copy_text(ascii(27));                    ! Insert text as substitute
        move_horizontal(-1);                     ! Back up 1 char before
    endloop;        ! searching again
endprocedure;
! The following are examples of ways to display control characters in a
! meaningful way by translating the buffer to a different visual format
! and mapping this new form to a window.

! You will want to perform the initialization once during your editing
! session if you plan to use the translation feature.  Things which could
! be added to this are key mappings for initialization, for prompting for
! the buffer to be translated, and for unmapping the translate_window
! when you're done.
!
PROCEDURE init_translate
    translate_buffer := CREATE_BUFFER ('translation');
    SET (NO_WRITE, translate_buffer);
    translate_window := CREATE_WINDOW (1, 10, on);
    control_char_pat := ANY (' ');
ENDPROCEDURE

! This procedure performs the substitution of "meaningful characters"
! for the single character.
!
PROCEDURE translate_controls (char)
! Case from NUL to US

! 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 view_controls (buf_name)
LOCAL
    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 (BEGINNING_OF (translate_buffer));
   MAP (translate_window, translate_buffer);
   RETURN;
ENDON_ERROR;

POSITION (translate_buffer);
ERASE (translate_buffer);
COPY_TEXT (buf_name);    ! 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
    translate_controls (char_to_translate);    ! Substitute the new text
ENDLOOP;

ENDPROCEDURE
