AnyEvent-EditText

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for AnyEvent-EditText

0.2 Tue Apr 29 18:12:39 CEST 2008
        - actually implemented set_editor
        - rename to AnyEvent::EditText

0.1 Thu Mar 20 10:18:52 CET 2008
        - first implementation

META.yml  view on Meta::CPAN

--- #YAML:1.0
name:                AnyEvent-EditText
version:             0.2
abstract:            An easy way to startup a text editor
license:             perl
author:              
    - Robin Redeker <elmex@ta-sa.org>
generated_by:        ExtUtils::MakeMaker version 6.42
distribution_type:   module
requires:     
    AnyEvent:                      0
    File::Temp:                    0
    Test::More:                    0
meta-spec:

README  view on Meta::CPAN

NAME
    AnyEvent::EditText - An easy way to startup a text editor

VERSION
    Version 0.2

SYNOPSIS
       my $content = "Hello There!";

       AnyEvent::EditText::edit ($content, sub {
          my ($newcontent, $has_changed) = @_;

          if ($has_changed) {
             print "the content was edited";
          }
       });

DESCRIPTION
    This little module will start a text editor in a seperate process
    without stopping the current process. Usually something like a terminal
    with a vim instance running in it will be started, but also a graphical
    editor could be used (like *gedit* or *gvim*).

    The editor will get the content passed to the "edit" routine as
    temporary file, and after you are done editing it (closed the editor)
    the callback will be called with the possibly new content.

FUNCTIONS
  set_editor (@sysargs)
    This function configures the editor used. @sysargs is a list of
    arguments for the "system" function, which will be called like this by
    "edit":

       system (@sysargs, $filename);

    The default editor used will be:

       AnyEvent::EditText::set_editor ("rxvt", "-e", "vim");

  edit ($content, $callback)
    This routine will write $content to a temporary file, fork and call the
    editing process. After the process terminates the temporary file is read
    and erased.

    After that the content is sent back to the calling process, where the
    $callback is called with two arguments: The first will be the new
    content and the second a flag indicating whether the content has
    changed.

AUTHOR
    Robin Redeker, "<elmex at ta-sa.org>"

TODO
    This module should probably first look in the environment to determine
    which editor and terminal to use. This will be fixed in the next
    release.

BUGS
    Please report any bugs or feature requests to "bug-text-edit at
    rt.cpan.org", or through the web interface at
    <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Edit>. I will be
    notified, and then you'll automatically be notified of progress on your
    bug as I make changes.

SUPPORT
    You can find documentation for this module with the perldoc command.

        perldoc AnyEvent::EditText

lib/AnyEvent/EditText.pm  view on Meta::CPAN

use IO::Handle;
use File::Temp qw/tempfile/;
use AnyEvent;

our %PIDS;
our %READER;
our @EDITOR = ("rxvt", "-e", "vim");

=head1 NAME

AnyEvent::EditText - An easy way to startup a text editor

=head1 VERSION

Version 0.2

=cut

our $VERSION = '0.2';

=head1 SYNOPSIS

   my $content = "Hello There!";

   AnyEvent::EditText::edit ($content, sub {
      my ($newcontent, $has_changed) = @_;

      if ($has_changed) {
         print "the content was edited";
      }
   });

=head1 DESCRIPTION

This little module will start a text editor in a seperate process without
stopping the current process. Usually something like a terminal with a vim
instance running in it will be started, but also a graphical editor could be
used (like I<gedit> or I<gvim>).

The editor will get the content passed to the C<edit> routine as temporary
file, and after you are done editing it (closed the editor) the callback
will be called with the possibly new content.

=head1 FUNCTIONS

=head2 set_editor (@sysargs)

This function configures the editor used. C<@sysargs> is a list of
arguments for the C<system> function, which will be called like this
by C<edit>:

   system (@sysargs, $filename);

The default editor used will be:

   AnyEvent::EditText::set_editor ("rxvt", "-e", "vim");

=cut

sub set_editor {
   @EDITOR = @_;
}

=head2 edit ($content, $callback)

This routine will write C<$content> to a temporary file, fork
and call the editing process. After the process terminates the
temporary file is read and erased.

After that the content is sent back to the calling process, where the
C<$callback> is called with two arguments: The first will be the new content
and the second a flag indicating whether the content has changed.

=cut

sub edit {
   my ($content, $finish) = @_;
   pipe (my $par_rdr, my $child_wtr);
   $par_rdr->autoflush (1);
   $child_wtr->autoflush (1);

   my $pid;
   if ($pid = fork) {
      $child_wtr->close;

      my $buffer = '';

lib/AnyEvent/EditText.pm  view on Meta::CPAN

         }
      });

      $PIDS{$pid} = AnyEvent->child (pid => $pid, cb => sub {
         delete $PIDS{$pid};
      });

   } else {
      $par_rdr->close;
      die "couldn't fork: $!" unless defined $pid;
      my ($fh, $filename) = tempfile ("text_edit_XXXXX", DIR => "/tmp");
      print $fh $content;
      close $fh;

      my $ex = system (@EDITOR, $filename);
      unless ($ex == 0) {
         my $err;
         if ($? == -1) {
            $err = "system call failed: $!\n";
         } elsif ($? & 127) {
            $err = sprintf "system call died with signal %d, %s coredump\n",

lib/AnyEvent/EditText.pm  view on Meta::CPAN

   }
}

=head1 AUTHOR

Robin Redeker, C<< <elmex at ta-sa.org> >>

=head1 TODO

This module should probably first look in the environment to determine
which editor and terminal to use. This will be fixed in the next release.

=head1 BUGS

Please report any bugs or feature requests to
C<bug-text-edit at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Edit>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc AnyEvent::EditText

samples/simple_example  view on Meta::CPAN

my $timer; $timer = sub {
   $t = AnyEvent->timer (after => 1, cb => sub {
      print "Tick " . ($cnt++) . "\n";;
      $timer->();
   });
};
$timer->();

my $content = "This is\nA simple\nExample!\n";

AnyEvent::EditText::set_editor ('rxvt', '-e', 'vim');

# The interesting stuff happens here:
AnyEvent::EditText::edit ($content, sub {
   my ($newcontent, $has_changed) = @_;

   if ($has_changed) {
      print "Content was changed:\n$newcontent";
   } else {
      print "No change has been made!\n";
   }

   $condvar->broadcast;
});



( run in 0.323 second using v1.01-cache-2.11-cpan-de7293f3b23 )