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
    "Usage: $app_name [OPTIONS]\n\n".
 
 
41
    "  -a, --all              display all sections\n".
 
 
42
    "  -e, --edit             edit your todo file\n".
 
 
43
    "  -h, --headers          show setion headers\n".
 
 
44
    "      --section=SECTION  display this named section\n".
 
 
45
    "      --help     display this help and exit\n".
 
 
46
    "      --version  output version information and exit\n";
 
49
51
if( $mode_version ) {
 
50
52
    print "todo 1.0\n".
 
51
 
                "Copyright (C) 2011 Tim Marston.\n".
 
52
 
                "http://ed.am/software/todo\n";
 
 
53
        "Copyright (C) 2011 Tim Marston.\n".
 
 
54
        "http://ed.am/software/todo\n";
 
 
71
73
    # 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";
 
 
74
    if( ! -d $todo_dir ) {
 
 
75
        mkdir $todo_dir or die "couldn't create todo directory";
 
 
76
        `bzr init --no-aliases -q "$todo_dir"`;
 
 
77
        $? == 0 or die "couldn't init bzr repo";
 
 
80
    # create a default todo file, as necessary
 
 
81
    if( ! -f "$todo_dir/todo" ) {
 
 
82
        open FILE, ">$todo_dir/todo" or die "couldn't create default todo file";
 
 
83
        my $content = <<"EOT";
 
82
84
<!-- This file uses Markdown syntax. For more info about Markdown
 
83
85
     syntax, see http://daringfireball.net/projects/markdown/syntax.
 
 
96
98
        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";
 
 
100
        `bzr add --no-aliases -q "$todo_dir/todo"`;
 
 
101
        $? == 0 or die "couldn't add todo file to bzr repo";
 
102
104
    # determine editor from environment, default to "emacs -nw"
 
103
105
    my $editor = $ENV{ 'EDITOR' };
 
 
105
107
    my @exec_array = split( / +/, $editor );
 
106
108
    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' );
 
 
110
    # detect emacs and try to use markdown-mode
 
 
111
    $exec_array[ 0 ] eq "emacs" and
 
 
112
        push( @exec_array, '--funcall=markdown-mode' );
 
113
115
    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";
 
 
117
        die "can't start editor, check EDITOR envionment variable";
 
 
119
    # check for changes and commit it
 
 
120
    $output = `bzr status --no-aliases "$todo_dir/todo"`;
 
 
121
    $? == 0 or die "couldn't check bzr rerpo status";
 
 
123
    if( $output ne "" ) {
 
 
124
        `bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
 
 
125
        $? == 0 or die "couldn't commit to bzr repo";
 
 
128
    # after editing, exit
 
 
132
# function to display a line
 
 
135
        my ( $line, $section ) = @_;
 
 
136
        state $old_section = '';
 
 
138
        # detect section change
 
 
139
        if( $section ne $old_section ) {
 
 
140
                $old_section = $section;
 
 
142
                # display section heading
 
 
143
                if( $display_headers || $display_all ) {
 
 
145
                                ( "=" x length( $section ) )."\n";
 
126
 
        # after editing, exit
 
 
149
        # replace tabs with 4 spaces
 
130
157
# scan through file
 
131
 
my $next_section = '';
 
 
158
my $candidate_section = '';
 
132
159
my $section = '';
 
133
 
my $old_section = '';
 
134
161
open FILE, "<$todo_dir/todo" or die "can't open todo file";
 
138
164
    # detect the line after section headings, and thus sections
 
139
 
    if( /^[-=]{2,}/ && $next_section ne '' ) {
 
140
 
                $section = $next_section;
 
 
165
    if( /^[-=]{2,}/ && $candidate_section ne '' ) {
 
 
166
        $section = $candidate_section;
 
 
167
        $candidate_section = '';
 
143
172
    # detect section headings
 
144
 
    elsif( /^[A-Z]+$/ ) {
 
146
 
                chomp( $next_section );
 
148
 
    # we have neither a section heading nor the line after
 
 
173
    if( /^[-_\.A-Za-z0-9 ]+$/ ) {
 
 
174
        $candidate_section = $_;
 
 
175
                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
 
 
178
        $candidate_section = '';
 
 
182
        display_line( $last_line, $section ) if( $last_line ne '' );
 
 
185
        if( ( $section eq $display_section ) ||
 
 
186
                ( $section && $display_all ) )
 
 
194
display_line( $last_line, $section ) if( $last_line ne '' );