2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_curses.h"
16 #include "mutt_menu.h"
31 extern int Charset_is_utf8; /* FIXME: bad modularisation */
33 extern size_t UngetCount;
35 static void print_enriched_string (int attr, unsigned char *s, int do_color)
39 size_t n = mutt_strlen ((char *) s);
42 memset (&mbstate, 0, sizeof (mbstate));
44 if (*s < M_TREE_MAX) {
46 SETCOLOR (MT_COLOR_TREE);
47 while (*s && *s < M_TREE_MAX) {
50 if (option (OPTASCIICHARS))
52 else if (Charset_is_utf8)
53 addstr ("\342\224\224"); /* WACS_LLCORNER */
58 if (option (OPTASCIICHARS))
60 else if (Charset_is_utf8)
61 addstr ("\342\224\214"); /* WACS_ULCORNER */
66 if (option (OPTASCIICHARS))
68 else if (Charset_is_utf8)
69 addstr ("\342\224\234"); /* WACS_LTEE */
74 if (option (OPTASCIICHARS))
76 else if (Charset_is_utf8)
77 addstr ("\342\224\200"); /* WACS_HLINE */
82 if (option (OPTASCIICHARS))
84 else if (Charset_is_utf8)
85 addstr ("\342\224\202"); /* WACS_VLINE */
90 if (option (OPTASCIICHARS))
92 else if (Charset_is_utf8)
93 addstr ("\342\224\254"); /* WACS_TTEE */
98 if (option (OPTASCIICHARS))
100 else if (Charset_is_utf8)
101 addstr ("\342\224\264"); /* WACS_BTEE */
112 addch ('*'); /* fake thread indicator */
129 else if ((k = mbrtowc (&wc, (char *) s, n, &mbstate)) > 0) {
130 addnstr ((char *) s, k);
138 static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i)
141 strncpy (s, menu->dialog[i], l);
142 menu->current = -1; /* hide menubar */
145 menu->make_entry (s, l, menu, i);
148 void menu_pad_string (char *s, size_t n)
150 int shift = option (OPTARROWCURSOR) ? 3 : 0;
152 char *tmpbuf = safe_malloc (n);
154 if (option (OPTMBOXPANE))
155 cols = COLS - shift - SidebarWidth;
158 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, mutt_strlen (s), 1);
160 snprintf (s, n, "%s", tmpbuf); /* overkill */
164 void menu_redraw_full (MUTTMENU * menu)
166 SETCOLOR (MT_COLOR_NORMAL);
167 /* clear() doesn't optimize screen redraws */
171 if (option (OPTHELP)) {
172 SETCOLOR (MT_COLOR_STATUS);
173 move (option (OPTSTATUSONTOP) ? LINES - 2 : 0, 0);
174 mutt_paddstr (COLS, menu->help);
175 SETCOLOR (MT_COLOR_NORMAL);
177 menu->pagelen = LINES - 3;
180 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
181 menu->pagelen = LINES - 2;
186 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
189 void menu_redraw_status (MUTTMENU * menu)
193 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
194 SETCOLOR (MT_COLOR_STATUS);
195 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
196 mutt_paddstr (COLS, buf);
197 SETCOLOR (MT_COLOR_NORMAL);
198 menu->redraw &= ~REDRAW_STATUS;
201 void menu_redraw_index (MUTTMENU * menu)
207 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
209 menu_make_entry (buf, sizeof (buf), menu, i);
210 menu_pad_string (buf, sizeof (buf));
212 if (option (OPTARROWCURSOR)) {
213 attrset (menu->color (i));
214 CLEARLINE_WIN (i - menu->top + menu->offset);
216 if (i == menu->current) {
217 attrset (menu->color (i));
218 ADDCOLOR (MT_COLOR_INDICATOR);
220 attrset (menu->color (i));
224 move (i - menu->top + menu->offset, SidebarWidth + 3);
226 print_enriched_string (menu->color (i), (unsigned char *) buf, 1);
227 SETCOLOR (MT_COLOR_NORMAL);
230 attrset (menu->color (i));
232 if (i == menu->current) {
233 ADDCOLOR (MT_COLOR_INDICATOR);
234 BKGDSET (MT_COLOR_INDICATOR);
237 CLEARLINE_WIN (i - menu->top + menu->offset);
238 print_enriched_string (menu->color (i), (unsigned char *) buf,
240 SETCOLOR (MT_COLOR_NORMAL);
241 BKGDSET (MT_COLOR_NORMAL);
245 CLEARLINE_WIN (i - menu->top + menu->offset);
250 void menu_redraw_motion (MUTTMENU * menu)
255 menu->redraw &= ~REDRAW_MOTION;
259 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
260 SETCOLOR (MT_COLOR_NORMAL);
261 BKGDSET (MT_COLOR_NORMAL);
263 if (option (OPTARROWCURSOR)) {
264 /* clear the pointer */
265 attrset (menu->color (menu->oldcurrent));
268 if (menu->redraw & REDRAW_MOTION_RESYNCH) {
270 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
271 menu_pad_string (buf, sizeof (buf));
272 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth + 3);
273 print_enriched_string (menu->color (menu->oldcurrent),
274 (unsigned char *) buf, 1);
275 SETCOLOR (MT_COLOR_NORMAL);
278 /* now draw it in the new location */
279 move (menu->current + menu->offset - menu->top, SidebarWidth);
280 attrset (menu->color (menu->current));
281 ADDCOLOR (MT_COLOR_INDICATOR);
283 SETCOLOR (MT_COLOR_NORMAL);
286 /* erase the current indicator */
287 attrset (menu->color (menu->oldcurrent));
289 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
290 menu_pad_string (buf, sizeof (buf));
291 print_enriched_string (menu->color (menu->oldcurrent),
292 (unsigned char *) buf, 1);
294 /* now draw the new one to reflect the change */
295 menu_make_entry (buf, sizeof (buf), menu, menu->current);
296 menu_pad_string (buf, sizeof (buf));
297 attrset (menu->color (menu->current));
298 ADDCOLOR (MT_COLOR_INDICATOR);
299 BKGDSET (MT_COLOR_INDICATOR);
300 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
301 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
303 SETCOLOR (MT_COLOR_NORMAL);
304 BKGDSET (MT_COLOR_NORMAL);
306 menu->redraw &= REDRAW_STATUS;
309 void menu_redraw_current (MUTTMENU * menu)
313 move (menu->current + menu->offset - menu->top, SidebarWidth);
314 menu_make_entry (buf, sizeof (buf), menu, menu->current);
315 menu_pad_string (buf, sizeof (buf));
317 if (option (OPTARROWCURSOR)) {
318 int attr = menu->color (menu->current);
322 attrset (menu->color (menu->current));
323 ADDCOLOR (MT_COLOR_INDICATOR);
327 menu_pad_string (buf, sizeof (buf));
328 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
330 SETCOLOR (MT_COLOR_NORMAL);
333 attrset (menu->color (menu->current));
334 ADDCOLOR (MT_COLOR_INDICATOR);
335 BKGDSET (MT_COLOR_INDICATOR);
337 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
339 SETCOLOR (MT_COLOR_NORMAL);
340 BKGDSET (MT_COLOR_NORMAL);
342 menu->redraw &= REDRAW_STATUS;
345 void menu_redraw_prompt (MUTTMENU * menu)
348 if (option (OPTMSGERR)) {
350 unset_option (OPTMSGERR);
356 SETCOLOR (MT_COLOR_NORMAL);
357 mvaddstr (LINES - 1, 0, menu->prompt);
362 void menu_check_recenter (MUTTMENU * menu)
364 int c = MIN (MenuContext, menu->pagelen / 2);
365 int old_top = menu->top;
367 if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) { /* less entries than lines */
368 if (menu->top != 0) {
370 set_option (OPTNEEDREDRAW);
373 else if (menu->current >= menu->top + menu->pagelen - c) { /* indicator below bottom threshold */
374 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
375 menu->top = menu->current - menu->pagelen + c + 1;
379 c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
381 else if (menu->current < menu->top + c) { /* indicator above top threshold */
382 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
383 menu->top = menu->current - c;
387 c) * ((menu->top + menu->pagelen - 1 -
388 menu->current) / (menu->pagelen - c)) - c;
391 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
392 menu->top = MIN (menu->top, menu->max - menu->pagelen);
393 menu->top = MAX (menu->top, 0);
395 if (menu->top != old_top)
396 menu->redraw |= REDRAW_INDEX;
399 void menu_jump (MUTTMENU * menu)
402 char buf[SHORT_STRING];
405 mutt_ungetch (LastKey, 0);
407 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
409 if (n >= 0 && n < menu->max) {
411 menu->redraw = REDRAW_MOTION;
414 mutt_error _("Invalid index number.");
418 mutt_error _("No entries.");
421 void menu_next_line (MUTTMENU * menu)
424 int c = MIN (MenuContext, menu->pagelen / 2);
426 if (menu->top + 1 < menu->max - c && (option (OPTMENUMOVEOFF)
427 || (menu->max > menu->pagelen
429 menu->max - menu->pagelen))) {
431 if (menu->current < menu->top + c && menu->current < menu->max - 1)
433 menu->redraw = REDRAW_INDEX;
436 mutt_error _("You cannot scroll down farther.");
439 mutt_error _("No entries.");
442 void menu_prev_line (MUTTMENU * menu)
445 int c = MIN (MenuContext, menu->pagelen / 2);
448 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
450 menu->redraw = REDRAW_INDEX;
453 mutt_error _("You cannot scroll up farther.");
456 void menu_next_page (MUTTMENU * menu)
459 if (menu->top + menu->pagelen < menu->max) {
460 menu->top += menu->pagelen;
461 if (menu->current < menu->top)
462 menu->current = menu->top;
463 menu->redraw = REDRAW_INDEX;
465 else if (menu->current != menu->max - 1 && !menu->dialog) {
466 menu->current = menu->max - 1;
467 menu->redraw = REDRAW_MOTION;
470 mutt_error _("You are on the last page.");
473 mutt_error _("No entries.");
476 void menu_prev_page (MUTTMENU * menu)
478 int c = MIN (MenuContext, menu->pagelen / 2);
481 if ((menu->top -= menu->pagelen) < 0)
483 if (menu->current >= menu->top + menu->pagelen)
484 menu->current = menu->top + menu->pagelen - 1;
485 menu->redraw = REDRAW_INDEX;
487 else if (menu->current && !menu->dialog) {
489 menu->redraw = REDRAW_MOTION;
492 mutt_error _("You are on the first page.");
495 void menu_top_page (MUTTMENU * menu)
497 if (menu->current != menu->top) {
498 menu->current = menu->top;
499 menu->redraw = REDRAW_MOTION;
503 void menu_bottom_page (MUTTMENU * menu)
506 menu->current = menu->top + menu->pagelen - 1;
507 if (menu->current > menu->max - 1)
508 menu->current = menu->max - 1;
509 menu->redraw = REDRAW_MOTION;
512 mutt_error _("No entries.");
515 void menu_middle_page (MUTTMENU * menu)
520 i = menu->top + menu->pagelen;
521 if (i > menu->max - 1)
523 menu->current = menu->top + (i - menu->top) / 2;
524 menu->redraw = REDRAW_MOTION;
527 mutt_error _("No entries.");
530 void menu_first_entry (MUTTMENU * menu)
534 menu->redraw = REDRAW_MOTION;
537 mutt_error _("No entries.");
540 void menu_last_entry (MUTTMENU * menu)
543 menu->current = menu->max - 1;
544 menu->redraw = REDRAW_MOTION;
547 mutt_error _("No entries.");
550 void menu_half_up (MUTTMENU * menu)
553 if ((menu->top -= menu->pagelen / 2) < 0)
555 if (menu->current >= menu->top + menu->pagelen)
556 menu->current = menu->top + menu->pagelen - 1;
557 menu->redraw = REDRAW_INDEX;
559 else if (menu->current && !menu->dialog) {
561 menu->redraw = REDRAW_MOTION;
564 mutt_error _("First entry is shown.");
567 void menu_half_down (MUTTMENU * menu)
570 if (menu->top + menu->pagelen < menu->max) {
571 menu->top += menu->pagelen / 2;
572 if (menu->current < menu->top)
573 menu->current = menu->top;
574 menu->redraw = REDRAW_INDEX;
576 else if (menu->current != menu->max - 1 && !menu->dialog) {
577 menu->current = menu->max - 1;
578 menu->redraw = REDRAW_INDEX;
581 mutt_error _("Last entry is shown.");
584 mutt_error _("No entries.");
587 void menu_current_top (MUTTMENU * menu)
590 menu->top = menu->current;
591 menu->redraw = REDRAW_INDEX;
594 mutt_error _("No entries.");
597 void menu_current_middle (MUTTMENU * menu)
600 menu->top = menu->current - menu->pagelen / 2;
603 menu->redraw = REDRAW_INDEX;
606 mutt_error _("No entries.");
609 void menu_current_bottom (MUTTMENU * menu)
612 menu->top = menu->current - menu->pagelen + 1;
615 menu->redraw = REDRAW_INDEX;
618 mutt_error _("No entries.");
621 void menu_next_entry (MUTTMENU * menu)
623 if (menu->current < menu->max - 1) {
625 menu->redraw = REDRAW_MOTION;
628 mutt_error _("You are on the last entry.");
631 void menu_prev_entry (MUTTMENU * menu)
635 menu->redraw = REDRAW_MOTION;
638 mutt_error _("You are on the first entry.");
641 static int default_color (int i)
643 return ColorDefs[MT_COLOR_NORMAL];
646 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
648 char buf[LONG_STRING];
650 menu_make_entry (buf, sizeof (buf), m, n);
651 return (regexec (re, buf, 0, NULL, 0));
654 MUTTMENU *mutt_new_menu (void)
656 MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
661 p->redraw = REDRAW_FULL;
662 p->pagelen = PAGELEN;
663 p->color = default_color;
664 p->search = menu_search_generic;
668 void mutt_menuDestroy (MUTTMENU ** p)
672 FREE (&(*p)->searchBuf);
675 for (i = 0; i < (*p)->max; i++)
676 FREE (&(*p)->dialog[i]);
678 FREE (&(*p)->dialog);
684 #define M_SEARCH_UP 1
685 #define M_SEARCH_DOWN 2
687 static int menu_search (MUTTMENU * menu, int op)
692 char buf[SHORT_STRING];
694 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
695 strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
696 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
697 _("Reverse search for: "),
698 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
700 mutt_str_replace (&menu->searchBuf, buf);
701 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
704 if (!menu->searchBuf) {
705 mutt_error _("No search pattern.");
711 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
712 if (op == OP_SEARCH_OPPOSITE)
713 searchDir = -searchDir;
716 REGCOMP (&re, menu->searchBuf,
717 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
718 regerror (r, &re, buf, sizeof (buf));
720 mutt_error ("%s", buf);
724 r = menu->current + searchDir;
725 while (r >= 0 && r < menu->max) {
726 if (menu->search (menu, &re, r) == 0) {
735 mutt_error _("Not found.");
740 static int menu_dialog_translate_op (int i)
750 case OP_CURRENT_BOTTOM:
752 return OP_BOTTOM_PAGE;
753 case OP_CURRENT_MIDDLE:
754 return OP_MIDDLE_PAGE;
760 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
772 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
773 *ip = OP_MAX + (p - menu->keys + 1);
777 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
782 int menu_redraw (MUTTMENU * menu)
784 /* See if all or part of the screen needs to be updated. */
785 if (menu->redraw & REDRAW_FULL) {
786 menu_redraw_full (menu);
787 /* allow the caller to do any local configuration */
792 menu_check_recenter (menu);
794 if (menu->redraw & REDRAW_STATUS)
795 menu_redraw_status (menu);
796 if (menu->redraw & REDRAW_INDEX)
797 menu_redraw_index (menu);
798 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
799 menu_redraw_motion (menu);
800 else if (menu->redraw == REDRAW_CURRENT)
801 menu_redraw_current (menu);
804 menu_redraw_prompt (menu);
809 int mutt_menuLoop (MUTTMENU * menu)
814 if (option (OPTMENUCALLER)) {
815 unset_option (OPTMENUCALLER);
826 if (menu_redraw (menu) == OP_REDRAW)
829 menu->oldcurrent = menu->current;
832 /* move the cursor out of the way */
833 move (menu->current - menu->top + menu->offset,
834 (option (OPTARROWCURSOR) ? 2 : COLS - 1));
838 /* try to catch dialog keys before ops */
839 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
842 i = km_dokey (menu->menu);
843 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
845 mvaddstr (LINES - 1, 0, "Tag-");
847 i = km_dokey (menu->menu);
849 CLEARLINE (LINES - 1);
851 else if (i == OP_TAG_PREFIX) {
852 mutt_error _("No tagged entries.");
856 else { /* None tagged, OP_TAG_PREFIX_COND */
860 while (UngetCount > 0) {
862 if (tmp.op == OP_END_COND)
865 mutt_message _("Nothing to do.");
870 else if (menu->tagged && option (OPTAUTOTAG))
877 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
879 mutt_resize_screen ();
880 menu->redraw = REDRAW_FULL;
882 clearok (stdscr, TRUE); /*force complete redraw */
892 /* Convert menubar movement to scrolling */
894 i = menu_dialog_translate_op (i);
898 menu_next_entry (menu);
901 menu_prev_entry (menu);
904 menu_half_down (menu);
910 menu_next_page (menu);
913 menu_prev_page (menu);
916 menu_next_line (menu);
919 menu_prev_line (menu);
922 menu_first_entry (menu);
925 menu_last_entry (menu);
928 menu_top_page (menu);
931 menu_middle_page (menu);
934 menu_bottom_page (menu);
937 menu_current_top (menu);
939 case OP_CURRENT_MIDDLE:
940 menu_current_middle (menu);
942 case OP_CURRENT_BOTTOM:
943 menu_current_bottom (menu);
946 case OP_SEARCH_REVERSE:
948 case OP_SEARCH_OPPOSITE:
949 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
950 menu->oldcurrent = menu->current;
951 if ((menu->current = menu_search (menu, i)) != -1)
952 menu->redraw = REDRAW_MOTION;
954 menu->current = menu->oldcurrent;
957 mutt_error _("Search is not implemented for this menu.");
962 mutt_error (_("Jumping is not implemented for dialogs."));
968 case OP_ENTER_COMMAND:
969 CurrentMenu = menu->menu;
970 mutt_enter_command ();
971 if (option (OPTFORCEREDRAWINDEX)) {
972 menu->redraw = REDRAW_FULL;
973 unset_option (OPTFORCEREDRAWINDEX);
974 unset_option (OPTFORCEREDRAWPAGER);
979 if (menu->tag && !menu->dialog) {
980 if (menu->tagprefix && !option (OPTAUTOTAG)) {
981 for (i = 0; i < menu->max; i++)
982 menu->tagged += menu->tag (menu, i, 0);
983 menu->redraw = REDRAW_INDEX;
985 else if (menu->max) {
986 int i = menu->tag (menu, menu->current, -1);
989 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1) {
991 menu->redraw = REDRAW_MOTION_RESYNCH;
994 menu->redraw = REDRAW_CURRENT;
997 mutt_error _("No entries.");
1000 mutt_error _("Tagging is not supported.");
1003 case OP_SHELL_ESCAPE:
1004 mutt_shell_escape ();
1005 MAYBE_REDRAW (menu->redraw);
1013 clearok (stdscr, TRUE);
1014 menu->redraw = REDRAW_FULL;
1018 mutt_help (menu->menu);
1019 menu->redraw = REDRAW_FULL;
1023 km_error_key (menu->menu);