-
Notifications
You must be signed in to change notification settings - Fork 0
/
narrow.c
151 lines (126 loc) · 3.3 KB
/
narrow.c
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
/*
$Header: /cvs/src/tdl/narrow.c,v 1.2.2.1 2004/01/07 00:09:05 richard Exp $
tdl - A console program for managing to-do lists
Copyright (C) 2001-2004 Richard P. Curnow
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "tdl.h"
static struct node *narrow_top = NULL;
static char *narrow_prefix = NULL;
struct node *get_narrow_top(void)/*{{{*/
{
return narrow_top;
}
/*}}}*/
char *get_narrow_prefix(void)/*{{{*/
{
return narrow_prefix;
}
/*}}}*/
static char *generate_prefix(struct node *x, char *suffix)
{
int count;
struct node *y, *parent;
char buffer[16];
int suffix_len, count_len;
int total_len;
char *result;
char *new_suffix;
parent = x->parent;
count = 1;
if (parent) {
for (y = parent->kids.next; y != x; y = y->chain.next) count++;
} else {
for (y = top.next; y != x; y = y->chain.next) count++;
}
sprintf(buffer, "%d", count);
count_len = strlen(buffer);
suffix_len = strlen(suffix);
total_len = count_len + suffix_len;
if (suffix_len) ++total_len; /* allow for '.' */
new_suffix = new_array(char, 1+total_len);
strcpy(new_suffix, buffer);
if (suffix_len) strcat(new_suffix, ".");
strcat(new_suffix, suffix);
if (parent) {
result = generate_prefix(parent, new_suffix);
free(new_suffix);
return result;
} else {
return new_suffix;
}
}
static void setup_narrow_prefix(void)/*{{{*/
{
if (narrow_prefix) {
free(narrow_prefix);
narrow_prefix = NULL;
}
narrow_prefix = generate_prefix(narrow_top, "");
}
/*}}}*/
int process_narrow(char **x)/*{{{*/
{
int argc;
struct node *n;
argc = count_args(x);
if (argc < 1) {
fprintf(stderr, "Usage : narrow <entry_index>\n");
return -1;
}
n = lookup_node(x[0], 0, NULL);
if (!n) return -1;
narrow_top = n;
/* Compute prefix string. */
setup_narrow_prefix();
return 0;
}
/*}}}*/
int process_widen(char **x)/*{{{*/
{
int argc;
int n_levels;
int i;
struct node *new_narrow_top;
argc = count_args(x);
if (argc > 1) {
fprintf(stderr, "Usage : widen [<n_levels>]\n");
return -1;
}
if (argc > 0) {
if (sscanf(x[0], "%d", &n_levels) != 1) {
fprintf(stderr, "Usage : widen [<n_levels>]\n");
return -1;
}
} else {
n_levels = 1;
}
new_narrow_top = narrow_top;
if (!new_narrow_top) goto widen_to_top;
for (i=0; i<n_levels; i++) {
new_narrow_top = new_narrow_top->parent;
if (!new_narrow_top) goto widen_to_top;
}
narrow_top = new_narrow_top;
setup_narrow_prefix();
return 0;
/* Widen back to top level */
widen_to_top:
narrow_top = NULL;
if (narrow_prefix) {
free(narrow_prefix);
narrow_prefix = NULL;
}
return 0;
}
/*}}}*/