Source

Kephra / lib / Kephra / File.pm

use strict;
use warnings;
use File::Spec;
use Kephra::API;
use Kephra::App::Panel::Editor;
use Kephra::DocumentStash;

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'},
 '+-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::Panel::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::Panel::Editor::raise_document($file), next
		  if Kephra::DocumentStash::file_loaded($file);

		my $doc = Kephra::App::Panel::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'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 save {
	for my $doc (@_){
		next unless ref $doc eq 'Kephra::Document';
		if ($doc->file_path){
			CORE::open my $FH, '>', $doc->file_path;
			print $FH $doc->editor->GetText;
		} else {
			$doc->raise;
			save_as();
		}
	}
}
sub save_active { save( Kephra::API::document() ) }
sub save_all    { save( Kephra::API::all_documents()   ) }

sub save_as {
	my $doc = Kephra::API::document();
	my $file = Kephra::App::Dialog::get_file_save();
	return unless $file;
	CORE::open my $FH, '>', $file;
	print $FH $doc->editor->GetText;
	$doc->assign_file_name($file);
	Kephra::API::docbar()->set_page_title( $doc->file_name);
	Kephra::API::main_window()->refresh_title();
}

sub rename {
	my $old_file = Kephra::API::document()->file_name;
	save_as();
	unlink $old_file;
}


sub close_active {
	my $doc = Kephra::API::document();
	return unless $doc;
	Kephra::App::Panel::Editor::remove_document( $doc );
}
sub close_other  {
	my $doc = Kephra::API::document();
	for (Kephra::API::all_documents()) {
		Kephra::App::Panel::Editor::remove_document($_) unless $_ eq $doc;
	}
}
sub close_all    {
	Kephra::App::Panel::Editor::remove_document($_) for Kephra::API::all_documents();
}

1;