Commits

Herbert Breunung committed 9ad56bb

copy over status to med

Comments (0)

Files changed (4)

lib/Kephra/File.pm

-use v5.12;
-use warnings;
-use File::Spec;
-use Kephra::File::Local;
-
-package Kephra::File;
-
-Kephra::API::Command::register({
- 'file-new'        =>{sub=> 'new',          label=> 'New',        keys=> 'ctrl+n'},
- 'file-open'       =>{sub=> 'open',         label=> 'Open ...',   keys=> 'ctrl+o'},
- 'file-reopen'     =>{sub=> 'reopen_active',label=> 'Reopen',     keys=> 'ctrl+shift+o'},
- 'file-save'       =>{sub=> 'save_active',  label=> 'Save',       keys=> 'ctrl+s'},
- 'file-save-as'    =>{sub=> 'save_as',      label=> 'Save As ...',keys=> 'ctrl+shift+s'},
- 'file-rename'     =>{sub=> 'rename',       label=> 'Rename ...', keys=> 'ctrl+alt+shift+s'},
- 'file-close'      =>{sub=> 'close_active', label=> 'Close',      keys=> 'ctrl+q'},
- 'file-close-other'=>{sub=> 'close_other',  label=> 'Close Other',keys=> 'ctrl+shift+q'},
- 'file-close-all'  =>{sub=> 'close_all',    label=> 'Close All',  keys=> 'ctrl+alt+q'},
-});
-use Scalar::Util qw(blessed);
-sub normalize_path {
-	my $file = shift;
-	return unless defined $file and $file;
-
-	$file = File::Spec->canonpath($file);
-	local $/ = "\r\n";
-	chomp($file);
-	return $file;
-}
-
-
-sub new  {
-	my $tabbar = Kephra::API::tabbar();
-	my $doc = Kephra::Document->new();
-	$_->add_page( $doc->panel->{$_}, 'rightmost', $doc->title, 0 ) for Kephra::API::all_tabbars();
-	Kephra::API::focus( $doc->editor->{$tabbar} );
-	Kephra::API::main_window()->refresh_title();
-}
-
-sub open {
-	# open dialog if no file was given
-	@_ = Kephra::App::Dialog::get_files_open() unless @_;
-
-	# new doc for each file name
-	for my $file (@_){
-		$file = normalize_path( $file );
-		next unless $file and -e $file;
-
-		# raise tab if doc is alread open
-		Kephra::App::Part::Editor::raise_document($file), next
-		  if Kephra::API::Doc::file_loaded($file);
-
-		my $db = Kephra::API::docbar();
-		my $doc = Kephra::API::document();
-		my $ed = Kephra::API::editor();
-		if ($ed->GetLength) {
-			$doc = Kephra::Document->new();
-			$doc->editor->{$_}->unmount_events() for Kephra::API::all_docbars();
-			$_->add_page( $doc->panel->{$_},'right', $doc->file_name, 1) for Kephra::API::all_docbars();
-			my $content_ref = $doc->editor->{$db}->GetDocPointer();
-			$doc->file_path($file);
-			$doc->editor->{$_}->SetDocPointer( $content_ref ) for Kephra::API::passive_docbars();
-		}
-		# reuse it if current doc is empty
-		else {
-			$doc->editor->{$_}->unmount_events() for Kephra::API::all_docbars();
-			$doc->file_path($file) 
-		}
-		Kephra::API::app()->Yield;
-		Kephra::File::Local::read( $doc, $file );
-		$ed = Kephra::API::editor();
-		$ed->EmptyUndoBuffer;
-		$doc->editor->{$_}->mount_events() for Kephra::API::all_docbars();
-		$_->mount_events() for Kephra::API::all_docbars();
-		Kephra::API::main_window()->refresh_title();
-	}
-}
-
-sub reopen {
-	for my $doc (@_){
-		next unless ref $doc eq 'Kephra::Document';
-		Kephra::Log::warning("can't reopen nonexising file"), next
-			unless $doc->file_path and -e $doc->file_path;
-		Kephra::File::Local::read( $doc );
-	}
-}
-sub reopen_active { reopen( Kephra::API::document() ) }
-sub reopen_all    { reopen( Kephra::API::all_documents()   ) }
-
-
-sub save {
-	$_[0] = Kephra::API::document() unless @_;
-	my $tabbar = Kephra::API::tabbar();
-
-	for my $doc (@_){
-		next unless ref $doc eq 'Kephra::Document';
-		next unless $doc->editor->{$tabbar}->GetModify;
-		if ($doc->file_path){
-			Kephra::File::Local::write($doc);
-		} else {
-			$doc->raise;
-			save_as($doc);
-		}
-	}
-}
-sub save_active { save( Kephra::API::document() ) }
-sub save_all    { save( Kephra::API::all_documents() ) }
-sub save_as {
-	my $doc = shift || Kephra::API::document();
-	my $file = normalize_name( Kephra::App::Dialog::get_file_save() );
-	return unless $file;
-	_write_file($doc, $file);
-	$doc->assign_file_path($file);
-	Kephra::API::tabbar()->set_page_title( $doc->file_name);
-	Kephra::API::main_window()->refresh_title();
-}
-sub rename {
-	my $doc = shift || Kephra::API::document();
-	my $old_file = $doc->file_name;
-	save_as( $doc );
-	unlink $old_file;
-}
-
-
-sub close {
-	$_[0] = Kephra::API::document() unless @_;
-	my $active_bar = Kephra::API::docbar();
-
-	for my $doc (@_) {
-		next unless ref $doc eq 'Kephra::Document';
-		my $ed = $doc->editor->{ $active_bar };
-		if ($ed->GetModify) {
-			my $save_answer = Kephra::App::Dialog::yes_no_cancel (
-				'close unsaved file', 'do you want to save it before?'
-			);
-			next if $save_answer == &Wx::wxCANCEL;
-			save($doc) if $save_answer == &Wx::wxYES;
-		}
-		if ($active_bar->GetPageCount > 1){
-			$_->remove_page( $doc->panel->{$_} ) for Kephra::API::all_tabbars();
-			$doc->DESTROY;
-		} else {
-			next unless $ed->GetLength;
-			$ed->ClearAll();
-			$ed->SetSavePoint;
-			$ed->EmptyUndoBuffer;
-			$doc->file_path('');
-		}
-	}
-	Kephra::API::main_window()->refresh_title();
-	Kephra::API::focus($active_bar->GetSelection)
-		if blessed($active_bar) and $active_bar->GetSelection;
-}
-sub close_active { Kephra::File::close( Kephra::API::document() )}
-sub close_all    { Kephra::File::close( Kephra::API::all_documents() ) }
-sub close_other  {
-	my $doc = Kephra::API::document();
-	for (Kephra::API::all_documents()) { Kephra::File::close($_) unless $_ eq $doc }
-}
-
-
-1;

