36
40
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";
41
"Usage: $app_name [OPTIONS]\n\n".
43
" -a, --all display all sections\n".
44
" -e, --edit edit your todo file\n".
45
" -h, --headers show setion headers\n".
46
" -l, --list show the list in your pager (see notes)\n".
47
" -s, --section=SECTION display this named section\n".
48
" --help display this help and exit\n".
49
" --version output version information and exit\n".
51
"The advantage of using '--list' is that the pager is run as if the\n".
52
"todo list were being edited. This means that if you decide to spawn an\n".
53
"editor from you pager and edit the list, these changes get noticed.\n".
55
"The environment variables EDITOR and PAGER are used.\n".
57
"Please report bugs to Tim Marston <tim\@ed.am>.\n";
49
62
if( $mode_version ) {
50
63
print "todo 1.0\n".
51
"Copyright (C) 2011 Tim Marston.\n".
52
"http://ed.am/software/todo\n";
64
"Copyright (C) 2011, 2012 Tim Marston.\n".
65
"http://ed.am/software/todo\n";
65
78
$todo_dir = glob( $todo_dir );
66
79
( -f $todo_dir ) and die "$todo_dir exists and is a file";
84
( ! -f "$todo_dir/todo" ) && die "no todo file";
71
91
# create the todo directory, as necessary
72
if( ! -d $todo_dir ) {
73
mkdir $todo_dir or die "couldn't create todo directory";
74
`bzr init --no-aliases -q "$todo_dir"`;
75
$? == 0 or die "couldn't init bzr repo";
78
# create a default todo file, as necessary
79
if( ! -f "$todo_dir/todo" ) {
80
open FILE, ">$todo_dir/todo" or die "couldn't create default todo file";
81
my $content = <<"EOT";
92
if( ! -d $todo_dir ) {
93
mkdir $todo_dir or die "couldn't create todo directory";
94
`bzr init --no-aliases -q "$todo_dir"`;
95
$? == 0 or die "couldn't init bzr repo";
98
# create a default todo file, as necessary
99
if( ! -f "$todo_dir/todo" ) {
100
open FILE, ">$todo_dir/todo" or die "couldn't create default todo file";
101
my $content = <<"EOT";
82
102
<!-- This file uses Markdown syntax. For more info about Markdown
83
103
syntax, see http://daringfireball.net/projects/markdown/syntax.
96
116
print FILE $content or die "couldn't write default todo file";
98
`bzr add --no-aliases -q "$todo_dir/todo"`;
99
$? == 0 or die "couldn't add todo file to bzr repo";
118
`bzr add --no-aliases -q "$todo_dir/todo"`;
119
$? == 0 or die "couldn't add todo file to bzr repo";
102
122
# determine editor from environment, default to "emacs -nw"
103
my $editor = $ENV{ 'EDITOR' };
104
defined $editor or $editor = 'emacs -nw';
125
$editor = $ENV{ 'PAGER' };
126
defined $editor or $editor = 'less';
129
$editor = $ENV{ 'EDITOR' };
130
defined $editor or $editor = 'emacs -nw';
105
132
my @exec_array = split( / +/, $editor );
106
133
push( @exec_array, "$todo_dir/todo" );
108
# detect emacs and try to use markdown-mode
109
$exec_array[ 0 ] eq "emacs" and
110
push( @exec_array, '--funcall=markdown-mode' );
135
# detect emacs and try to use markdown-mode
136
$exec_array[ 0 ] eq "emacs" and
137
push( @exec_array, '--funcall=markdown-mode' );
113
140
system( @exec_array );
115
die "can't start editor, check EDITOR envionment variable";
117
# check for changes and commit it
118
$output = `bzr status --no-aliases "$todo_dir/todo"`;
119
$? == 0 or die "couldn't check bzr rerpo status";
121
if( $output ne "" ) {
122
`bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
123
$? == 0 or die "couldn't commit to bzr repo";
142
die "can't start editor, check EDITOR envionment variable";
144
# check for changes and commit it
145
$output = `bzr status --no-aliases "$todo_dir/todo"`;
146
$? == 0 or die "couldn't check bzr rerpo status";
148
if( $output ne "" ) {
149
`bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
150
$? == 0 or die "couldn't commit to bzr repo";
153
# after editing, exit
157
# function to display a line
160
my ( $line, $section ) = @_;
161
state $old_section = '';
163
# detect section change
164
if( $section ne $old_section ) {
165
$old_section = $section;
167
# display section heading
168
if( $display_headers || $display_all ) {
170
( "=" x length( $section ) )."\n";
126
# after editing, exit
174
# replace tabs with 4 spaces
130
182
# scan through file
131
my $next_section = '';
183
my $candidate_section = '';
132
184
my $section = '';
133
my $old_section = '';
134
186
open FILE, "<$todo_dir/todo" or die "can't open todo file";
138
189
# detect the line after section headings, and thus sections
139
if( /^[-=]{2,}/ && $next_section ne '' ) {
140
$section = $next_section;
190
if( /^[-=]{2,}/ && $candidate_section ne '' ) {
191
$section = $candidate_section;
192
$candidate_section = '';
143
197
# detect section headings
144
elsif( /^[A-Z]+$/ ) {
146
chomp( $next_section );
148
# we have neither a section heading nor the line after
198
if( /^[-_\.A-Za-z0-9 ]+$/ ) {
199
$candidate_section = $_;
200
chomp $candidate_section;
153
if( ( $section eq $display_section ) ||
154
( $section && $display_all ) )
156
# detect section change
157
if( $section ne $old_section ) {
158
$old_section = $section;
160
# display section heading
161
if( $display_headers ||
165
( "=" x length( $section ) )."\n";
169
# replace tabs with 4 spaces
203
$candidate_section = '';
207
display_line( $last_line, $section ) if( $last_line ne '' );
210
if( ( lc( $section ) eq lc( $display_section ) ) ||
211
( $section && $display_all ) )
219
display_line( $last_line, $section ) if( $last_line ne '' );