Commits

Herbert Breunung  committed 088ca5f

simple edit almost done

  • Participants
  • Parent commits 5eb5e00
  • Branches sp2

Comments (0)

Files changed (4)

File lib/Kephra/App/Editor.pm

 	$font = {
 		#family => 'Courier New', # old default
 		family => 'DejaVu Sans Mono',
-		size => 10,
+		size => 13,
 		style => 'normal',
 		weight => 'normal',    
 	} unless defined $font;

File lib/Kephra/IO/.DS_Store

Binary file added.

File lib/Kephra/IO/LocalFile.pm

+use v5.12;
+use warnings;
+use Encode;
+use Encode::Guess;
+
+package Kephra::IO::LocalFile;
+
+
+sub read {
+	my $doc = shift;
+	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->{ 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;
+	}
+}
+
+sub write {
+	my $doc = shift;
+	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 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;
+}
+
+
+1;
 package SimpleEdit;
 use Wx qw/ :everything /;
 use base qw(Wx::App);
+use Encode::Guess;
 use Kephra::App::Editor;
 
+our $file;
+our $encoding;
+
 sub OnInit {
-	my $app   = shift;
-	my $frame = Wx::Frame->new( undef, -1, __PACKAGE__ . 'Kephra xp testbed');
-	my $ed = Kephra::App::Editor->new($frame, -1);
-	Wx::Window::SetFocus( $ed );
+        my $app   = shift;
+        my $frame = Wx::Frame->new( undef, -1, __PACKAGE__ . 'Kephra xp testbed');
+        my $ed = Kephra::App::Editor->new($frame, -1);
+        Wx::Window::SetFocus( $ed );
 
-	Wx::Event::EVT_KEY_DOWN($ed , sub {
-		my ($ed, $event) = @_;
-		my $code = $event->GetUnicodeKey;
-		if ($code == 81 and $event->ControlDown){
-			$frame->Close;
-		} else {
-			$event->Skip;
-		}
-	});
+        Wx::Event::EVT_KEY_DOWN($ed , sub {
+                my ($ed, $event) = @_;
+                my $code = $event->GetUnicodeKey;
+                if ($code == 79 and $event->ControlDown){
+                	$file = get_file_open();
+                	$ed->SetText( read_file($file) );
+                }
+                elsif ($code == 81 and $event->ControlDown){ $frame->Close }
+                elsif ($code == 83 and $event->ControlDown){
+                	write_file( $file, $ed->GetText() );
+                } 
+                else { $event->Skip }
+        });
 
-	$frame->Show(1);
-	$app->SetTopWindow($frame);
-	1;
+        $frame->Show(1);
+        $app->SetTopWindow($frame);
+        1;
 }
 
+sub get_file_open {
+	my $title  = shift // 'Open File ...';
+	my $dir    = shift // '.';
+	my $filter = shift // '(*)|*';
+	my $parent = shift // undef;
+	Wx::FileSelector( $title, $dir, '', '', $filter, &Wx::wxFD_OPEN, $parent);
+}
+
+sub read_file {
+	my $file = shift;
+	return unless $file and -r $file;
+	CORE::open my $FH, '<', $file;
+	binmode($FH);
+	my $content = do { local $/; <$FH> };
+	if ($content) {
+		my $guess = Encode::Guess::guess_encoding( $content, qw/utf-8 iso8859-1 latin1/ );
+		if ( ref($guess) and ref($guess) =~ m/^Encode::/ ) {
+			$encoding = $guess->name;
+		} elsif ( $guess =~ m/utf8/ ) {
+			$encoding = 'utf-8';
+		} elsif ( $guess =~ m/or/ ) {
+			my @suggest_encodings = split /\sor\s/, $guess;
+			$encoding = $suggest_encodings[0];
+		} else { $encoding = 'utf-8' }
+		return Encode::decode( $encoding,  $content );		
+	}
+}
+
+sub write_file {
+	my ($file, $content) = @_;
+	return unless $file and -w $file;
+	CORE::open my $FH, '> :raw :encoding('.$encoding.')', $file;
+	print $FH $content;
+	print $file , $encoding;
+}
 
 package main;
-SimpleEdit->new->MainLoop;
+SimpleEdit->new->MainLoop;