]>
code.delx.au - gnu-emacs-elpa/blob - packages/notes-mode/mknew
5 # $Id: mknew,v 1.19 2006/01/14 18:28:41 johnh Exp $
7 # Copyright (C) 1996,2012 Free Software Foundation, Inc.
8 # Comments to <johnh@isi.edu>.
10 # This file is under the Gnu Public License, version 2.
11 # For details see the COPYING which accompanies this distribution.
19 Create a new notes file by cloning the most recent date.
20 Output goes to stdout.
22 This program makes several assumptions about the notes-file format.
25 1. Before the first real entry, lines of the form
26 "12-Jan-96 Friday" and "12 Jan 1996" are updated.
28 2. A "today" entry is brought forward each day.
29 (Some people use this as a to-do list.)
31 3. If an entry named according to the day of the week exists, a new
36 We assume that notes are created on the day that they correspond to.
37 The date is not inferred from the filename.
39 Known non-bug: this program is Y2K OK.
45 &usage
if ($#ARGV == -1 || ($#ARGV >= 0 && $ARGV[0] eq '-?'));
49 BEGIN { unshift(@INC, $ENV{'NOTES_BIN_DIR'}); };
52 use POSIX
qw(strftime);
56 # xxx: dumb arg parsing
58 if ($ARGV[0] eq '-c') {
62 &usage
if ($#ARGV != 0);
64 my($date_epoch) = pathname_to_epoch
($date);
65 my($name, $path) = fileparse
($date);
71 my(@days, @months, @short_days, @short_months, $all_days_regexp_switch, $all_months_regexp_switch);
74 sub generate_constants
{
75 # this stuff is based on the suggestion in perllocale(1)
76 # The junk at the end is an list that is struct tm;
77 # things are hardcoded to year 106 == 2006 since Jan 1 is nicely on a Sunday.
79 push(@days, strftime
("%A", 1,0,0,$_+1,0, 106,$_));
80 push(@short_days, strftime
("%a", 1,0,0,$_+1,0, 106,$_));
83 push(@months, strftime
("%B", 1,0,0,1,$_, 106));
84 push(@short_months, strftime
("%b", 1,0,0,1,$_, 106));
86 $all_days_regexp_switch = join("|", @days, @short_days);
87 $all_months_regexp_switch = join("|", @months, @short_months);
90 my($prev) = &figure_prev
($name, $path);
93 print "mknew.cache 830494922\n$prev\n$date\n";
95 my($prev_notes) = new Notes
($prev);
103 my($name, $path) = @_;
105 # Given ${name,path}form, back-compute noon of the current date.
106 my($epoch) = &pathname_to_epoch
("$path/$name");
109 # search back up to a year
110 for ($tries = 0; $tries < 365; $tries++) {
111 my($newpathname) = &epoch_to_pathname
($epoch);
112 # print "$newpathname\n";
113 return $newpathname if (-f
$newpathname);
114 $epoch -= 24 * 60 * 60;
117 # die("$0: could not find prior note.\n");
121 my($note, $title) = @_;
122 $note =~ s/\nprev: <.*>\nnext: <.*>\n/\n/m;
123 $note =~ s/\* .*\n-+\n//m if ($title);
129 return '' if ($sample eq '');
130 return '%a' if (length($sample) == 3);
134 sub infer_month_form
{
136 return '' if ($sample eq '');
137 return '%b' if (length($sample) == 3);
141 sub infer_year_form
{
143 return '' if ($sample eq '');
144 return '%y' if (length($sample) == 2);
149 my($prev_notes) = @_;
150 my($pre) = $prev_notes->prelude();
154 # Case 1: dates at the beginning
155 # This convetion in the format ``30-Apr-96 Tuesday'' is in use by johnh,
156 # and in the format ``30 Apr 1996'' by geoff.
158 # Case 1a: DayName? DayNum Month Year DayName?
159 @F = ($pre =~ /[\s
\n]?
160 ($all_days_regexp_switch)?
(\W
+)?
162 ($all_months_regexp_switch)(\W
+)
164 (\W
+)?
($all_days_regexp_switch)?
[\n]
169 # Sigh. Back-infer date format.
171 $F[$_] = '' if (!defined($F[$_]));
174 $form = &infer_day_form
($F[0]) . $F[1] .
176 &infer_month_form
($F[4]) . $F[5] .
177 &infer_year_form
($F[6]) .
178 $F[7] . &infer_day_form
($F[8]);
179 # This next (bogus) line works around
180 # a bug in redhat 5.0's perl-5.004-2.
181 my($x) = sprintf("%x", 10);
182 # print STDERR "mknew: 1a1b\n";
183 my($new_date) = strftime_epoch
($form, $date_epoch);
184 # Hack to fix leading zeros.
185 # strftime should support something like %!0d.
186 if ($form =~ /^%d/m && $new_date =~ /^0\d/m) {
187 $new_date =~ s/^0//m;
189 print "\n$new_date\n";
190 print "" . ("-" x
length($new_date))
194 # Sigh, reverse month and DayNum
195 # Case 1b: DayName? Month DayNum Year DayName?
196 @F = ($pre =~ /[\s
\n]?
197 ($all_days_regexp_switch)?
(\W
+)?
198 ($all_months_regexp_switch)(\W
+)
201 (\W
+)?
($all_days_regexp_switch)?
[\n]
206 # Sigh. Back-infer date format.
208 $F[$_] = '' if (!defined($F[$_]));
211 $form = &infer_day_form
($F[0]) . $F[1] .
212 &infer_month_form
($F[2]) . $F[3] .
214 &infer_year_form
($F[6]) .
215 $F[7] . &infer_day_form
($F[8]);
216 my($new_date) = strftime_epoch
($form, $date_epoch);
217 print "\n$new_date\n";
218 print "" . ("-" x
length($new_date)) . "\n\n"
224 # Case 2: the "today" entry.
225 # This convention is in use by johnh.
227 my(@todays) = $prev_notes->by_subject('Today');
229 die ("Too many today entries.\n")
231 print sanitize_note
($todays[0], 0);
235 # Case 3: a day-of-the-week entry.
236 # This convention is in use by geoff.
240 my(@entries) = $prev_notes->by_subject($i);
241 if ($#entries != -1) {
242 # Generate a raw entry; don't bother to move forward contents.
243 my($t) = "* " . strftime_epoch
("%A", $date_epoch);
249 sanitize_note
($entries[0], 1);