lib/Kephra/Log.pm

+use v5.12;
+use warnings;
+
+package Kephra::Log;
+
+use Kephra::API;
+
+our $level;
+our @output;
+
+our $global_switch = 1;    # do_logging
+our $GUI_switch = 0;
+our $CLI_switch = 1;
+our %local_switch;
+
+sub mode {
+	my $mode = shift;
+	return unless defined $mode;
+	return unless $mode eq 'on' or $mode eq 'off'
+		or $mode eq 'gui' or $mode eq 'text' or $mode eq 'all';
+	$global_switch = $mode eq 'off' ? 0 : 1;
+	if ($mode eq 'text'){ $GUI_switch = 0; $CLI_switch = 1 }
+	if ($mode eq 'gui') { $GUI_switch = 1; $CLI_switch = 0 }
+	if ($mode eq 'all') { $GUI_switch = 1; $CLI_switch = 1 }
+}
+
+
+sub note {
+	return unless $global_switch;
+	say message( @_ ) if $CLI_switch;
+}
+
+sub warning {
+	return unless $global_switch;
+	my $msg = message( @_ );
+	warn $msg if $CLI_switch;
+	Kephra::API::output('warn', $msg) if $GUI_switch;
+}
+
+sub error {
+	return unless $global_switch;
+	my $msg = message( @_ );
+	die $msg if $CLI_switch;
+	Kephra::API::output('warn', 'ERROR: '.$msg) if $GUI_switch;
+}
+
+sub message     { 
+	my ($text, $name_parent) = @_;
+	my $msg = timestamp() . ' ' . calling_sub(3) . ': "'. $text.'"';
+	$msg .= ', when called by '. calling_sub(3+$name_parent) if $name_parent;
+	return $msg;
+}
+sub timestamp   { sprintf("[%02u:%02u:%02u]", (localtime)[2,1,0]) }
+sub calling_sub { ( caller($_[0]) )[3] } 
+
+1;
 use v5.12;
 use warnings;
 
-package MediumEdit;
+package MultiEdit;
+use Wx;
 use base qw(Wx::App);
-use Encode::Guess;
-use Kephra::App::Editor;
+use Kephra::App::Dialog;
+use Kephra::App::Editor;
+use Kephra::IO::LocalFile;
 
 our $file = __FILE__;
-our $encoding;
+our ($encoding, $content);
 
 sub OnInit {
-        my $app   = shift;
-        my $frame = Wx::Frame->new( undef, -1, __PACKAGE__ . 'Kephra xp testbed', [-1,-1], [1000,800]);
-        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 step 2', [-1,-1], [1000,800]);
+  	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 == 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 }
+	Wx::Event::EVT_KEY_DOWN($ed , sub {
+		my ($ed, $event) = @_;
+		my $code = $event->GetUnicodeKey;
+		if ($code == 79 and $event->ControlDown){
+			my $selected = Kephra::App::Dialog::get_file_open(undef,undef,undef,undef,$frame);
+			return unless $selected and -r $selected;
+			($content, $encoding) = Kephra::IO::LocalFile::read( $file = $selected );
+			$ed->SetText( $content );
+		}
+		elsif ($code == 81 and $event->ControlDown){ 
+			$frame->Close
+		}
+		elsif ($code == 83 and $event->ControlDown){
+			Kephra::IO::LocalFile::write( $file, $encoding, $ed->GetText() );
+		} 
+		else { $event->Skip }
         });
-		$ed->SetText( read_file($file) );
-        
-        $frame->Center();
-        $frame->Show(1);
-        $app->SetTopWindow($frame);
-        1;
+    ($content, $encoding) = Kephra::IO::LocalFile::read( $file );
+	$ed->SetText( $content );
+
+	$frame->Center();
+	$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;
-MediumEdit->new->MainLoop;
+MultiEdit->new->MainLoop;
 package SingleEdit;
 use Wx;
 use base qw(Wx::App);
-use Encode::Guess;
 use Kephra::App::Dialog;
 use Kephra::App::Editor;
 use Kephra::IO::LocalFile;
 }
 
 package main;
-SingleEdit->new->MainLoop;
+SingleEdit->new->MainLoop;
+
+# autosave
+# save status
+# logging