/todo

To get this branch, use:
bzr branch http://bzr.ed.am/todo

« back to all changes in this revision

Viewing changes to todo

  • Committer: edam
  • Date: 2011-12-10 20:53:29 UTC
  • Revision ID: edam@waxworlds.org-20111210205329-7c5gfo6af3m3pz9c
fixed spaces

Show diffs side-by-side

added added

removed removed

34
34
# help mode
35
35
if( $mode_help ) {
36
36
    print "todo - display your todo file\n\n".
37
 
        "Usage: $app_name [OPTIONS]\n\n".
38
 
        "Options:\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";
 
37
    "Usage: $app_name [OPTIONS]\n\n".
 
38
    "Options:\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";
45
45
    exit( 0 );
46
46
}
47
47
 
48
48
# version mode
49
49
if( $mode_version ) {
50
50
    print "todo 1.0\n".
51
 
                "Copyright (C) 2011 Tim Marston.\n".
52
 
                "http://ed.am/software/todo\n";
 
51
        "Copyright (C) 2011 Tim Marston.\n".
 
52
        "http://ed.am/software/todo\n";
53
53
    exit( 0 );
54
54
}
55
55
 
69
69
if( $mode_edit )
70
70
{
71
71
    # 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";
76
 
        }
77
 
        
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";
 
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";
 
76
    }
 
77
    
 
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";
82
82
<!-- This file uses Markdown syntax. For more info about Markdown
83
83
     syntax, see http://daringfireball.net/projects/markdown/syntax.
84
84
 
94
94
 
95
95
EOT
96
96
        print FILE $content or die "couldn't write default todo file";
97
 
                close FILE;
98
 
                `bzr add --no-aliases -q "$todo_dir/todo"`;
99
 
                $? == 0 or die "couldn't add todo file to bzr repo";
100
 
        }
 
97
        close FILE;
 
98
        `bzr add --no-aliases -q "$todo_dir/todo"`;
 
99
        $? == 0 or die "couldn't add todo file to bzr repo";
 
100
    }
101
101
 
102
102
    # determine editor from environment, default to "emacs -nw"
103
103
    my $editor = $ENV{ 'EDITOR' };
105
105
    my @exec_array = split( / +/, $editor );
106
106
    push( @exec_array, "$todo_dir/todo" );
107
107
 
108
 
        # detect emacs and try to use markdown-mode
109
 
        $exec_array[ 0 ] eq "emacs" and
110
 
                push( @exec_array, '--funcall=markdown-mode' );
 
108
    # detect emacs and try to use markdown-mode
 
109
    $exec_array[ 0 ] eq "emacs" and
 
110
        push( @exec_array, '--funcall=markdown-mode' );
111
111
 
112
112
    # edit todo file
113
113
    system( @exec_array );
114
 
        $? == 0 or
115
 
                die "can't start editor, check EDITOR envionment variable";
116
 
 
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";
120
 
        chomp $output;
121
 
        if( $output ne "" ) {
122
 
                `bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
123
 
                $? == 0 or die "couldn't commit to bzr repo";
124
 
        }
125
 
 
126
 
        # after editing, exit
127
 
        exit
 
114
    $? == 0 or
 
115
        die "can't start editor, check EDITOR envionment variable";
 
116
 
 
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";
 
120
    chomp $output;
 
121
    if( $output ne "" ) {
 
122
        `bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
 
123
        $? == 0 or die "couldn't commit to bzr repo";
 
124
    }
 
125
 
 
126
    # after editing, exit
 
127
    exit
128
128
}
129
129
 
130
130
# scan through file
137
137
 
138
138
    # detect the line after section headings, and thus sections
139
139
    if( /^[-=]{2,}/ && $next_section ne '' ) {
140
 
                $section = $next_section;
141
 
                $next_section = '';
 
140
        $section = $next_section;
 
141
        $next_section = '';
142
142
    }
143
143
    # detect section headings
144
144
    elsif( /^[A-Z]+$/ ) {
145
 
                $next_section = $_;
146
 
                chomp( $next_section );
 
145
        $next_section = $_;
 
146
        chomp( $next_section );
147
147
    }
148
148
    # we have neither a section heading nor the line after
149
149
    else {
150
 
                $next_section = '';
151
 
 
152
 
                # display line
153
 
                if( ( $section eq $display_section ) ||
154
 
                        ( $section && $display_all ) )
155
 
                {
156
 
                        # detect section change
157
 
                        if( $section ne $old_section ) {
158
 
                                $old_section = $section;
159
 
                                
160
 
                                # display section heading
161
 
                                if( $display_headers ||
162
 
                                        $display_all )
163
 
                                {
164
 
                                        print "$section\n".
165
 
                                                ( "=" x length( $section ) )."\n";
166
 
                                }
167
 
                        }
168
 
 
169
 
                        # replace tabs with 4 spaces
170
 
                        s/\t/    /g;
171
 
 
172
 
                        # display the line
173
 
                        print;
174
 
                }
 
150
        $next_section = '';
 
151
 
 
152
        # display line
 
153
        if( ( $section eq $display_section ) ||
 
154
            ( $section && $display_all ) )
 
155
        {
 
156
            # detect section change
 
157
            if( $section ne $old_section ) {
 
158
                $old_section = $section;
 
159
                
 
160
                # display section heading
 
161
                if( $display_headers ||
 
162
                    $display_all )
 
163
                {
 
164
                    print "$section\n".
 
165
                        ( "=" x length( $section ) )."\n";
 
166
                }
 
167
            }
 
168
 
 
169
            # replace tabs with 4 spaces
 
170
            s/\t/    /g;
 
171
 
 
172
            # display the line
 
173
            print;
 
174
        }
175
175
    }
176
176
}