Commits

Herbert Breunung committed 2ec85a6

command can now be registered any time

Comments (0)

Files changed (7)

 our @external_dependencies = (
 	qw/Benchmark Encode Encode::Guess Cwd/,
 	qw/File::Find File::Spec File::UserConfig Config::General YAML/,
-	qw/PPI Moo/,
+	qw/Moo/, #PPI 
 	qw/Wx Wx::STC Wx::Perl::ProcessStream/, #  Wx::Scintilla
 );
 our @used_wx_modules = qw/Wx::AUI Wx::DND Wx::RichText/; # Wx::STC Wx::HTML

lib/Kephra/API.pm

 #use Kephra::API::Sandrum;
 use Kephra::App::Focus;
 use Kephra::App::Window;
+use Kephra::File;
 use Kephra::Config;
-use Kephra::File;
 use Kephra::Help;
 use Kephra::Log;
 #use Kephra::Worker;

lib/Kephra/API/Command.pm

 use strict;
 use warnings;
 use Kephra::API;
+use Kephra::Log;
+use Kephra::API::KeyMap;
 
 package Kephra::API::Command;
 
 my %list;     # by ID
-my %keyproxy; # by keycode
-my %namespace;
+#my %keyproxy; # by keycode
+my %namespace;# module => cmd prefix 
 my %switch   = ( 'name' => {}, 'package' => {} ); #switchvar vor cmd definitions
