Source

Kephra / lib / Kephra / Document.pm

Full commit
Herbert Breunung 36adcac 

Herbert Breunung a9abbed 
Herbert Breunung 340531b 
Herbert Breunung b4fa086 
Herbert Breunung 36adcac 

Herbert Breunung 7315592 

Herbert Breunung b9cfdfa 

Herbert Breunung 36adcac 
Herbert Breunung fe78f9b 
Herbert Breunung e5bcfb3 
Herbert Breunung 7315592 
Herbert Breunung b9cfdfa 
Herbert Breunung 36adcac 

Herbert Breunung b9cfdfa 
Herbert Breunung 36adcac 



Herbert Breunung b9cfdfa 
Herbert Breunung 29977f3 

Herbert Breunung 7315592 
Herbert Breunung b9cfdfa 

Herbert Breunung 29977f3 



Herbert Breunung b9cfdfa 



Herbert Breunung 29977f3 

Herbert Breunung b9cfdfa 
Herbert Breunung ac839f5 
Herbert Breunung b9cfdfa 
Herbert Breunung ac839f5 

Herbert Breunung 36adcac 




Herbert Breunung b9cfdfa 
Herbert Breunung 36adcac 

















Herbert Breunung 0785c3f 
Herbert Breunung 8d0a2ed 
Herbert Breunung 0785c3f 


Herbert Breunung 8d0a2ed 

Herbert Breunung 0785c3f 

Herbert Breunung 36adcac 




Herbert Breunung b9cfdfa 

Herbert Breunung 7315592 
Herbert Breunung b9cfdfa 

Herbert Breunung 7315592 
Herbert Breunung 8d0a2ed 
Herbert Breunung 36adcac 
Herbert Breunung 7315592 






















Herbert Breunung 36adcac 
Herbert Breunung 3dbe587 


Herbert Breunung e5bcfb3 
use v5.10;
use File::Spec;
use Kephra::API;
use Kephra::File;

package Kephra::Document;
use Moo; 
use Scalar::Util qw(blessed);

my $max_title_width = 15;

# export SUB_QUOTE_DEBUG=1


has ID     => ( is  => 'rwp' );                             # API::Doc 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->file_path
				and $self->old_file_path eq $self->file_path;
		Kephra::API::Doc::rename_file($self) if $self->ID;

		if ($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] );

			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);
		} else {
			$self->_set_title('<untitled '.$self->anon_NR.'>');
		}

		$self->_set_old_file_path( $self->file_path );
		$_->set_page_title( $self->title, $self->panel->{$_} )
			for Kephra::API::all_docbars();
	});
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::Doc::add($self) );
	$self->_set_title('<untitled '.$self->anon_NR.'>') unless $self->file_path;
}
sub DEMOLISH { Kephra::API::Doc::remove($_[0]); #say "demolish $_[0]"; 
}

sub add_instance {
	my ($self, $bar) = @_;
	return Kephra::Log::warning("need an existing Kephra::App::Bar::Document instance")
		unless blessed($bar) eq 'Kephra::App::Bar::Document';

	my @editor = values $self->editor;
	my $panel = $self->panel->{$bar} = Kephra::App::Panel->new( $bar );
	my $ed = $self->editor->{$bar} = Kephra::App::Editor->new( $panel );
	$panel->append( \$ed );
	$ed->SetDocPointer( $editor[0]->GetDocPointer() ) if @editor;
	Kephra::API::Doc::add_instance($self, $bar);
}

sub del_instance {
	my ($self, $bar) = @_;
	return Kephra::Log::warning("need an existing Kephra::App::Bar::Document instance")
		unless blessed($bar) eq 'Kephra::App::Bar::Document'
		and exists $self->panel->{$bar};
	Kephra::API::Doc::remove_instance($self, $bar);
	$bar->remove_page( $self->panel->{$bar} );
	delete $self->panel->{$bar};
	delete $self->editor->{$bar};
}
sub raise { Kephra::API::focus( shift->panel ) }
sub insert_before_caret {}
sub insert_after_caret {}

1;