Rocco Rutte:
[apps/madmutt.git] / menu.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "mutt_curses.h"
25 #include "mutt_menu.h"
26 #include "mbyte.h"
27 #include "sidebar.h"
28
29 #ifdef USE_IMAP
30 #include "imap.h"
31 #endif
32
33 #include <string.h>
34 #include <stdlib.h>
35
36 extern int Charset_is_utf8; /* FIXME: bad modularisation */
37
38 extern size_t UngetCount;
39
40 static void print_enriched_string (int attr, unsigned char *s, int do_color)
41 {
42   wchar_t wc;
43   size_t k;
44   size_t n = mutt_strlen ((char *)s);
45   mbstate_t mbstate;
46
47   memset (&mbstate, 0, sizeof (mbstate));
48   while (*s)
49   {
50     if (*s < M_TREE_MAX)
51     {
52       if (do_color)
53         SETCOLOR (MT_COLOR_TREE);
54       while (*s && *s < M_TREE_MAX)
55       {
56         switch (*s)
57         {
58           case M_TREE_LLCORNER:
59             if (option (OPTASCIICHARS))
60               addch ('`');
61             else if (Charset_is_utf8)
62               addstr ("\342\224\224"); /* WACS_LLCORNER */
63             else
64               addch (ACS_LLCORNER);
65             break;
66           case M_TREE_ULCORNER:
67             if (option (OPTASCIICHARS))
68               addch (',');
69             else if (Charset_is_utf8)
70               addstr ("\342\224\214"); /* WACS_ULCORNER */
71             else
72               addch (ACS_ULCORNER);
73             break;
74           case M_TREE_LTEE:
75             if (option (OPTASCIICHARS))
76               addch ('|');
77             else if (Charset_is_utf8)
78               addstr ("\342\224\234"); /* WACS_LTEE */
79             else
80               addch (ACS_LTEE);
81             break;
82           case M_TREE_HLINE:
83             if (option (OPTASCIICHARS))
84               addch ('-');
85             else if (Charset_is_utf8)
86               addstr ("\342\224\200"); /* WACS_HLINE */
87             else
88               addch (ACS_HLINE);
89             break;
90           case M_TREE_VLINE:
91             if (option (OPTASCIICHARS))
92               addch ('|');
93             else if (Charset_is_utf8)
94               addstr ("\342\224\202"); /* WACS_VLINE */
95             else
96               addch (ACS_VLINE);
97             break;
98           case M_TREE_TTEE:
99             if (option (OPTASCIICHARS))
100               addch ('-');
101             else if (Charset_is_utf8)
102               addstr ("\342\224\254"); /* WACS_TTEE */
103             else
104               addch (ACS_TTEE);
105             break;
106           case M_TREE_BTEE:
107             if (option (OPTASCIICHARS))
108               addch ('-');
109             else if (Charset_is_utf8)
110               addstr ("\342\224\264"); /* WACS_BTEE */
111             else
112               addch (ACS_BTEE);
113             break;
114           case M_TREE_SPACE:
115             addch (' ');
116             break;
117           case M_TREE_RARROW:
118             addch ('>');
119             break;
120           case M_TREE_STAR:
121             addch ('*'); /* fake thread indicator */
122             break;
123           case M_TREE_HIDDEN:
124             addch ('&');
125             break;
126           case M_TREE_EQUALS:
127             addch ('=');
128             break;
129           case M_TREE_MISSING:
130             addch ('?');
131             break;
132         }
133         s++, n--;
134       }
135       if (do_color) attrset(attr);
136     }
137     else if ((k = mbrtowc (&wc, (char *)s, n, &mbstate)) > 0)
138     {
139       addnstr ((char *)s, k);
140       s += k, n-= k;
141     }
142     else
143       break;
144   }
145 }
146
147 static void menu_make_entry (char *s, int l, MUTTMENU *menu, int i) 
148 {
149   if (menu->dialog) 
150   {
151     strncpy (s, menu->dialog[i], l);
152     menu->current = -1; /* hide menubar */
153   }
154   else
155     menu->make_entry (s, l, menu, i);
156 }
157
158 void menu_pad_string (char *s, size_t n)
159 {
160   int shift = option (OPTARROWCURSOR) ? 3 : 0;
161   int cols = COLS - shift - SidebarWidth;
162
163   char tmpbuf[n];
164
165   mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, strlen (s), 1);
166   tmpbuf[n - 1] = 0;
167
168   snprintf(s,n,"%s",tmpbuf); /* overkill */
169 }
170
171 void menu_redraw_full (MUTTMENU *menu)
172 {
173   SETCOLOR (MT_COLOR_NORMAL);
174   /* clear() doesn't optimize screen redraws */
175   move (0, 0);
176   clrtobot ();
177
178   if (option (OPTHELP))
179   {
180     SETCOLOR (MT_COLOR_STATUS);
181     move (option (OPTSTATUSONTOP) ? LINES-2 : 0, 0);
182     mutt_paddstr (COLS, menu->help);
183     SETCOLOR (MT_COLOR_NORMAL);
184     menu->offset = 1;
185     menu->pagelen = LINES - 3;
186   }
187   else
188   {
189     menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
190     menu->pagelen = LINES - 2;
191   }
192
193   mutt_show_error ();
194
195   menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
196 }
197
198 void menu_redraw_status (MUTTMENU *menu)
199 {
200   char buf[STRING];
201
202   snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
203   SETCOLOR (MT_COLOR_STATUS);
204   move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
205   mutt_paddstr (COLS, buf);
206   SETCOLOR (MT_COLOR_NORMAL);
207   menu->redraw &= ~REDRAW_STATUS;
208 }
209
210 void menu_redraw_index (MUTTMENU *menu)
211 {
212   char buf[STRING];
213   int i;
214
215   draw_sidebar(1);
216   for (i = menu->top; i < menu->top + menu->pagelen; i++)
217   {
218     if (i < menu->max)
219     {
220       menu_make_entry (buf, sizeof (buf), menu, i);
221       menu_pad_string (buf, sizeof (buf));
222
223       if (option (OPTARROWCURSOR))
224       {
225         attrset (menu->color (i));
226         CLEARLINE_WIN (i - menu->top + menu->offset);
227
228         if (i == menu->current)
229         {
230           attrset (menu->color (i));
231           ADDCOLOR (MT_COLOR_INDICATOR);
232           addstr ("->");
233           attrset (menu->color (i));
234           addch (' ');
235         }
236         else
237           move (i - menu->top + menu->offset, SidebarWidth + 3);
238
239         print_enriched_string (menu->color(i), (unsigned char *) buf, 1);
240         SETCOLOR (MT_COLOR_NORMAL);          
241       }
242       else
243       {
244         attrset (menu->color (i));
245             
246         if (i == menu->current)
247         {
248           ADDCOLOR (MT_COLOR_INDICATOR);
249           BKGDSET (MT_COLOR_INDICATOR);
250         }
251
252         CLEARLINE_WIN (i - menu->top + menu->offset);
253         print_enriched_string (menu->color(i), (unsigned char *) buf, i != menu->current);
254         SETCOLOR (MT_COLOR_NORMAL);
255         BKGDSET (MT_COLOR_NORMAL);
256       }
257     }
258     else
259       CLEARLINE_WIN (i - menu->top + menu->offset);
260   }
261   menu->redraw = 0;
262 }
263
264 void menu_redraw_motion (MUTTMENU *menu)
265 {
266   char buf[STRING];
267
268   if (menu->dialog) 
269   {
270     menu->redraw &= ~REDRAW_MOTION;
271     return;
272   }
273   
274   move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
275   SETCOLOR (MT_COLOR_NORMAL);
276   BKGDSET (MT_COLOR_NORMAL);
277
278   if (option (OPTARROWCURSOR))
279   {
280     /* clear the pointer */
281     attrset (menu->color (menu->oldcurrent));
282     addstr ("  ");
283
284     if (menu->redraw & REDRAW_MOTION_RESYNCH)
285     {
286       clrtoeol ();
287       menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
288       menu_pad_string (buf, sizeof (buf));
289       move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth + 3);
290       print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
291       SETCOLOR (MT_COLOR_NORMAL);
292     }
293
294     /* now draw it in the new location */
295     move (menu->current + menu->offset - menu->top, SidebarWidth);
296     attrset (menu->color (menu->current));
297     ADDCOLOR (MT_COLOR_INDICATOR);
298     addstr ("->");
299     SETCOLOR (MT_COLOR_NORMAL);
300   }
301   else
302   {
303     /* erase the current indicator */
304     attrset (menu->color (menu->oldcurrent));
305     clrtoeol ();
306     menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
307     menu_pad_string (buf, sizeof (buf));
308     print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
309
310     /* now draw the new one to reflect the change */
311     menu_make_entry (buf, sizeof (buf), menu, menu->current);
312     menu_pad_string (buf, sizeof (buf));
313     attrset (menu->color (menu->current));
314     ADDCOLOR (MT_COLOR_INDICATOR);
315     BKGDSET (MT_COLOR_INDICATOR);
316     CLEARLINE_WIN (menu->current - menu->top + menu->offset);
317     print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
318     SETCOLOR (MT_COLOR_NORMAL);
319     BKGDSET (MT_COLOR_NORMAL);
320   }
321   menu->redraw &= REDRAW_STATUS;
322 }
323
324 void menu_redraw_current (MUTTMENU *menu)
325 {
326   char buf[STRING];
327   
328   move (menu->current + menu->offset - menu->top, SidebarWidth);
329   menu_make_entry (buf, sizeof (buf), menu, menu->current);
330   menu_pad_string (buf, sizeof (buf));
331
332   if (option (OPTARROWCURSOR))
333   {
334     int attr = menu->color (menu->current);
335     attrset (attr);
336     clrtoeol ();
337     attrset (menu->color (menu->current));
338     ADDCOLOR (MT_COLOR_INDICATOR);
339     addstr ("->");
340     attrset (attr);
341     addch (' ');
342     menu_pad_string (buf, sizeof (buf));
343     print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 1);
344     SETCOLOR (MT_COLOR_NORMAL);
345   }
346   else
347   {
348     attrset (menu->color (menu->current));
349     ADDCOLOR (MT_COLOR_INDICATOR);
350     BKGDSET (MT_COLOR_INDICATOR);
351     clrtoeol ();
352     print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
353     SETCOLOR (MT_COLOR_NORMAL);
354     BKGDSET (MT_COLOR_NORMAL);
355   }
356   menu->redraw &= REDRAW_STATUS;
357 }
358
359 void menu_redraw_prompt (MUTTMENU *menu)
360 {
361   if (menu->dialog) 
362   {
363     if (option (OPTMSGERR)) 
364     {
365       mutt_sleep (1);
366       unset_option (OPTMSGERR);
367     }
368
369     if (*Errorbuf)
370       mutt_clear_error ();
371
372     SETCOLOR (MT_COLOR_NORMAL);
373     mvaddstr (LINES - 1, 0, menu->prompt);
374     clrtoeol ();
375   }
376 }
377
378 void menu_check_recenter (MUTTMENU *menu)
379 {
380   int c = MIN (MenuContext, menu->pagelen / 2);
381   int old_top = menu->top;
382
383   if (menu->max <= menu->pagelen) /* less entries than lines */
384   {
385     if (menu->top != 0) {
386       menu->top = 0;
387       set_option (OPTNEEDREDRAW);
388     }
389   }
390   else if (menu->current >= menu->top + menu->pagelen - c) /* indicator below bottom threshold */
391   {
392     if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
393       menu->top = menu->current - menu->pagelen + c + 1;
394     else
395       menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
396   }
397   else if (menu->current < menu->top + c) /* indicator above top threshold */
398   {
399     if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
400       menu->top = menu->current - c;
401     else
402       menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
403   }
404
405   /* make entries stick to bottom */
406   menu->top = MIN (menu->top, menu->max - menu->pagelen);
407   menu->top = MAX (menu->top, 0);
408
409   if (menu->top != old_top)
410     menu->redraw |= REDRAW_INDEX;
411 }
412
413 void menu_jump (MUTTMENU *menu)
414 {
415   int n;
416   char buf[SHORT_STRING];
417
418   if (menu->max)
419   {
420     mutt_ungetch (LastKey, 0);
421     buf[0] = 0;
422     if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0])
423     {
424       n = atoi (buf) - 1;
425       if (n >= 0 && n < menu->max)
426       {
427         menu->current = n;
428         menu->redraw = REDRAW_MOTION;
429       }
430       else
431         mutt_error _("Invalid index number.");
432     }
433   }
434   else
435     mutt_error _("No entries.");
436 }
437
438 void menu_next_line (MUTTMENU *menu)
439 {
440   if (menu->max)
441   {
442     if (menu->top + 1 < menu->max)
443     {
444       menu->top++;
445       if (menu->current < menu->top)
446         menu->current++;
447       menu->redraw = REDRAW_INDEX;
448     }
449     else
450       mutt_error _("You cannot scroll down farther.");
451   }
452   else
453     mutt_error _("No entries.");
454 }
455
456 void menu_prev_line (MUTTMENU *menu)
457 {
458   if (menu->top > 0)
459   {
460     menu->top--;
461     if (menu->current >= menu->top + menu->pagelen)
462       menu->current--;
463     menu->redraw = REDRAW_INDEX;
464   }
465   else
466     mutt_error _("You cannot scroll up farther.");
467 }
468
469 void menu_next_page (MUTTMENU *menu)
470 {
471   if (menu->max)
472   {
473     if (menu->top + menu->pagelen < menu->max)
474     {
475       menu->top += menu->pagelen;
476       if (menu->current < menu->top)
477         menu->current = menu->top;
478       menu->redraw = REDRAW_INDEX;
479     }
480     else if (menu->current != menu->max - 1 && !menu->dialog)
481     {
482       menu->current = menu->max - 1;
483       menu->redraw = REDRAW_MOTION;
484     }
485     else
486       mutt_error _("You are on the last page.");
487   }
488   else
489     mutt_error _("No entries.");
490 }
491
492 void menu_prev_page (MUTTMENU *menu)
493 {
494   int c = MIN (MenuContext, menu->pagelen / 2);
495
496   if (menu->top > c)
497   {
498     if ((menu->top -= menu->pagelen) < 0)
499       menu->top = 0;
500     if (menu->current >= menu->top + menu->pagelen)
501       menu->current = menu->top + menu->pagelen - 1;
502     menu->redraw = REDRAW_INDEX;
503   }
504   else if (menu->current && !menu->dialog)
505   {
506     menu->current = 0;
507     menu->redraw = REDRAW_MOTION;
508   }
509   else
510     mutt_error _("You are on the first page.");
511 }
512
513 void menu_top_page (MUTTMENU *menu)
514 {
515   if (menu->current != menu->top)
516   {
517     menu->current = menu->top;
518     menu->redraw = REDRAW_MOTION;
519   }
520 }
521
522 void menu_bottom_page (MUTTMENU *menu)
523 {
524   if (menu->max)
525   {
526     menu->current = menu->top + menu->pagelen - 1;
527     if (menu->current > menu->max - 1)
528       menu->current = menu->max - 1;
529     menu->redraw = REDRAW_MOTION;
530   }
531   else
532     mutt_error _("No entries.");
533 }
534
535 void menu_middle_page (MUTTMENU *menu)
536 {
537   int i;
538
539   if (menu->max)
540   {
541     i = menu->top + menu->pagelen;
542     if (i > menu->max - 1)
543       i = menu->max - 1;
544     menu->current = menu->top + (i - menu->top) / 2;
545     menu->redraw = REDRAW_MOTION;
546   }
547   else
548     mutt_error _("No entries.");
549 }
550
551 void menu_first_entry (MUTTMENU *menu)
552 {
553   if (menu->max)
554   {
555     menu->current = 0;
556     menu->redraw = REDRAW_MOTION;
557   }
558   else
559     mutt_error _("No entries.");
560 }
561
562 void menu_last_entry (MUTTMENU *menu)
563 {
564   if (menu->max)
565   {
566     menu->current = menu->max - 1;
567     menu->redraw = REDRAW_MOTION;
568   }
569   else
570     mutt_error _("No entries.");
571 }
572
573 void menu_half_up (MUTTMENU *menu)
574 {
575   if (menu->top > 0)
576   {
577     if ((menu->top -= menu->pagelen / 2) < 0)
578       menu->top = 0;
579     if (menu->current >= menu->top + menu->pagelen)
580       menu->current = menu->top + menu->pagelen - 1;
581     menu->redraw = REDRAW_INDEX;
582   }
583   else if (menu->current && !menu->dialog)
584   {
585     menu->current = 0;
586     menu->redraw = REDRAW_MOTION;
587   }
588   else
589     mutt_error _("First entry is shown.");
590 }
591
592 void menu_half_down (MUTTMENU *menu)
593 {
594   if (menu->max)
595   {
596     if (menu->top + menu->pagelen < menu->max)
597     {
598       menu->top += menu->pagelen / 2;
599       if (menu->current < menu->top)
600         menu->current = menu->top;
601       menu->redraw = REDRAW_INDEX;
602     }
603     else if (menu->current != menu->max - 1 && !menu->dialog)
604     {
605       menu->current = menu->max - 1;
606       menu->redraw = REDRAW_INDEX;
607     }
608     else
609       mutt_error _("Last entry is shown.");
610   }
611   else
612     mutt_error _("No entries.");
613 }
614
615 void menu_current_top (MUTTMENU *menu)
616 {
617   if (menu->max)
618   {
619     menu->top = menu->current;
620     menu->redraw = REDRAW_INDEX;
621   }
622   else
623     mutt_error _("No entries.");
624 }
625
626 void menu_current_middle (MUTTMENU *menu)
627 {
628   if (menu->max)
629   {
630     menu->top = menu->current - menu->pagelen / 2;
631     if (menu->top < 0)
632       menu->top = 0;
633     menu->redraw = REDRAW_INDEX;
634   }
635   else
636     mutt_error _("No entries.");
637 }
638
639 void menu_current_bottom (MUTTMENU *menu)
640 {
641   if (menu->max)
642   {
643     menu->top = menu->current - menu->pagelen + 1;
644     if (menu->top < 0)
645       menu->top = 0;
646     menu->redraw = REDRAW_INDEX;
647   }
648   else
649     mutt_error _("No entries.");
650 }
651
652 void menu_next_entry (MUTTMENU *menu)
653 {
654   if (menu->current < menu->max - 1)
655   {
656     menu->current++;
657     menu->redraw = REDRAW_MOTION;
658   }
659   else
660     mutt_error _("You are on the last entry.");
661 }
662
663 void menu_prev_entry (MUTTMENU *menu)
664 {
665   if (menu->current)
666   {
667     menu->current--;
668     menu->redraw = REDRAW_MOTION;
669   }
670   else
671     mutt_error _("You are on the first entry.");
672 }
673
674 static int default_color (int i)
675 {
676    return ColorDefs[MT_COLOR_NORMAL];
677 }
678
679 static int menu_search_generic (MUTTMENU *m, regex_t *re, int n)
680 {
681   char buf[LONG_STRING];
682
683   menu_make_entry (buf, sizeof (buf), m, n);
684   return (regexec (re, buf, 0, NULL, 0));
685 }
686
687 MUTTMENU *mutt_new_menu (void)
688 {
689   MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
690
691   p->current = 0;
692   p->top = 0;
693   p->offset = 1;
694   p->redraw = REDRAW_FULL;
695   p->pagelen = PAGELEN;
696   p->color = default_color;
697   p->search = menu_search_generic;
698   return (p);
699 }
700
701 void mutt_menuDestroy (MUTTMENU **p)
702 {
703   int i;
704
705   FREE (&(*p)->searchBuf);
706
707   if ((*p)->dialog) 
708   {
709     for (i=0; i < (*p)->max; i++)
710       FREE (&(*p)->dialog[i]);
711
712     FREE (& (*p)->dialog);
713   }
714
715   FREE (p);
716 }
717
718 #define M_SEARCH_UP   1
719 #define M_SEARCH_DOWN 2
720
721 static int menu_search (MUTTMENU *menu, int op)
722 {
723   int r;
724   int searchDir;
725   regex_t re;
726   char buf[SHORT_STRING];
727
728   if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
729   {
730     strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
731     if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") : 
732                                             _("Reverse search for: "),
733                          buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
734       return (-1);
735     mutt_str_replace (&menu->searchBuf, buf);
736     menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
737   }
738   else 
739   {
740     if (!menu->searchBuf)
741     {
742       mutt_error _("No search pattern.");
743       return (-1);
744     }
745   }
746
747   searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
748   if (op == OP_SEARCH_OPPOSITE)
749     searchDir = -searchDir;
750
751   if ((r = REGCOMP (&re, menu->searchBuf, REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0)
752   {
753     regerror (r, &re, buf, sizeof (buf));
754     regfree (&re);
755     mutt_error ("%s", buf);
756     return (-1);
757   }
758
759   r = menu->current + searchDir;
760   while (r >= 0 && r < menu->max)
761   {
762     if (menu->search (menu, &re, r) == 0)
763     {
764       regfree (&re);
765       return r;
766     }
767
768     r += searchDir;
769   }
770
771   regfree (&re);
772   mutt_error _("Not found.");
773   return (-1);
774 }
775
776 static int menu_dialog_translate_op (int i)
777 {
778   switch (i)
779   {
780     case OP_NEXT_ENTRY:   
781       return OP_NEXT_LINE;
782     case OP_PREV_ENTRY:          
783       return OP_PREV_LINE;
784     case OP_CURRENT_TOP:   case OP_FIRST_ENTRY:  
785       return OP_TOP_PAGE;
786     case OP_CURRENT_BOTTOM:    case OP_LAST_ENTRY:          
787       return OP_BOTTOM_PAGE;
788     case OP_CURRENT_MIDDLE: 
789       return OP_MIDDLE_PAGE; 
790   }
791   
792   return i;
793 }
794
795 static int menu_dialog_dokey (MUTTMENU *menu, int *ip)
796 {
797   event_t ch;
798   char *p;
799
800   ch = mutt_getch ();
801
802   if (ch.ch == -1)
803   {
804     *ip = -1;
805     return 0;
806   }
807
808   if (ch.ch && (p = strchr (menu->keys, ch.ch)))
809   {
810     *ip = OP_MAX + (p - menu->keys + 1);
811     return 0;
812   }
813   else
814   {
815     mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
816     return -1;
817   }
818 }
819
820 int menu_redraw (MUTTMENU *menu)
821 {
822   /* See if all or part of the screen needs to be updated.  */
823   if (menu->redraw & REDRAW_FULL)
824   {
825     menu_redraw_full (menu);
826     /* allow the caller to do any local configuration */
827     return (OP_REDRAW);
828   }
829   
830   if (!menu->dialog)
831     menu_check_recenter (menu);
832   
833   if (menu->redraw & REDRAW_STATUS)
834     menu_redraw_status (menu);
835   if (menu->redraw & REDRAW_INDEX)
836     menu_redraw_index (menu);
837   else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
838     menu_redraw_motion (menu);
839   else if (menu->redraw == REDRAW_CURRENT)
840     menu_redraw_current (menu);
841   
842   if (menu->dialog)
843     menu_redraw_prompt (menu);
844   
845   return OP_NULL;
846 }
847
848 int mutt_menuLoop (MUTTMENU *menu)
849 {
850   int i = OP_NULL;
851
852   FOREVER
853   {
854     if (option (OPTMENUCALLER))
855     {
856       unset_option (OPTMENUCALLER);
857       return OP_NULL;
858     }
859     
860     
861     mutt_curs_set (0);
862
863 #ifdef USE_IMAP
864     imap_keepalive ();
865 #endif
866
867     if (menu_redraw (menu) == OP_REDRAW)
868       return OP_REDRAW;
869     
870     menu->oldcurrent = menu->current;
871
872
873     /* move the cursor out of the way */
874     move (menu->current - menu->top + menu->offset,
875           (option (OPTARROWCURSOR) ? 2 : COLS-1));
876
877     mutt_refresh ();
878     
879     /* try to catch dialog keys before ops */
880     if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
881       return i;
882                     
883     i = km_dokey (menu->menu);
884     if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND)
885     {
886       if (menu->tagged)
887       {
888         mvaddstr (LINES - 1, 0, "Tag-");
889         clrtoeol ();
890         i = km_dokey (menu->menu);
891         menu->tagprefix = 1;
892         CLEARLINE (LINES - 1);
893       }
894       else if (i == OP_TAG_PREFIX)
895       {
896         mutt_error _("No tagged entries.");
897         i = -1;
898       }
899       else /* None tagged, OP_TAG_PREFIX_COND */
900       {
901         event_t tmp;
902         while(UngetCount>0)
903         {
904           tmp=mutt_getch();
905           if(tmp.op==OP_END_COND)break;
906         }
907         mutt_message _("Nothing to do.");
908         i = -1;
909       }
910     }
911     else if (menu->tagged && option (OPTAUTOTAG))
912       menu->tagprefix = 1;
913     else
914       menu->tagprefix = 0;
915
916     mutt_curs_set (1);
917
918 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
919     if (SigWinch)
920     {
921       mutt_resize_screen ();
922       menu->redraw = REDRAW_FULL;
923       SigWinch = 0;
924       clearok(stdscr,TRUE);/*force complete redraw*/
925     }
926 #endif
927
928     if (i == -1)
929       continue;
930
931     if (!menu->dialog)
932       mutt_clear_error ();
933
934     /* Convert menubar movement to scrolling */
935     if (menu->dialog) 
936       i = menu_dialog_translate_op (i);
937
938     switch (i)
939     {
940       case OP_NEXT_ENTRY:
941         menu_next_entry (menu);
942         break;
943       case OP_PREV_ENTRY:
944         menu_prev_entry (menu);
945         break;
946       case OP_HALF_DOWN:
947         menu_half_down (menu);
948         break;
949       case OP_HALF_UP:
950         menu_half_up (menu);
951         break;
952       case OP_NEXT_PAGE:
953         menu_next_page (menu);
954         break;
955       case OP_PREV_PAGE:
956         menu_prev_page (menu);
957         break;
958       case OP_NEXT_LINE:
959         menu_next_line (menu);
960         break;
961       case OP_PREV_LINE:
962         menu_prev_line (menu);
963         break;
964       case OP_FIRST_ENTRY:
965         menu_first_entry (menu);
966         break;
967       case OP_LAST_ENTRY:
968         menu_last_entry (menu);
969         break;
970       case OP_TOP_PAGE:
971         menu_top_page (menu);
972         break;
973       case OP_MIDDLE_PAGE:
974         menu_middle_page (menu);
975         break;
976       case OP_BOTTOM_PAGE:
977         menu_bottom_page (menu);
978         break;
979       case OP_CURRENT_TOP:
980         menu_current_top (menu);
981         break;
982       case OP_CURRENT_MIDDLE:
983         menu_current_middle (menu);
984         break;
985       case OP_CURRENT_BOTTOM:
986         menu_current_bottom (menu);
987         break;
988       case OP_SEARCH:
989       case OP_SEARCH_REVERSE:
990       case OP_SEARCH_NEXT:
991       case OP_SEARCH_OPPOSITE:
992         if (menu->search && !menu->dialog) /* Searching dialogs won't work */
993         {
994           menu->oldcurrent = menu->current;
995           if ((menu->current = menu_search (menu, i)) != -1)
996             menu->redraw = REDRAW_MOTION;
997           else
998             menu->current = menu->oldcurrent;
999         }
1000         else
1001           mutt_error _("Search is not implemented for this menu.");
1002         break;
1003
1004       case OP_JUMP:
1005         if (menu->dialog)
1006           mutt_error _("Jumping is not implemented for dialogs.");
1007         else
1008           menu_jump (menu);
1009         break;
1010
1011       case OP_ENTER_COMMAND:
1012         CurrentMenu = menu->menu;
1013         mutt_enter_command ();
1014         if (option (OPTFORCEREDRAWINDEX))
1015         {
1016           menu->redraw = REDRAW_FULL;
1017           unset_option (OPTFORCEREDRAWINDEX);
1018           unset_option (OPTFORCEREDRAWPAGER);
1019         }
1020         break;
1021
1022       case OP_TAG:
1023         if (menu->tag && !menu->dialog)
1024         {
1025           if (menu->tagprefix && !option (OPTAUTOTAG))
1026           {
1027             for (i = 0; i < menu->max; i++)
1028               menu->tagged += menu->tag (menu, i, 0);
1029             menu->redraw = REDRAW_INDEX;
1030           }
1031           else if (menu->max)
1032           {
1033             int i = menu->tag (menu, menu->current, -1);
1034             menu->tagged += i;
1035             if (i && option (OPTRESOLVE) && menu->current < menu->max - 1)
1036             {
1037               menu->current++;
1038               menu->redraw = REDRAW_MOTION_RESYNCH;
1039             }
1040             else
1041               menu->redraw = REDRAW_CURRENT;
1042           }
1043           else
1044             mutt_error _("No entries.");
1045         }
1046         else
1047           mutt_error _("Tagging is not supported.");
1048         break;
1049
1050       case OP_SHELL_ESCAPE:
1051         mutt_shell_escape ();
1052         MAYBE_REDRAW (menu->redraw);
1053         break;
1054
1055       case OP_WHAT_KEY:
1056         mutt_what_key ();
1057         break;
1058
1059       case OP_REDRAW:
1060         clearok (stdscr, TRUE);
1061         menu->redraw = REDRAW_FULL;
1062         break;
1063
1064       case OP_HELP:
1065         mutt_help (menu->menu);
1066         menu->redraw = REDRAW_FULL;
1067         break;
1068
1069       case OP_NULL:
1070         km_error_key (menu->menu);
1071         break;
1072
1073       case OP_END_COND:
1074         break;
1075
1076       default:
1077         return (i);
1078     }
1079   }
1080   /* not reached */
1081 }