Commit 83fd9e40 83fd9e40c3c4f6b7bc9556b72152b0ac01773b97 by Sergey Poznyakoff

Rewritten using new iterator functions.

1 parent 7d408629
......@@ -22,47 +22,55 @@
*/
static int
mail_delete0 (void)
mail_delete_msg (msgset_t *mspec, message_t msg, void *data)
{
message_t msg;
attribute_t attr;
if (util_get_message (mbox, cursor, &msg, MSG_NODELETED))
return 1;
message_get_attribute (msg, &attr);
attribute_set_deleted (attr);
if (cursor == mspec->msg_part[0])
{
/* deleting current message. let the caller know */
*(int *)data = 1;
}
return 0;
}
int
mail_delete (int argc, char **argv)
{
int rc = 0;
int reset_cursor = 0;
int rc = util_foreach_msg (argc, argv, MSG_NODELETED,
mail_delete_msg, &reset_cursor);
if (argc > 1)
rc = util_msglist_command (mail_delete, argc, argv, 0);
else
rc = mail_delete0 ();
/* Readjust the realcursor to no point to the deleted messages. */
if (cursor == realcursor)
/* Readjust the cursor not to point to the deleted messages. */
if (reset_cursor)
{
unsigned int here = realcursor;
do
unsigned int here;
for (here = cursor; here <= total; here++)
{
message_t msg = NULL;
attribute_t attr = NULL;
mailbox_get_message (mbox, realcursor, &msg);
mailbox_get_message (mbox, here, &msg);
message_get_attribute (msg, &attr);
if (!attribute_is_deleted (attr))
break;
if (++realcursor > total)
realcursor = 1;
}
while (realcursor != here);
cursor = realcursor;
if (here > total)
for (here = cursor; here > 0; here--)
{
message_t msg = NULL;
attribute_t attr = NULL;
mailbox_get_message (mbox, here, &msg);
message_get_attribute (msg, &attr);
if (!attribute_is_deleted (attr))
break;
}
cursor = here;
}
if (util_getenv (NULL, "autoprint", Mail_env_boolean, 0) == 0)
......
......@@ -25,10 +25,7 @@
int
mail_dp (int argc, char **argv)
{
if (argc > 1)
util_msglist_command (mail_delete, argc, argv, 0);
else
util_do_command ("delete");
mail_delete (argc, argv);
util_do_command ("print");
return 0;
}
......
......@@ -21,19 +21,21 @@
* e[dit] [msglist]
*/
int
mail_edit (int argc, char **argv)
static int
edit0 (msgset_t *mspec, message_t msg, void *data)
{
if (argc > 1)
return util_msglist_command (mail_edit, argc, argv, 1);
else
{
char *file = mu_tempname (NULL);
util_do_command ("copy %s", file);
util_do_command ("shell %s %s", getenv ("EDITOR"), file);
remove (file);
free (file);
cursor = mspec->msg_part[0];
return 0;
}
return 1;
}
int
mail_edit (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_NODELETED, edit0, NULL);
}
......
......@@ -29,7 +29,7 @@ mail_eq (int argc, char **argv)
switch (argc)
{
case 1:
fprintf (ofile, "%d\n", realcursor);
fprintf (ofile, "%d\n", cursor);
break;
case 2:
......@@ -37,8 +37,8 @@ mail_eq (int argc, char **argv)
{
if (list->msg_part[0] <= total)
{
realcursor = cursor = list->msg_part[0];
fprintf (ofile, "%d\n", realcursor);
cursor = list->msg_part[0];
fprintf (ofile, "%d\n", cursor);
}
else
util_error_range (list->msg_part[0]);
......
......@@ -111,7 +111,7 @@ mail_file (int argc, char **argv)
mbox = newbox;
mailbox_messages_count (mbox, &total);
cursor = realcursor = 1;
cursor = 1;
if (util_getenv (NULL, "header", Mail_env_boolean, 0) == 0)
{
util_do_command ("summary");
......
......@@ -22,19 +22,8 @@
*/
int
mail_from (int argc, char **argv)
mail_from0 (msgset_t *mspec, message_t msg, void *data)
{
if (argc > 1)
return util_msglist_command (mail_from, argc, argv, 0);
else
return mail_from0 (cursor, 1);
return 1;
}
int
mail_from0 (int msgno, int verbose)
{
message_t msg;
header_t hdr = NULL;
envelope_t env;
attribute_t attr;
......@@ -48,10 +37,6 @@ mail_from0 (int msgno, int verbose)
struct tm tm;
mu_timezone tz;
if (util_get_message (mbox, msgno, &msg,
MSG_NODELETED|(verbose ? 0 : MSG_SILENT)))
return 1;
message_get_header (msg, &hdr);
if (header_aget_value (hdr, MU_HEADER_FROM, &from) == 0)
{
......@@ -113,7 +98,8 @@ mail_from0 (int msgno, int verbose)
fromp = from ? from : "";
subjp = subj ? subj : fromp;
fprintf (ofile, "%c%c%4d %-18.18s %-16.16s %s %.*s\n",
msgno == realcursor ? '>' : ' ', cflag, msgno,
mspec->msg_part[0] == cursor ? '>' : ' ', cflag,
mspec->msg_part[0],
fromp, date, st, (subjl < 0) ? 0 : subjl, subjp);
free (from);
......@@ -121,3 +107,10 @@ mail_from0 (int msgno, int verbose)
return 0;
}
int
mail_from (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_NODELETED, mail_from0, NULL);
}
......
......@@ -52,7 +52,7 @@ mail_headers (int argc, char **argv)
low = list->msg_part[0] - (lines / 2);
if (low < 1)
low = 1;
high = low + lines;
high = low + util_screen_lines ();
if ((unsigned int)high > total)
{
high = total;
......@@ -60,8 +60,7 @@ mail_headers (int argc, char **argv)
}
}
for (; low <= high; low++)
mail_from0 (low, 0);
util_range_msg (low, high, MSG_NODELETED|MSG_SILENT, mail_from0, NULL);
msgset_free (list);
return 0;
......
......@@ -22,21 +22,22 @@
* pre[serve] [msglist]
*/
int
mail_hold (int argc, char **argv)
static int
hold0 (msgset_t *mspec, message_t msg, void *data)
{
message_t msg;
attribute_t attr;
if (argc > 1)
return util_msglist_command (mail_hold, argc, argv, 1);
else
{
if (util_get_message (mbox, cursor, &msg, MSG_ALL))
return 1;
message_get_attribute (msg, &attr);
attribute_unset_userflag (attr, MAIL_ATTRIBUTE_MBOXED);
}
cursor = mspec->msg_part[0];
return 0;
}
int
mail_hold (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_ALL, hold0, NULL);
}
......
......@@ -21,22 +21,21 @@
* mb[ox] [msglist]
*/
int
mail_mbox (int argc, char **argv)
static int
mbox0 (msgset_t *mspec, message_t msg, void *data)
{
message_t msg;
attribute_t attr;
if (argc > 1)
return util_msglist_command (mail_mbox, argc, argv, 1);
else
{
if (util_get_message (mbox, cursor, &msg, MSG_NODELETED))
return 1;
/* Mark the message */
message_get_attribute (msg, &attr);
attribute_set_userflag (attr, MAIL_ATTRIBUTE_MBOXED);
}
cursor = mspec->msg_part[0];
return 0;
}
int
mail_mbox (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_NODELETED, mbox0, NULL);
}
......
......@@ -53,7 +53,7 @@ mail_next (int argc, char **argv)
if (util_get_message (mbox, n, &msg, MSG_NODELETED|MSG_SILENT))
return 1;
}
cursor = realcursor = n;
cursor = n;
util_do_command("print");
return 0;
}
......
......@@ -53,7 +53,7 @@ mail_previous (int argc, char **argv)
if (util_get_message (mbox, n, &msg, MSG_NODELETED|MSG_SILENT))
return 1;
}
cursor = realcursor = n;
cursor = n;
util_do_command ("print");
return 0;
}
......
......@@ -24,14 +24,9 @@
* T[ype] [msglist]
*/
int
mail_print (int argc, char **argv)
static int
mail_print_msg (msgset_t *mspec, message_t mesg, void *data)
{
if (argc > 1)
return util_msglist_command (mail_print, argc, argv, 1);
else
{
message_t mesg;
header_t hdr;
body_t body;
stream_t stream;
......@@ -42,9 +37,6 @@ mail_print (int argc, char **argv)
attribute_t attr;
int pagelines = util_get_crt ();
if (util_get_message (mbox, cursor, &mesg, MSG_NODELETED))
return 1;
message_lines (mesg, &lines);
/* If it is POP or IMAP the lines number is not known, so try
......@@ -66,7 +58,7 @@ mail_print (int argc, char **argv)
if (pagelines && lines > pagelines)
out = popen (getenv ("PAGER"), "w");
if (islower (argv[0][0]))
if (*(int *) data) /* print was called with a lowercase 'p' */
{
size_t i, num = 0;
char buf[512];
......@@ -109,7 +101,17 @@ mail_print (int argc, char **argv)
message_get_attribute (mesg, &attr);
attribute_set_read (attr);
cursor = mspec->msg_part[0];
return 0;
}
return 1;
}
int
mail_print (int argc, char **argv)
{
int lower = islower (argv[0][0]);
int rc = util_foreach_msg (argc, argv, MSG_NODELETED,
mail_print_msg, &lower);
return rc;
}
......
......@@ -169,13 +169,8 @@ make_references (compose_env_t *env, message_t msg)
*/
int
mail_reply (int argc, char **argv)
reply0 (msgset_t *mspec, message_t msg, void *data)
{
if (argc > 1)
return util_msglist_command (mail_reply, argc, argv, 1);
else
{
message_t msg;
header_t hdr;
compose_env_t env;
int status;
......@@ -183,15 +178,12 @@ mail_reply (int argc, char **argv)
compose_init (&env);
if (util_get_message (mbox, cursor, &msg, MSG_NODELETED))
return 1;
message_get_header (msg, &hdr);
compose_header_set (&env, MU_HEADER_TO, util_get_sender (cursor, 0),
compose_header_set (&env, MU_HEADER_TO, util_get_sender (mspec->msg_part[0], 0),
COMPOSE_SINGLE_LINE);
if (islower (argv[0][0]))
if (*(int*) data) /* reply starts with a lowercase */
{
/* Add all recepients of the originate letter */
......@@ -247,7 +239,16 @@ mail_reply (int argc, char **argv)
make_references (&env, msg);
status = mail_send0 (&env, 0);
compose_destroy (&env);
cursor = mspec->msg_part[0];
return status;
}
return 1;
}
int
mail_reply (int argc, char **argv)
{
int lower = islower (argv[0][0]);
return util_foreach_msg (argc, argv, MSG_NODELETED, reply0, &lower);
}
......
......@@ -21,25 +21,24 @@
* si[ze] [msglist]
*/
int
mail_size (int argc, char **argv)
static int
size0 (msgset_t *mspec, message_t msg, void *data)
{
if (argc > 1)
util_msglist_command (mail_size, argc, argv, 0);
else
{
size_t size = 0, lines = 0;
message_t msg;
if (util_get_message (mbox, cursor, &msg, MSG_ALL))
return 1;
message_size (msg, &size);
message_lines (msg, &lines);
fprintf (ofile, "%c%2d %3d/%-5d\n", cursor == realcursor ? '>' : ' ',
cursor, lines, size);
fprintf (ofile, "%c%2d %3d/%-5d\n",
mspec->msg_part[0] == cursor ? '>' : ' ',
mspec->msg_part[0], lines, size);
return 0;
}
return 1;
}
int
mail_size (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_ALL, size0, NULL);
}
......
......@@ -19,7 +19,7 @@
/* Simple summary dysplaying a blurb on the name of the
mailbox and how many new:deleted:read messages.
The side effect is that is set the cursor/realcursor
The side effect is that is set the cursor/cursor
to the newest or read message number. */
int
mail_summary (int argc, char **argv)
......@@ -73,7 +73,7 @@ mail_summary (int argc, char **argv)
printf("\n");
/* Set the cursor. */
cursor = realcursor = (first_new == 0) ? ((first_unread == 0) ?
cursor = (first_new == 0) ? ((first_unread == 0) ?
1 : first_unread) : first_new ;
return 0;
}
......
......@@ -21,14 +21,9 @@
* to[p] [msglist]
*/
int
mail_top (int argc, char **argv)
static int
top0 (msgset_t *mspec, message_t msg, void *data)
{
if (argc > 1)
return util_msglist_command (mail_top, argc, argv, 1);
else
{
message_t msg;
stream_t stream;
char buf[512];
size_t n;
......@@ -39,9 +34,6 @@ mail_top (int argc, char **argv)
|| lines < 0)
return 1;
if (util_get_message (mbox, cursor, &msg, MSG_NODELETED))
return 1;
message_get_stream (msg, &stream);
for (n = 0, off = 0; lines > 0; lines--, off += n)
{
......@@ -50,7 +42,13 @@ mail_top (int argc, char **argv)
break;
fprintf (ofile, "%s", buf);
}
cursor = mspec->msg_part[0];
return 0;
}
return 1;
}
int
mail_top (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_NODELETED, top0, NULL);
}
......
......@@ -21,22 +21,19 @@
* u[ndelete] [msglist]
*/
int
mail_undelete (int argc, char **argv)
static int
undelete0 (msgset_t *mspec, message_t msg, void *data)
{
if (argc > 1)
return util_msglist_command (mail_undelete, argc, argv, 1);
else
{
message_t msg;
attribute_t attr;
if (util_get_message (mbox, cursor, &msg, MSG_ALL))
return 1;
message_get_attribute (msg, &attr);
if (attribute_is_deleted (attr))
attribute_unset_deleted (attr);
return 0;
}
return 1;
}
int
mail_undelete (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_ALL, undelete0, NULL);
}
......
......@@ -436,14 +436,10 @@ var_insert (int argc, char **argv, compose_env_t *send_env)
/* ~m[mesg-list] */
/* ~M[mesg-list] */
int
var_quote (int argc, char **argv, compose_env_t *env)
quote0 (msgset_t *mspec, message_t mesg, void *data)
{
if (argc > 1)
return util_msglist_esccmd (var_quote, argc, argv, env, 0);
else
{
message_t mesg;
header_t hdr;
body_t body;
stream_t stream;
......@@ -452,14 +448,11 @@ var_quote (int argc, char **argv, compose_env_t *env)
size_t n = 0;
char *prefix = "\t";
if (util_get_message (mbox, cursor, &mesg, MSG_NODELETED))
return 1;
fprintf (stdout, _("Interpolating: %d\n"), cursor);
fprintf (stdout, _("Interpolating: %d\n"), mspec->msg_part[0]);
util_getenv (&prefix, "indentprefix", Mail_env_string, 0);
if (islower (argv[0][0]))
if (*(int*)data)
{
size_t i, num = 0;
char buf[512];
......@@ -505,8 +498,15 @@ var_quote (int argc, char **argv, compose_env_t *env)
fprintf (ofile, "%s%s", prefix, buffer);
off += n;
}
return 0;
}
int
var_quote (int argc, char **argv, compose_env_t *env)
{
int lower = islower (argv[0][0]);
util_foreach_msg (argc, argv, MSG_NODELETED|MSG_SILENT, quote0, &lower);
var_continue ();
}
return 0;
}
......
......@@ -21,14 +21,9 @@
* v[isual] [msglist]
*/
int
mail_visual (int argc, char **argv)
static int
visual0 (msgset_t *mspec, message_t msg, void *data)
{
if (argc > 1)
return util_msglist_command (mail_visual, argc, argv, 1);
else
{
message_t msg = NULL;
attribute_t attr = NULL;
char *file = mu_tempname (NULL);
......@@ -39,11 +34,17 @@ mail_visual (int argc, char **argv)
free (file);
/* Mark as read */
mailbox_get_message (mbox, cursor, &msg);
message_get_attribute (msg, &attr);
attribute_set_read (attr);
cursor = mspec->msg_part[0];
return 0;
}
return 1;
}
int
mail_visual (int argc, char **argv)
{
return util_foreach_msg (argc, argv, MSG_NODELETED, visual0, NULL);
}
......
......@@ -31,7 +31,8 @@
*/
static int
z_parse_args(int argc, char **argv, unsigned int *return_count, int *return_dir)
z_parse_args(int argc, char **argv,
unsigned int *return_count, int *return_dir)
{
int count = 1;
int mul = 1;
......@@ -109,6 +110,7 @@ mail_z (int argc, char **argv)
unsigned int pagelines = util_screen_lines();
unsigned int count;
int dir;
int crs;
if (z_parse_args(argc, argv, &count, &dir))
return 1;
......@@ -116,31 +118,32 @@ mail_z (int argc, char **argv)
nlines = pagelines;
count *= pagelines;
crs = cursor;
switch (dir)
{
case D_BWD:
if (cursor < nlines)
if (crs < nlines)
{
fprintf (stdout, _("On first screenful of messages\n"));
return 0;
}
if (cursor < count)
cursor = 1;
if (crs < count)
crs = 1;
else
cursor -= count;
crs -= count;
break;
case D_FWD:
if (cursor + pagelines > total)
if (crs + pagelines > total)
{
fprintf (stdout, _("On last screenful of messages\n"));
return 0;
}
cursor += count;
crs += count;
if (cursor + nlines > total)
nlines = total - cursor + 1;
if (crs + nlines > total)
nlines = total - crs + 1;
if (nlines <= 0)
{
......@@ -156,43 +159,43 @@ mail_z (int argc, char **argv)
of the last message. This behaviour is used on startup
when displaying the summary and the headers, new messages
are last but we want to display a screenful with the
real cursor set by summary() to the new message. */
real crs set by summary() to the new message. */
/* Find the start of the last screen page. */
int lastpage = total - pagelines + 1;
if (lastpage <= 0)
lastpage = 1;
if (cursor > (unsigned int)lastpage)
if (crs > (unsigned int)lastpage)
{
realcursor = cursor;
cursor = lastpage;
crs = lastpage;
if (cursor + nlines > total)
nlines = total - cursor + 1;
if (crs + nlines > total)
nlines = total - crs;
for (i = 0; i < nlines; i++)
{
mail_from0 (cursor, 0);
cursor++;
}
cursor = realcursor;
util_range_msg (crs, crs + nlines,
MSG_NODELETED|MSG_SILENT, mail_from0, NULL);
return 1;
}
else if (crs + nlines > total)
nlines = total - crs + 1;
}
break;
}
realcursor = cursor;
cursor = crs;
for (i = 0; i < nlines && cursor <= total; )
i = 0;
do
{
if (mail_from0 (cursor, 0) == 0)
i++;
cursor++;
int cnt = util_range_msg (crs, crs + nlines - 1,
MSG_NODELETED|MSG_SILENT, mail_from0, NULL);
if (cnt == 0)
break;
i += cnt;
crs += nlines;
}
cursor = realcursor;
while (i < nlines && crs <= total);
return 1;
}
......