Kephra / lib / Kephra / API / Doc.pm

use v5.10;
use strict;
use warnings;
use Wx;
use Kephra::App::Editor;
use Kephra::App::Panel;
use Kephra::Document;

package Kephra::API::Doc;
my %document = ('ID'=>{});             # keys of subhashes: ID editor panel file
my $active;
my $lowest_free_ID = 1;
my $lowest_free_anon_NR = 1;

use constant DOC_CLASS => 'Kephra::Document';
use Scalar::Util qw(blessed looks_like_number);


sub active        { $active || '' }
sub active_editor { $active->editor->{Kephra::API::docbar()} if $active }
sub active_panel  { $active->panel->{Kephra::API::docbar()} if $active }
sub set_active {
	my $doc = find( shift );
	$active = $doc if $doc;
}
sub previous      { App::Kephra::Focus::last( DOC_CLASS ) }
sub all           { values %{$document{'ID'}} }

sub is { return 1 if blessed($_[0]) and $_[0]->isa( DOC_CLASS ); return 0 }


sub add {
	my $doc = shift;
	Kephra::Log::warning('need a ' . DOC_CLASS . " thats in stash, not $doc!", 1)
		unless is($doc);
	$document{'ID'}  {$lowest_free_ID} = $doc;
	add_instance($doc, $_) for keys $doc->editor;
	if ($doc->file_path) {
		$document{'file'}{$doc->file_path} = $doc;
	} else {
		$doc->anon_NR( $lowest_free_anon_NR++ );
		$document{'anon'}{$doc->anon_NR} = $doc;
	}
	$lowest_free_ID++;
}


sub rename_file {
	my $doc = find( shift );
	Kephra::Log::warning('need a ' . DOC_CLASS . " thats in stash, not $doc!", 1)
		unless is($doc);
	my $file = $doc->prev_file_path;
	delete $document{'file'}{$file} if $file and $document{'file'}{$file};
	if ($doc->file_path){
		$document{'file'}{$doc->file_path} = $doc;
		unless ($doc->prev_file_path) {
			delete $document{'anon'}{$doc->anon_NR};
			$doc->anon_NR(0);
		}
	} else {
		if ($doc->prev_file_path) {
			$doc->anon_NR( $lowest_free_anon_NR++ );
			$document{'anon'}{$doc->anon_NR} = $doc;
		}
	}
}


sub remove {
	my $doc = shift;
#say "remove $doc ", scalar keys %{$document{'ID'}}, ' ',scalar keys %{$document{'file'}}; 
	Kephra::Log::warning('need a ' . DOC_CLASS . ' thats in stash, not $doc!', 1)
		unless is($doc);
	delete $document{'ID'}{$doc->ID} if $doc->ID;
	#remove_instance($doc, $_) for values $doc->panel;
	delete $document{'file'}{$doc->file_path} if $doc->file_path;
#say "removed $doc ", scalar keys %{$document{'ID'}}, ' ',scalar keys %{$document{'file'}}; 
}


sub add_instance {
	my ($doc, $bar) = @_;
	return Kephra::Log::warning('need as first parameter a'.DOC_CLASS.' thats in stash, not $doc!', 1)
		unless is($doc);
	#return Kephra::Log::warning("need a Kephra::App::Bar::Document instance as second parameter, not $bar")
	#	unless blessed($bar) eq 'Kephra::App::Bar::Document';
	$document{'panel'} { $doc->panel->{$bar}  } = $doc;
	$document{'editor'}{ $doc->editor->{$bar} } = $doc;
}


sub remove_instance {
	my ($doc, $bar) = @_;
	return Kephra::Log::warning('need as first parameter a'.DOC_CLASS.' thats in stash, not $doc!', 1)
		unless is($doc);
	return Kephra::Log::warning("need a Kephra::App::Bar::Document instance as second parameter, not $bar")
		unless blessed($bar) eq 'Kephra::App::Bar::Document';
	delete $document{'panel'}{ $doc->panel->{$bar} };
	delete $document{'editor'}{ $doc->editor->{$bar} };
}


sub find {
	my $any = shift;
	Kephra::Log::warning("need a parameter!", 1) unless defined $any and $any;
	return     looks_like_number($any)                                     ? $document{'ID'}    {$any}
	         : ref($any) eq ''                                             ? $document{'file'}  {$any}
	         : $any->isa('Kephra::App::Panel')                             ? $document{'panel'} {$any}
	         : $any->isa('Kephra::App::Editor')                            ? $document{'editor'}{$any}
	         :($any->isa(DOC_CLASS) and $document{'ID'}{$any->ID} eq $any) ? $any
	         :                                                               undef;
}


sub file_loaded {
	my $file_path = shift;
	#Kephra::Log::warning("needs a file path as input!") unless $file_path and -e $file_path;
	for my $doc ( all() ) {
		return 1 if $doc->file_path and $doc->file_path eq $file_path;
	}
}


sub all_editors { keys %{$document{'editor'}} }
sub do_with_all_editors {
}

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.