Kephra / lib / Kephra /

use v5.10;
use strict;
use warnings;
use File::Spec;
use Kephra::API;
use Kephra::App::Dialog;
use Kephra::File::Local;

package Kephra::File;

 '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";
	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} );

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->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();
		Kephra::File::Local::read( $doc, $file );
		$ed = Kephra::API::editor();
		$doc->editor->{$_}->mount_events() for Kephra::API::all_docbars();
		$_->mount_events() for Kephra::API::all_docbars();

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){
		} else {
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);
	Kephra::API::docbar()->set_page_title( $doc->file_name);
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();
		} else {
			next unless $ed->GetLength;
		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 }

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
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.