Source

Kephra / lib / Kephra / API / Docs.pm

Full commit
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;
my $lowest_free_anon_NR = 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 is_doc { return 1 if ref $_[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($doc);
	$document{'ID'}  {$lowest_free_ID} = $doc;
	$document{'panel'}{$_} = $doc for values $doc->panel;
	$document{'editor'}{$_} = $doc for values $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($doc);
	my $file = $doc->old_file_path;
	delete $document{'file'}{$file} if $file and $document{'file'}{$file};
	if ($doc->file_path){
		$document{'file'}{$doc->file_path} = $doc;
		unless ($doc->old_file_path) {
			delete $document{'anon'}{$doc->anon_NR};
			$doc->anon_NR(0);
		}
	} else {
		if ($doc->old_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($doc);
	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 all_editors { keys %{$document{'editor'}} }
sub do_with_all_editors {
}

sub add_bar{}
sub remove_bar{}
1;