=================================================================== RCS file: /cvs/todotxt/Text-Todo/lib/Text/Todo/Entry.pm,v retrieving revision 1.3 retrieving revision 1.11 diff -u -r1.3 -r1.11 --- todotxt/Text-Todo/lib/Text/Todo/Entry.pm 2009/07/10 23:52:08 1.3 +++ todotxt/Text-Todo/lib/Text/Todo/Entry.pm 2010/01/09 07:08:45 1.11 @@ -1,6 +1,6 @@ package Text::Todo::Entry; -# $RedRiver: Entry.pm,v 1.2 2009/07/10 22:28:28 andrew Exp $ +# $RedRiver: Entry.pm,v 1.9 2010/01/08 17:41:56 andrew Exp $ use warnings; use strict; @@ -16,12 +16,38 @@ my %tags_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 ); + }; + } + + sub replace { _update_entry(@_) } + sub new { my ( $class, $text ) = @_; @@ -43,10 +69,11 @@ foreach my $tag ( keys %tags ) { my $symbol = quotemeta $tags{$tag}; - $tags_of{$ident}{$tag} - = { map { $_ => q{} } $text =~ / $symbol (\S+)/gxms }; + $tags_of{$ident}{$tag} = { map { $_ => q{} } + $text =~ / (?:^|\s) $symbol (\S+)/gxms }; } - ( $priority_of{$ident} ) = $text =~ /\( ([A-Z]) \)/ixms; + ( $completion_status_of{$ident}, $priority_of{$ident} ) + = $text =~ / $priority_completion_regex /xms; return 1; } @@ -60,10 +87,8 @@ } sub _is_in { - my ( $self, $type, $item ) = @_; - my $ident = ident($self); - - return defined first { $_ eq $item } $self->$type; + my ( $self, $tags, $item ) = @_; + return defined first { $_ eq $item } $self->$tags; } sub text { @@ -73,53 +98,77 @@ return $text_of{$ident}; } - sub priority { - my ($self) = @_; + sub depri { pri( @_, '' ) } + + sub pri { + my ( $self, $new_pri ) = @_; my $ident = ident($self); - return $priority_of{$ident}; - } + if ( $new_pri !~ /^[a-zA-Z]?$/xms ) { + croak "Invalid priority [$new_pri]"; + } - sub contexts { my ($self) = @_; return $self->_tags('context') } - sub projects { my ($self) = @_; return $self->_tags('project') } + $priority_of{$ident} = $new_pri; - sub in_context { - my ( $self, $context ) = @_; - return $self->_is_in( 'contexts', $context ); + return $self->prepend(); } - sub in_project { - my ( $self, $project ) = @_; - return $self->_is_in( 'projects', $project ); - } + sub priority { + my ( $self, $new_pri ) = @_; + my $ident = ident($self); - sub change { - my ( $self, $text ) = @_; - return $self->_update_entry($text); + return $priority_of{$ident}; } sub prepend { my ( $self, $addition ) = @_; my $new = $self->text; + my @new; - if ( my $priority = $self->priority ) { - $new =~ s/^( \s* \( $priority \))/$1 $addition/xms; + $new =~ s/$priority_completion_regex//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 { 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 __END__ @@ -172,12 +221,19 @@ =head2 in_project -=head2 change +=head2 replace =head2 prepend =head2 append +=head2 do + +=head2 done + +=head2 pri + +=head2 depri =head1 DIAGNOSTICS