App-Pvim

 view release on metacpan or  search on metacpan

pvim  view on Meta::CPAN

    die "Not a regular file: $path\n" unless $input || -f $path;
    my $fh = $input;
    unless ($fh) { open $fh, '<', $path or die "Read $path: $!\n" }
    binmode $fh;
    my $label = $input ? 'standard input' : $path;
    my $digest = Digest::SHA->new(256);
    my ($offset, $start, $prev, $first, $last_term) = (0, 0, '', 1, 0);
    while (1) {
        my $got = read($fh, my $chunk, 65536);
        die "Read $label: $!\n" unless defined $got;
        last unless $got;
        $digest->add($chunk);
        print {$s->{data}} $chunk or die "Temporary data: $!\n";
        while ($chunk =~ /\n/g) {
            my $end = $offset + pos($chunk) - 1;
            my $cr = pos($chunk) > 1 ? substr($chunk, pos($chunk) - 2, 1) eq "\r" : $prev eq "\r";
            my $term = $cr ? 2 : 1;
            $s->_record($start, $end - $start - ($cr ? 1 : 0), $term);
            if ($first) { $s->{eol} = $cr ? "\r\n" : "\n"; $first = 0 }
            $start = $end + 1; $last_term = $term;
        }
        $offset += $got; $prev = substr($chunk, -1);
    }
    unless ($input) { close $fh or die "Close $path: $!\n" }
    if ($start < $offset || !$offset) { $s->_record($start, $offset - $start, 0); $last_term = 0 }
    $s->{final} = $last_term ? 1 : 0;
    $s->{fingerprint} = $digest->hexdigest;
    $s->{pieces} = [[0, $s->{next}]]; $s->reindex;
    if ($input) { $s->{state} = ++$s->{serial}; $s->{fingerprint} = '' }
    return $s;
}
sub write_to {
    my ($s, $out) = @_;
    # Every run has physical newlines internally; its boundary may need a newline.
    for my $p (@{$s->{pieces}}) {
        my ($offset) = $s->record($p->[0]);
        my ($end, $body, $term) = $s->record($p->[0]+$p->[1]-1);
        my $len = $end + $body - $offset;
        _seek($s->{data}, $offset);
        while ($len) {
            my $got = read($s->{data}, my $chunk, $len > 65536 ? 65536 : $len);
            die "Temporary data read failed\n" unless $got;
            print {$out} $chunk or die "Write: $!\n"; $len -= $got;
        }
        my $last = $p->[2] + $p->[1] == $s->{count};
        my $eol = $term == 2 ? "\r\n" : $term == 1 ? "\n" : $s->{eol};
        print {$out} $eol or die "Write: $!\n" if !$last || $s->{final};
    }
}
sub save {
    my ($s, $path, $force) = @_;
    $s->commit;
    $path = $s->{path} unless defined($path) && length($path);
    die "No filename; use :w filename\n" unless defined($path) && length($path);
    die "Refusing to replace a symlink\n" if -l $path;
    die "Not a regular file: $path\n" if -e $path && !-f $path;
    my $same = defined($s->{path}) && File::Spec->rel2abs($path) eq File::Spec->rel2abs($s->{path});
    my $expected = $same ? $s->{fingerprint} : '';
    my $current = fingerprint($path);
    die "File exists or changed on disk; use :w! to overwrite\n" if !$force && $current ne $expected;
    my @stat = stat($path);
    die "File has multiple hard links; refusing atomic replacement\n" if @stat && $stat[3] > 1;
    die "File is read-only\n" if @stat && !($stat[2] & 0222);
    my ($fh, $tmp) = tempfile('.pvim-save-XXXXXX', DIR => dirname(File::Spec->rel2abs($path)), UNLINK => 0);
    binmode $fh;
    my $ok = eval {
        $s->write_to($fh); close($fh) or die "Close output: $!\n";
        my $mode = @stat ? $stat[2] & 0777 : 0666 & ~umask;
        chmod($mode, $tmp) or die "chmod: $!\n";
        die "Target changed during save\n" if -l $path || fingerprint($path) ne $current;
        rename($tmp, $path) or die "Rename: $!\n";
        1;
    };
    my $error = $@;
    unless ($ok) { close $fh; unlink $tmp; die $error }
    $s->{path} = $path; $s->{fingerprint} = fingerprint($path); $s->{saved} = $s->{state};
    return $path;
}

package PVim::Editor;
sub new {
    my ($class, $store) = @_;
    return bless { b => $store, row => 0, col => 0, mode => 'normal', number => 0,
        tabstop => 8, message => '', pending => '', count => '', register => [], page => 20 }, $class;
}
sub clamp {
    my ($e) = @_;
    $e->{row} = 0 if $e->{row} < 0;
    $e->{row} = $e->{b}{count} - 1 if $e->{row} >= $e->{b}{count};
    my $max = length($e->{b}->line($e->{row})) - ($e->{mode} eq 'normal' ? 1 : 0);
    $max = 0 if $max < 0;
    $e->{col} = 0 if $e->{col} < 0; $e->{col} = $max if $e->{col} > $max;
}
sub edit {
    my ($e, $cb) = @_;
    my $b = $e->{b}; $b->{cursor} = [@{$e}{qw(row col)}]; $b->begin;
    my $ok = eval { $cb->(); 1 };
    unless ($ok) { my $err = $@; $b->rollback; die $err }
    $b->commit; $e->clamp;
}
sub insert_mode { my ($e, $mode) = @_; $e->{b}{cursor} = [@{$e}{qw(row col)}]; $e->{b}->begin; $e->{mode} = $mode || 'insert' }
sub undo {
    my ($e,$redo) = @_; my $b = $e->{b}; $b->{cursor} = [@{$e}{qw(row col)}];
    return 0 unless $b->undo($redo); @{$e}{qw(row col)} = @{$b->{cursor}}; $e->clamp; return 1;
}
sub word {
    my ($e, $key, $count) = @_;
    for (1 .. $count) {
        my $line = $e->{b}->line($e->{row}); my $c = $e->{col};
        if ($key eq 'b') {
            if (!$c && $e->{row}) { --$e->{row}; $line = $e->{b}->line($e->{row}); $c = length($line) }
            $c-- if $c; $c-- while $c && substr($line,$c,1) =~ /\s/;
            my $type = substr($line,$c,1) =~ /\w/ ? qr/\w/ : qr/[^\w\s]/;
            $c-- while $c && substr($line,$c-1,1) =~ $type;
        } elsif ($key eq 'w') {
            my $tail = substr($line,$c); $tail =~ /^(?:\w+|[^\w\s]+)?\s*/; $c += length($&);
            if ($c >= length($line) && $e->{row} < $e->{b}{count}-1) {
                ++$e->{row}; $line = $e->{b}->line($e->{row}); $line =~ /^\s*/; $c = length($&);
            }
        } else {
            $c++ if $c < length($line)-1;



( run in 0.734 second using v1.01-cache-2.11-cpan-500dd33cbaf )