page.c
5 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2005 Free Software Foundation, Inc.
GNU Mailutils 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, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include "mail.h"
size_t top_of_page = 1; /* Number of the topmost message on the page */
size_t cursor; /* Number of current message */
size_t *page_map; /* Array of message numbers. page_map[N] holds
number of the message occupying Nth line on
the screen */
unsigned page_size; /* Capacity of page_map */
unsigned page_avail; /* First non-used entry in page map. Can be
equal to page_size */
/* Auxiliary function: Store number of message from mspec into page_map */
static int
_fill_map (msgset_t *mspec, message_t msg, void *data)
{
unsigned *pos = data;
page_map[*pos] = mspec->msg_part[0];
++*pos;
return 0;
}
/* Fill page_map.
page_avail must be set to zero before calling this function */
static void
fill_page_map ()
{
util_range_msg (top_of_page, page_size,
MSG_COUNT|MSG_NODELETED|MSG_SILENT, _fill_map,
&page_avail);
}
/* Check if the page_map is valid. If not, fill it.
Page_avail can be set to zero to force re-filling page_map. In particular
this happens when deleting current message or when SIGWINCH is delivered
to the program */
static void
check_page_map ()
{
if (!page_map)
{
page_size = util_screen_lines ();
page_map = xrealloc (page_map, sizeof (page_map[0]) * page_size);
page_avail = 0;
}
if (page_avail == 0)
fill_page_map ();
}
/* Invalidate page_map. HARD=1 means 'hard invalidation', implying
the need to reallocate the array */
void
page_invalidate (int hard)
{
page_avail = 0;
if (hard)
{
free (page_map);
page_map = NULL;
}
}
/* Invalidate page_map if VALUE is number of the current message */
void
cond_page_invalidate (size_t value)
{
unsigned i;
if (page_map == NULL || page_avail == 0)
return;
for (i = 0; i < page_avail-1; i++)
if (page_map[i] >= value && value <= page_map[i+1])
{
page_invalidate (0);
return;
}
}
/* Return a 1-based index of page_map entry occupied by number VALUE.
Return 0 if VALUE is not found in page_map */
static int
page_line (size_t value)
{
unsigned i;
for (i = 0; i < page_avail; i++)
if (page_map[i] == value)
return i+1;
return 0;
}
/* Return number of the current message.
Zero is returned if page_map is empty (i.e. mailbox is empy) */
size_t
get_cursor ()
{
check_page_map ();
if (page_avail == 0)
return 0;
return page_map[cursor];
}
/* Move cursor to message number VALUE */
void
set_cursor (unsigned value)
{
int n;
if (total == 0)
{
cursor = 0;
return;
}
check_page_map ();
n = page_line (value);
if (n == 0)
{
top_of_page = value;
cursor = 0;
page_avail = 0;
page_move (0);
}
else
cursor = n - 1;
}
/* Return T if the cursor points to message number N */
int
is_current_message (size_t n)
{
check_page_map ();
return page_map[cursor] == n;
}
/* Apply function FUNC to each message from the page. DATA supplies
call-specific data to the function. */
void
page_do (msg_handler_t func, void *data)
{
unsigned i;
check_page_map ();
for (i = 0; i < page_avail; i++)
{
message_t msg;
msgset_t set;
set.next = NULL;
set.npart = 1;
set.msg_part = page_map + i;
mailbox_get_message (mbox, page_map[i], &msg);
func (&set, msg, data);
}
}
/* Move current page OFFSET lines forward, if OFFSET > 0, or backward,
if OFFSET < 0.
Return number of items that will be displayed */
size_t
page_move (off_t offset)
{
size_t start;
size_t count = 0;
check_page_map ();
if (offset < 0 && -offset > page_map[0])
start = 1;
else
start = page_map[0] + offset;
util_range_msg (start, page_size,
MSG_COUNT|MSG_NODELETED|MSG_SILENT, _fill_map, &count);
if (offset < 0 && top_of_page == page_map[0])
{
page_avail = count;
return 0;
}
if (count)
{
top_of_page = page_map[0];
if (count < page_size && top_of_page > 1)
{
for (start = top_of_page - 1; count < page_size && start > 1;
start--)
{
if (!util_isdeleted (start))
{
top_of_page = start;
cursor++;
count++;
}
}
page_avail = 0;
fill_page_map ();
}
else
page_avail = count;
}
return count;
}