]> Joshua Wise's Git repositories - tdl.git/blob - narrow.c
Fall back on index for sort if the due date is the same, not just if both due dates...
[tdl.git] / narrow.c
1 /*
2    $Header: /cvs/src/tdl/narrow.c,v 1.2.2.1 2004/01/07 00:09:05 richard Exp $
3   
4    tdl - A console program for managing to-do lists
5    Copyright (C) 2001-2004  Richard P. Curnow
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20    */
21
22 #include "tdl.h"
23
24 static struct node *narrow_top = NULL;
25 static char *narrow_prefix = NULL;
26
27 struct node *get_narrow_top(void)/*{{{*/
28 {
29   return narrow_top;
30 }
31 /*}}}*/
32 char *get_narrow_prefix(void)/*{{{*/
33 {
34   return narrow_prefix;
35 }
36 /*}}}*/
37 static char *generate_prefix(struct node *x, char *suffix)
38 {
39   int count;
40   struct node *y, *parent;
41   char buffer[16];
42   int suffix_len, count_len;
43   int total_len;
44   char *result;
45   char *new_suffix;
46
47   parent = x->parent;
48   count = 1;
49   if (parent) {
50     for (y = parent->kids.next; y != x; y = y->chain.next) count++;
51   } else {
52     for (y = top.next; y != x; y = y->chain.next) count++;
53   }
54   sprintf(buffer, "%d", count);
55   count_len = strlen(buffer);
56   suffix_len = strlen(suffix);
57   total_len = count_len + suffix_len;
58   if (suffix_len) ++total_len; /* allow for '.' */
59   
60   new_suffix = new_array(char, 1+total_len);
61   strcpy(new_suffix, buffer);
62   if (suffix_len) strcat(new_suffix, ".");
63   strcat(new_suffix, suffix);
64
65   if (parent) {
66     result = generate_prefix(parent, new_suffix);
67     free(new_suffix);
68     return result;
69   } else {
70     return new_suffix;
71   }
72 }
73
74 static void setup_narrow_prefix(void)/*{{{*/
75 {
76   if (narrow_prefix) {
77     free(narrow_prefix);
78     narrow_prefix = NULL;
79   }
80
81   narrow_prefix = generate_prefix(narrow_top, "");
82 }
83 /*}}}*/
84 int process_narrow(char **x)/*{{{*/
85 {
86   int argc;
87   struct node *n;
88
89   argc = count_args(x);
90
91   if (argc < 1) {
92     fprintf(stderr, "Usage : narrow <entry_index>\n");
93     return -1;
94   }
95
96   n = lookup_node(x[0], 0, NULL);
97   if (!n) return -1;
98
99   narrow_top = n;
100
101   /* Compute prefix string. */
102   setup_narrow_prefix();
103
104   return 0;
105 }
106 /*}}}*/
107 int process_widen(char **x)/*{{{*/
108 {
109   int argc;
110   int n_levels;
111   int i;
112   struct node *new_narrow_top;
113   
114   argc = count_args(x);
115   if (argc > 1) {
116     fprintf(stderr, "Usage : widen [<n_levels>]\n");
117     return -1;
118   }
119   if (argc > 0) {
120     if (sscanf(x[0], "%d", &n_levels) != 1) {
121       fprintf(stderr, "Usage : widen [<n_levels>]\n");
122       return -1;
123     }
124   } else {
125     n_levels = 1;
126   }
127
128   new_narrow_top = narrow_top;
129   if (!new_narrow_top) goto widen_to_top;
130
131   for (i=0; i<n_levels; i++) {
132     new_narrow_top = new_narrow_top->parent;
133     if (!new_narrow_top) goto widen_to_top;
134   }
135
136   narrow_top = new_narrow_top;
137   setup_narrow_prefix();
138   return 0;
139
140     /* Widen back to top level */
141 widen_to_top:
142   narrow_top = NULL;
143   if (narrow_prefix) {
144     free(narrow_prefix);
145     narrow_prefix = NULL;
146   }
147
148   return 0;
149 }
150 /*}}}*/
151
This page took 0.029508 seconds and 4 git commands to generate.