Kephra / lib / Kephra / Document.pm

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  => 'rwp', default => sub { {} } );
has panel    => ( is  => 'rwp', default => sub { {} } );
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->prev_file_path and $self->file_path
				and $self->prev_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_prev_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 prev_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 = @args unless scalar @args % 2;
	$args{'encoding'} = 'utf-8';

	return \%args;
}
sub BUILD    {
	my $self = shift;
	$self->add_instance($_) for Kephra::API::all_docbars();
	$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]) }


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;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.