!++
! FILENAME: TAB_MGT.TPU
! FUNCTION: This file contains procedures for manipulating tabs within files.
! 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_set_show_tabs
!           eve_set_noshow_tabs
!           eve_eliminate_tabs
!           eve_skip_tab
!           eve_space_tab
!
!23456789A123456789B123456789C123456789D123456789E123456789F123456789G123456789H
!--
!*----------------------------------------------------------------------------*!

procedure tab_mgt_module_ident 

  local file_date,
        module_vers;

  file_date := "-<( 29-NOV-1988 17:07:40.13 )>-";
  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_set_show_tabs

! tabs are displayed as graphic characters

    set (text,current_window,graphic_tabs);

endprocedure;

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

procedure eve_set_noshow_tabs

! tabs are displayed as space characters

    set (text,current_window,blank_tabs);

endprocedure;

!*----------------------------------------------------------------------------*!
!
! This command can be invoked with a select range active or without a select
! range active. If it is invoked with a select range active it will replace all
! tabs within the select range and return the cursor to the position it was in
! when the command was invoked. If it is invoked with no select range it will
! replace all tabs between the current position and the beginning or end of the
! buffer depending on the setting of the current direction in the buffer and
! return the cursor to the position it was in when the command was invoked.
! SKS, 15-MAY-1987.
!
procedure eve_eliminate_tabs

local   curr_col,
        found,
        found_range,
        orig_pos,
        rpl_cnt,
        spc_cnt,
        srch_dir,
        start_range,
        end_range,
        the_range,
        here,
        tab_siz;

    orig_pos := mark(none);

    if (eve$x_select_position = 0)
    then
        start_range := beginning_of(current_buffer);
        end_range := end_of(current_buffer);
    else
        start_range := beginning_of(select_range);
        end_range := end_of(select_range);
        eve$x_select_position := 0;
    endif;

    the_range := create_range (start_range, end_range, none);

    position(start_range);

    loop
        found_range := search_quietly(ascii(9),forward,exact,the_range);

        if found_range = 0 then
          found := false;
        else
          found := true;   
        endif;

        exitif not found;
        here := mark(none);
        exitif not get_info(here, "within_range", the_range);
        position(beginning_of(found_range));
        curr_col := get_info(current_buffer,"offset_column");
        tab_siz := (((curr_col-1)/8+1)*8+1)-curr_col;
        erase(found_range);
        evedt_insert_text(substr('        ',1,tab_siz));
        rpl_cnt := rpl_cnt + 1;
        spc_cnt := spc_cnt + tab_siz;
    endloop;

    if rpl_cnt = 1 then
      message (str(rpl_cnt) +" tab replaced with "+ str(spc_cnt) +" spaces");
    else
      message (str(rpl_cnt) +" tabs replaced with "+ str(spc_cnt) +" spaces");
    endif;

    position(orig_pos);

endprocedure;

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

! Move the cursor TAB number of spaces. Do not insert a tab or spaces.

procedure eve_skip_tab

local curr_col,
      tab_wid,
      tab_siz;

  tab_wid := get_info(current_buffer,"tab_stops");
  curr_col := get_info(current_buffer,"offset_column");
  tab_siz := (((curr_col-1)/tab_wid+1)*tab_wid+1)-curr_col;
  cursor_horizontal (tab_siz);

endprocedure;

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

procedure eve_space_tab

local curr_col,
      tab_wid,
      tab_siz;

  tab_wid := get_info(current_buffer,"tab_stops");
  curr_col := get_info(current_buffer,"offset_column");
  tab_siz := (((curr_col-1)/tab_wid+1)*tab_wid+1)-curr_col;
  eve_insertchars (tab_siz);

endprocedure;

