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.
16 #include "mutt_curses.h"
17 #include "mutt_menu.h"
32 extern int Charset_is_utf8; /* FIXME: bad modularisation */
34 extern size_t UngetCount;
36 static void print_enriched_string (int attr, unsigned char *s, int do_color)
40 size_t n = str_len ((char *) s);
43 memset (&mbstate, 0, sizeof (mbstate));
45 if (*s < M_TREE_MAX) {
47 SETCOLOR (MT_COLOR_TREE);
48 while (*s && *s < M_TREE_MAX) {
51 if (option (OPTASCIICHARS))
53 else if (Charset_is_utf8)
54 addstr ("\342\224\224"); /* WACS_LLCORNER */
59 if (option (OPTASCIICHARS))
61 else if (Charset_is_utf8)
62 addstr ("\342\224\214"); /* WACS_ULCORNER */
67 if (option (OPTASCIICHARS))
69 else if (Charset_is_utf8)
70 addstr ("\342\224\234"); /* WACS_LTEE */
75 if (option (OPTASCIICHARS))
77 else if (Charset_is_utf8)
78 addstr ("\342\224\200"); /* WACS_HLINE */
83 if (option (OPTASCIICHARS))
85 else if (Charset_is_utf8)
86 addstr ("\342\224\202"); /* WACS_VLINE */
91 if (option (OPTASCIICHARS))
93 else if (Charset_is_utf8)
94 addstr ("\342\224\254"); /* WACS_TTEE */
99 if (option (OPTASCIICHARS))
101 else if (Charset_is_utf8)
102 addstr ("\342\224\264"); /* WACS_BTEE */
113 addch ('*'); /* fake thread indicator */
130 else if ((k = mbrtowc (&wc, (char *) s, n, &mbstate)) > 0) {
131 addnstr ((char *) s, k);
139 static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i)
142 strncpy (s, menu->dialog[i], l);
143 menu->current = -1; /* hide menubar */
146 menu->make_entry (s, l, menu, i);
149 void menu_pad_string (char *s, size_t n)
151 int shift = option (OPTARROWCURSOR) ? 3 : 0;
153 char *tmpbuf = mem_malloc (n);
155 if (option (OPTMBOXPANE))
156 cols = COLS - shift - SidebarWidth;
159 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, str_len (s), 1);
161 snprintf (s, n, "%s", tmpbuf); /* overkill */
165 void menu_redraw_full (MUTTMENU * menu)
167 SETCOLOR (MT_COLOR_NORMAL);
168 /* clear() doesn't optimize screen redraws */
172 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;
181 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
182 menu->pagelen = LINES - 2;
187 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
190 void menu_redraw_status (MUTTMENU * menu)
194 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
195 SETCOLOR (MT_COLOR_STATUS);
196 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
197 mutt_paddstr (COLS, buf);
198 SETCOLOR (MT_COLOR_NORMAL);
199 menu->redraw &= ~REDRAW_STATUS;
202 void menu_redraw_index (MUTTMENU * menu)
208 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
210 menu_make_entry (buf, sizeof (buf), menu, i);
211 menu_pad_string (buf, sizeof (buf));
213 if (option (OPTARROWCURSOR)) {
214 attrset (menu->color (i));
215 CLEARLINE_WIN (i - menu->top + menu->offset);
217 if (i == menu->current) {
218 attrset (menu->color (i));
219 ADDCOLOR (MT_COLOR_INDICATOR);
221 attrset (menu->color (i));
225 attrset (menu->color (i));
226 move (i - menu->top + menu->offset, SidebarWidth);
230 print_enriched_string (menu->color (i), (unsigned char *) buf, 1);
231 SETCOLOR (MT_COLOR_NORMAL);
234 attrset (menu->color (i));
236 if (i == menu->current) {
237 ADDCOLOR (MT_COLOR_INDICATOR);
238 BKGDSET (MT_COLOR_INDICATOR);
241 CLEARLINE_WIN (i - menu->top + menu->offset);
242 print_enriched_string (menu->color (i), (unsigned char *) buf,
244 SETCOLOR (MT_COLOR_NORMAL);
245 BKGDSET (MT_COLOR_NORMAL);
249 CLEARLINE_WIN (i - menu->top + menu->offset);
254 void menu_redraw_motion (MUTTMENU * menu)
259 menu->redraw &= ~REDRAW_MOTION;
263 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
264 SETCOLOR (MT_COLOR_NORMAL);
265 BKGDSET (MT_COLOR_NORMAL);
267 if (option (OPTARROWCURSOR)) {
268 /* clear the pointer */
269 attrset (menu->color (menu->oldcurrent));
272 if (menu->redraw & REDRAW_MOTION_RESYNCH) {
274 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
275 menu_pad_string (buf, sizeof (buf));
276 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth + 3);
277 print_enriched_string (menu->color (menu->oldcurrent),
278 (unsigned char *) buf, 1);
279 SETCOLOR (MT_COLOR_NORMAL);
282 /* now draw it in the new location */
283 move (menu->current + menu->offset - menu->top, SidebarWidth);
284 attrset (menu->color (menu->current));
285 ADDCOLOR (MT_COLOR_INDICATOR);
287 SETCOLOR (MT_COLOR_NORMAL);
290 /* erase the current indicator */
291 attrset (menu->color (menu->oldcurrent));
293 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
294 menu_pad_string (buf, sizeof (buf));
295 print_enriched_string (menu->color (menu->oldcurrent),
296 (unsigned char *) buf, 1);
298 /* now draw the new one to reflect the change */
299 menu_make_entry (buf, sizeof (buf), menu, menu->current);
300 menu_pad_string (buf, sizeof (buf));
301 attrset (menu->color (menu->current));
302 ADDCOLOR (MT_COLOR_INDICATOR);
303 BKGDSET (MT_COLOR_INDICATOR);
304 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
305 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
307 SETCOLOR (MT_COLOR_NORMAL);
308 BKGDSET (MT_COLOR_NORMAL);
310 menu->redraw &= REDRAW_STATUS;
313 void menu_redraw_current (MUTTMENU * menu)
317 move (menu->current + menu->offset - menu->top, SidebarWidth);
318 menu_make_entry (buf, sizeof (buf), menu, menu->current);
319 menu_pad_string (buf, sizeof (buf));
321 if (option (OPTARROWCURSOR)) {
322 int attr = menu->color (menu->current);
326 attrset (menu->color (menu->current));
327 ADDCOLOR (MT_COLOR_INDICATOR);
331 menu_pad_string (buf, sizeof (buf));
332 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
334 SETCOLOR (MT_COLOR_NORMAL);
337 attrset (menu->color (menu->current));
338 ADDCOLOR (MT_COLOR_INDICATOR);
339 BKGDSET (MT_COLOR_INDICATOR);
341 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
343 SETCOLOR (MT_COLOR_NORMAL);
344 BKGDSET (MT_COLOR_NORMAL);
346 menu->redraw &= REDRAW_STATUS;
349 void menu_redraw_prompt (MUTTMENU * menu)
352 if (option (OPTMSGERR)) {
354 unset_option (OPTMSGERR);
360 SETCOLOR (MT_COLOR_NORMAL);
361 mvaddstr (LINES - 1, 0, menu->prompt);
366 void menu_check_recenter (MUTTMENU * menu)
368 int c = MIN (MenuContext, menu->pagelen / 2);
369 int old_top = menu->top;
371 if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) { /* less entries than lines */
372 if (menu->top != 0) {
374 set_option (OPTNEEDREDRAW);
377 else if (menu->current >= menu->top + menu->pagelen - c) { /* indicator below bottom threshold */
378 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
379 menu->top = menu->current - menu->pagelen + c + 1;
383 c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
385 else if (menu->current < menu->top + c) { /* indicator above top threshold */
386 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
387 menu->top = menu->current - c;
391 c) * ((menu->top + menu->pagelen - 1 -
392 menu->current) / (menu->pagelen - c)) - c;
395 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
396 menu->top = MIN (menu->top, menu->max - menu->pagelen);
397 menu->top = MAX (menu->top, 0);
399 if (menu->top != old_top)
400 menu->redraw |= REDRAW_INDEX;
403 void menu_jump (MUTTMENU * menu)
406 char buf[SHORT_STRING];
409 mutt_ungetch (LastKey, 0);
411 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
413 if (n >= 0 && n < menu->max) {
415 menu->redraw = REDRAW_MOTION;
418 mutt_error _("Invalid index number.");
422 mutt_error _("No entries.");
425 void menu_next_line (MUTTMENU * menu)
428 int c = MIN (MenuContext, menu->pagelen / 2);
430 if (menu->top + 1 < menu->max - c && (option (OPTMENUMOVEOFF)
431 || (menu->max > menu->pagelen
433 menu->max - menu->pagelen))) {
435 if (menu->current < menu->top + c && menu->current < menu->max - 1)
437 menu->redraw = REDRAW_INDEX;
440 mutt_error _("You cannot scroll down farther.");
443 mutt_error _("No entries.");
446 void menu_prev_line (MUTTMENU * menu)
449 int c = MIN (MenuContext, menu->pagelen / 2);
452 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
454 menu->redraw = REDRAW_INDEX;
457 mutt_error _("You cannot scroll up farther.");
460 void menu_next_page (MUTTMENU * menu)
463 if (menu->top + menu->pagelen < menu->max) {
464 menu->top += menu->pagelen;
465 if (menu->current < menu->top)
466 menu->current = menu->top;
467 menu->redraw = REDRAW_INDEX;
469 else if (menu->current != menu->max - 1 && !menu->dialog) {
470 menu->current = menu->max - 1;
471 menu->redraw = REDRAW_MOTION;
474 mutt_error _("You are on the last page.");
477 mutt_error _("No entries.");
480 void menu_prev_page (MUTTMENU * menu)
482 int c = MIN (MenuContext, menu->pagelen / 2);
485 if ((menu->top -= menu->pagelen) < 0)
487 if (menu->current >= menu->top + menu->pagelen)
488 menu->current = menu->top + menu->pagelen - 1;
489 menu->redraw = REDRAW_INDEX;
491 else if (menu->current && !menu->dialog) {
493 menu->redraw = REDRAW_MOTION;
496 mutt_error _("You are on the first page.");
499 void menu_top_page (MUTTMENU * menu)
501 if (menu->current != menu->top) {
502 menu->current = menu->top;
503 menu->redraw = REDRAW_MOTION;
507 void menu_bottom_page (MUTTMENU * menu)
510 menu->current = menu->top + menu->pagelen - 1;
511 if (menu->current > menu->max - 1)
512 menu->current = menu->max - 1;
513 menu->redraw = REDRAW_MOTION;
516 mutt_error _("No entries.");
519 void menu_middle_page (MUTTMENU * menu)
524 i = menu->top + menu->pagelen;
525 if (i > menu->max - 1)
527 menu->current = menu->top + (i - menu->top) / 2;
528 menu->redraw = REDRAW_MOTION;
531 mutt_error _("No entries.");
534 void menu_first_entry (MUTTMENU * menu)
538 menu->redraw = REDRAW_MOTION;
541 mutt_error _("No entries.");
544 void menu_last_entry (MUTTMENU * menu)
547 menu->current = menu->max - 1;
548 menu->redraw = REDRAW_MOTION;
551 mutt_error _("No entries.");
554 void menu_half_up (MUTTMENU * menu)
557 if ((menu->top -= menu->pagelen / 2) < 0)
559 if (menu->current >= menu->top + menu->pagelen)
560 menu->current = menu->top + menu->pagelen - 1;
561 menu->redraw = REDRAW_INDEX;
563 else if (menu->current && !menu->dialog) {
565 menu->redraw = REDRAW_MOTION;
568 mutt_error _("First entry is shown.");
571 void menu_half_down (MUTTMENU * menu)
574 if (menu->top + menu->pagelen < menu->max) {
575 menu->top += menu->pagelen / 2;
576 if (menu->current < menu->top)
577 menu->current = menu->top;
578 menu->redraw = REDRAW_INDEX;
580 else if (menu->current != menu->max - 1 && !menu->dialog) {
581 menu->current = menu->max - 1;
582 menu->redraw = REDRAW_INDEX;
585 mutt_error _("Last entry is shown.");
588 mutt_error _("No entries.");
591 void menu_current_top (MUTTMENU * menu)
594 menu->top = menu->current;
595 menu->redraw = REDRAW_INDEX;
598 mutt_error _("No entries.");
601 void menu_current_middle (MUTTMENU * menu)
604 menu->top = menu->current - menu->pagelen / 2;
607 menu->redraw = REDRAW_INDEX;
610 mutt_error _("No entries.");
613 void menu_current_bottom (MUTTMENU * menu)
616 menu->top = menu->current - menu->pagelen + 1;
619 menu->redraw = REDRAW_INDEX;
622 mutt_error _("No entries.");
625 void menu_next_entry (MUTTMENU * menu)
627 if (menu->current < menu->max - 1) {
629 menu->redraw = REDRAW_MOTION;
632 mutt_error _("You are on the last entry.");
635 void menu_prev_entry (MUTTMENU * menu)
639 menu->redraw = REDRAW_MOTION;
642 mutt_error _("You are on the first entry.");
645 static int default_color (int i)
647 return ColorDefs[MT_COLOR_NORMAL];
650 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
652 char buf[LONG_STRING];
654 menu_make_entry (buf, sizeof (buf), m, n);
655 return (regexec (re, buf, 0, NULL, 0));
658 MUTTMENU *mutt_new_menu (void)
660 MUTTMENU *p = (MUTTMENU *) mem_calloc (1, sizeof (MUTTMENU));
665 p->redraw = REDRAW_FULL;
666 p->pagelen = PAGELEN;
667 p->color = default_color;
668 p->search = menu_search_generic;
672 void mutt_menuDestroy (MUTTMENU ** p)
676 mem_free (&(*p)->searchBuf);
679 for (i = 0; i < (*p)->max; i++)
680 mem_free (&(*p)->dialog[i]);
682 mem_free (&(*p)->dialog);
688 #define M_SEARCH_UP 1
689 #define M_SEARCH_DOWN 2
691 static int menu_search (MUTTMENU * menu, int op)
696 char buf[SHORT_STRING];
698 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
699 strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
700 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
701 _("Reverse search for: "),
702 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
704 str_replace (&menu->searchBuf, buf);
705 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
708 if (!menu->searchBuf) {
709 mutt_error _("No search pattern.");
715 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
716 if (op == OP_SEARCH_OPPOSITE)
717 searchDir = -searchDir;
720 REGCOMP (&re, menu->searchBuf,
721 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
722 regerror (r, &re, buf, sizeof (buf));
724 mutt_error ("%s", buf);
728 r = menu->current + searchDir;
729 while (r >= 0 && r < menu->max) {
730 if (menu->search (menu, &re, r) == 0) {
739 mutt_error _("Not found.");
744 static int menu_dialog_translate_op (int i)
754 case OP_CURRENT_BOTTOM:
756 return OP_BOTTOM_PAGE;
757 case OP_CURRENT_MIDDLE:
758 return OP_MIDDLE_PAGE;
764 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
776 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
777 *ip = OP_MAX + (p - menu->keys + 1);
781 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
786 int menu_redraw (MUTTMENU * menu)
788 /* See if all or part of the screen needs to be updated. */
789 if (menu->redraw & REDRAW_FULL) {
790 menu_redraw_full (menu);
791 /* allow the caller to do any local configuration */
796 menu_check_recenter (menu);
798 if (menu->redraw & REDRAW_STATUS)
799 menu_redraw_status (menu);
800 if (menu->redraw & REDRAW_INDEX)
801 menu_redraw_index (menu);
802 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
803 menu_redraw_motion (menu);
804 else if (menu->redraw == REDRAW_CURRENT)
805 menu_redraw_current (menu);
808 menu_redraw_prompt (menu);
813 int mutt_menuLoop (MUTTMENU * menu)
818 if (option (OPTMENUCALLER)) {
819 unset_option (OPTMENUCALLER);
830 if (menu_redraw (menu) == OP_REDRAW)
833 menu->oldcurrent = menu->current;
835 if (option (OPTARROWCURSOR))
836 move (menu->current - menu->top + menu->offset, 2);
837 else if (option (OPTBRAILLEFRIENDLY))
838 move (menu->current - menu->top + menu->offset, 0);
840 move (menu->current - menu->top + menu->offset, COLS - 1);
845 /* try to catch dialog keys before ops */
846 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
849 i = km_dokey (menu->menu);
850 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
852 mvaddstr (LINES - 1, 0, "Tag-");
854 i = km_dokey (menu->menu);
856 CLEARLINE (LINES - 1);
858 else if (i == OP_TAG_PREFIX) {
859 mutt_error _("No tagged entries.");
863 else { /* None tagged, OP_TAG_PREFIX_COND */
867 while (UngetCount > 0) {
869 if (tmp.op == OP_END_COND)
872 mutt_message _("Nothing to do.");
877 else if (menu->tagged && option (OPTAUTOTAG))
884 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
886 mutt_resize_screen ();
887 menu->redraw = REDRAW_FULL;
889 clearok (stdscr, TRUE); /*force complete redraw */
899 /* Convert menubar movement to scrolling */
901 i = menu_dialog_translate_op (i);
905 menu_next_entry (menu);
908 menu_prev_entry (menu);
911 menu_half_down (menu);
917 menu_next_page (menu);
920 menu_prev_page (menu);
923 menu_next_line (menu);
926 menu_prev_line (menu);
929 menu_first_entry (menu);
932 menu_last_entry (menu);
935 menu_top_page (menu);
938 menu_middle_page (menu);
941 menu_bottom_page (menu);
944 menu_current_top (menu);
946 case OP_CURRENT_MIDDLE:
947 menu_current_middle (menu);
949 case OP_CURRENT_BOTTOM:
950 menu_current_bottom (menu);
953 case OP_SEARCH_REVERSE:
955 case OP_SEARCH_OPPOSITE:
956 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
957 menu->oldcurrent = menu->current;
958 if ((menu->current = menu_search (menu, i)) != -1)
959 menu->redraw = REDRAW_MOTION;
961 menu->current = menu->oldcurrent;
964 mutt_error _("Search is not implemented for this menu.");
969 mutt_error (_("Jumping is not implemented for dialogs."));
975 case OP_ENTER_COMMAND:
976 CurrentMenu = menu->menu;
977 mutt_enter_command ();
978 if (option (OPTFORCEREDRAWINDEX)) {
979 menu->redraw = REDRAW_FULL;
980 unset_option (OPTFORCEREDRAWINDEX);
981 unset_option (OPTFORCEREDRAWPAGER);
986 if (menu->tag && !menu->dialog) {
987 if (menu->tagprefix && !option (OPTAUTOTAG)) {
988 for (i = 0; i < menu->max; i++)
989 menu->tagged += menu->tag (menu, i, 0);
990 menu->redraw = REDRAW_INDEX;
992 else if (menu->max) {
993 int i = menu->tag (menu, menu->current, -1);
996 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1) {
998 menu->redraw = REDRAW_MOTION_RESYNCH;
1001 menu->redraw = REDRAW_CURRENT;
1004 mutt_error _("No entries.");
1007 mutt_error _("Tagging is not supported.");
1010 case OP_SHELL_ESCAPE:
1011 mutt_shell_escape ();
1012 MAYBE_REDRAW (menu->redraw);
1020 clearok (stdscr, TRUE);
1021 menu->redraw = REDRAW_FULL;
1025 mutt_help (menu->menu);
1026 menu->redraw = REDRAW_FULL;
1030 km_error_key (menu->menu);