[BACK]Return to Entry.pm CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo / lib / Text / Todo

Diff for /todotxt/Text-Todo/lib/Text/Todo/Entry.pm between version 1.1 and 1.9

version 1.1, 2009/07/10 23:26:14 version 1.9, 2010/01/08 17:41:56
Line 1 
Line 1 
 package Text::Todo::Entry;  package Text::Todo::Entry;
   
 # $RedRiver$  # $RedRiver: Entry.pm,v 1.8 2010/01/08 04:50:41 andrew Exp $
   
 use warnings;  use warnings;
 use strict;  use strict;
Line 14 
Line 14 
 {  {
     my %text_of;      my %text_of;
   
     my %contexts_of;      my %tags_of;
     my %projects_of;  
     my %priority_of;      my %priority_of;
       my %completion_status_of;
   
       my %tags = (
           context => q{@},
           project => q{+},
       );
   
       # XXX Should the completion (x) be case sensitive?
       my $priority_completion_regex = qr/
           ^ \s*
           (?i:   (x)        \s+)?
           (?i:\( ([A-Z]) \) \s+)?
       /xms;
   
       for my $tag ( keys %tags ) {
           ## no critic strict
           no strict 'refs';    # Violates use strict, but allows code generation
           ## use critic
   
           *{ $tag . 's' } = sub {
               my ($self) = @_;
               return $self->_tags($tag);
           };
   
           *{ 'in_' . $tag } = sub {
               my ( $self, $item ) = @_;
               return $self->_is_in( $tag . 's', $item );
           };
       }
   
       # Aliases
       sub change  { _update_entry(@_) }
       sub depri   { _set_priority( @_, '' ) }
       sub pri     { priority(@_) }
       sub replace { _update_entry(@_) }
   
     sub new {      sub new {
         my ( $class, $text ) = @_;          my ( $class, $text ) = @_;
   
Line 37 
Line 71 
   
         $text_of{$ident} = $text;          $text_of{$ident} = $text;
   
         %{ $contexts_of{$ident} } = map { $_ => q{} } $text =~ /\@ (\S+)/gxms;          foreach my $tag ( keys %tags ) {
         %{ $projects_of{$ident} } = map { $_ => q{} } $text =~ /\+ (\S+)/gxms;              my $symbol = quotemeta $tags{$tag};
         ( $priority_of{$ident} ) = $text =~ /\( ([A-Z]) \)/ixms;              $tags_of{$ident}{$tag} = { map { $_ => q{} }
                       $text =~ / (?:^|\s) $symbol  (\S+)/gxms };
           }
           ( $completion_status_of{$ident}, $priority_of{$ident} )
               = $text =~ / $priority_completion_regex /xms;
   
         return 1;          return 1;
     }      }
   
     sub text {      sub _tags {
         my ($self) = @_;          my ( $self, $tag ) = @_;
         my $ident = ident($self);          my $ident = ident($self);
   
         return $text_of{$ident};          my @tags = sort keys %{ $tags_of{$ident}{$tag} };
           return wantarray ? @tags : \@tags;
     }      }
   
     sub contexts {      sub _is_in {
           my ( $self, $tags, $item ) = @_;
           return defined first { $_ eq $item } $self->$tags;
       }
   
       sub text {
         my ($self) = @_;          my ($self) = @_;
         my $ident = ident($self);          my $ident = ident($self);
   
         return sort keys %{ $contexts_of{$ident} };          return $text_of{$ident};
     }      }
   
     sub in_context {      sub _set_priority {
         my ( $self, $context ) = @_;          my ( $self, $new_pri ) = @_;
         return $self->_is_in( $context, 'contexts' );  
     }  
   
     sub projects {  
         my ($self) = @_;  
         my $ident = ident($self);          my $ident = ident($self);
   
         return sort keys %{ $projects_of{$ident} };          if ( $new_pri !~ /^[a-zA-Z]?$/xms ) {
     }              croak "Invalid priority [$new_pri]";
           }
   
     sub in_project {          $priority_of{$ident} = $new_pri;
         my ( $self, $project ) = @_;  
         return $self->_is_in( $project, 'projects' );          return $self->prepend();
     }      }
   
     sub priority {      sub priority {
         my ($self) = @_;          my ( $self, $new_pri ) = @_;
         my $ident = ident($self);          my $ident = ident($self);
   
           if ($new_pri) {
               return $self->_set_priority($new_pri);
           }
   
         return $priority_of{$ident};          return $priority_of{$ident};
     }      }
   
     sub _is_in {  
         my ( $self, $item, $type ) = @_;  
         my $ident = ident($self);  
   
         return defined first { $_ eq $item } $self->$type;  
     }  
   
     sub change {  
         my ( $self, $text ) = @_;  
         return $self->_update_entry($text);  
     }  
   
     sub prepend {      sub prepend {
         my ( $self, $addition ) = @_;          my ( $self, $addition ) = @_;
   
         my $new = $self->text;          my $new = $self->text;
           my @new;
   
         if ( my $priority = $self->priority ) {          $new =~ s/$priority_completion_regex//xms;
             $new =~ s/^( \s* \( $priority \))/$1 $addition/xms;  
           if ( $self->done) {
               push @new, $self->done;
         }          }
         else {  
             $new = join q{ }, $addition, $new;          if ( $self->priority ) {
               push @new, '(' . $self->priority . ')';
         }          }
   
         return $self->change($new);          if ( defined $addition && length $addition ) {
               push @new, $addition;
           }
   
           return $self->_update_entry( join q{ }, @new, $new );
     }      }
   
     sub append {      sub append {
         my ( $self, $addition ) = @_;          my ( $self, $addition ) = @_;
         return $self->change( join q{ }, $self->text, $addition );          return $self->_update_entry( join q{ }, $self->text, $addition );
     }      }
   
 }      sub do {
           my ($self) = @_;
           my $ident = ident($self);
   
           if ( $self->done) {
               return 1;
           }
   
           $completion_status_of{$ident} = 'x';
   
           return $self->prepend();
       }
   
       sub done {
           my ($self) = @_;
           my $ident = ident($self);
   
           return $completion_status_of{$ident};
       }
   
   }
 1;    # Magic true value required at end of module  1;    # Magic true value required at end of module
 __END__  __END__
   
Line 173 
Line 232 
 =head2 prepend  =head2 prepend
   
 =head2 append  =head2 append
   
   =head2 do
   
   =head2 done
   
   
 =head1 DIAGNOSTICS  =head1 DIAGNOSTICS

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.9

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>