Padre
view release on metacpan or search on metacpan
lib/Padre/Document.pm view on Meta::CPAN
return $self->{readonly};
}
# Returns true if file has changed on the disk
# since load time or the last time we saved it.
# Check if the file on the disk has changed
# 1) when document gets the focus (gvim, notepad++)
# 2) when we try to save the file (gvim)
# 3) every time we type something ????
sub has_changed_on_disk {
my $self = shift;
return 0 unless defined $self->file;
return 0 unless defined $self->timestamp;
# Caching the result for two lines saved one stat-I/O each time this sub is run
my $timestamp_now = $self->timestamp_now;
return 0 unless defined $timestamp_now; # there may be no mtime on remote files
# Return -1 if file has been deleted from disk
return -1 unless $timestamp_now;
# Return 1 if the file has changed on disk, otherwise 0
return $self->timestamp < $timestamp_now ? 1 : 0;
}
sub timestamp_now {
my $self = shift;
my $file = $self->file;
return 0 unless defined $file;
# It's important to return undef if there is no ->mtime for this filetype
return undef unless $file->can('mtime');
return $file->mtime;
}
=head2 load_file
$doc->load_file;
Loads the current file.
Sets the B<Encoding> bit using L<Encode::Guess> and tries to figure
out what kind of newlines are in the file. Defaults to C<utf-8> if it
could not figure out the encoding.
Returns true on success false on failure. Sets C<< $doc->errstr >>.
=cut
sub load_file {
my $self = shift;
my $file = $self->file;
if (DEBUG) {
my $name = $file->{filename} || '';
TRACE("Loading file '$name'");
}
# Show the file-changed-dialog again after the file was (re)loaded:
delete $self->{_already_popup_file_changed};
# check if file exists
if ( !$file->exists ) {
# file doesn't exist, try to create an empty one
if ( !$file->write('') ) {
# oops, error creating file. abort operation
$self->set_errstr( $file->error );
return;
}
}
# load file
$self->set_errstr('');
my $content = $file->read;
if ( !defined($content) ) {
$self->set_errstr( $file->error );
return;
}
$self->{timestamp} = $self->timestamp_now;
# if guess encoding fails then use 'utf-8'
require Padre::Locale;
require Encode;
$self->{encoding} = Padre::Locale::encoding_from_string($content);
$content = Encode::decode( $self->{encoding}, $content );
# Determine new line type using file content.
$self->{newline_type} = Padre::Util::newline_type($content);
# Cache the original value of various things so we can do
# smart things at save time later.
$self->{original_content} = $content;
$self->{original_newline} = $self->{newline_type};
return 1;
}
# New line type acan be one of these values:
# WIN, MAC (for classic Mac) or UNIX (for Mac OS X and Linux/*BSD)
# Special cases:
# 'Mixed' for mixed end of lines,
# 'None' for one-liners (no EOL)
sub newline_type {
$_[0]->{newline_type} or $_[0]->default_newline_type;
}
# Get the newline char(s) for this document.
# TO DO: This solution is really terrible - it should be {newline} or at least a caching of the value
# because of speed issues:
sub newline {
my $self = shift;
if ( $self->newline_type eq 'WIN' ) {
return "\r\n";
} elsif ( $self->newline_type eq 'MAC' ) {
return "\r";
}
return "\n";
}
lib/Padre/Document.pm view on Meta::CPAN
} else {
my $nextChar;
if ( $editor->GetTextLength > $pos ) {
$nextChar = $editor->GetTextRange( $pos, $pos + 1 );
}
unless ( defined($nextChar) && ord($nextChar) == $table{$key}
and ( !$config->autocomplete_multiclosebracket ) )
{
$editor->AddText( chr( $table{$key} ) );
$editor->CharLeft;
}
}
return 1;
}
sub set_filename {
my $self = shift;
my $filename = shift;
unless ( defined $filename ) {
warn 'Request to set filename to undef from ' . join( ',', caller );
return 0;
}
# Shortcut if no change in file name
if ( defined $self->{filename} and $self->{filename} eq $filename ) {
return 1;
}
# Flush out old state information, primarily the file object.
# Additionally, whenever we change the name of the file we can no
# longer trust that we are in the same project, so flush that as well.
delete $self->{filename};
delete $self->{file};
delete $self->{project_dir};
# Save the new filename
$self->{file} = Padre::File->new($filename);
# Padre::File reformats filenames to the protocol/OS specific format, so use this:
$self->{filename} = $self->{file}->{filename};
}
# Only a dummy for documents which don't support this
sub autoclean {
my $self = shift;
return 1;
}
sub save_file {
my $self = shift;
my $current = $self->current;
my $manager = $current->ide->plugin_manager;
unless ( $manager->hook( 'before_save', $self ) ) {
return;
}
# Show the file-changed-dialog again after the file was saved:
delete $self->{_already_popup_file_changed};
# If padre is run on files that have no project
# I.E Padre foo.pl &
# The assumption of $self->project as defined will cause a fail
my $config;
$config = $self->project->config if $self->project;
$self->set_errstr('');
if ( $config and $config->save_autoclean ) {
$self->autoclean;
}
my $content = $self->text_get;
my $file = $self->file;
unless ( defined $file ) {
# NOTE: Now we have ->set_filename, should this situation ever occur?
$file = Padre::File->new( $self->filename );
$self->{file} = $file;
}
# This is just temporary for security and should prevend data loss:
if ( $self->{filename} ne $file->{filename} ) {
my $ret = Wx::MessageBox(
sprintf(
Wx::gettext('Visual filename %s does not match the internal filename %s, do you want to abort saving?'),
$self->{filename},
$file->{filename}
),
Wx::gettext("Save Warning"),
Wx::YES_NO | Wx::CENTRE,
$current->main,
);
return 0 if $ret == Wx::YES;
}
# Not set when first time to save
# Allow the upgrade from ascii to utf-8 if there were unicode characters added
unless ( $self->{encoding} and $self->{encoding} ne 'ascii' ) {
require Padre::Locale;
$self->{encoding} = Padre::Locale::encoding_from_string($content);
}
my $encode = '';
if ( defined $self->{encoding} ) {
$encode = ":raw:encoding($self->{encoding})";
} else {
warn "encoding is not set, (couldn't get from contents) when saving file $file->{filename}\n";
}
unless ( $file->write( $content, $encode ) ) {
$self->set_errstr( $file->error );
return;
}
# File must be closed at this time, slow fs/userspace-fs may not
# return the correct result otherwise!
$self->{timestamp} = $self->timestamp_now;
( run in 0.851 second using v1.01-cache-2.11-cpan-364913b4093 )