Kephra / lib / Kephra / File.pm

Herbert Breunung c585290 
Herbert Breunung 51d326f 

Herbert Breunung 340531b 
Herbert Breunung dfbe38b 
Herbert Breunung c585290 

Herbert Breunung 51d326f 


Herbert Breunung 320b6b3 
Herbert Breunung 95c7b31 








Herbert Breunung 47b0474 
Herbert Breunung ef60923 
Herbert Breunung c585290 
Herbert Breunung 340531b 
Herbert Breunung d9d4098 
Herbert Breunung 340531b 






Herbert Breunung 571574d 
Herbert Breunung c585290 
Herbert Breunung 08777d6 
Herbert Breunung c585290 


Herbert Breunung 524faf8 
Herbert Breunung c585290 

Herbert Breunung ac1e13b 
Herbert Breunung b5020cc 
Herbert Breunung c585290 
Herbert Breunung b5020cc 
Herbert Breunung d6255da 
Herbert Breunung 59f7450 
Herbert Breunung c585290 
Herbert Breunung 59f7450 
Herbert Breunung 340531b 
Herbert Breunung 59f7450 
Herbert Breunung c96f552 
Herbert Breunung 5d63aec 
Herbert Breunung e5217c2 
Herbert Breunung c585290 

















Herbert Breunung 524faf8 
Herbert Breunung 59f7450 
Herbert Breunung ac1e13b 
Herbert Breunung c585290 
Herbert Breunung 8674e91 
Herbert Breunung 56545c8 
Herbert Breunung 571574d 


Herbert Breunung c585290 
Herbert Breunung 56545c8 

Herbert Breunung 8674e91 

Herbert Breunung 56545c8 
Herbert Breunung d9d4098 
Herbert Breunung 59f7450 
Herbert Breunung 4832fba 
Herbert Breunung c585290 

Herbert Breunung 59f7450 
Herbert Breunung a9abbed 
Herbert Breunung c585290 
Herbert Breunung a9abbed 
Herbert Breunung c585290 
Herbert Breunung a9abbed 

Herbert Breunung 4832fba 
Herbert Breunung a9abbed 
Herbert Breunung 59f7450 

Herbert Breunung 47b0474 
Herbert Breunung 571574d 
Herbert Breunung 2655cce 
Herbert Breunung 4832fba 
Herbert Breunung 6f0870a 
Herbert Breunung 2655cce 
Herbert Breunung d4f2ecd 
Herbert Breunung cd3dce8 
Herbert Breunung 2655cce 



Herbert Breunung 571574d 


Herbert Breunung 2655cce 


Herbert Breunung dfbe38b 
Herbert Breunung 571574d 

Herbert Breunung c585290 

Herbert Breunung 571574d 

Herbert Breunung c585290 



Herbert Breunung 571574d 
Herbert Breunung 66e70b1 
Herbert Breunung 571574d 

Herbert Breunung c585290 









Herbert Breunung 571574d 
Herbert Breunung c585290 
Herbert Breunung ef60923 

Herbert Breunung dfbe38b 
Herbert Breunung 571574d 

Herbert Breunung 614851e 

Herbert Breunung 571574d 
Herbert Breunung 614851e 
Herbert Breunung 51d326f 
Herbert Breunung 571574d 
Herbert Breunung 51d326f 
use v5.10;
use strict;
use warnings;
use File::Spec;
use Kephra::API;
use Kephra::App::Dialog;
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 $docbar = Kephra::API::docbar();
	my $doc = Kephra::Document->new();
	$_->add_page( $doc->panel->{$_}, 'rightmost', $doc->title, 0 ) for Kephra::API::all_docbars();;
	Kephra::API::focus( $doc->editor->{$docbar} );
	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($file);
			$_->add_page( $doc->panel->{$_},'right', $doc->file_name, 1) for Kephra::API::all_docbars();
			my $content_ref = $doc->editor->{$db}->GetDocPointer();
			$doc->editor->{$_}->SetDocPointer( $content_ref ) for Kephra::API::passive_docbars()
		}
		# reuse it if current doc is empty
		else {
			$doc->file_path($file) 
		}
		Kephra::API::app()->Yield;
		Kephra::File::Local::read( $doc, $file );
		$ed = Kephra::API::editor();
		$ed->EmptyUndoBuffer;
		#$ed->mount_events();
		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 $docbar = Kephra::API::docbar();

	for my $doc (@_){
		next unless ref $doc eq 'Kephra::Document';
		next unless $doc->editor->{$docbar}->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::docbar()->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_docbars();
			$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;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.