36
38
print "todo - display your todo file\n\n".
37
"Usage: $app_name [OPTIONS]\n\n".
39
" -a, --all display all sections\n".
40
" -e, --edit edit your todo file\n".
41
" -h, --headers show setion headers\n".
42
" -s, --section=SECTION display this named section\n".
43
" --help display this help and exit\n".
44
" --version output version information and exit\n";
39
#01234567890123456789012345678901234567890123456789012345678901234567890123456789
40
"Usage: $app_name [OPTIONS]\n\n".
42
" -a, --all display all sections\n".
43
" -e, --edit edit your todo file\n".
44
" -h, --headers show setion headers\n".
45
" -s, --section=SECTION display sections matching the regular expression\n".
46
" --help display this help and exit\n".
47
" --version output version information and exit\n".
49
"Running without any options is the same as running with --section=TODO and\n".
50
"lists the default section of the todo file.\n".
52
"The environment variables EDITOR is used.\n".
54
"Please report bugs to Tim Marston <tim\@ed.am>.\n";
49
59
if( $mode_version ) {
51
"Copyright (C) 2011 Tim Marston.\n";
61
"Copyright (C) 2011, 2012 Tim Marston.\n".
62
"http://ed.am/software/todo\n";
70
81
# create the todo directory, as necessary
71
if( ! -d $todo_dir ) {
72
mkdir $todo_dir or die "couldn't create todo directory";
73
`bzr init --no-aliases -q "$todo_dir"`;
74
$? == 0 or die "couldn't init bzr repo";
76
$? == 0 or die "couldn't create initial todo file";
77
`bzr add --no-aliases -q "$todo_dir/todo"`;
78
$? == 0 or die "couldn't add todo file to bzr repo";
82
if( ! -d $todo_dir ) {
83
mkdir $todo_dir or die "couldn't create todo directory";
84
`bzr init --no-aliases -q "$todo_dir"`;
85
$? == 0 or die "couldn't init bzr repo";
88
# create a default todo file, as necessary
89
if( ! -f "$todo_dir/todo" ) {
90
open FILE, ">$todo_dir/todo" or die "couldn't create default todo file";
91
my $content = <<"EOT";
92
<!-- This file uses Markdown syntax. For more info about Markdown
93
syntax, see http://daringfireball.net/projects/markdown/syntax.
95
There should be a main H1 header called "TODO" for the main todo
96
list section (one has been added for you below). You can also add
97
as many more sections as you like for other lists. And feel free
98
to delete this comment! -->
103
* make a list of things to do
106
print FILE $content or die "couldn't write default todo file";
108
`bzr add --no-aliases -q "$todo_dir/todo"`;
109
$? == 0 or die "couldn't add todo file to bzr repo";
81
112
# determine editor from environment, default to "emacs -nw"
82
my $editor = $ENV{ 'EDITOR' };
83
defined $editor or $editor = 'emacs -nw';
114
$editor = $ENV{ 'EDITOR' };
115
defined $editor or $editor = 'emacs -nw';
84
116
my @exec_array = split( / +/, $editor );
85
117
push( @exec_array, "$todo_dir/todo" );
87
# detect emacs and try to use markdown-mode
88
$exec_array[ 0 ] eq "emacs" and push( @exec_array, '--funcall=markdown-mode' );
119
# detect emacs and try to use markdown-mode
120
$exec_array[ 0 ] eq "emacs" and
121
push( @exec_array, '--funcall=markdown-mode' );
91
124
system( @exec_array );
92
$? == 0 or die "unable to exec editor";
94
# check for changes and commit it
95
$output = `bzr status --no-aliases "$todo_dir/todo"`;
96
$? == 0 or die "couldn't check bzr rerpo status";
99
`bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
100
$? == 0 or die "couldn't commit to bzr repo";
126
die "can't start editor, check EDITOR envionment variable";
128
# check for changes and commit it
129
$output = `bzr status --no-aliases "$todo_dir/todo"`;
130
$? == 0 or die "couldn't check bzr rerpo status";
132
if( $output ne "" ) {
133
`bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
134
$? == 0 or die "couldn't commit to bzr repo";
137
# after editing, exit
141
# function to display a line
144
my ( $line, $section ) = @_;
145
state $old_section = '';
147
# detect section change
148
if( $section ne $old_section ) {
149
$old_section = $section;
151
# display section heading
152
if( $display_headers || $display_all ) {
154
( "=" x length( $section ) )."\n";
158
# replace tabs with 4 spaces
107
166
# scan through file
108
my $next_section = '';
167
my $candidate_section = '';
109
168
my $section = '';
110
my $old_section = '';
111
170
open FILE, "<$todo_dir/todo" or die "can't open todo file";
115
173
# detect the line after section headings, and thus sections
116
if( /^[-=]{2,}/ && $next_section ne '' ) {
117
$section = $next_section;
174
if( /^[-=]{2,}/ && $candidate_section ne '' ) {
175
$section = $candidate_section;
176
$candidate_section = '';
120
181
# detect section headings
121
elsif( /^[A-Z]+$/ ) {
123
chomp( $next_section );
125
# we have neither a section heading nor the line after
182
if( /^[-_\.A-Za-z0-9 ]+$/ ) {
183
$candidate_section = $_;
184
chomp $candidate_section;
130
if( $section eq $display_section ||
133
# detect section change
134
if( $section ne $old_section ) {
135
$old_section = $section;
137
# display section heading
138
if( $display_headers ||
141
my $heading = 'TODO';
142
$heading .= ' ('.lc( $section ).')' if $section ne "IMMEDIATE";
144
( "=" x length( $heading ) )."\n";
148
# replace tabs with 4 spaces
187
$candidate_section = '';
191
display_line( $last_line, $section ) if( $last_line ne '' );
194
if( ( lc( $section ) eq lc( $display_section ) ) ||
195
( $section && $display_all ) )
203
display_line( $last_line, $section ) if( $last_line ne '' );