+ fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb);
+ fputs(buffer, stdout);
+ mlistfree();
+
+ if (p->pq_text)
+ free(p->pq_text);
+ if (p->pq_error)
+ free(p->pq_error);
+ q = p->pq_next;
+ free(p);
+ }
+ }
+}
+
+/*
+ * Process messages and run them through the format engine. A lot taken
+ * from scan.c.
+ */
+
+static void
+process_messages(struct format *fmt, struct msgs_array *comps,
+ struct msgs_array *msgs, char *buffer, char *folder,
+ int bufsize, int outwidth, int files, int *dat,
+ struct fmt_callbacks *cb)
+{
+ int i, msgnum, msgsize = dat[2], num = dat[0], cur = dat[1];
+ int num_unseen_seq = 0;
+ ivector_t seqnum = ivector_create (0);
+ char *maildir, *cp;
+ struct msgs *mp;
+ FILE *in;
+
+ /*
+ * If 'files' is set, short-circuit everything else and just process
+ * everything now.
+ */
+
+ if (files) {
+ for (i = 0; i < msgs->size; i++) {
+ if ((in = fopen(cp = msgs->msgs[i], "r")) == NULL) {
+ admonish(cp, "unable to open file");
+ continue;
+ }
+ process_single_file(in, comps, dat, msgsize, fmt, buffer,
+ bufsize, outwidth, cb);
+ }
+
+ return;
+ }
+
+ if (! folder)
+ folder = getfolder(1);
+
+ maildir = m_maildir(folder);
+
+ if (chdir(maildir) < 0)
+ adios(maildir, "unable to change directory to");
+
+ if (!(mp = folder_read(folder, 1)))
+ adios(NULL, "unable to read folder %s", folder);
+
+ if (mp->nummsg == 0)
+ adios(NULL, "no messages in %s", folder);
+
+ for (i = 0; i < msgs->size; i++)
+ if (!m_convert(mp, msgs->msgs[i]))
+ done(1);
+ seq_setprev(mp); /* set the Previous-Sequence */
+
+ context_replace(pfolder, folder); /* update curren folder */
+ seq_save(mp); /* synchronize message sequences */
+ context_save(); /* save the context file */
+
+ /*
+ * We want to set the unseen flag if requested, so we have to check
+ * the unseen sequence as well.
+ */
+
+ if (dat[4] == -1) {
+ if ((cp = context_find(usequence)) && *cp) {
+ char **ap, *dp;
+
+ dp = getcpy(cp);
+ ap = brkstring(dp, " ", "\n");
+ for (i = 0; ap && *ap; i++, ap++)
+ ivector_push_back (seqnum, seq_getnum(mp, *ap));
+
+ num_unseen_seq = i;
+ if (dp)
+ free(dp);
+ }
+ }
+
+ for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
+ if (is_selected(mp, msgnum)) {
+ if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
+ admonish(cp, "unable to open message");
+ continue;
+ }
+
+ fmt_freecomptext();
+
+ if (num == -1)
+ dat[0] = msgnum;
+
+ if (cur == -1)
+ dat[1] = msgnum == mp->curmsg;
+
+ /*
+ * Check to see if this is in the unseen sequence
+ */
+
+ dat[4] = 0;
+ for (i = 0; i < num_unseen_seq; i++) {
+ if (in_sequence(mp, ivector_at (seqnum, i), msgnum)) {
+ dat[4] = 1;
+ break;