+my $done_init = 0;
 
 sub _raw_list { \%list }
 
 		no strict 'refs';
 		#return Kephra::Log::error #("$- already registered as ".$switch{'name'}{$_}) if exists $switch{'name'}{$_};
 		for my $name (keys %$var){
-			$var->{$name} = $calling_module.'::'.$var->{$name} if index($var->{$name},'::') == -1;
+			$var->{$name} = $calling_module.'::'.$var->{$name}
+				if index($var->{$name},'::') == -1 and $calling_module;
 			$switch{'name'}{$name} = $var->{$name}
 				if $var->{$name} and defined &{ $var->{$name} };
 		}
 
 sub register {
 	my $cmd = shift;
-	return Kephra::Log::error( '"$cmd" has to be a hash ref') unless ref $cmd eq ref {};
+	return Kephra::Log::error( '"$cmd" has to be a hash ref', 1) unless ref $cmd eq 'HASH';
 	my $calling_module = (caller)[0];
 	my $cmd_namespace = $namespace{ $calling_module }; 
 	for my $ID (keys %$cmd) {
 		#for (qw/sub options state event label help keys icon/){} filter input later
 		$list{$fullID}           = $cmd->{$ID};
 		$list{$fullID}{'source'} = $calling_module;
+		compile($fullID) if $done_init;
 	}
 }
 
 
 sub init {
-	for my $cmd (keys %list) {
+	compile( keys %list );
+	$done_init = 1;
+}
+
+
+sub compile {
+	for my $cmd (@_) {
 		my $data = $list{$cmd};
 		my $call = $data->{'sub'};
-		Kephra::Log::warning( "$cmd lacks sub value"), next unless $call;
+		Kephra::Log::warning( "$cmd lacks value on hashkey 'sub'"), next unless $call;
 		# insert start (prefix) of the call, module name mostly
 		if (substr($call, 0, 1) eq '$'){
 			if (substr($call, 1, 1) eq '_'){
 				Kephra::Log::warning
 					( "$cmd lacks switch declared by ".$data->{'source'}), next
 						unless $switch{'package'}{ $data->{'source'} };
-				$call = $switch{'package'}{ $data->{'source'} }.'()'.substr($call, 2);
+				$call = $switch{'package'}{ $data->{'source'}}.'()'.substr($call, 2);
 			}
 			else {
 				my $vl = index($call,'->');
-				$call = $switch{'name'}{ substr($call, 0, $vl) }.'()'.
-						substr($call, $vl) ;
+				my $switchvar = substr($call, 0, $vl);
+				if (exists $switch{'name'}{$switchvar}){
+					$call = $switch{'name'}{$switchvar}.'()'.substr($call, $vl)
+				} else {
+					Kephra::Log::error( "used unknown switchvar $switchvar", 1)
+				}
 			}
 		}
-		else {
-			$call = $data->{'source'}.'::'.$call if index($call,'::') == -1;
-		}
+		else { $call = $data->{'source'}.'::'.$call if index($call,'::') == -1 }
 		# adding tail (suffix) of the call, signature part
 		$call .= exists $data->{'parameter'} # just one parameter yet
 			? '(\''.$data->{'parameter'}.'\')'
 	}
 }
 
-
-sub compile {}
-
 sub run {
 	my $cmd = shift;
 	$cmd = [$cmd] unless ref $cmd eq ref [];

lib/Kephra/API/KeyMap.pm

 
 sub register_keys {
 	my ($keys, $map) = @_;
-	return Kephra::Log::warning ("got no hashref") unless ref $keys eq ref {};
+	return Kephra::Log::warning("got no hashref") unless ref $keys eq ref {};
 	$map = $main_map unless defined $map;
 	for my $cmd (keys %$keys) {
 		$definition{$map}{ $cmd } = $keys->{$cmd};
 
 sub code_registered {
 	my ($code, $map) = @_;
-	Kephra::Log::warning ('got no key code') unless $code;
+	Kephra::Log::warning('got no key code') unless $code;
 	$map = $main_map unless defined $map;
 	return 1 if defined $code{$map}{$code} and $code{$map}{$code}
 }
 
 sub cmd_from_code {
 	my ($code, $map) = @_;
-	Kephra::Log::warning ('got no key code') unless $code;
+	Kephra::Log::warning('got no key code') unless $code;
 	$map = $main_map unless defined $map;
 	$code{$map}{$code};
 }
 sub code_from_definition {
 	my $def = shift;
 	$def =~ tr/ 	//d;
-	Kephra::Log::warning ('got no key definition') unless $def;
+	Kephra::Log::warning('got no key definition') unless $def;
 
 	my @key = split '\+', $def;           # only + can combine key in definition
 	my $code = length($key[-1]) == 1 ? ord uc $key[-1] : $key_value{ $key[-1] };
-	Kephra::Log::warning ('don\'t know this key '.$key[-1]) unless $code;
+	Kephra::Log::warning('don\'t know this key '.$key[-1]) unless $code;
 
 	$code += $mod_key_value{ shift(@key) } while @key > 1;
-	Kephra::Log::warning ("got unknown key definition $def") unless $code;
+	Kephra::Log::warning("got unknown key definition $def") unless $code;
 	return $code;
 }
 
 sub code_from_event {
 	my $event = shift;
-	Kephra::Log::warning ("got no event, but $event")unless $event and $event->isa('Wx::Event');
+	Kephra::Log::warning ("got no event, but $event") unless ref $event and $event->isa('Wx::Event');
 	my $code = $event->GetKeyCode;
 	$code += $mod_key_value{'shift'} if $event->ShiftDown;
 	$code += $mod_key_value{'ctrl'} if $event->ControlDown;
 
 sub react_on_event {
 	my ($event, $map) = @_;
-	Kephra::Log::warning ("got no event, but $event") unless $event and $event->isa('Wx::Event');
+	Kephra::Log::warning ("got no event, but $event") unless ref $event and $event->isa('Wx::Event');
 	$map = $main_map unless defined $map;
 	my $key = code_from_event($event);
 	Kephra::API::log("pressed key $key inside the ".(caller)[0]);

lib/Kephra/App/Bar/Document.pm

 
 sub refresh_page_title {
 	my $self = shift;
-print "$_[1]--------\n";
 	my $doc = shift // Kephra::API::document();
-print Kephra::API::document(), "$doc--------\n";
 	my $unsaved_mark = '*';
 	my $position = $self->GetSelection;
 	my $label = $self->GetPageText( $position);

lib/Kephra/File.pm

 	for my $doc (@_) {
 		next unless ref $doc eq 'Kephra::Document';
 		if ($doc->editor->GetModify) {
-			my $save_answer = Kephra::App::Dialog::get_confirm_3 (
-				'close unsaved file', 'do you want to save it before?'
+			my $save_answer = Kephra::App::Dialog::yes_no_cancel(
+				'do you want to save it before?', 'close unsaved file'
 			);
 			next if $save_answer == &Wx::wxCANCEL;
 			save($doc) if $save_answer == &Wx::wxYES;

lib/Kephra/Log.pm

 use v5.10;
 use strict;
 use warnings;
-use Kephra::API;
 
 package Kephra::Log;
 
+use Kephra::API;
+
 our $level;
 our @output;
 our $test_mode = 0;