2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
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.
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.
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.
24 #include "mutt_curses.h"
25 #include "mutt_menu.h"
36 extern int Charset_is_utf8; /* FIXME: bad modularisation */
38 extern size_t UngetCount;
40 static void print_enriched_string (int attr, unsigned char *s, int do_color)
44 size_t n = mutt_strlen ((char *)s);
47 memset (&mbstate, 0, sizeof (mbstate));
53 SETCOLOR (MT_COLOR_TREE);
54 while (*s && *s < M_TREE_MAX)
59 if (option (OPTASCIICHARS))
61 else if (Charset_is_utf8)
62 addstr ("\342\224\224"); /* WACS_LLCORNER */
67 if (option (OPTASCIICHARS))
69 else if (Charset_is_utf8)
70 addstr ("\342\224\214"); /* WACS_ULCORNER */
75 if (option (OPTASCIICHARS))
77 else if (Charset_is_utf8)
78 addstr ("\342\224\234"); /* WACS_LTEE */
83 if (option (OPTASCIICHARS))
85 else if (Charset_is_utf8)
86 addstr ("\342\224\200"); /* WACS_HLINE */
91 if (option (OPTASCIICHARS))
93 else if (Charset_is_utf8)
94 addstr ("\342\224\202"); /* WACS_VLINE */
99 if (option (OPTASCIICHARS))
101 else if (Charset_is_utf8)
102 addstr ("\342\224\254"); /* WACS_TTEE */
107 if (option (OPTASCIICHARS))
109 else if (Charset_is_utf8)
110 addstr ("\342\224\264"); /* WACS_BTEE */
121 addch ('*'); /* fake thread indicator */
135 if (do_color) attrset(attr);
137 else if ((k = mbrtowc (&wc, (char *)s, n, &mbstate)) > 0)
139 addnstr ((char *)s, k);
147 static void menu_make_entry (char *s, int l, MUTTMENU *menu, int i)
151 strncpy (s, menu->dialog[i], l);
152 menu->current = -1; /* hide menubar */
155 menu->make_entry (s, l, menu, i);
158 void menu_pad_string (char *s, size_t n)
160 int shift = option (OPTARROWCURSOR) ? 3 : 0;
161 int cols = COLS - shift - SidebarWidth;
165 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, strlen (s), 1);
168 snprintf(s,n,"%s",tmpbuf); /* overkill */
171 void menu_redraw_full (MUTTMENU *menu)
173 SETCOLOR (MT_COLOR_NORMAL);
174 /* clear() doesn't optimize screen redraws */
178 if (option (OPTHELP))
180 SETCOLOR (MT_COLOR_STATUS);
181 move (option (OPTSTATUSONTOP) ? LINES-2 : 0, 0);
182 mutt_paddstr (COLS, menu->help);
183 SETCOLOR (MT_COLOR_NORMAL);
185 menu->pagelen = LINES - 3;
189 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
190 menu->pagelen = LINES - 2;
195 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
198 void menu_redraw_status (MUTTMENU *menu)
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;
210 void menu_redraw_index (MUTTMENU *menu)
216 for (i = menu->top; i < menu->top + menu->pagelen; i++)
220 menu_make_entry (buf, sizeof (buf), menu, i);
221 menu_pad_string (buf, sizeof (buf));
223 if (option (OPTARROWCURSOR))
225 attrset (menu->color (i));
226 CLEARLINE_WIN (i - menu->top + menu->offset);
228 if (i == menu->current)
230 attrset (menu->color (i));
231 ADDCOLOR (MT_COLOR_INDICATOR);
233 attrset (menu->color (i));
237 move (i - menu->top + menu->offset, SidebarWidth + 3);
239 print_enriched_string (menu->color(i), (unsigned char *) buf, 1);
240 SETCOLOR (MT_COLOR_NORMAL);
244 attrset (menu->color (i));
246 if (i == menu->current)
248 ADDCOLOR (MT_COLOR_INDICATOR);
249 BKGDSET (MT_COLOR_INDICATOR);
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);
259 CLEARLINE_WIN (i - menu->top + menu->offset);
264 void menu_redraw_motion (MUTTMENU *menu)
270 menu->redraw &= ~REDRAW_MOTION;
274 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
275 SETCOLOR (MT_COLOR_NORMAL);
276 BKGDSET (MT_COLOR_NORMAL);
278 if (option (OPTARROWCURSOR))
280 /* clear the pointer */
281 attrset (menu->color (menu->oldcurrent));
284 if (menu->redraw & REDRAW_MOTION_RESYNCH)
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);
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);
299 SETCOLOR (MT_COLOR_NORMAL);
303 /* erase the current indicator */
304 attrset (menu->color (menu->oldcurrent));
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);
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);
321 menu->redraw &= REDRAW_STATUS;
324 void menu_redraw_current (MUTTMENU *menu)
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));
332 if (option (OPTARROWCURSOR))
334 int attr = menu->color (menu->current);
337 attrset (menu->color (menu->current));
338 ADDCOLOR (MT_COLOR_INDICATOR);
342 menu_pad_string (buf, sizeof (buf));
343 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 1);
344 SETCOLOR (MT_COLOR_NORMAL);
348 attrset (menu->color (menu->current));
349 ADDCOLOR (MT_COLOR_INDICATOR);
350 BKGDSET (MT_COLOR_INDICATOR);
352 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
353 SETCOLOR (MT_COLOR_NORMAL);
354 BKGDSET (MT_COLOR_NORMAL);
356 menu->redraw &= REDRAW_STATUS;
359 void menu_redraw_prompt (MUTTMENU *menu)
363 if (option (OPTMSGERR))
366 unset_option (OPTMSGERR);
372 SETCOLOR (MT_COLOR_NORMAL);
373 mvaddstr (LINES - 1, 0, menu->prompt);
378 void menu_check_recenter (MUTTMENU *menu)
380 int c = MIN (MenuContext, menu->pagelen / 2);
381 int old_top = menu->top;
383 if (menu->max <= menu->pagelen) /* less entries than lines */
385 if (menu->top != 0) {
387 set_option (OPTNEEDREDRAW);
390 else if (menu->current >= menu->top + menu->pagelen - c) /* indicator below bottom threshold */
392 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
393 menu->top = menu->current - menu->pagelen + c + 1;
395 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
397 else if (menu->current < menu->top + c) /* indicator above top threshold */
399 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
400 menu->top = menu->current - c;
402 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
405 /* make entries stick to bottom */
406 menu->top = MIN (menu->top, menu->max - menu->pagelen);
407 menu->top = MAX (menu->top, 0);
409 if (menu->top != old_top)
410 menu->redraw |= REDRAW_INDEX;
413 void menu_jump (MUTTMENU *menu)
416 char buf[SHORT_STRING];
420 mutt_ungetch (LastKey, 0);
422 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0])
425 if (n >= 0 && n < menu->max)
428 menu->redraw = REDRAW_MOTION;
431 mutt_error _("Invalid index number.");
435 mutt_error _("No entries.");
438 void menu_next_line (MUTTMENU *menu)
442 if (menu->top + 1 < menu->max)
445 if (menu->current < menu->top)
447 menu->redraw = REDRAW_INDEX;
450 mutt_error _("You cannot scroll down farther.");
453 mutt_error _("No entries.");
456 void menu_prev_line (MUTTMENU *menu)
461 if (menu->current >= menu->top + menu->pagelen)
463 menu->redraw = REDRAW_INDEX;
466 mutt_error _("You cannot scroll up farther.");
469 void menu_next_page (MUTTMENU *menu)
473 if (menu->top + menu->pagelen < menu->max)
475 menu->top += menu->pagelen;
476 if (menu->current < menu->top)
477 menu->current = menu->top;
478 menu->redraw = REDRAW_INDEX;
480 else if (menu->current != menu->max - 1 && !menu->dialog)
482 menu->current = menu->max - 1;
483 menu->redraw = REDRAW_MOTION;
486 mutt_error _("You are on the last page.");
489 mutt_error _("No entries.");
492 void menu_prev_page (MUTTMENU *menu)
494 int c = MIN (MenuContext, menu->pagelen / 2);
498 if ((menu->top -= menu->pagelen) < 0)
500 if (menu->current >= menu->top + menu->pagelen)
501 menu->current = menu->top + menu->pagelen - 1;
502 menu->redraw = REDRAW_INDEX;
504 else if (menu->current && !menu->dialog)
507 menu->redraw = REDRAW_MOTION;
510 mutt_error _("You are on the first page.");
513 void menu_top_page (MUTTMENU *menu)
515 if (menu->current != menu->top)
517 menu->current = menu->top;
518 menu->redraw = REDRAW_MOTION;
522 void menu_bottom_page (MUTTMENU *menu)
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;
532 mutt_error _("No entries.");
535 void menu_middle_page (MUTTMENU *menu)
541 i = menu->top + menu->pagelen;
542 if (i > menu->max - 1)
544 menu->current = menu->top + (i - menu->top) / 2;
545 menu->redraw = REDRAW_MOTION;
548 mutt_error _("No entries.");
551 void menu_first_entry (MUTTMENU *menu)
556 menu->redraw = REDRAW_MOTION;
559 mutt_error _("No entries.");
562 void menu_last_entry (MUTTMENU *menu)
566 menu->current = menu->max - 1;
567 menu->redraw = REDRAW_MOTION;
570 mutt_error _("No entries.");
573 void menu_half_up (MUTTMENU *menu)
577 if ((menu->top -= menu->pagelen / 2) < 0)
579 if (menu->current >= menu->top + menu->pagelen)
580 menu->current = menu->top + menu->pagelen - 1;
581 menu->redraw = REDRAW_INDEX;
583 else if (menu->current && !menu->dialog)
586 menu->redraw = REDRAW_MOTION;
589 mutt_error _("First entry is shown.");
592 void menu_half_down (MUTTMENU *menu)
596 if (menu->top + menu->pagelen < menu->max)
598 menu->top += menu->pagelen / 2;
599 if (menu->current < menu->top)
600 menu->current = menu->top;
601 menu->redraw = REDRAW_INDEX;
603 else if (menu->current != menu->max - 1 && !menu->dialog)
605 menu->current = menu->max - 1;
606 menu->redraw = REDRAW_INDEX;
609 mutt_error _("Last entry is shown.");
612 mutt_error _("No entries.");
615 void menu_current_top (MUTTMENU *menu)
619 menu->top = menu->current;
620 menu->redraw = REDRAW_INDEX;
623 mutt_error _("No entries.");
626 void menu_current_middle (MUTTMENU *menu)
630 menu->top = menu->current - menu->pagelen / 2;
633 menu->redraw = REDRAW_INDEX;
636 mutt_error _("No entries.");
639 void menu_current_bottom (MUTTMENU *menu)
643 menu->top = menu->current - menu->pagelen + 1;
646 menu->redraw = REDRAW_INDEX;
649 mutt_error _("No entries.");
652 void menu_next_entry (MUTTMENU *menu)
654 if (menu->current < menu->max - 1)
657 menu->redraw = REDRAW_MOTION;
660 mutt_error _("You are on the last entry.");
663 void menu_prev_entry (MUTTMENU *menu)
668 menu->redraw = REDRAW_MOTION;
671 mutt_error _("You are on the first entry.");
674 static int default_color (int i)
676 return ColorDefs[MT_COLOR_NORMAL];
679 static int menu_search_generic (MUTTMENU *m, regex_t *re, int n)
681 char buf[LONG_STRING];
683 menu_make_entry (buf, sizeof (buf), m, n);
684 return (regexec (re, buf, 0, NULL, 0));
687 MUTTMENU *mutt_new_menu (void)
689 MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
694 p->redraw = REDRAW_FULL;
695 p->pagelen = PAGELEN;
696 p->color = default_color;
697 p->search = menu_search_generic;
701 void mutt_menuDestroy (MUTTMENU **p)
705 FREE (&(*p)->searchBuf);
709 for (i=0; i < (*p)->max; i++)
710 FREE (&(*p)->dialog[i]);
712 FREE (& (*p)->dialog);
718 #define M_SEARCH_UP 1
719 #define M_SEARCH_DOWN 2
721 static int menu_search (MUTTMENU *menu, int op)
726 char buf[SHORT_STRING];
728 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
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])
735 mutt_str_replace (&menu->searchBuf, buf);
736 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
740 if (!menu->searchBuf)
742 mutt_error _("No search pattern.");
747 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
748 if (op == OP_SEARCH_OPPOSITE)
749 searchDir = -searchDir;
751 if ((r = REGCOMP (&re, menu->searchBuf, REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0)
753 regerror (r, &re, buf, sizeof (buf));
755 mutt_error ("%s", buf);
759 r = menu->current + searchDir;
760 while (r >= 0 && r < menu->max)
762 if (menu->search (menu, &re, r) == 0)
772 mutt_error _("Not found.");
776 static int menu_dialog_translate_op (int i)
784 case OP_CURRENT_TOP: case OP_FIRST_ENTRY:
786 case OP_CURRENT_BOTTOM: case OP_LAST_ENTRY:
787 return OP_BOTTOM_PAGE;
788 case OP_CURRENT_MIDDLE:
789 return OP_MIDDLE_PAGE;
795 static int menu_dialog_dokey (MUTTMENU *menu, int *ip)
808 if (ch.ch && (p = strchr (menu->keys, ch.ch)))
810 *ip = OP_MAX + (p - menu->keys + 1);
815 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
820 int menu_redraw (MUTTMENU *menu)
822 /* See if all or part of the screen needs to be updated. */
823 if (menu->redraw & REDRAW_FULL)
825 menu_redraw_full (menu);
826 /* allow the caller to do any local configuration */
831 menu_check_recenter (menu);
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);
843 menu_redraw_prompt (menu);
848 int mutt_menuLoop (MUTTMENU *menu)
854 if (option (OPTMENUCALLER))
856 unset_option (OPTMENUCALLER);
867 if (menu_redraw (menu) == OP_REDRAW)
870 menu->oldcurrent = menu->current;
873 /* move the cursor out of the way */
874 move (menu->current - menu->top + menu->offset,
875 (option (OPTARROWCURSOR) ? 2 : COLS-1));
879 /* try to catch dialog keys before ops */
880 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
883 i = km_dokey (menu->menu);
884 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND)
888 mvaddstr (LINES - 1, 0, "Tag-");
890 i = km_dokey (menu->menu);
892 CLEARLINE (LINES - 1);
894 else if (i == OP_TAG_PREFIX)
896 mutt_error _("No tagged entries.");
899 else /* None tagged, OP_TAG_PREFIX_COND */
905 if(tmp.op==OP_END_COND)break;
907 mutt_message _("Nothing to do.");
911 else if (menu->tagged && option (OPTAUTOTAG))
918 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
921 mutt_resize_screen ();
922 menu->redraw = REDRAW_FULL;
924 clearok(stdscr,TRUE);/*force complete redraw*/
934 /* Convert menubar movement to scrolling */
936 i = menu_dialog_translate_op (i);
941 menu_next_entry (menu);
944 menu_prev_entry (menu);
947 menu_half_down (menu);
953 menu_next_page (menu);
956 menu_prev_page (menu);
959 menu_next_line (menu);
962 menu_prev_line (menu);
965 menu_first_entry (menu);
968 menu_last_entry (menu);
971 menu_top_page (menu);
974 menu_middle_page (menu);
977 menu_bottom_page (menu);
980 menu_current_top (menu);
982 case OP_CURRENT_MIDDLE:
983 menu_current_middle (menu);
985 case OP_CURRENT_BOTTOM:
986 menu_current_bottom (menu);
989 case OP_SEARCH_REVERSE:
991 case OP_SEARCH_OPPOSITE:
992 if (menu->search && !menu->dialog) /* Searching dialogs won't work */
994 menu->oldcurrent = menu->current;
995 if ((menu->current = menu_search (menu, i)) != -1)
996 menu->redraw = REDRAW_MOTION;
998 menu->current = menu->oldcurrent;
1001 mutt_error _("Search is not implemented for this menu.");
1006 mutt_error _("Jumping is not implemented for dialogs.");
1011 case OP_ENTER_COMMAND:
1012 CurrentMenu = menu->menu;
1013 mutt_enter_command ();
1014 if (option (OPTFORCEREDRAWINDEX))
1016 menu->redraw = REDRAW_FULL;
1017 unset_option (OPTFORCEREDRAWINDEX);
1018 unset_option (OPTFORCEREDRAWPAGER);
1023 if (menu->tag && !menu->dialog)
1025 if (menu->tagprefix && !option (OPTAUTOTAG))
1027 for (i = 0; i < menu->max; i++)
1028 menu->tagged += menu->tag (menu, i, 0);
1029 menu->redraw = REDRAW_INDEX;
1033 int i = menu->tag (menu, menu->current, -1);
1035 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1)
1038 menu->redraw = REDRAW_MOTION_RESYNCH;
1041 menu->redraw = REDRAW_CURRENT;
1044 mutt_error _("No entries.");
1047 mutt_error _("Tagging is not supported.");
1050 case OP_SHELL_ESCAPE:
1051 mutt_shell_escape ();
1052 MAYBE_REDRAW (menu->redraw);
1060 clearok (stdscr, TRUE);
1061 menu->redraw = REDRAW_FULL;
1065 mutt_help (menu->menu);
1066 menu->redraw = REDRAW_FULL;
1070 km_error_key (menu->menu);