Source

Kephra / lib / Kephra / File.pm

use strict;
use warnings;
use File::Spec;
use Kephra::API;

package Kephra::File;

Kephra::CommandList::register_cmd_namespace('file');
Kephra::CommandList::register_cmd({
 '+-new'        =>{sub=> 'new',          label=> 'New',        keys=> 'ctrl+n'},
 '+-open'       =>{sub=> 'open',         label=> 'Open ...',   keys=> 'ctrl+o'},
 '+-reopen'     =>{sub=> 'reopen_active',label=> 'Reopen',     keys=> 'ctrl+shift+o'},
 '+-save'       =>{sub=> 'save_active',  label=> 'Save',       keys=> 'ctrl+s'},
 '+-save-as'    =>{sub=> 'save_as',      label=> 'Save As ...',keys=> 'ctrl+shift+s'},
 '+-rename'     =>{sub=> 'rename',       label=> 'Rename ...', keys=> 'ctrl+alt+shift+s'},
 '+-close'      =>{sub=> 'close_active', label=> 'Close',      keys=> 'ctrl+q'},
 '+-close-other'=>{sub=> 'close_other',  label=> 'Close Other',keys=> 'ctrl+shift+q'},
 '+-close-all'  =>{sub=> 'close_all',    label=> 'Close All',  keys=> 'ctrl+alt+q'},
});

sub normalize_name {
	my $file = shift;
	return unless $file;

	$file = File::Spec->canonpath($file);
	local $/ = "\r\n";
	chomp($file);
	return $file;
}


sub new  { Kephra::App::Part::Editor::create_document() }
sub open {
	# open dialog if no file was given
	$_[0] = Kephra::App::Dialog::get_file_open() unless @_;

	# new doc for each file name
	for my $file (@_){
		$file = normalize_name( $file );
		next unless $file and -e $file;

		# raise tab if doc is alread open
		Kephra::App::Part::Editor::raise_document($file), next
		  if Kephra::DocumentStash::file_loaded($file);

		my $doc = Kephra::App::Part::Editor::create_document($file);
		next unless ref $doc eq 'Kephra::Document';
		CORE::open my $FH, '<', $file;
		my $file_content = do { local $/; <$FH> };
		my $ed = $doc->editor;
		$ed->SetText( $file_content ) if $file_content;
		$ed->EmptyUndoBuffer;
		$ed->SetSavePoint;
	}
}
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;
		CORE::open my $FH, '<', $doc->file_path;
		my $file_content = do { local $/; <$FH> };
		my $ed = $doc->editor;
		$ed->SetText( $file_content ) if $file_content;
	}
}
sub reopen_active { reopen( Kephra::API::document() ) }
sub reopen_all    { reopen( Kephra::API::all_documents()   ) }


sub _store_under_filename {
	my $doc = shift;
	my $file = shift || $doc->file_path;
	CORE::open my $FH, '>', $file;
	print $FH $doc->editor->GetText;
	$doc->editor->SetSavePoint;
}
sub save {
	$_[0] = Kephra::API::document() unless @_;
	for my $doc (@_){
		next unless ref $doc eq 'Kephra::Document';
		next unless $doc->editor->GetModify;
		if ($doc->file_path){
			_store_under_filename($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 = Kephra::App::Dialog::get_file_save();
	return unless $file;
	_store_under_filename($doc, $file);
	$doc->assign_file_name($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 @_;
	for my $doc (@_) {
		next unless ref $doc eq 'Kephra::Document';
		if ($doc->editor->GetModify) {
			my $save_answer = Kephra::App::Dialog::get_confirm_3 (
				'close unsaved file', 'do you want to save it before?'
			);
			next if $save_answer == &Wx::wxCANCEL;
			save($doc) if $save_answer == &Wx::wxYES;
		}
		Kephra::App::Part::Editor::remove_document( $doc );
	}
}
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;