Kephra / lib / Kephra / API / Doc.pm

use strict;
use warnings;

use Kephra::API;
use Kephra::Document;

package Kephra::API::Doc;

my %document;
my $active;
my $previous;

sub previous_doc  { $previous }
sub active    { $active }
sub active_editor { $active->editor }
sub set_active_doc{
	my $any = shift;
	Kephra::Log::warning('need a parameter!') unless $any;
	my $new_doc = doc_by_anything( $any ) ;
	return unless defined $new_doc;
	_activate($new_doc);
	return 1;
}
sub _activate {
	$previous = $active;
	$active = shift;
}
sub add_and_activate {
	my $doc = shift;
	add($doc);
	_activate($doc);
}
sub add {            # silently
	my $doc = shift;
	Kephra::Log::warning("add to me only docs, not $doc!") unless $doc->isa('Kephra::Document');
	$document{$doc->editor} = $doc;
}
sub del {
	my $doc = shift;
	Kephra::Log::warning("add to me only docs, not $doc!") unless $doc->isa('Kephra::Document');
	Kephra::Log::warning("$doc was not in the stash!") unless $document{$doc->editor};
	delete $document{$doc->editor};
}


sub doc_by_anything {
	my $any = shift;
	Kephra::Log::warning("need a parameter!") unless $any;
	return     ref($any) eq ''                  ? doc_by_path ($any)
			 : $any->isa('Wx::Panel')           ? doc_by_panel($any)
			 : $any->isa('Kephra::App::Editor') ? doc_by_ed   ($any)
			 : $any->isa('Kephra::Document')    ? $document{$any->editor} 
			 :                                    undef;
}
sub doc_by_ed {
	my $ed = shift;
	Kephra::Log::warning("needs a Kephra::App::Editor, not $ed!")
		unless $ed->isa('Kephra::App::Editor');
	return $document{$ed};
}
sub doc_by_panel {
	my $panel = shift;
	Kephra::Log::warning("needs a Wx::Panel, not $panel!") unless $panel->isa('Wx::Panel');
	for my $ed (keys %document) {
		return $document{$ed} if $document{$ed}->panel and $document{$ed}->panel eq $panel;
	}
}
sub doc_by_path {
	my $file_path = shift;
	Kephra::Log::warning("needs a file path as input!") unless $file_path and -e $file_path;
	for my $ed (keys %document) {
		return $document{$ed} if $document{$ed}->file_path eq $file_path;
	}
}
sub all { values %document }

sub dump_all_ed { print "$_ + \n" for keys %document }

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 $ed (keys %document) {
		return 1 if $document{$ed}->file_path and $document{$ed}->file_path eq $file_path;
	}
}
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.