Source

Kephra / lib / Kephra / API / Docs.pm

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

package Kephra::API::Docs;
my %document;                           # keys of subhashes: ID editor panel file
my $active;
my $lowest_free_ID = 1;
use constant DOC_CLASS => 'Kephra::Document';
use Scalar::Util qw(looks_like_number);


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



sub add {
	my $doc = shift;
	Kephra::Log::warning('need a ' . DOC_CLASS . " thats in stash, not $doc!", 1)
		unless ref $doc and $doc->isa( DOC_CLASS );
	$document{'ID'}  {$lowest_free_ID} = $doc;
	$document{'panel'}{$_} = $doc for values $doc->panel;
	$document{'editor'}{$_} = $doc for values $doc->editor;
	$document{'file'}{$doc->file_path} = $doc if $doc->file_path;
	$lowest_free_ID++;
}

sub rename_file {
	my $doc = find( shift );
	Kephra::Log::warning('need a ' . DOC_CLASS . " thats in stash, not $doc!", 1)
		unless ref $doc and $doc->isa( DOC_CLASS );
	for (keys %{$document{'file'}} ){
		delete $document{'file'}{$_} if $document{'file'}{$_} eq $doc;
	}
	$document{'file'}{$doc->file_path} = $doc if $doc->file_path;
}

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 ref $doc and $doc->isa( DOC_CLASS );
	delete $document{'ID'}{$doc->ID};
#	delete $document{'panel'}{$_}    for values $doc->panel;
#	delete $document{'editor'}{$_}   for values $doc->editor;
	delete $document{'file'}{$doc->file_path} if $doc->file_path;
#say "removed $doc ", scalar keys %{$document{'ID'}}, ' ',scalar keys %{$document{'file'}}; 
}

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 dump_all_ed { say $_ for keys %{$document{'editor'}} }


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.