Source

Kephra / lib / Kephra / Document.pm

Full commit
use v5.10;
use File::Spec;
use Kephra::API;
use Kephra::File;

package Kephra::Document;
use Moo; 
my $max_title_width = 15;

# export SUB_QUOTE_DEBUG=1


has ID     => ( is  => 'rwp' );                             # API::Docs ID
has title  => ( is  => 'rwp' );                             # shown as tab label
has editor => ( is  => 'ro' );
has panel  => ( is  => 'ro' );
has anon_NR=> ( is  => 'rw' );
has file_path=>( 
	is      => 'rw',
	coerce  =>  sub { Kephra::File::normalize_path($_[0]) },
	trigger =>  sub {
		my $self = shift;
		return if $self->old_file_path and $self->old_file_path eq $self->file_path;
		my @path_parts = File::Spec->splitpath( $self->file_path );
		$self->_set_file_dir( $path_parts[1] );
		$self->_set_file_name( $path_parts[2] );
		Kephra::API::Docs::rename_file($self) if $self->ID;

		if ($self->file_path) {
			my $title = $path_parts[2];
			$title = substr( $title, 0, $max_title_width - 2 ) . '..'
				if length($title) > $max_title_width and $max_title_width > 7;
			$self->_set_title($title);
		}

		$self->_set_old_file_path( $self->file_path );
	});
has file_dir     => ( is => 'rwp', );
has file_name    => ( is => 'rwp', );
has old_file_path=> ( is => 'rwp' );
has content      => ( is => 'rw', reader=> sub{ $_[0] } );
has notes        => ( is => 'rw', );

has syntax_mode  => ( is => 'rw', default => sub { 'perl' } );
has readonly     => ( is => 'rw', );
has active_config=> ( is => 'rw', );
has caret_pos    => ( is => 'rw', );
has edit_pos     => ( is => 'rw', );
has encoding     => ( is => 'rw', );
has EOL          => ( is => 'rw', );
has tab_size     => ( is => 'rw', );
has tab_use      => ( is => 'rw', );
has folded_lines => ( is => 'rw', );
has marked_lines => ( is => 'rw', );

sub BUILDARGS {
	my ( $self, @args ) = @_;
	unshift @args, "file_path" if @args % 2 == 1;
	my %args = @args;

	my (%panel, %editor);
	for my $docbar (Kephra::API::all_docbars()){
		$panel{$docbar} = Kephra::App::Panel->new( $docbar );
		$editor{$docbar} = Kephra::App::Editor->new( $panel{$docbar} );
		$panel{$docbar}->append( \$editor{$docbar} );
	}

	$args{'panel'} = \%panel;
	$args{'editor'} = \%editor;
	$args{'encoding'} = 'utf-8';

	return \ %args;
};

sub BUILD    {
	my $self = shift;
	$self->_set_ID( Kephra::API::Docs::add($self) );
	$self->_set_title('<untitled '.$self->anon_NR.'>') unless $self->file_path;
}
sub DEMOLISH { Kephra::API::Docs::remove($_[0]); #say "demolish $_[0]"; 
}

sub raise { Kephra::API::focus( shift->panel ) }
sub insert_before_caret {}
sub insert_after_caret {}

1;