let the caller deal with M_FORMAT_ARROWCURSOR, not the callee.
[apps/madmutt.git] / compose.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 2004 g10 Code GmbH
5  *
6  * Parts were written/modified by:
7  * Nico Golde <nico@ngolde.de>
8  *
9  * This file is part of mutt-ng, see http://www.muttng.org/.
10  * It's licensed under the GNU General Public License,
11  * please see the file GPL in the top level source directory.
12  */
13
14 #include <lib-lib/lib-lib.h>
15
16 #include <lib-sys/unix.h>
17 #include <lib-mime/mime.h>
18 #include <lib-crypt/crypt.h>
19
20 #include <lib-ui/curses.h>
21 #include <lib-ui/enter.h>
22 #include <lib-ui/menu.h>
23 #include <lib-mx/mx.h>
24
25 #include "mutt.h"
26 #include "alias.h"
27 #include "mutt_idna.h"
28 #include "attach.h"
29 #include "recvattach.h"
30 #include "sort.h"
31 #include "charset.h"
32 #include "buffy.h"
33 #include "remailer.h"
34
35 #ifdef USE_NNTP
36 #include <nntp/nntp.h>
37 #endif
38
39 #define CHECK_COUNT \
40     if (idxlen == 0) {                             \
41         mutt_error _("There are no attachments."); \
42         break;                                     \
43     }
44
45 enum {
46   HDR_FROM = 1,
47   HDR_TO,
48   HDR_CC,
49   HDR_BCC,
50   HDR_SUBJECT,
51   HDR_REPLYTO,
52   HDR_FCC,
53   HDR_MIX,
54
55   HDR_CRYPT,
56   HDR_CRYPTINFO,
57
58 #ifdef USE_NNTP
59   HDR_NEWSGROUPS,
60   HDR_FOLLOWUPTO,
61   HDR_XCOMMENTTO,
62 #endif
63
64 #ifndef USE_NNTP
65   HDR_ATTACH = (HDR_FCC + 5)    /* where to start printing the attachments */
66 #else
67   HDR_ATTACH = (HDR_FCC + 7)
68 #endif
69 };
70
71 #define HDR_XOFFSET     14
72 #define TITLE_FMT       "%14s"        /* Used for Prompts, which are ASCII */
73 #define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
74 #define W               (COLS - HDR_XOFFSET - SW)
75
76 static const char *Prompts[] = {
77     "From: ",
78     "To: ",
79     "Cc: ",
80     "Bcc: ",
81     "Subject: ",
82     "Reply-To: ",
83     "Fcc: ",
84 #ifdef USE_NNTP
85     "", "", "", "Newsgroups: ", "Followup-To: ", "X-Comment-To: "
86 #endif
87 };
88
89 static struct mapping_t ComposeHelp[] = {
90   {N_("Send"), OP_COMPOSE_SEND_MESSAGE},
91   {N_("Abort"), OP_EXIT},
92   {"To", OP_COMPOSE_EDIT_TO},
93   {"CC", OP_COMPOSE_EDIT_CC},
94   {"Subj", OP_COMPOSE_EDIT_SUBJECT},
95   {N_("Attach file"), OP_COMPOSE_ATTACH_FILE},
96   {N_("Descrip"), OP_COMPOSE_EDIT_DESCRIPTION},
97   {N_("Help"), OP_HELP},
98   {NULL, OP_NULL}
99 };
100
101 #ifdef USE_NNTP
102 static struct mapping_t ComposeNewsHelp[] = {
103   {N_("Send"), OP_COMPOSE_SEND_MESSAGE},
104   {N_("Abort"), OP_EXIT},
105   {"Newsgroups", OP_COMPOSE_EDIT_NEWSGROUPS},
106   {"Subj", OP_COMPOSE_EDIT_SUBJECT},
107   {N_("Attach file"), OP_COMPOSE_ATTACH_FILE},
108   {N_("Descrip"), OP_COMPOSE_EDIT_DESCRIPTION},
109   {N_("Help"), OP_HELP},
110   {NULL, OP_NULL}
111 };
112 #endif
113
114
115 static void snd_entry (char *b, ssize_t blen, MUTTMENU * menu, int num) {
116   int w=(COLS-SW)>blen?blen:COLS-SW;
117   m_strformat (b, w, NONULL (AttachFormat), mutt_attach_fmt,
118                      (unsigned long) (((ATTACHPTR **) menu->data)[num]),
119                      M_FORMAT_STAT_FILE | (option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0));
120 }
121
122 static void redraw_crypt_lines (HEADER * msg)
123 {
124   int off = 0;
125
126   if (!msg->security)
127     mvaddstr (HDR_CRYPT, SW, "    Security: ");
128   else if (msg->security & APPLICATION_SMIME)
129     mvaddstr (HDR_CRYPT, SW, "      S/MIME: ");
130   else if (msg->security & APPLICATION_PGP)
131     mvaddstr (HDR_CRYPT, SW, "         PGP: ");
132
133   if ((msg->security & (ENCRYPT | SIGN)) == (ENCRYPT | SIGN))
134     addstr (_("Sign, Encrypt"));
135   else if (msg->security & ENCRYPT)
136     addstr (_("Encrypt"));
137   else if (msg->security & SIGN)
138     addstr (_("Sign"));
139   else
140     addstr (_("Clear"));
141
142   if ((msg->security & APPLICATION_PGP)
143       && (msg->security & (ENCRYPT | SIGN))) {
144     if ((msg->security & INLINE))
145       addstr (_(" (inline)"));
146     else
147       addstr (_(" (PGP/MIME)"));
148   }
149   clrtoeol ();
150
151   move (HDR_CRYPTINFO, SW);
152   clrtoeol ();
153   if (msg->security & APPLICATION_PGP && msg->security & SIGN)
154     printw ("%s%s", _("     sign as: "),
155             PgpSignAs ? PgpSignAs : _("<default>"));
156
157   if (msg->security & APPLICATION_SMIME && msg->security & SIGN) {
158     printw ("%s%s", _("     sign as: "),
159             SmimeDefaultKey ? SmimeDefaultKey : _("<default>"));
160   }
161
162   if ((msg->security & APPLICATION_SMIME)
163       && (msg->security & ENCRYPT)
164       && SmimeCryptAlg && *SmimeCryptAlg) {
165     mvprintw (HDR_CRYPTINFO, SW + 40, "%s%s", _("Encrypt with: "),
166               NONULL (SmimeCryptAlg));
167     off = 20;
168   }
169 }
170
171 static void redraw_mix_line (string_list_t * chain)
172 {
173   int c;
174   const char *t;
175
176   mvaddstr (HDR_MIX, SW, "         Mix: ");
177
178   if (!chain) {
179     addstr ("<no chain defined>");
180     clrtoeol ();
181     return;
182   }
183
184   for (c = 12; chain; chain = chain->next) {
185     t = chain->data;
186     if (t && t[0] == '0' && t[1] == '\0')
187       t = "<random>";
188
189     if (c + m_strlen(t) + 2 >= COLS - SW)
190       break;
191
192     addstr (NONULL (t));
193     if (chain->next)
194       addstr (", ");
195
196     c += m_strlen(t) + 2;
197   }
198 }
199
200 static int check_attachments (ATTACHPTR ** idx, short idxlen)
201 {
202   int i, r;
203   struct stat st;
204   char pretty[_POSIX_PATH_MAX], msg[_POSIX_PATH_MAX + STRING];
205
206   for (i = 0; i < idxlen; i++) {
207     m_strcpy(pretty, sizeof(pretty), idx[i]->content->filename);
208     if (stat (idx[i]->content->filename, &st) != 0) {
209       mutt_pretty_mailbox (pretty);
210       mutt_error (_("%s [#%d] no longer exists!"), pretty, i + 1);
211       return -1;
212     }
213
214     if (idx[i]->content->stamp < st.st_mtime) {
215       mutt_pretty_mailbox (pretty);
216       snprintf (msg, sizeof (msg), _("%s [#%d] modified. Update encoding?"),
217                 pretty, i + 1);
218
219       if ((r = mutt_yesorno (msg, M_YES)) == M_YES)
220         mutt_update_encoding (idx[i]->content);
221       else if (r == -1)
222         return -1;
223     }
224   }
225
226   return 0;
227 }
228
229 static void draw_envelope_addr (int line, address_t * addr)
230 {
231   char buf[STRING];
232
233   buf[0] = 0;
234   rfc822_addrcat(buf, sizeof (buf), addr, 1);
235   mvprintw (line, SW, TITLE_FMT, Prompts[line - 1]);
236   mutt_paddstr (W, buf);
237 }
238
239 static void draw_envelope (HEADER * msg, char *fcc)
240 {
241   draw_envelope_addr (HDR_FROM, msg->env->from);
242 #ifdef USE_NNTP
243   if (!option (OPTNEWSSEND)) {
244 #endif
245     draw_envelope_addr (HDR_TO, msg->env->to);
246     draw_envelope_addr (HDR_CC, msg->env->cc);
247     draw_envelope_addr (HDR_BCC, msg->env->bcc);
248 #ifdef USE_NNTP
249   } else {
250     mvprintw (HDR_TO, SW, TITLE_FMT, Prompts[HDR_NEWSGROUPS - 1]);
251     mutt_paddstr (W, NONULL (msg->env->newsgroups));
252     mvprintw (HDR_CC, SW, TITLE_FMT, Prompts[HDR_FOLLOWUPTO - 1]);
253     mutt_paddstr (W, NONULL (msg->env->followup_to));
254     if (option (OPTXCOMMENTTO)) {
255       mvprintw (HDR_BCC, 0, TITLE_FMT, Prompts[HDR_XCOMMENTTO - 1]);
256       mutt_paddstr (W, NONULL (msg->env->x_comment_to));
257     }
258   }
259 #endif
260   mvprintw (HDR_SUBJECT, SW, TITLE_FMT, Prompts[HDR_SUBJECT - 1]);
261   mutt_paddstr (W, NONULL (msg->env->subject));
262   draw_envelope_addr (HDR_REPLYTO, msg->env->reply_to);
263   mvprintw (HDR_FCC, SW, TITLE_FMT, Prompts[HDR_FCC - 1]);
264   mutt_paddstr (W, fcc);
265
266   redraw_crypt_lines (msg);
267   redraw_mix_line (msg->chain);
268
269   SETCOLOR (MT_COLOR_STATUS);
270   mvaddstr (HDR_ATTACH - 1, SW, _("-- Attachments"));
271   BKGDSET (MT_COLOR_STATUS);
272   clrtoeol ();
273
274   BKGDSET (MT_COLOR_NORMAL);
275   SETCOLOR (MT_COLOR_NORMAL);
276 }
277
278 static int edit_address_list (int line, address_t ** addr)
279 {
280   char buf[HUGE_STRING] = "";   /* needs to be large for alias expansion */
281   char *err = NULL;
282
283   mutt_addrlist_to_local (*addr);
284   rfc822_addrcat(buf, sizeof (buf), *addr, 0);
285   if (mutt_get_field (Prompts[line - 1], buf, sizeof (buf), M_ALIAS) == 0) {
286     address_list_wipe(addr);
287     *addr = mutt_parse_adrlist (*addr, buf);
288     *addr = mutt_expand_aliases (*addr);
289   }
290
291   if (option (OPTNEEDREDRAW)) {
292     unset_option (OPTNEEDREDRAW);
293     return REDRAW_FULL;
294   }
295
296   if (mutt_addrlist_to_idna (*addr, &err) != 0) {
297     mutt_error (_("Warning: '%s' is a bad IDN."), err);
298     mutt_refresh ();
299     p_delete(&err);
300   }
301
302   /* redraw the expanded list so the user can see the result */
303   buf[0] = 0;
304   rfc822_addrcat(buf, sizeof (buf), *addr, 1);
305   move (line, HDR_XOFFSET + SW);
306   mutt_paddstr (W, buf);
307
308   return 0;
309 }
310
311 static int delete_attachment (MUTTMENU * menu, short *idxlen, int x)
312 {
313   ATTACHPTR **idx = (ATTACHPTR **) menu->data;
314   int y;
315
316   menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
317
318   if (x == 0 && menu->max == 1) {
319     mutt_error _("You may not delete the only attachment.");
320
321     idx[x]->content->tagged = 0;
322     return -1;
323   }
324
325   for (y = 0; y < *idxlen; y++) {
326     if (idx[y]->content->next == idx[x]->content) {
327       idx[y]->content->next = idx[x]->content->next;
328       break;
329     }
330   }
331
332   idx[x]->content->next = NULL;
333   idx[x]->content->parts = NULL;
334   body_list_wipe(&(idx[x]->content));
335   p_delete(&idx[x]->tree);
336   p_delete(&idx[x]);
337   for (; x < *idxlen - 1; x++)
338     idx[x] = idx[x + 1];
339   menu->max = --(*idxlen);
340
341   return (0);
342 }
343
344 static void update_idx (MUTTMENU * menu, ATTACHPTR ** idx, short idxlen)
345 {
346   idx[idxlen]->level = (idxlen > 0) ? idx[idxlen - 1]->level : 0;
347   if (idxlen)
348     idx[idxlen - 1]->content->next = idx[idxlen]->content;
349   idx[idxlen]->content->aptr = idx[idxlen];
350   menu->current = idxlen++;
351   mutt_update_tree (idx, idxlen);
352   menu->max = idxlen;
353   return;
354 }
355
356
357 /*
358  * cum_attachs_size: Cumulative Attachments Size
359  *
360  * Returns the total number of bytes used by the attachments in the
361  * attachment list _after_ content-transfer-encodings have been
362  * applied.
363  *
364  */
365 static unsigned long cum_attachs_size(MUTTMENU * menu)
366 {
367   ssize_t s;
368   unsigned short i;
369   ATTACHPTR **idx = menu->data;
370   CONTENT *info;
371   BODY *b;
372
373   for (i = 0, s = 0; i < menu->max; i++) {
374     b = idx[i]->content;
375
376     if (!b->content)
377       b->content = mutt_get_content_info (b->filename, b);
378
379     if ((info = b->content)) {
380       switch (b->encoding) {
381       case ENCQUOTEDPRINTABLE:
382         s += 3 * (info->lobin + info->hibin) + info->ascii + info->crlf;
383         break;
384       case ENCBASE64:
385         s += (4 * (info->lobin + info->hibin + info->ascii + info->crlf)) / 3;
386         break;
387       default:
388         s += info->lobin + info->hibin + info->ascii + info->crlf;
389         break;
390       }
391     }
392   }
393
394   return s;
395 }
396
397 /*
398  * compose_format_str()
399  *
400  * %a = total number of attachments
401  * %h = hostname  [option]
402  * %l = approx. length of current message (in bytes)
403  * %v = Mutt version
404  *
405  * This function is similar to status_format_str().  Look at that function for
406  * help when modifying this function.
407  */
408 static void compose_status_line (char *buf, ssize_t buflen, MUTTMENU * menu,
409                                  const char *p);
410
411 static const char *compose_format_str (char *buf, ssize_t buflen, char op,
412                                        const char *src, const char *prefix,
413                                        const char *ifstring,
414                                        const char *elsestring,
415                                        unsigned long data, format_flag flags)
416 {
417   char fmt[STRING], tmp[STRING];
418   int optional = (flags & M_FORMAT_OPTIONAL);
419   MUTTMENU *menu = (MUTTMENU *) data;
420
421   *buf = 0;
422   switch (op) {
423   case 'a':                    /* total number of attachments */
424     snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
425     snprintf (buf, buflen, fmt, menu->max);
426     break;
427
428   case 'h':                    /* hostname */
429     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
430     snprintf (buf, buflen, fmt, NONULL (Hostname));
431     break;
432
433   case 'l':                    /* approx length of current message in bytes */
434     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
435     mutt_pretty_size (tmp, sizeof (tmp), menu ? cum_attachs_size (menu) : 0);
436     snprintf (buf, buflen, fmt, tmp);
437     break;
438
439   case 'v':
440     m_strcpy(buf, buflen, mutt_make_version (0));
441     break;
442
443   case 0:
444     *buf = 0;
445     return (src);
446
447   default:
448     snprintf (buf, buflen, "%%%s%c", prefix, op);
449     break;
450   }
451
452   if (optional)
453     compose_status_line (buf, buflen, menu, ifstring);
454   else if (flags & M_FORMAT_OPTIONAL)
455     compose_status_line (buf, buflen, menu, elsestring);
456
457   return (src);
458 }
459
460 static void compose_status_line (char *buf, ssize_t buflen, MUTTMENU * menu,
461                                  const char *p)
462 {
463   int w=(COLS-SW)>buflen?buflen:(COLS-SW);
464   m_strformat(buf, w, p, compose_format_str, (unsigned long)menu, 0);
465 }
466
467 /* return values:
468  *
469  * 1    message should be postponed
470  * 0    normal exit
471  * -1   abort message
472  */
473 int mutt_compose_menu (HEADER * msg,    /* structure for new message */
474                        char *fcc,       /* where to save a copy of the message */
475                        ssize_t fcclen,
476                        HEADER * cur __attribute__ ((unused)))
477 {                               /* current message */
478   char helpstr[STRING];
479   char buf[LONG_STRING];
480   char fname[_POSIX_PATH_MAX];
481   MUTTMENU *menu;
482   ATTACHPTR **idx = NULL;
483   short idxlen = 0;
484   short idxmax = 0;
485   int i, closed = 0;
486   int r = -1;                   /* return value */
487   int op = 0;
488   int loop = 1;
489   int fccSet = 0;               /* has the user edited the Fcc: field ? */
490   CONTEXT *ctx = NULL, *this = NULL;
491
492   /* Sort, SortAux could be changed in mutt_index_menu() */
493   int oldSort, oldSortAux;
494   struct stat st;
495
496 #ifdef USE_NNTP
497   int news = 0;                 /* is it a news article ? */
498
499   if (option (OPTNEWSSEND))
500     news++;
501 #endif
502
503   mutt_attach_init (msg->content);
504   idx = mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0, 1);
505
506   menu = mutt_new_menu ();
507   menu->menu = MENU_COMPOSE;
508   menu->offset = HDR_ATTACH;
509   menu->max = idxlen;
510   menu->make_entry = snd_entry;
511   menu->tag = mutt_tag_attach;
512   menu->data = idx;
513 #ifdef USE_NNTP
514   if (news)
515     menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_COMPOSE,
516                                    ComposeNewsHelp);
517   else
518 #endif
519     menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_COMPOSE,
520                                    ComposeHelp);
521
522   if (option (OPTMBOXPANE))
523     buffy_check (0);
524   while (loop) {
525 #ifdef USE_NNTP
526     unset_option (OPTNEWS);     /* for any case */
527 #endif
528     switch (op = mutt_menuLoop (menu)) {
529     case OP_REDRAW:
530       draw_envelope (msg, fcc);
531       menu->offset = HDR_ATTACH;
532       menu->pagelen = LINES - HDR_ATTACH - 2;
533       break;
534     case OP_COMPOSE_EDIT_FROM:
535       menu->redraw = edit_address_list (HDR_FROM, &msg->env->from);
536       mutt_message_hook (NULL, msg, M_SEND2HOOK);
537       break;
538     case OP_COMPOSE_EDIT_TO:
539 #ifdef USE_NNTP
540       if (!news) {
541 #endif
542         menu->redraw = edit_address_list (HDR_TO, &msg->env->to);
543         mutt_message_hook (NULL, msg, M_SEND2HOOK);
544 #ifdef USE_NNTP
545       }
546 #endif
547       break;
548     case OP_COMPOSE_EDIT_BCC:
549 #ifdef USE_NNTP
550       if (!news) {
551 #endif
552         menu->redraw = edit_address_list (HDR_BCC, &msg->env->bcc);
553         mutt_message_hook (NULL, msg, M_SEND2HOOK);
554 #ifdef USE_NNTP
555       }
556 #endif
557       break;
558     case OP_COMPOSE_EDIT_CC:
559 #ifdef USE_NNTP
560       if (!news) {
561 #endif
562         menu->redraw = edit_address_list (HDR_CC, &msg->env->cc);
563         mutt_message_hook (NULL, msg, M_SEND2HOOK);
564 #ifdef USE_NNTP
565       }
566 #endif
567       break;
568 #ifdef USE_NNTP
569     case OP_COMPOSE_EDIT_NEWSGROUPS:
570       if (news) {
571         if (msg->env->newsgroups)
572           m_strcpy(buf, sizeof(buf), msg->env->newsgroups);
573         else
574           buf[0] = 0;
575         if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) == 0
576             && buf[0]) {
577           p_delete(&msg->env->newsgroups);
578           m_strrtrim(buf);
579           msg->env->newsgroups = m_strdup(skipspaces(buf));
580           move (HDR_TO, HDR_XOFFSET);
581           clrtoeol ();
582           if (msg->env->newsgroups)
583             printw ("%-*.*s", W, W, msg->env->newsgroups);
584         }
585       }
586       break;
587
588     case OP_COMPOSE_EDIT_FOLLOWUP_TO:
589       if (news) {
590         buf[0] = 0;
591         if (msg->env->followup_to)
592           m_strcpy(buf, sizeof(buf), msg->env->followup_to);
593         if (mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) == 0
594             && buf[0]) {
595           p_delete(&msg->env->followup_to);
596           m_strrtrim(buf);
597           msg->env->followup_to = m_strdup(skipspaces(buf));
598           move (HDR_CC, HDR_XOFFSET);
599           clrtoeol ();
600           if (msg->env->followup_to)
601             printw ("%-*.*s", W, W, msg->env->followup_to);
602         }
603       }
604       break;
605
606     case OP_COMPOSE_EDIT_X_COMMENT_TO:
607       if (news && option (OPTXCOMMENTTO)) {
608         buf[0] = 0;
609         if (msg->env->x_comment_to)
610           m_strcpy(buf, sizeof(buf), msg->env->x_comment_to);
611         if (mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) == 0
612             && buf[0]) {
613           p_delete(&msg->env->x_comment_to);
614           msg->env->x_comment_to = m_strdup(buf);
615           move (HDR_BCC, HDR_XOFFSET);
616           clrtoeol ();
617           if (msg->env->x_comment_to)
618             printw ("%-*.*s", W, W, msg->env->x_comment_to);
619         }
620       }
621       break;
622 #endif
623     case OP_COMPOSE_EDIT_SUBJECT:
624       if (msg->env->subject)
625         m_strcpy(buf, sizeof(buf), msg->env->subject);
626       else
627         buf[0] = 0;
628       if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) == 0) {
629         m_strreplace(&msg->env->subject, buf);
630         move (HDR_SUBJECT, HDR_XOFFSET + SW);
631         clrtoeol ();
632         if (msg->env->subject)
633           mutt_paddstr (W, msg->env->subject);
634       }
635       mutt_message_hook (NULL, msg, M_SEND2HOOK);
636       break;
637     case OP_COMPOSE_EDIT_REPLY_TO:
638       menu->redraw = edit_address_list (HDR_REPLYTO, &msg->env->reply_to);
639       mutt_message_hook (NULL, msg, M_SEND2HOOK);
640       break;
641     case OP_COMPOSE_EDIT_FCC:
642       m_strcpy(buf, sizeof(buf), fcc);
643       if (mutt_get_field ("Fcc: ", buf, sizeof (buf), M_FILE | M_CLEAR) == 0) {
644         m_strcpy(fcc, _POSIX_PATH_MAX, buf);
645         mutt_pretty_mailbox (fcc);
646         move (HDR_FCC, HDR_XOFFSET + SW);
647         mutt_paddstr (W, fcc);
648         fccSet = 1;
649       }
650       MAYBE_REDRAW (menu->redraw);
651       mutt_message_hook (NULL, msg, M_SEND2HOOK);
652       break;
653     case OP_COMPOSE_EDIT_MESSAGE:
654       if (Editor && !option (OPTEDITHDRS)) {
655         mutt_edit_file (Editor, msg->content->filename);
656         mutt_update_encoding (msg->content);
657         menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
658         mutt_message_hook (NULL, msg, M_SEND2HOOK);
659         break;
660       }
661       /* fall through */
662     case OP_COMPOSE_EDIT_HEADERS:
663       if ((op == OP_COMPOSE_EDIT_HEADERS ||
664            (op == OP_COMPOSE_EDIT_MESSAGE && option (OPTEDITHDRS)))) {
665         const char *tag = NULL;
666         char *err = NULL;
667
668         mutt_env_to_local (msg->env);
669         mutt_edit_headers (NONULL (Editor), msg->content->filename, msg,
670                            fcc, fcclen);
671         if (mutt_env_to_idna (msg->env, &tag, &err)) {
672           mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
673           p_delete(&err);
674         }
675       }
676       mutt_update_encoding (msg->content);
677
678       /* attachments may have been added */
679       if (idxlen && idx[idxlen - 1]->content->next) {
680         for (i = 0; i < idxlen; i++)
681           p_delete(&idx[i]);
682         idxlen = 0;
683         idx =
684           mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0,
685                                 1);
686         menu->data = idx;
687         menu->max = idxlen;
688       }
689
690       menu->redraw = REDRAW_FULL;
691       mutt_message_hook (NULL, msg, M_SEND2HOOK);
692       break; 
693
694     case OP_COMPOSE_ATTACH_KEY:
695       if (idxlen == idxmax) {
696         p_realloc(&idx, idxmax += 5);
697         menu->data = idx;
698       }
699
700       idx[idxlen] = p_new(ATTACHPTR, 1);
701       if ((idx[idxlen]->content =
702            crypt_pgp_make_key_attachment (NULL)) != NULL) {
703         update_idx (menu, idx, idxlen++);
704         menu->redraw |= REDRAW_INDEX;
705       }
706       else
707         p_delete(&idx[idxlen]);
708
709       menu->redraw |= REDRAW_STATUS;
710
711       if (option (OPTNEEDREDRAW)) {
712         menu->redraw = REDRAW_FULL;
713         unset_option (OPTNEEDREDRAW);
714       }
715
716       mutt_message_hook (NULL, msg, M_SEND2HOOK);
717       break; 
718
719     case OP_COMPOSE_ATTACH_FILE:
720       {
721         char *prompt, **files;
722         int error, numfiles;
723
724         fname[0] = 0;
725         prompt = _("Attach file");
726         numfiles = 0;
727         files = NULL;
728
729         if (_mutt_enter_fname
730             (prompt, fname, sizeof (fname), &menu->redraw, 0, 1, &files,
731              &numfiles) == -1 || *fname == '\0')
732           break;
733
734         if (idxlen + numfiles >= idxmax) {
735           p_realloc(&idx, idxmax += 5 + numfiles);
736           menu->data = idx;
737         }
738
739         error = 0;
740         if (numfiles > 1)
741           mutt_message _("Attaching selected files...");
742
743         for (i = 0; i < numfiles; i++) {
744           char *att = files[i];
745
746           idx[idxlen] = p_new(ATTACHPTR, 1);
747           idx[idxlen]->unowned = 1;
748           idx[idxlen]->content = mutt_make_file_attach (att);
749           if (idx[idxlen]->content != NULL)
750             update_idx (menu, idx, idxlen++);
751           else {
752             error = 1;
753             mutt_error (_("Unable to attach %s!"), att);
754             p_delete(&idx[idxlen]);
755           }
756         }
757
758         p_delete(&files);
759         if (!error)
760           mutt_clear_error ();
761
762         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
763       }
764       mutt_message_hook (NULL, msg, M_SEND2HOOK);
765       break;
766
767     case OP_COMPOSE_ATTACH_MESSAGE:
768 #ifdef USE_NNTP
769     case OP_COMPOSE_ATTACH_NEWS_MESSAGE:
770 #endif
771       {
772         char *prompt;
773         HEADER *h;
774
775         fname[0] = 0;
776         prompt = _("Open mailbox to attach message from");
777
778 #ifdef USE_NNTP
779         unset_option (OPTNEWS);
780         if (op == OP_COMPOSE_ATTACH_NEWS_MESSAGE) {
781           if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)))
782             break;
783
784           prompt = _("Open newsgroup to attach message from");
785           set_option (OPTNEWS);
786         }
787 #endif
788
789         if (Context)
790 #ifdef USE_NNTP
791           if ((op == OP_COMPOSE_ATTACH_MESSAGE) ^ (Context->magic == M_NNTP))
792 #endif
793           {
794             m_strcpy(fname, sizeof(fname), NONULL(Context->path));
795             mutt_pretty_mailbox (fname);
796           }
797
798         if (mutt_enter_fname (prompt, fname, sizeof (fname), &menu->redraw, 1)
799             == -1 || !fname[0])
800           break;
801
802 #ifdef USE_NNTP
803         if (option (OPTNEWS))
804           nntp_expand_path (fname, sizeof (fname),
805                             &CurrentNewsSrv->conn->account);
806         else
807 #endif
808           mutt_expand_path (fname, sizeof (fname));
809         if (mx_get_magic (fname) != M_IMAP)
810           if (mx_get_magic (fname) != M_POP)
811 #ifdef USE_NNTP
812             if (mx_get_magic (fname) != M_NNTP && !option (OPTNEWS))
813 #endif
814               /* check to make sure the file exists and is readable */
815               if (access (fname, R_OK) == -1) {
816                 mutt_perror (fname);
817                 break;
818               }
819
820         menu->redraw = REDRAW_FULL;
821
822         ctx = mx_open_mailbox (fname, M_READONLY, NULL);
823         if (ctx == NULL) {
824           mutt_perror (fname);
825           break;
826         }
827
828         if (!ctx->msgcount) {
829           mx_close_mailbox (ctx, NULL);
830           p_delete(&ctx);
831           mutt_error _("No messages in that folder.");
832
833           break;
834         }
835
836         this = Context;         /* remember current folder and sort methods */
837         oldSort = Sort;
838         oldSortAux = SortAux;
839
840         Context = ctx;
841         set_option (OPTATTACHMSG);
842         mutt_message _("Tag the messages you want to attach!");
843
844         closed = mutt_index_menu ();
845         unset_option (OPTATTACHMSG);
846
847         if (!Context) {
848           /* go back to the folder we started from */
849           Context = this;
850           /* Restore old $sort and $sort_aux */
851           Sort = oldSort;
852           SortAux = oldSortAux;
853           menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
854           break;
855         }
856
857         if (idxlen + Context->tagged >= idxmax) {
858           p_realloc(&idx, idxmax += 5 + Context->tagged);
859           menu->data = idx;
860         }
861
862         for (i = 0; i < Context->msgcount; i++) {
863           h = Context->hdrs[i];
864           if (h->tagged) {
865             idx[idxlen] = p_new(ATTACHPTR, 1);
866             idx[idxlen]->content = mutt_make_message_attach (Context, h, 1);
867             if (idx[idxlen]->content != NULL)
868               update_idx (menu, idx, idxlen++);
869             else {
870               mutt_error _("Unable to attach!");
871
872               p_delete(&idx[idxlen]);
873             }
874           }
875         }
876         menu->redraw |= REDRAW_FULL;
877
878         if (closed == OP_QUIT)
879           mx_close_mailbox (Context, NULL);
880         else
881           mx_fastclose_mailbox (Context);
882         p_delete(&Context);
883
884         /* go back to the folder we started from */
885         Context = this;
886         /* Restore old $sort and $sort_aux */
887         Sort = oldSort;
888         SortAux = oldSortAux;
889       }
890       mutt_message_hook (NULL, msg, M_SEND2HOOK);
891       break;
892
893     case OP_DELETE:
894       CHECK_COUNT;
895       if (idx[menu->current]->unowned)
896         idx[menu->current]->content->unlink = 0;
897       if (delete_attachment (menu, &idxlen, menu->current) == -1)
898         break;
899       mutt_update_tree (idx, idxlen);
900       if (idxlen) {
901         if (menu->current > idxlen - 1)
902           menu->current = idxlen - 1;
903       }
904       else
905         menu->current = 0;
906
907       if (menu->current == 0)
908         msg->content = idx[0]->content;
909
910       menu->redraw |= REDRAW_STATUS;
911       mutt_message_hook (NULL, msg, M_SEND2HOOK);
912       break;
913
914 #define CURRENT idx[menu->current]->content
915
916     case OP_COMPOSE_TOGGLE_RECODE:
917       {
918         CHECK_COUNT;
919         if (!mutt_is_text_part (CURRENT)) {
920           mutt_error (_("Recoding only affects text attachments."));
921           break;
922         }
923         CURRENT->noconv = !CURRENT->noconv;
924         if (CURRENT->noconv)
925           mutt_message (_("The current attachment won't be converted."));
926         else
927           mutt_message (_("The current attachment will be converted."));
928         menu->redraw = REDRAW_CURRENT;
929         mutt_message_hook (NULL, msg, M_SEND2HOOK);
930         break;
931       }
932 #undef CURRENT
933
934     case OP_COMPOSE_EDIT_DESCRIPTION:
935       CHECK_COUNT;
936       m_strcpy(buf, sizeof(buf),
937                NONULL(idx[menu->current]->content->description));
938       /* header names should not be translated */
939       if (mutt_get_field ("Description: ", buf, sizeof (buf), 0) == 0) {
940         m_strreplace(&idx[menu->current]->content->description, buf);
941         menu->redraw = REDRAW_CURRENT;
942       }
943       mutt_message_hook (NULL, msg, M_SEND2HOOK);
944       break;
945
946     case OP_COMPOSE_UPDATE_ENCODING:
947       CHECK_COUNT;
948       if (menu->tagprefix) {
949         BODY *top;
950
951         for (top = msg->content; top; top = top->next) {
952           if (top->tagged)
953             mutt_update_encoding (top);
954         }
955         menu->redraw = REDRAW_FULL;
956       }
957       else {
958         mutt_update_encoding (idx[menu->current]->content);
959         menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
960       }
961       mutt_message_hook (NULL, msg, M_SEND2HOOK);
962       break;
963
964     case OP_COMPOSE_TOGGLE_DISPOSITION:
965       /* toggle the content-disposition between inline/attachment */
966       idx[menu->current]->content->disposition =
967         (idx[menu->current]->content->disposition ==
968          DISPINLINE) ? DISPATTACH : DISPINLINE;
969       menu->redraw = REDRAW_CURRENT;
970       break;
971
972     case OP_EDIT_TYPE:
973       CHECK_COUNT;
974       {
975         mutt_edit_content_type (NULL, idx[menu->current]->content, NULL);
976
977         /* this may have been a change to text/something */
978         mutt_update_encoding (idx[menu->current]->content);
979
980         menu->redraw = REDRAW_CURRENT;
981       }
982       mutt_message_hook (NULL, msg, M_SEND2HOOK);
983       break;
984
985     case OP_COMPOSE_EDIT_ENCODING:
986       CHECK_COUNT;
987       m_strcpy(buf, sizeof(buf),
988                ENCODING(idx[menu->current]->content->encoding));
989       if (mutt_get_field ("Content-Transfer-Encoding: ", buf,
990                           sizeof (buf), 0) == 0 && buf[0]) {
991         if ((i = mutt_check_encoding (buf)) != ENCOTHER && i != ENCUUENCODED) {
992           idx[menu->current]->content->encoding = i;
993           menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
994           mutt_clear_error ();
995         }
996         else
997           mutt_error _("Invalid encoding.");
998       }
999       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1000       break;
1001
1002     case OP_COMPOSE_SEND_MESSAGE:
1003
1004       /* Note: We don't invoke send2-hook here, since we want to leave
1005        * users an opportunity to change settings from the ":" prompt.
1006        */
1007
1008       if (check_attachments (idx, idxlen) != 0) {
1009         menu->redraw = REDRAW_FULL;
1010         break;
1011       }
1012
1013       if (msg->chain && mix_check_message (msg) != 0)
1014         break;
1015
1016       if (!fccSet && *fcc) {
1017         if ((i = query_quadoption (OPT_COPY,
1018                                    _("Save a copy of this message?"))) == -1)
1019           break;
1020         else if (i == M_NO)
1021           *fcc = 0;
1022       }
1023
1024       loop = 0;
1025       r = 0;
1026       break;
1027
1028     case OP_COMPOSE_EDIT_FILE:
1029       CHECK_COUNT;
1030       mutt_edit_file (NONULL (Editor), idx[menu->current]->content->filename);
1031       mutt_update_encoding (idx[menu->current]->content);
1032       menu->redraw = REDRAW_CURRENT | REDRAW_STATUS;
1033       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1034       break;
1035
1036     case OP_COMPOSE_TOGGLE_UNLINK:
1037       CHECK_COUNT;
1038       idx[menu->current]->content->unlink =
1039         !idx[menu->current]->content->unlink;
1040
1041       menu->redraw = REDRAW_INDEX;
1042       /* No send2hook since this doesn't change the message. */
1043       break;
1044
1045     case OP_COMPOSE_GET_ATTACHMENT:
1046       CHECK_COUNT;
1047       if (menu->tagprefix) {
1048         BODY *top;
1049
1050         for (top = msg->content; top; top = top->next) {
1051           if (top->tagged)
1052             mutt_get_tmp_attachment (top);
1053         }
1054         menu->redraw = REDRAW_FULL;
1055       }
1056       else if (mutt_get_tmp_attachment (idx[menu->current]->content) == 0)
1057         menu->redraw = REDRAW_CURRENT;
1058
1059       /* No send2hook since this doesn't change the message. */
1060       break;
1061
1062     case OP_COMPOSE_RENAME_FILE:
1063       CHECK_COUNT;
1064       m_strcpy(fname, sizeof(fname), idx[menu->current]->content->filename);
1065       mutt_pretty_mailbox (fname);
1066       if (mutt_get_field (_("Rename to: "), fname, sizeof (fname), M_FILE)
1067           == 0 && fname[0]) {
1068         if (stat (idx[menu->current]->content->filename, &st) == -1) {
1069           mutt_error (_("Can't stat %s: %s"), fname, strerror (errno));
1070           break;
1071         }
1072
1073         mutt_expand_path (fname, sizeof (fname));
1074         if (mutt_rename_file (idx[menu->current]->content->filename, fname))
1075           break;
1076
1077         m_strreplace(&idx[menu->current]->content->filename, fname);
1078         menu->redraw = REDRAW_CURRENT;
1079
1080         if (idx[menu->current]->content->stamp >= st.st_mtime)
1081           mutt_stamp_attachment (idx[menu->current]->content);
1082
1083       }
1084       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1085       break;
1086
1087     case OP_COMPOSE_NEW_MIME:
1088       {
1089         char type[STRING];
1090         char *p;
1091         int itype;
1092         FILE *fp;
1093
1094         CLEARLINE (LINES - 1);
1095         fname[0] = 0;
1096         if (mutt_get_field (_("New file: "), fname, sizeof (fname), M_FILE)
1097             != 0 || !fname[0])
1098           continue;
1099         mutt_expand_path (fname, sizeof (fname));
1100
1101         /* Call to lookup_mime_type () ?  maybe later */
1102         type[0] = 0;
1103         if (mutt_get_field ("Content-Type: ", type, sizeof (type), 0) != 0
1104             || !type[0])
1105           continue;
1106
1107         if (!(p = strchr (type, '/'))) {
1108           mutt_error _("Content-Type is of the form base/sub");
1109
1110           continue;
1111         }
1112         *p++ = 0;
1113         if ((itype = mutt_check_mime_type (type)) == TYPEOTHER) {
1114           mutt_error (_("Unknown Content-Type %s"), type);
1115           continue;
1116         }
1117         if (idxlen == idxmax) {
1118           p_realloc(&idx, idxmax += 5);
1119           menu->data = idx;
1120         }
1121
1122         idx[idxlen] = p_new(ATTACHPTR, 1);
1123         /* Touch the file */
1124         if (!(fp = safe_fopen (fname, "w"))) {
1125           mutt_error (_("Can't create file %s"), fname);
1126           p_delete(&idx[idxlen]);
1127           continue;
1128         }
1129         m_fclose(&fp);
1130
1131         if ((idx[idxlen]->content = mutt_make_file_attach (fname)) == NULL) {
1132           mutt_error
1133             _("What we have here is a failure to make an attachment");
1134           continue;
1135         }
1136         update_idx (menu, idx, idxlen++);
1137
1138         idx[menu->current]->content->type = itype;
1139         m_strreplace(&idx[menu->current]->content->subtype, p);
1140         idx[menu->current]->content->unlink = 1;
1141         menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
1142
1143         if (mutt_compose_attachment (idx[menu->current]->content)) {
1144           mutt_update_encoding (idx[menu->current]->content);
1145           menu->redraw = REDRAW_FULL;
1146         }
1147       }
1148       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1149       break;
1150
1151     case OP_COMPOSE_EDIT_MIME:
1152       CHECK_COUNT;
1153       if (mutt_edit_attachment (idx[menu->current]->content)) {
1154         mutt_update_encoding (idx[menu->current]->content);
1155         menu->redraw = REDRAW_FULL;
1156       }
1157       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1158       break;
1159
1160     case OP_VIEW_ATTACH:
1161     case OP_DISPLAY_HEADERS:
1162       CHECK_COUNT;
1163       mutt_attach_display_loop (menu, op, NULL, NULL, NULL, &idx, &idxlen,
1164                                 NULL, 0);
1165       menu->redraw = REDRAW_FULL;
1166       /* no send2hook, since this doesn't modify the message */
1167       break;
1168
1169     case OP_SAVE:
1170       CHECK_COUNT;
1171       mutt_save_attachment_list (NULL, menu->tagprefix,
1172                                  menu->tagprefix ? msg->content : idx[menu->
1173                                                                       current]->
1174                                  content, NULL, menu);
1175       MAYBE_REDRAW (menu->redraw);
1176       /* no send2hook, since this doesn't modify the message */
1177       break;
1178
1179     case OP_PRINT:
1180       CHECK_COUNT;
1181       mutt_print_attachment_list (NULL, menu->tagprefix,
1182                                   menu->tagprefix ? msg->content : idx[menu->
1183                                                                        current]->
1184                                   content);
1185       /* no send2hook, since this doesn't modify the message */
1186       break;
1187
1188     case OP_PIPE:
1189     case OP_FILTER:
1190       CHECK_COUNT;
1191       mutt_pipe_attachment_list (NULL, menu->tagprefix,
1192                                  menu->tagprefix ? msg->content : idx[menu->
1193                                                                       current]->
1194                                  content, op == OP_FILTER);
1195       if (op == OP_FILTER)      /* cte might have changed */
1196         menu->redraw = menu->tagprefix ? REDRAW_FULL : REDRAW_CURRENT;
1197       menu->redraw |= REDRAW_STATUS;
1198       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1199       break;
1200
1201     case OP_EXIT:
1202       if ((i =
1203            query_quadoption (OPT_POSTPONE,
1204                              _("Postpone this message?"))) == M_NO) {
1205         while (idxlen-- > 0) {
1206           /* avoid freeing other attachments */
1207           idx[idxlen]->content->next = NULL;
1208           idx[idxlen]->content->parts = NULL;
1209           if (idx[idxlen]->unowned)
1210             idx[idxlen]->content->unlink = 0;
1211           body_list_wipe(&idx[idxlen]->content);
1212           p_delete(&idx[idxlen]->tree);
1213           p_delete(&idx[idxlen]);
1214         }
1215         p_delete(&idx);
1216         idxlen = 0;
1217         idxmax = 0;
1218         r = -1;
1219         loop = 0;
1220         break;
1221       }
1222       else if (i == -1)
1223         break;                  /* abort */
1224
1225       /* fall through to postpone! */
1226
1227     case OP_COMPOSE_POSTPONE_MESSAGE:
1228
1229       if (check_attachments (idx, idxlen) != 0) {
1230         menu->redraw = REDRAW_FULL;
1231         break;
1232       }
1233
1234       loop = 0;
1235       r = 1;
1236       break;
1237
1238     case OP_COMPOSE_ISPELL:
1239       endwin ();
1240       snprintf (buf, sizeof (buf), "%s -x %s", NONULL (Ispell),
1241                 msg->content->filename);
1242       if (mutt_system (buf) == -1)
1243         mutt_error (_("Error running \"%s\"!"), buf);
1244       else {
1245         mutt_update_encoding (msg->content);
1246         menu->redraw |= REDRAW_STATUS;
1247       }
1248       break;
1249
1250     case OP_COMPOSE_WRITE_MESSAGE:
1251
1252       fname[0] = '\0';
1253       if (Context) {
1254         m_strcpy(fname, sizeof(fname), NONULL(Context->path));
1255         mutt_pretty_mailbox (fname);
1256       }
1257       if (idxlen)
1258         msg->content = idx[0]->content;
1259       if (mutt_enter_fname
1260           (_("Write message to mailbox"), fname, sizeof (fname),
1261            &menu->redraw, 1) != -1 && fname[0]) {
1262         mutt_message (_("Writing message to %s ..."), fname);
1263         mutt_expand_path (fname, sizeof (fname));
1264
1265         if (msg->content->next)
1266           msg->content = mutt_make_multipart (msg->content);
1267
1268         if (mutt_write_fcc (NONULL (fname), msg, NULL, 1, NULL) < 0)
1269           msg->content = mutt_remove_multipart (msg->content);
1270         else
1271           mutt_message _("Message written.");
1272       }
1273       break;
1274
1275     case OP_COMPOSE_PGP_MENU:
1276       if (msg->security & APPLICATION_SMIME) {
1277         if (mutt_yesorno (_("S/MIME already selected. Clear & continue ? "),
1278                           M_YES) != M_YES) {
1279           mutt_clear_error ();
1280           break;
1281         }
1282         msg->security = 0;
1283       }
1284       msg->security = crypt_pgp_send_menu (msg, &menu->redraw);
1285       redraw_crypt_lines (msg);
1286       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1287       break;
1288
1289     case OP_FORGET_PASSPHRASE:
1290       crypt_forget_passphrase ();
1291       break;
1292
1293     case OP_COMPOSE_SMIME_MENU:
1294       if (msg->security & APPLICATION_PGP) {
1295         if (mutt_yesorno (_("PGP already selected. Clear & continue ? "),
1296                           M_YES) != M_YES) {
1297           mutt_clear_error ();
1298           break;
1299         }
1300         msg->security = 0;
1301       }
1302       msg->security = crypt_smime_send_menu (msg, &menu->redraw);
1303       redraw_crypt_lines (msg);
1304       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1305       break;
1306
1307     case OP_COMPOSE_MIX:
1308       mix_make_chain (&msg->chain, &menu->redraw);
1309       mutt_message_hook (NULL, msg, M_SEND2HOOK);
1310       break;
1311     }
1312
1313     /* Draw formated compose status line */
1314     if (menu->redraw & REDRAW_STATUS) {
1315       compose_status_line (buf, sizeof (buf), menu, NONULL (ComposeFormat));
1316       CLEARLINE (option (OPTSTATUSONTOP) ? 0 : LINES - 2);
1317       SETCOLOR (MT_COLOR_STATUS);
1318       move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, SW);
1319       printw ("%-*.*s", COLS-SW, COLS-SW, buf);
1320       SETCOLOR (MT_COLOR_NORMAL);
1321       menu->redraw &= ~REDRAW_STATUS;
1322     }
1323   }
1324
1325   mutt_menuDestroy (&menu);
1326
1327   if (idxlen) {
1328     msg->content = idx[0]->content;
1329     for (i = 0; i < idxlen; i++) {
1330       idx[i]->content->aptr = NULL;
1331       p_delete(&idx[i]);
1332     }
1333   } else {
1334     msg->content = NULL;
1335   }
1336
1337   p_delete(&idx);
1338
1339   return (r);
1340 }