/todo

To get this branch, use:
bzr branch http://bzr.ed.am/todo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use File::Basename;

# get app name
my $app_name = basename( $0 );

# option defaults
my $todo_dir = "~/.todo";
my $display_all = 0;
my $display_section = "TODO";
my $display_headers = 0;
my $mode_edit = 0;
my $mode_help = 0;
my $mode_version = 0;

# parse command line opts
Getopt::Long::Configure( 'gnu_getopt' );
if( !GetOptions(
    'a|all' => \$display_all,
    'e|edit' => \$mode_edit,
    'h|headers' => \$display_headers,
    'section=s' => \$display_section,
    'help' => \$mode_help,
    'version' => \$mode_version,
) ) {
    print "Try `$app_name --help' for more information.\n";
    exit( 1 );
}

# help mode
if( $mode_help ) {
    print "todo - display your todo file\n\n".
	"Usage: $app_name [OPTIONS]\n\n".
	"Options:\n".
	"  -a, --all              display all sections\n".
	"  -e, --edit             edit your todo file\n".
	"  -h, --headers          show setion headers\n".
	"  -s, --section=SECTION  display this named section\n".
	"      --help     display this help and exit\n".
	"      --version  output version information and exit\n";
    exit( 0 );
}

# version mode
if( $mode_version ) {
    print "todo 1.0\n".
		"Copyright (C) 2011 Tim Marston.\n".
		"http://ed.am/software/todo\n";
    exit( 0 );
}

#_______________________________________________________________________________


# check we have bazaar installed
my $output = `which bzr`;
chomp $output;
$output eq "" and die "Bazaar is not installed";

# glob todo directory
$todo_dir = glob( $todo_dir );
( -f $todo_dir ) and die "$todo_dir exists and is a file";

# edit mode
if( $mode_edit )
{
    # create the todo directory, as necessary
	if( ! -d $todo_dir ) {
		mkdir $todo_dir or die "couldn't create todo directory";
		`bzr init --no-aliases -q "$todo_dir"`;
		$? == 0 or die "couldn't init bzr repo";
	}
	
	# create a default todo file, as necessary
	if( ! -f "$todo_dir/todo" ) {
		open FILE, ">$todo_dir/todo" or die "couldn't create default todo file";
		my $content = <<"EOT";
<!-- This file uses Markdown syntax. For more info about Markdown
     syntax, see http://daringfireball.net/projects/markdown/syntax.

     There should be a main H1 header called "TODO" for the main todo
     list section (one has been added for you below). You can also add
     as many more sections as you like for other lists. And feel free
     to delete this comment! -->

TODO
====

* make a list of things to do

EOT
        print FILE $content or die "couldn't write default todo file";
		close FILE;
		`bzr add --no-aliases -q "$todo_dir/todo"`;
		$? == 0 or die "couldn't add todo file to bzr repo";
	}

    # determine editor from environment, default to "emacs -nw"
    my $editor = $ENV{ 'EDITOR' };
    defined $editor or $editor = 'emacs -nw';
    my @exec_array = split( / +/, $editor );
    push( @exec_array, "$todo_dir/todo" );

	# detect emacs and try to use markdown-mode
	$exec_array[ 0 ] eq "emacs" and
		push( @exec_array, '--funcall=markdown-mode' );

    # edit todo file
    system( @exec_array );
	$? == 0 or
		die "can't start editor, check EDITOR envionment variable";

	# check for changes and commit it
	$output = `bzr status --no-aliases "$todo_dir/todo"`;
	$? == 0 or die "couldn't check bzr rerpo status";
	chomp $output;
	if( $output ne "" ) {
		`bzr commit --no-aliases -q -m - "$todo_dir/todo"`;
		$? == 0 or die "couldn't commit to bzr repo";
	}

	# after editing, exit
	exit
}

# scan through file
my $next_section = '';
my $section = '';
my $old_section = '';
open FILE, "<$todo_dir/todo" or die "can't open todo file";
while( <FILE> ) {
    my $display = 0;

    # detect the line after section headings, and thus sections
    if( /^[-=]{2,}/ && $next_section ne '' ) {
		$section = $next_section;
		$next_section = '';
    }
    # detect section headings
    elsif( /^[A-Z]+$/ ) {
		$next_section = $_;
		chomp( $next_section );
    }
    # we have neither a section heading nor the line after
    else {
		$next_section = '';

		# display line
		if( ( $section eq $display_section ) ||
			( $section && $display_all ) )
		{
			# detect section change
			if( $section ne $old_section ) {
				$old_section = $section;
				
				# display section heading
				if( $display_headers ||
					$display_all )
				{
					print "$section\n".
						( "=" x length( $section ) )."\n";
				}
			}

			# replace tabs with 4 spaces
			s/\t/    /g;

			# display the line
			print;
		}
    }
}