Commits

Herbert Breunung committed 5d63aec

rename docs API to doc

Comments (0)

Files changed (10)

 
 package Kephra;
 our $NAME     = __PACKAGE__;    # name of entire application
-our $VERSION  = '0.4.5.12';
+our $VERSION  = '0.4.5.13';
 
 our @external_dependencies = (
 	qw/Benchmark Encode Encode::Guess Cwd/,

lib/Kephra/API.pm

 use warnings;
 #use Carp;
 use Kephra::API::Command;
-use Kephra::API::Docs;
+use Kephra::API::Doc;
 use Kephra::API::Event;
 use Kephra::API::KeyMap;
 #use Kephra::API::Macro;
 
 sub app             { $Kephra::App::_ref }
 sub main_window     { $Kephra::App::Window::_ref }
-sub editor          { Kephra::API::Docs::active_editor() }
+sub editor          { Kephra::API::Doc::active_editor() }
 sub docbar          { Kephra::App::Part::Editor::active_docbar() }
-sub document        { Kephra::API::Docs::active_doc() }
-sub all_documents   { Kephra::API::Docs::all() }
+sub document        { Kephra::API::Doc::active() }
+sub all_documents   { Kephra::API::Doc::all() }
 sub do_with_all_documents { }
 
 sub focus           { Kephra::App::Focus::set(@_) }

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;

lib/Kephra/API/Docs.pm

-use strict;
-use warnings;
-
-use Kephra::API;
-use Kephra::Document;
-
-package Kephra::API::Docs;
-
-my %document;
-my $active;
-my $previous;
-
-sub previous_doc  { $previous }
-sub active_doc    { $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;

lib/Kephra/App/Bar/Document.pm

 	Wx::Event::EVT_AUINOTEBOOK_PAGE_CHANGED( $self, -1, sub {
 		my ($nb, $event ) = @_;
 		my $new_page = $nb->GetPage( $event->GetSelection );
-		Kephra::API::Docs::set_active_doc( $new_page );
+		Kephra::API::Doc::set_active_doc( $new_page );
 		Kephra::API::main_window()->refresh_title();
 		Wx::Window::SetFocus( $new_page );
 		$event->Skip;

lib/Kephra/App/Editor.pm

-use strict;
+use v5.10;
 use warnings;
 
 use Wx;
 			my $cmd = Kephra::API::KeyMap::cmd_from_keycode($key);
 			Kephra::API::log("run command: $cmd");
 			Kephra::API::Command::run( $cmd );
+			#$ed->GetStyleAt($ed->GetCurrentPos);
 		}
 		else {$event->Skip}
 	});

lib/Kephra/App/Part/Editor.pm

 		$doc->panel( Kephra::App::Panel->new( $main_doc_bar ) );
 		$doc->editor( Kephra::App::Editor->new( $doc->panel ) );
 		$doc->panel->append( \$doc->editor );
-		Kephra::API::Docs::add_and_activate( $doc );
+		Kephra::API::Doc::add_and_activate( $doc );
 		$main_doc_bar->add_page($doc->panel, $doc->file_name, -1, 1);
 		Kephra::API::focus( $doc->editor );
 	}
 	my $main_doc_bar = active_docbar();
 	if ($main_doc_bar->GetPageCount > 1) {
 		$main_doc_bar->remove_page( $main_doc_bar->GetPageIndex( $doc->panel ) );
-		Kephra::API::Docs::del($doc);
+		Kephra::API::Doc::del($doc);
 	} else {
 		$doc->editor->ClearAll;
 		$doc->assign_file_path('');
 }
 
 sub raise_document {
-	my $doc = Kephra::API::Docs::doc_by_anything(shift);
+	my $doc = Kephra::API::Doc::doc_by_anything(shift);
 	active_docbar()->raise_page( $doc->{panel} ) if $doc;
 }
 

lib/Kephra/App/Window.pm

 sub set_title { $_ref->SetTitle( $_[1] ) }
 sub refresh_title {
 	my $self = shift;
-	my $doc = Kephra::API::Docs::active_doc();
+	my $doc = Kephra::API::Doc::active();
 	my $title = (ref $doc and $doc->file_name)
 		? $doc->file_name.' ('.$doc->file_path.')'
 		: Kephra::App::Part::Editor::active_title();

lib/Kephra/File.pm

 
 		# raise tab if doc is alread open
 		Kephra::App::Part::Editor::raise_document($file), next
-		  if Kephra::API::Docs::file_loaded($file);
+		  if Kephra::API::Doc::file_loaded($file);
 
 		my $doc = Kephra::App::Part::Editor::create_document($file);
 		next unless ref $doc eq 'Kephra::Document';

lib/Kephra/File/Local.pm

 use v5.10;
 use strict;
 use warnings;
+use Encode;
+use Encode::Guess;
 use Kephra::API;
 use Kephra::File;
 
 	my $file = Kephra::File::normalize_path( shift ) || $doc->file_path;
 	return Kephra::Log::warning("can't load nonexising file") unless $file and -e $file;
 	return Kephra::Log::warning("can't read $file") unless -r $file;
-	my $ed = $doc->editor;
+	my $ed = $doc->editor->{ Kephra::API::docbar() };
 	CORE::open my $FH, '<', $file;
 	binmode($FH);
 	my $content = do { local $/; <$FH> };
 	if ($content) {
+
+		my @guesses = qw/utf-8 iso8859-1 latin1/;
+		my $guess = Encode::Guess::guess_encoding( $content, @guesses );
+		if ( ref($guess) and ref($guess) =~ m/^Encode::/ ) {
+			$doc->encoding( $guess->name );
+		} elsif ( $guess =~ m/utf8/ ) {
+			$doc->encoding( 'utf-8' );
+		} elsif ( $guess =~ m/or/ ) {
+			my @suggest_encodings = split /\sor\s/, "$guess";
+			$doc->encoding( $suggest_encodings[0] );
+		} else { $doc->encoding( 'utf-8' ) }
+		$content = Encode::decode( $doc->encoding,  $content );
+		#say $doc->encoding; 
+
 		$ed->SetText( $content );
 		$ed->SetSavePoint;
 	}
 	my $file = Kephra::File::normalize_path( shift ) || $doc->file_path;
 	return Kephra::Log::warning("need a file path") unless $file;
 	return Kephra::Log::warning("can't overwrite $file") if -e $file and not -w $file;
-	$doc->file_path($file) unless $doc->file_path eq $file;
-	my $ed = $doc->editor;
-	CORE::open my $FH, '>', $file;
+	$doc->file_path($file) unless defined $doc->file_path and $doc->file_path eq $file;
+	my $ed = $doc->editor->{ Kephra::API::docbar() };
+	CORE::open my $FH, '> :raw :encoding('.$doc->encoding.')', $file;
 	print $FH $ed->GetText;
 	$ed->SetSavePoint;
 }