| | 3294 | =head3 C<on_save> |
| | 3295 | |
| | 3296 | my $success = $main->on_save; |
| | 3297 | |
| | 3298 | Try to save current document. Prompt user for a file name if document was |
| | 3299 | new (see C<on_save_as()> above). Return true if document has been saved, |
| | 3300 | false otherwise. |
| | 3301 | |
| | 3302 | =cut |
| | 3303 | |
| | 3304 | sub on_save { |
| | 3305 | my $self = shift; |
| | 3306 | my $document = shift || $self->current->document; |
| | 3307 | return unless $document; |
| | 3308 | |
| | 3309 | #print $document->filename, "\n"; |
| | 3310 | |
| | 3311 | my $pageid = $self->find_id_of_editor( $document->editor ); |
| | 3312 | if ( $document->is_new ) { |
| | 3313 | |
| | 3314 | # move focus to document to be saved |
| | 3315 | $self->on_nth_pane($pageid); |
| | 3316 | return $self->on_save_as; |
| | 3317 | } elsif ( $document->is_modified ) { |
| | 3318 | return $self->_save_buffer($pageid); |
| | 3319 | } |
| | 3320 | |
| | 3321 | return; |
| | 3322 | } |
| | 3323 | |
| | 3324 | =pod |
| | 3325 | |
| 3402 | | =head3 C<on_save> |
| 3403 | | |
| 3404 | | my $success = $main->on_save; |
| 3405 | | |
| 3406 | | Try to save current document. Prompt user for a file name if document was |
| 3407 | | new (see C<on_save_as()> above). Return true if document has been saved, |
| 3408 | | false otherwise. |
| 3409 | | |
| 3410 | | =cut |
| 3411 | | |
| 3412 | | sub on_save { |
| 3413 | | my $self = shift; |
| 3414 | | my $document = shift || $self->current->document; |
| 3415 | | return unless $document; |
| 3416 | | |
| 3417 | | #print $document->filename, "\n"; |
| 3418 | | |
| 3419 | | my $pageid = $self->find_id_of_editor( $document->editor ); |
| 3420 | | if ( $document->is_new ) { |
| 3421 | | |
| 3422 | | # move focus to document to be saved |
| 3423 | | $self->on_nth_pane($pageid); |
| 3424 | | return $self->on_save_as; |
| 3425 | | } elsif ( $document->is_modified ) { |
| 3426 | | return $self->_save_buffer($pageid); |
| 3427 | | } |
| 3428 | | |
| 3429 | | return; |
| | 3434 | =head3 C<on_save_intuition> |
| | 3435 | |
| | 3436 | my $success = $main->on_save_intuition; |
| | 3437 | |
| | 3438 | Try to automatically determine an appropriate file name and save it, |
| | 3439 | based entirely on the content of the file. |
| | 3440 | |
| | 3441 | Only do this for new documents, otherwise behave like a regular save. |
| | 3442 | |
| | 3443 | =cut |
| | 3444 | |
| | 3445 | sub on_save_intuition { |
| | 3446 | my $self = shift; |
| | 3447 | my $document = $self->current->document or return; |
| | 3448 | |
| | 3449 | # We only use Save Intuition for new files |
| | 3450 | unless ( $document->is_new ) { |
| | 3451 | if ( $document->is_saved ) { |
| | 3452 | # Nothing to do |
| | 3453 | return; |
| | 3454 | } else { |
| | 3455 | # Regular save |
| | 3456 | return $self->on_save(@_); |
| | 3457 | } |
| | 3458 | } |
| | 3459 | |
| | 3460 | # Empty files get done via the normal save |
| | 3461 | if ( $document->is_unused ) { |
| | 3462 | return $self->on_save_as(@_); |
| | 3463 | } |
| | 3464 | |
| | 3465 | # We need both a guessed path and file name to do anything |
| | 3466 | my @subpath = $document->guess_subpath; |
| | 3467 | my $filename = $document->guess_filename; |
| | 3468 | unless ( @subpath and defined Params::Util::_STRING($filename) ) { |
| | 3469 | # Cannot come up with a suitable guess |
| | 3470 | return $self->on_save_as(@_); |
| | 3471 | } |
| | 3472 | |
| | 3473 | # Convert the guesses to full paths |
| | 3474 | my $dir = File::Spec->catdir( $document->project_dir, @subpath ); |
| | 3475 | my $path = File::Spec->catfile( $dir, $filename ); |
| | 3476 | if ( -f $path ) { |
| | 3477 | # Potential collision, error and fall back |
| | 3478 | $self->error(Wx::gettext('File already exists')); |
| | 3479 | return $self->on_save_as(@_); |
| | 3480 | } |
| | 3481 | |
| | 3482 | # Create the directory, if needed |
| | 3483 | unless ( -d $dir ) { |
| | 3484 | my $error = [ ]; |
| | 3485 | File::Path::make_path( $dir, { |
| | 3486 | verbose => 0, |
| | 3487 | error => \$error, |
| | 3488 | } ); |
| | 3489 | if ( @$error ) { |
| | 3490 | $self->error(Wx::gettext("Failed to create path '$dir'")); |
| | 3491 | return $self->on_save_as(@_); |
| | 3492 | } |
| | 3493 | } |
| | 3494 | |
| | 3495 | # Save the file |
| | 3496 | $document->_set_filename($path); |
| | 3497 | $document->save_file; |
| | 3498 | $document->set_newline_type(Padre::Constant::NEWLINE); |
| | 3499 | |
| | 3500 | # Laborious copy of the above. |
| | 3501 | # Generalise it later |
| | 3502 | my $pageid = $self->notebook->GetSelection; |
| | 3503 | $self->_save_buffer($pageid); |
| | 3504 | |
| | 3505 | $document->set_mimetype( $document->guess_mimetype ); |
| | 3506 | $document->editor->padre_setup; |
| | 3507 | $document->rebless; |
| | 3508 | $document->colourize; |
| | 3509 | |
| | 3510 | $filename = $document->{file}->filename if defined( $document->{file} ); |
| | 3511 | if ( defined($filename) ) { |
| | 3512 | Padre::DB::History->create( |
| | 3513 | type => 'files', |
| | 3514 | name => $filename, |
| | 3515 | ); |
| | 3516 | $self->menu->file->update_recentfiles; |
| | 3517 | } |
| | 3518 | |
| | 3519 | $self->refresh; |
| | 3520 | |
| | 3521 | return 1; |