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)
43 size_t n = mutt_strlen ((char *)s);
50 SETCOLOR (MT_COLOR_TREE);
51 while (*s && *s < M_TREE_MAX)
56 if (option (OPTASCIICHARS))
58 else if (Charset_is_utf8)
59 addstr ("\342\224\224"); /* WACS_LLCORNER */
64 if (option (OPTASCIICHARS))
66 else if (Charset_is_utf8)
67 addstr ("\342\224\214"); /* WACS_ULCORNER */
72 if (option (OPTASCIICHARS))
74 else if (Charset_is_utf8)
75 addstr ("\342\224\234"); /* WACS_LTEE */
80 if (option (OPTASCIICHARS))
82 else if (Charset_is_utf8)
83 addstr ("\342\224\200"); /* WACS_HLINE */
88 if (option (OPTASCIICHARS))
90 else if (Charset_is_utf8)
91 addstr ("\342\224\202"); /* WACS_VLINE */
96 if (option (OPTASCIICHARS))
98 else if (Charset_is_utf8)
99 addstr ("\342\224\254"); /* WACS_TTEE */
104 if (option (OPTASCIICHARS))
106 else if (Charset_is_utf8)
107 addstr ("\342\224\264"); /* WACS_BTEE */
118 addch ('*'); /* fake thread indicator */
132 if (do_color) attrset(attr);
136 addnstr ((char *)s, 1);
144 static void menu_make_entry (char *s, int l, MUTTMENU *menu, int i)
148 strncpy (s, menu->dialog[i], l);
149 menu->current = -1; /* hide menubar */
152 menu->make_entry (s, l, menu, i);
155 void menu_pad_string (char *s, size_t n)
157 int shift = option (OPTARROWCURSOR) ? 3 : 0;
158 int cols = COLS - shift - SidebarWidth;
160 mutt_format_string (s, n, cols, cols, 0, ' ', s, strlen (s), 1);
164 void menu_redraw_full (MUTTMENU *menu)
166 SETCOLOR (MT_COLOR_NORMAL);
167 /* clear() doesn't optimize screen redraws */
171 if (option (OPTHELP))
173 SETCOLOR (MT_COLOR_STATUS);
174 move (option (OPTSTATUSONTOP) ? LINES-2 : 0, 0);
175 mutt_paddstr (COLS, menu->help);
176 SETCOLOR (MT_COLOR_NORMAL);
178 menu->pagelen = LINES - 3;
182 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
183 menu->pagelen = LINES - 2;
188 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
191 void menu_redraw_status (MUTTMENU *menu)
195 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
196 SETCOLOR (MT_COLOR_STATUS);
197 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
198 mutt_paddstr (COLS, buf);
199 SETCOLOR (MT_COLOR_NORMAL);
200 menu->redraw &= ~REDRAW_STATUS;
203 void menu_redraw_index (MUTTMENU *menu)
209 for (i = menu->top; i < menu->top + menu->pagelen; i++)
213 menu_make_entry (buf, sizeof (buf), menu, i);
214 menu_pad_string (buf, sizeof (buf));
216 if (option (OPTARROWCURSOR))
218 attrset (menu->color (i));
219 CLEARLINE_WIN (i - menu->top + menu->offset);
221 if (i == menu->current)
223 attrset (menu->color (i));
224 ADDCOLOR (MT_COLOR_INDICATOR);
226 attrset (menu->color (i));
230 move (i - menu->top + menu->offset, SidebarWidth + 3);
232 print_enriched_string (menu->color(i), (unsigned char *) buf, 1);
233 SETCOLOR (MT_COLOR_NORMAL);
237 attrset (menu->color (i));
239 if (i == menu->current)
241 ADDCOLOR (MT_COLOR_INDICATOR);
242 BKGDSET (MT_COLOR_INDICATOR);
245 CLEARLINE_WIN (i - menu->top + menu->offset);
246 print_enriched_string (menu->color(i), (unsigned char *) buf, i != menu->current);
247 SETCOLOR (MT_COLOR_NORMAL);
248 BKGDSET (MT_COLOR_NORMAL);
252 CLEARLINE_WIN (i - menu->top + menu->offset);
257 void menu_redraw_motion (MUTTMENU *menu)
263 menu->redraw &= ~REDRAW_MOTION;
267 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
268 SETCOLOR (MT_COLOR_NORMAL);
269 BKGDSET (MT_COLOR_NORMAL);
271 if (option (OPTARROWCURSOR))
273 /* clear the pointer */
274 attrset (menu->color (menu->oldcurrent));
277 if (menu->redraw & REDRAW_MOTION_RESYNCH)
280 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
281 menu_pad_string (buf, sizeof (buf));
282 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth + 3);
283 print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
284 SETCOLOR (MT_COLOR_NORMAL);
287 /* now draw it in the new location */
288 move (menu->current + menu->offset - menu->top, SidebarWidth);
289 attrset (menu->color (menu->current));
290 ADDCOLOR (MT_COLOR_INDICATOR);
292 SETCOLOR (MT_COLOR_NORMAL);
296 /* erase the current indicator */
297 attrset (menu->color (menu->oldcurrent));
299 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
300 menu_pad_string (buf, sizeof (buf));
301 print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
303 /* now draw the new one to reflect the change */
304 menu_make_entry (buf, sizeof (buf), menu, menu->current);
305 menu_pad_string (buf, sizeof (buf));
306 attrset (menu->color (menu->current));
307 ADDCOLOR (MT_COLOR_INDICATOR);
308 BKGDSET (MT_COLOR_INDICATOR);
309 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
310 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
311 SETCOLOR (MT_COLOR_NORMAL);
312 BKGDSET (MT_COLOR_NORMAL);
314 menu->redraw &= REDRAW_STATUS;
317 void menu_redraw_current (MUTTMENU *menu)
321 move (menu->current + menu->offset - menu->top, SidebarWidth);
322 menu_make_entry (buf, sizeof (buf), menu, menu->current);
323 menu_pad_string (buf, sizeof (buf));
325 if (option (OPTARROWCURSOR))
327 int attr = menu->color (menu->current);
330 attrset (menu->color (menu->current));
331 ADDCOLOR (MT_COLOR_INDICATOR);
335 menu_pad_string (buf, sizeof (buf));
336 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 1);
337 SETCOLOR (MT_COLOR_NORMAL);
341 attrset (menu->color (menu->current));
342 ADDCOLOR (MT_COLOR_INDICATOR);
343 BKGDSET (MT_COLOR_INDICATOR);
345 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
346 SETCOLOR (MT_COLOR_NORMAL);
347 BKGDSET (MT_COLOR_NORMAL);
349 menu->redraw &= REDRAW_STATUS;
352 void menu_redraw_prompt (MUTTMENU *menu)
356 if (option (OPTMSGERR))
359 unset_option (OPTMSGERR);
365 SETCOLOR (MT_COLOR_NORMAL);
366 mvaddstr (LINES - 1, 0, menu->prompt);
371 void menu_check_recenter (MUTTMENU *menu)
373 int c = MIN (MenuContext, menu->pagelen / 2);
374 int old_top = menu->top;
376 if (menu->max <= menu->pagelen) /* less entries than lines */
378 if (menu->top != 0) {
380 set_option (OPTNEEDREDRAW);
383 else if (menu->current >= menu->top + menu->pagelen - c) /* indicator below bottom threshold */
385 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
386 menu->top = menu->current - menu->pagelen + c + 1;
388 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
390 else if (menu->current < menu->top + c) /* indicator above top threshold */
392 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
393 menu->top = menu->current - c;
395 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
398 /* make entries stick to bottom */
399 menu->top = MIN (menu->top, menu->max - menu->pagelen);
400 menu->top = MAX (menu->top, 0);
402 if (menu->top != old_top)
403 menu->redraw |= REDRAW_INDEX;
406 void menu_jump (MUTTMENU *menu)
409 char buf[SHORT_STRING];
413 mutt_ungetch (LastKey, 0);
415 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0])
418 if (n >= 0 && n < menu->max)
421 menu->redraw = REDRAW_MOTION;
424 mutt_error _("Invalid index number.");
428 mutt_error _("No entries.");
431 void menu_next_line (MUTTMENU *menu)
435 if (menu->top + 1 < menu->max)
438 if (menu->current < menu->top)
440 menu->redraw = REDRAW_INDEX;
443 mutt_error _("You cannot scroll down farther.");
446 mutt_error _("No entries.");
449 void menu_prev_line (MUTTMENU *menu)
454 if (menu->current >= menu->top + menu->pagelen)
456 menu->redraw = REDRAW_INDEX;
459 mutt_error _("You cannot scroll up farther.");
462 void menu_next_page (MUTTMENU *menu)
466 if (menu->top + menu->pagelen < menu->max)
468 menu->top += menu->pagelen;
469 if (menu->current < menu->top)
470 menu->current = menu->top;
471 menu->redraw = REDRAW_INDEX;
473 else if (menu->current != menu->max - 1 && !menu->dialog)
475 menu->current = menu->max - 1;
476 menu->redraw = REDRAW_MOTION;
479 mutt_error _("You are on the last page.");
482 mutt_error _("No entries.");
485 void menu_prev_page (MUTTMENU *menu)
487 int c = MIN (MenuContext, menu->pagelen / 2);
491 if ((menu->top -= menu->pagelen) < 0)
493 if (menu->current >= menu->top + menu->pagelen)
494 menu->current = menu->top + menu->pagelen - 1;
495 menu->redraw = REDRAW_INDEX;
497 else if (menu->current && !menu->dialog)
500 menu->redraw = REDRAW_MOTION;
503 mutt_error _("You are on the first page.");
506 void menu_top_page (MUTTMENU *menu)
508 if (menu->current != menu->top)
510 menu->current = menu->top;
511 menu->redraw = REDRAW_MOTION;
515 void menu_bottom_page (MUTTMENU *menu)
519 menu->current = menu->top + menu->pagelen - 1;
520 if (menu->current > menu->max - 1)
521 menu->current = menu->max - 1;
522 menu->redraw = REDRAW_MOTION;
525 mutt_error _("No entries.");
528 void menu_middle_page (MUTTMENU *menu)
534 i = menu->top + menu->pagelen;
535 if (i > menu->max - 1)
537 menu->current = menu->top + (i - menu->top) / 2;
538 menu->redraw = REDRAW_MOTION;
541 mutt_error _("No entries.");
544 void menu_first_entry (MUTTMENU *menu)
549 menu->redraw = REDRAW_MOTION;
552 mutt_error _("No entries.");
555 void menu_last_entry (MUTTMENU *menu)
559 menu->current = menu->max - 1;
560 menu->redraw = REDRAW_MOTION;
563 mutt_error _("No entries.");
566 void menu_half_up (MUTTMENU *menu)
570 if ((menu->top -= menu->pagelen / 2) < 0)
572 if (menu->current >= menu->top + menu->pagelen)
573 menu->current = menu->top + menu->pagelen - 1;
574 menu->redraw = REDRAW_INDEX;
576 else if (menu->current && !menu->dialog)
579 menu->redraw = REDRAW_MOTION;
582 mutt_error _("First entry is shown.");
585 void menu_half_down (MUTTMENU *menu)
589 if (menu->top + menu->pagelen < menu->max)
591 menu->top += menu->pagelen / 2;
592 if (menu->current < menu->top)
593 menu->current = menu->top;
594 menu->redraw = REDRAW_INDEX;
596 else if (menu->current != menu->max - 1 && !menu->dialog)
598 menu->current = menu->max - 1;
599 menu->redraw = REDRAW_INDEX;
602 mutt_error _("Last entry is shown.");
605 mutt_error _("No entries.");
608 void menu_current_top (MUTTMENU *menu)
612 menu->top = menu->current;
613 menu->redraw = REDRAW_INDEX;
616 mutt_error _("No entries.");
619 void menu_current_middle (MUTTMENU *menu)
623 menu->top = menu->current - menu->pagelen / 2;
626 menu->redraw = REDRAW_INDEX;
629 mutt_error _("No entries.");
632 void menu_current_bottom (MUTTMENU *menu)
636 menu->top = menu->current - menu->pagelen + 1;
639 menu->redraw = REDRAW_INDEX;
642 mutt_error _("No entries.");
645 void menu_next_entry (MUTTMENU *menu)
647 if (menu->current < menu->max - 1)
650 menu->redraw = REDRAW_MOTION;
653 mutt_error _("You are on the last entry.");
656 void menu_prev_entry (MUTTMENU *menu)
661 menu->redraw = REDRAW_MOTION;
664 mutt_error _("You are on the first entry.");
667 static int default_color (int i)
669 return ColorDefs[MT_COLOR_NORMAL];
672 static int menu_search_generic (MUTTMENU *m, regex_t *re, int n)
674 char buf[LONG_STRING];
676 menu_make_entry (buf, sizeof (buf), m, n);
677 return (regexec (re, buf, 0, NULL, 0));
680 MUTTMENU *mutt_new_menu (void)
682 MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
687 p->redraw = REDRAW_FULL;
688 p->pagelen = PAGELEN;
689 p->color = default_color;
690 p->search = menu_search_generic;
694 void mutt_menuDestroy (MUTTMENU **p)
698 FREE (&(*p)->searchBuf);
702 for (i=0; i < (*p)->max; i++)
703 FREE (&(*p)->dialog[i]);
705 FREE (& (*p)->dialog);
711 #define M_SEARCH_UP 1
712 #define M_SEARCH_DOWN 2
714 static int menu_search (MUTTMENU *menu, int op)
719 char buf[SHORT_STRING];
721 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
723 strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
724 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
725 _("Reverse search for: "),
726 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
728 mutt_str_replace (&menu->searchBuf, buf);
729 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
733 if (!menu->searchBuf)
735 mutt_error _("No search pattern.");
740 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
741 if (op == OP_SEARCH_OPPOSITE)
742 searchDir = -searchDir;
744 if ((r = REGCOMP (&re, menu->searchBuf, REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0)
746 regerror (r, &re, buf, sizeof (buf));
748 mutt_error ("%s", buf);
752 r = menu->current + searchDir;
753 while (r >= 0 && r < menu->max)
755 if (menu->search (menu, &re, r) == 0)
765 mutt_error _("Not found.");
769 static int menu_dialog_translate_op (int i)
777 case OP_CURRENT_TOP: case OP_FIRST_ENTRY:
779 case OP_CURRENT_BOTTOM: case OP_LAST_ENTRY:
780 return OP_BOTTOM_PAGE;
781 case OP_CURRENT_MIDDLE:
782 return OP_MIDDLE_PAGE;
788 static int menu_dialog_dokey (MUTTMENU *menu, int *ip)
801 if (ch.ch && (p = strchr (menu->keys, ch.ch)))
803 *ip = OP_MAX + (p - menu->keys + 1);
808 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
813 int menu_redraw (MUTTMENU *menu)
815 /* See if all or part of the screen needs to be updated. */
816 if (menu->redraw & REDRAW_FULL)
818 menu_redraw_full (menu);
819 /* allow the caller to do any local configuration */
824 menu_check_recenter (menu);
826 if (menu->redraw & REDRAW_STATUS)
827 menu_redraw_status (menu);
828 if (menu->redraw & REDRAW_INDEX)
829 menu_redraw_index (menu);
830 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
831 menu_redraw_motion (menu);
832 else if (menu->redraw == REDRAW_CURRENT)
833 menu_redraw_current (menu);
836 menu_redraw_prompt (menu);
841 int mutt_menuLoop (MUTTMENU *menu)
847 if (option (OPTMENUCALLER))
849 unset_option (OPTMENUCALLER);
860 if (menu_redraw (menu) == OP_REDRAW)
863 menu->oldcurrent = menu->current;
866 /* move the cursor out of the way */
867 move (menu->current - menu->top + menu->offset,
868 (option (OPTARROWCURSOR) ? 2 : COLS-1));
872 /* try to catch dialog keys before ops */
873 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
876 i = km_dokey (menu->menu);
877 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND)
881 mvaddstr (LINES - 1, 0, "Tag-");
883 i = km_dokey (menu->menu);
885 CLEARLINE (LINES - 1);
887 else if (i == OP_TAG_PREFIX)
889 mutt_error _("No tagged entries.");
892 else /* None tagged, OP_TAG_PREFIX_COND */
898 if(tmp.op==OP_END_COND)break;
900 mutt_message _("Nothing to do.");
904 else if (menu->tagged && option (OPTAUTOTAG))
911 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
914 mutt_resize_screen ();
915 menu->redraw = REDRAW_FULL;
917 clearok(stdscr,TRUE);/*force complete redraw*/
927 /* Convert menubar movement to scrolling */
929 i = menu_dialog_translate_op (i);
934 menu_next_entry (menu);
937 menu_prev_entry (menu);
940 menu_half_down (menu);
946 menu_next_page (menu);
949 menu_prev_page (menu);
952 menu_next_line (menu);
955 menu_prev_line (menu);
958 menu_first_entry (menu);
961 menu_last_entry (menu);
964 menu_top_page (menu);
967 menu_middle_page (menu);
970 menu_bottom_page (menu);
973 menu_current_top (menu);
975 case OP_CURRENT_MIDDLE:
976 menu_current_middle (menu);
978 case OP_CURRENT_BOTTOM:
979 menu_current_bottom (menu);
982 case OP_SEARCH_REVERSE:
984 case OP_SEARCH_OPPOSITE:
985 if (menu->search && !menu->dialog) /* Searching dialogs won't work */
987 menu->oldcurrent = menu->current;
988 if ((menu->current = menu_search (menu, i)) != -1)
989 menu->redraw = REDRAW_MOTION;
991 menu->current = menu->oldcurrent;
994 mutt_error _("Search is not implemented for this menu.");
999 mutt_error _("Jumping is not implemented for dialogs.");
1004 case OP_ENTER_COMMAND:
1005 CurrentMenu = menu->menu;
1006 mutt_enter_command ();
1007 if (option (OPTFORCEREDRAWINDEX))
1009 menu->redraw = REDRAW_FULL;
1010 unset_option (OPTFORCEREDRAWINDEX);
1011 unset_option (OPTFORCEREDRAWPAGER);
1016 if (menu->tag && !menu->dialog)
1018 if (menu->tagprefix && !option (OPTAUTOTAG))
1020 for (i = 0; i < menu->max; i++)
1021 menu->tagged += menu->tag (menu, i, 0);
1022 menu->redraw = REDRAW_INDEX;
1026 int i = menu->tag (menu, menu->current, -1);
1028 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1)
1031 menu->redraw = REDRAW_MOTION_RESYNCH;
1034 menu->redraw = REDRAW_CURRENT;
1037 mutt_error _("No entries.");
1040 mutt_error _("Tagging is not supported.");
1043 case OP_SHELL_ESCAPE:
1044 mutt_shell_escape ();
1045 MAYBE_REDRAW (menu->redraw);
1053 clearok (stdscr, TRUE);
1054 menu->redraw = REDRAW_FULL;
1058 mutt_help (menu->menu);
1059 menu->redraw = REDRAW_FULL;
1063 km_error_key (menu->menu);