Text::Editor::Vip::Buffer::Plugins::Display - Text position to display position utilities


Text-Editor-Vip documentation Contained in the Text-Editor-Vip distribution.

Index


Code Index:

NAME

Top

Text::Editor::Vip::Buffer::Plugins::Display - Text position to display position utilities

SYNOPSIS

Top

  use Text::Editor::Vip::Buffer::Dispaly

DESCRIPTION

Top

This module let's you define a tab size and convert text and display positions.

Tab size is set to 8 by default.

FUNCTIONS

Top

SetTabSize

Sets the tab size used.

GetTabSize

Return the tab size

GetCharacterPositionInText

Given a display position, returns the the position in text

GetCharacterDisplayPosition

Given a position in the text, returns the the display position

AUTHOR

Top

	Khemir Nadim ibn Hamouda
	CPAN ID: NKH
	mailto:nadim@khemir.net
	http:// no web site

COPYRIGHT

Top


Text-Editor-Vip documentation Contained in the Text-Editor-Vip distribution.
package Text::Editor::Vip::Buffer::Plugins::Display;
use strict;
use warnings ;

BEGIN 
{
use Exporter ();

use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION     = 0.01;
@ISA         = qw (Exporter);
@EXPORT      = qw ();
@EXPORT_OK   = qw ();
%EXPORT_TAGS = ();
}

#------------------------------------------------------------------------------

sub SetTabSize
{

$_[0]->{'Text::Editor::Vip::Buffer::Plugins::Display::TAB_SIZE'} = $_[1] ;
}

#-------------------------------------------------------------------------------

sub GetTabSize
{

return ($_[0]->{'Text::Editor::Vip::Buffer::Plugins::Display::TAB_SIZE'} || 8) ;
}

#-------------------------------------------------------------------------------

sub GetCharacterPositionInText
{

my ($buffer, $line_index, $position, $line_text) = @_ ;

$line_text = $buffer->GetLineText($line_index) unless defined $line_text ;

my ($character_position, $display_position) = (0, 0) ;

my $tab_size = $buffer->GetTabSize() ;

for (split //, $line_text)
	{
	if($_ eq "\t")
		{
		$display_position += $tab_size ;
		}
	else
		{
		$display_position++ ;
		}
		
	last if $display_position > $position ;
	$character_position++ ;
	}

if($display_position < $position)
	{
	return(length($line_text) + ($position - $display_position)) ;
	}
else
	{
	return($character_position) ;
	}
}

#-------------------------------------------------------------------------------

sub GetCharacterDisplayPosition
{

my ($buffer, $line_index, $position, $line_text) = @_ ;

$line_text = $buffer->GetLineText($line_index) ;
substr($line_text, $position) = '' if $position < length($line_text) ;

my $tab_size = $buffer->GetTabSize() ;

return(($line_text =~ tr/\t/\t/ * ($tab_size - 1)) + $position) ;
}

#-------------------------------------------------------------------------------

1 ;