Commits

Herbert Breunung committed e7e3bad

setting sed up for using IO::LocalFile

Comments (0)

Files changed (7)

lib/Kephra/App/.DS_Store

Binary file added.

lib/Kephra/App/Dialog.pm

+use v5.12;
+use warnings;
+use Wx;
+
+package Kephra::App::Dialog;
+
+
+
+
+sub _parent { undef }
+
+# standard dialogs
+
+sub message {
+	$_[1] = $_[1] || 'Kephra Message'; 
+	splice @_, 2, 0, &Wx::wxOK | &Wx::wxSTAY_ON_TOP;
+	_box( @_ );
+}
+sub info   {
+	$_[1] = $_[1] || 'Kephra Information'; 
+	splice @_, 2, 0, &Wx::wxOK | &Wx::wxICON_INFORMATION | &Wx::wxSTAY_ON_TOP;
+	_box( @_ );
+}
+sub warning {
+	$_[1] = $_[1] || 'Kephra Warning'; 
+	splice @_, 2, 0, &Wx::wxOK | &Wx::wxICON_WARNING | &Wx::wxSTAY_ON_TOP;
+	_box( @_ );
+}
+sub yes_no     { 
+	$_[1] = $_[1] || 'Kephra Question'; 
+	splice @_, 2, 0, &Wx::wxYES_NO | &Wx::wxICON_QUESTION | &Wx::wxSTAY_ON_TOP;
+	_box( @_ );
+}
+sub yes_no_cancel{
+	$_[1] = $_[1] || 'Kephra Question'; 
+	splice @_, 2, 0, &Wx::wxYES_NO | &Wx::wxCANCEL | &Wx::wxICON_QUESTION | &Wx::wxSTAY_ON_TOP;
+	_box( @_ );
+}
+sub _box {                                   # $message, $title, $style, $parent
+	Kephra::Log::warning('need at least a message as first parameter', 2) unless $_[0];
+	$_[3] = $_[3] || _parent(); 
+	Wx::MessageBox( @_ );
+}
+
+sub get_file_open {
+	my $title  = shift // 'Open File ...';
+	my $dir    = shift // '.';
+	my $filter = shift // '(*)|*';
+	my $parent = shift // _parent();
+	Wx::FileSelector( $title, $dir, '', '', $filter, &Wx::wxFD_OPEN, $parent);
+}
+
+sub get_files_open {
+	my $title  = shift // 'Open File ...';
+	my $dir    = shift // '.';
+	my $filter = shift // '(*)|*';
+	my $parent = shift // _parent();
+	my $dialog = Wx::FileDialog->new
+		($parent, $title, $dir, '', $filter, &Wx::wxFD_OPEN | &Wx::wxFD_MULTIPLE);
+	if ($dialog->ShowModal != &Wx::wxID_CANCEL) {
+		return $dialog->GetPaths;
+	}
+}
+
+sub get_file_save { 
+	my $title  = shift // 'Save File As ...';
+	my $dir    = shift // '.';
+	my $filter = shift // '(*)|*';
+	my $parent = shift // _parent();
+	Wx::FileSelector( $title, $dir, '', '', $filter, &Wx::wxFD_SAVE, $parent);
+}
+
+sub get_dir  {  Wx::DirSelector      ( @_[0,1], 0, [-1,-1], _parent()) }
+sub get_font {  Wx::GetFontFromUser  ( _parent(), $_[0]) }
+sub get_text {  Wx::GetTextFromUser  ( $_[0], $_[1], "", _parent()) }
+sub get_number {Wx::GetNumberFromUser( $_[0], '', $_[1],$_[2], 0, 100000, _parent())}
+
+# own dialogs
+#sub find {
+	#require Kephra::App::Dialog::Search; &Kephra::App::Dialog::Search::find;
+#}
+#sub replace {
+	#require Kephra::App::Dialog::Search; &Kephra::App::Dialog::Search::replace;
+#}
+#sub choose_color {
+	#require Kephra::App::Dialog::Color; Kephra::App::Dialog::Color::choose_color();
+#}
+sub about {
+	require Kephra::App::Dialog::About;
+	Kephra::App::Dialog::About::create( )->ShowModal;
+}
+sub config {
+	require Kephra::App::Dialog::Config;
+	Kephra::App::Dialog::Config::create( )->ShowModal;
+}
+sub documentation {
+	require Kephra::App::Dialog::Documentation;
+	Kephra::App::Dialog::Documentation::create( )->ShowModal;
+}
+sub keymap {
+	require Kephra::App::Dialog::Keymap;
+	Kephra::App::Dialog::Keymap::create( )->ShowModal;
+}
+
+#sub notify_file_changed {
+	#require Kephra::App::Dialog::Notify; &Kephra::App::Dialog::Notify::file_changed;
+#}
+#sub notify_file_deleted {
+	#require Kephra::App::Dialog::Notify; &Kephra::App::Dialog::Notify::file_deleted;
+#}
+#sub save_on_exit {
+	#require Kephra::App::Dialog::Exit; &Kephra::App::Dialog::Exit::save_on_exit;
+#}
+
+
+1;

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/IO/.DS_Store

Binary file added.

lib/Kephra/IO/LocalFile.pm

 use warnings;
 use Encode;
 use Encode::Guess;
+use File::Spec;
 
 package Kephra::IO::LocalFile;
 
 
+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 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() };
+	my $file = normalize_path( shift );
+	my $encoding;
+	#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;
 	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/ ) {
+		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";
-			$doc->encoding( $suggest_encodings[0] );
-		} else { $doc->encoding( 'utf-8' ) }
-		$content = Encode::decode( $doc->encoding,  $content );
-		#say $doc->encoding; 
-
-		$ed->SetText( $content );
-		$ed->SetSavePoint;
+			$encoding = $suggest_encodings[0];
+		} else                                             { $encoding = 'utf-8' }
+		$content = Encode::decode( $doc->encoding,  $content ); 
 	}
+	return $content, $encoding;
 }
 
 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;
+	my ($file, $encoding, $content) = @_;
+	my $file = normalize_path( $file );
+	#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;
+	CORE::open my $FH, "> :raw :encoding($encoding)", $file;
+	print $FH $content;
 }
 
 
 		} 
 		else { $event->Skip }
     });
-	Wx::Window::SetFocus( $ed );
 
 	$frame->Show(1);
 	$app->SetTopWindow($frame);
 	1;
-}
+}
 use v5.12;
 use warnings;
 
-package SimpleEdit;
+package SingleEdit;
 use Wx;
 use base qw(Wx::App);
 use Encode::Guess;
+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', [-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){
-			$ed->SetText( read_file($file = get_file_open()) );
+			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){
-			write_file( $file, $ed->GetText() );
+			Kephra::IO::LocalFile::write( $file, $encoding, $ed->GetText() );
 		} 
 		else { $event->Skip }
         });
 	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;
 }
 
 package main;
-SimpleEdit->new->MainLoop;
+SingleEdit->new->MainLoop;