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 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
34 extern int Charset_is_utf8; /* FIXME: bad modularisation */
36 extern size_t UngetCount;
38 static void print_enriched_string (int attr, unsigned char *s, int do_color)
42 size_t n = str_len ((char *) s);
45 memset (&mbstate, 0, sizeof (mbstate));
47 if (*s < M_TREE_MAX) {
49 SETCOLOR (MT_COLOR_TREE);
50 while (*s && *s < M_TREE_MAX) {
53 if (option (OPTASCIICHARS))
55 else if (Charset_is_utf8)
56 addstr ("\342\224\224"); /* WACS_LLCORNER */
61 if (option (OPTASCIICHARS))
63 else if (Charset_is_utf8)
64 addstr ("\342\224\214"); /* WACS_ULCORNER */
69 if (option (OPTASCIICHARS))
71 else if (Charset_is_utf8)
72 addstr ("\342\224\234"); /* WACS_LTEE */
77 if (option (OPTASCIICHARS))
79 else if (Charset_is_utf8)
80 addstr ("\342\224\200"); /* WACS_HLINE */
85 if (option (OPTASCIICHARS))
87 else if (Charset_is_utf8)
88 addstr ("\342\224\202"); /* WACS_VLINE */
93 if (option (OPTASCIICHARS))
95 else if (Charset_is_utf8)
96 addstr ("\342\224\254"); /* WACS_TTEE */
101 if (option (OPTASCIICHARS))
103 else if (Charset_is_utf8)
104 addstr ("\342\224\264"); /* WACS_BTEE */
115 addch ('*'); /* fake thread indicator */
132 else if ((k = mbrtowc (&wc, (char *) s, n, &mbstate)) > 0) {
133 addnstr ((char *) s, k);
141 static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i)
144 strncpy (s, menu->dialog[i], l);
145 menu->current = -1; /* hide menubar */
148 menu->make_entry (s, l, menu, i);
151 void menu_pad_string (char *s, size_t n)
153 int shift = option (OPTARROWCURSOR) ? 3 : 0;
155 char *tmpbuf = mem_malloc (n);
157 if (option (OPTMBOXPANE))
158 cols = COLS - shift - SidebarWidth;
161 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, str_len (s), 1);
163 snprintf (s, n, "%s", tmpbuf); /* overkill */
167 void menu_redraw_full (MUTTMENU * menu)
169 SETCOLOR (MT_COLOR_NORMAL);
170 /* clear() doesn't optimize screen redraws */
174 if (option (OPTHELP)) {
175 SETCOLOR (MT_COLOR_STATUS);
176 move (option (OPTSTATUSONTOP) ? LINES - 2 : 0, 0);
177 mutt_paddstr (COLS, menu->help);
178 SETCOLOR (MT_COLOR_NORMAL);
180 menu->pagelen = LINES - 3;
183 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
184 menu->pagelen = LINES - 2;
189 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
192 void menu_redraw_status (MUTTMENU * menu)
196 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
197 SETCOLOR (MT_COLOR_STATUS);
198 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
199 mutt_paddstr (COLS, buf);
200 SETCOLOR (MT_COLOR_NORMAL);
201 menu->redraw &= ~REDRAW_STATUS;
204 void menu_redraw_index (MUTTMENU * menu)
210 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
212 menu_make_entry (buf, sizeof (buf), menu, i);
213 menu_pad_string (buf, sizeof (buf));
215 if (option (OPTARROWCURSOR)) {
216 attrset (menu->color (i));
217 CLEARLINE_WIN (i - menu->top + menu->offset);
219 if (i == menu->current) {
220 attrset (menu->color (i));
221 ADDCOLOR (MT_COLOR_INDICATOR);
222 BKGDSET (MT_COLOR_INDICATOR);
224 attrset (menu->color (i));
228 attrset (menu->color (i));
229 move (i - menu->top + menu->offset, SW);
233 print_enriched_string (menu->color (i), (unsigned char *) buf, 1);
234 SETCOLOR (MT_COLOR_NORMAL);
235 BKGDSET (MT_COLOR_NORMAL);
238 attrset (menu->color (i));
240 if (i == menu->current) {
241 ADDCOLOR (MT_COLOR_INDICATOR);
242 BKGDSET (MT_COLOR_INDICATOR);
245 CLEARLINE_WIN (i - menu->top + menu->offset);
247 move (i - menu->top + menu->offset, SW);
248 print_enriched_string (menu->color (i), (unsigned char *) buf,
250 SETCOLOR (MT_COLOR_NORMAL);
251 BKGDSET (MT_COLOR_NORMAL);
255 CLEARLINE_WIN (i - menu->top + menu->offset);
260 void menu_redraw_motion (MUTTMENU * menu)
265 menu->redraw &= ~REDRAW_MOTION;
269 move (menu->oldcurrent + menu->offset - menu->top, SW);
270 SETCOLOR (MT_COLOR_NORMAL);
271 BKGDSET (MT_COLOR_NORMAL);
273 if (option (OPTARROWCURSOR)) {
274 /* clear the pointer */
275 attrset (menu->color (menu->oldcurrent));
278 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, SW + 3);
283 print_enriched_string (menu->color (menu->oldcurrent),
284 (unsigned char *) buf, 1);
285 SETCOLOR (MT_COLOR_NORMAL);
288 /* now draw it in the new location */
289 move (menu->current + menu->offset - menu->top, SW);
290 attrset (menu->color (menu->current));
291 ADDCOLOR (MT_COLOR_INDICATOR);
293 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),
302 (unsigned char *) buf, 1);
304 /* now draw the new one to reflect the change */
305 menu_make_entry (buf, sizeof (buf), menu, menu->current);
306 menu_pad_string (buf, sizeof (buf));
307 attrset (menu->color (menu->current));
308 ADDCOLOR (MT_COLOR_INDICATOR);
309 BKGDSET (MT_COLOR_INDICATOR);
310 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
311 move (menu->current + menu->offset - menu->top, SW);
312 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
314 SETCOLOR (MT_COLOR_NORMAL);
315 BKGDSET (MT_COLOR_NORMAL);
317 menu->redraw &= REDRAW_STATUS;
320 void menu_redraw_current (MUTTMENU * menu)
324 move (menu->current + menu->offset - menu->top, SW);
325 menu_make_entry (buf, sizeof (buf), menu, menu->current);
326 menu_pad_string (buf, sizeof (buf));
328 if (option (OPTARROWCURSOR)) {
329 int attr = menu->color (menu->current);
333 attrset (menu->color (menu->current));
334 ADDCOLOR (MT_COLOR_INDICATOR);
338 menu_pad_string (buf, sizeof (buf));
339 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
341 SETCOLOR (MT_COLOR_NORMAL);
344 attrset (menu->color (menu->current));
345 ADDCOLOR (MT_COLOR_INDICATOR);
346 BKGDSET (MT_COLOR_INDICATOR);
348 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
350 SETCOLOR (MT_COLOR_NORMAL);
351 BKGDSET (MT_COLOR_NORMAL);
353 menu->redraw &= REDRAW_STATUS;
356 void menu_redraw_prompt (MUTTMENU * menu)
359 if (option (OPTMSGERR)) {
361 unset_option (OPTMSGERR);
367 SETCOLOR (MT_COLOR_NORMAL);
368 mvaddstr (LINES - 1, 0, menu->prompt);
373 void menu_check_recenter (MUTTMENU * menu)
375 int c = MIN (MenuContext, menu->pagelen / 2);
376 int old_top = menu->top;
378 if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) { /* less entries than lines */
379 if (menu->top != 0) {
381 set_option (OPTNEEDREDRAW);
384 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0) || (c < MenuContext)) {
385 if (menu->current < menu->top + c)
386 menu->top = menu->current - c;
387 else if (menu->current >= menu->top + menu->pagelen - c)
388 menu->top = menu->current - menu->pagelen + c + 1;
390 if (menu->current < menu->top + c)
391 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
392 else if ((menu->current >= menu->top + menu->pagelen - c))
393 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
397 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
398 menu->top = MIN (menu->top, menu->max - menu->pagelen);
399 menu->top = MAX (menu->top, 0);
401 if (menu->top != old_top)
402 menu->redraw |= REDRAW_INDEX;
405 void menu_jump (MUTTMENU * menu)
408 char buf[SHORT_STRING];
411 mutt_ungetch (LastKey, 0);
413 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
415 if (n >= 0 && n < menu->max) {
417 menu->redraw = REDRAW_MOTION;
420 mutt_error _("Invalid index number.");
424 mutt_error _("No entries.");
427 void menu_next_line (MUTTMENU * menu)
430 int c = MIN (MenuContext, menu->pagelen / 2);
432 if (menu->top + 1 < menu->max - c && (option (OPTMENUMOVEOFF)
433 || (menu->max > menu->pagelen
435 menu->max - menu->pagelen))) {
437 if (menu->current < menu->top + c && menu->current < menu->max - 1)
439 menu->redraw = REDRAW_INDEX;
442 mutt_error _("You cannot scroll down farther.");
445 mutt_error _("No entries.");
448 void menu_prev_line (MUTTMENU * menu)
451 int c = MIN (MenuContext, menu->pagelen / 2);
454 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
456 menu->redraw = REDRAW_INDEX;
459 mutt_error _("You cannot scroll up farther.");
463 * pageup: jumplen == -pagelen
464 * pagedown: jumplen == pagelen
465 * halfup: jumplen == -pagelen/2
466 * halfdown: jumplen == pagelen/2
468 #define DIRECTION ((neg * 2) + 1)
469 void menu_length_jump (MUTTMENU *menu, int jumplen) {
470 int tmp, neg = (jumplen >= 0) ? 0 : -1;
471 int c = MIN (MenuContext, menu->pagelen / 2);
474 /* possible to scroll? */
475 if (DIRECTION * menu->top <
476 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
477 menu->top += jumplen;
479 /* jumped too long? */
480 if ((neg || !option (OPTMENUMOVEOFF)) && DIRECTION * menu->top > tmp)
483 /* need to move the cursor? */
485 (tmp = (menu->current - (menu->top +
486 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
487 menu->current -= tmp;
489 menu->redraw = REDRAW_INDEX;
491 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
492 menu->current += jumplen;
493 menu->redraw = REDRAW_MOTION;
496 mutt_error (neg ? _("You are on the first page.")
497 : _("You are on the last page."));
499 menu->current = MIN (menu->current, menu->max - 1);
500 menu->current = MAX (menu->current, 0);
503 mutt_error _("No entries.");
507 void menu_next_page (MUTTMENU *menu) {
508 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
511 void menu_prev_page (MUTTMENU *menu) {
512 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
515 void menu_half_down (MUTTMENU *menu) {
516 menu_length_jump (menu, menu->pagelen / 2);
519 void menu_half_up (MUTTMENU *menu) {
520 menu_length_jump (menu, 0 - menu->pagelen / 2);
523 void menu_top_page (MUTTMENU *menu) {
524 if (menu->current != menu->top) {
525 menu->current = menu->top;
526 menu->redraw = REDRAW_MOTION;
530 void menu_bottom_page (MUTTMENU *menu) {
532 menu->current = menu->top + menu->pagelen - 1;
533 if (menu->current > menu->max - 1)
534 menu->current = menu->max - 1;
535 menu->redraw = REDRAW_MOTION;
538 mutt_error _("No entries.");
541 void menu_middle_page (MUTTMENU *menu) {
545 i = menu->top + menu->pagelen;
546 if (i > menu->max - 1)
548 menu->current = menu->top + (i - menu->top) / 2;
549 menu->redraw = REDRAW_MOTION;
552 mutt_error _("No entries.");
555 void menu_first_entry (MUTTMENU *menu) {
558 menu->redraw = REDRAW_MOTION;
561 mutt_error _("No entries.");
564 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_current_top (MUTTMENU * menu)
576 menu->top = menu->current;
577 menu->redraw = REDRAW_INDEX;
580 mutt_error _("No entries.");
583 void menu_current_middle (MUTTMENU * menu)
586 menu->top = menu->current - menu->pagelen / 2;
589 menu->redraw = REDRAW_INDEX;
592 mutt_error _("No entries.");
595 void menu_current_bottom (MUTTMENU * menu)
598 menu->top = menu->current - menu->pagelen + 1;
601 menu->redraw = REDRAW_INDEX;
604 mutt_error _("No entries.");
607 void menu_next_entry (MUTTMENU * menu)
609 if (menu->current < menu->max - 1) {
611 menu->redraw = REDRAW_MOTION;
614 mutt_error _("You are on the last entry.");
617 void menu_prev_entry (MUTTMENU * menu)
621 menu->redraw = REDRAW_MOTION;
624 mutt_error _("You are on the first entry.");
627 static int default_color (int i)
629 return ColorDefs[MT_COLOR_NORMAL];
632 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
634 char buf[LONG_STRING];
636 menu_make_entry (buf, sizeof (buf), m, n);
637 return (regexec (re, buf, 0, NULL, 0));
640 MUTTMENU *mutt_new_menu (void)
642 MUTTMENU *p = (MUTTMENU *) mem_calloc (1, sizeof (MUTTMENU));
647 p->redraw = REDRAW_FULL;
648 p->pagelen = PAGELEN;
649 p->color = default_color;
650 p->search = menu_search_generic;
654 void mutt_menuDestroy (MUTTMENU ** p)
658 mem_free (&(*p)->searchBuf);
661 for (i = 0; i < (*p)->max; i++)
662 mem_free (&(*p)->dialog[i]);
664 mem_free (&(*p)->dialog);
670 #define M_SEARCH_UP 1
671 #define M_SEARCH_DOWN 2
673 static int menu_search (MUTTMENU * menu, int op)
678 char buf[SHORT_STRING];
680 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
681 strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
682 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
683 _("Reverse search for: "),
684 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
686 str_replace (&menu->searchBuf, buf);
687 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
690 if (!menu->searchBuf) {
691 mutt_error _("No search pattern.");
697 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
698 if (op == OP_SEARCH_OPPOSITE)
699 searchDir = -searchDir;
702 REGCOMP (&re, menu->searchBuf,
703 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
704 regerror (r, &re, buf, sizeof (buf));
706 mutt_error ("%s", buf);
710 r = menu->current + searchDir;
711 while (r >= 0 && r < menu->max) {
712 if (menu->search (menu, &re, r) == 0) {
721 mutt_error _("Not found.");
726 static int menu_dialog_translate_op (int i)
736 case OP_CURRENT_BOTTOM:
738 return OP_BOTTOM_PAGE;
739 case OP_CURRENT_MIDDLE:
740 return OP_MIDDLE_PAGE;
746 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
758 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
759 *ip = OP_MAX + (p - menu->keys + 1);
763 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
768 int menu_redraw (MUTTMENU * menu)
770 /* See if all or part of the screen needs to be updated. */
771 if (menu->redraw & REDRAW_FULL) {
772 menu_redraw_full (menu);
773 /* allow the caller to do any local configuration */
778 menu_check_recenter (menu);
780 if (menu->redraw & REDRAW_STATUS)
781 menu_redraw_status (menu);
782 if (menu->redraw & REDRAW_INDEX)
783 menu_redraw_index (menu);
784 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
785 menu_redraw_motion (menu);
786 else if (menu->redraw == REDRAW_CURRENT)
787 menu_redraw_current (menu);
790 menu_redraw_prompt (menu);
795 int mutt_menuLoop (MUTTMENU * menu)
800 if (option (OPTMENUCALLER)) {
801 unset_option (OPTMENUCALLER);
812 if (menu_redraw (menu) == OP_REDRAW)
815 menu->oldcurrent = menu->current;
817 if (option (OPTARROWCURSOR))
818 move (menu->current - menu->top + menu->offset, SW + 2);
819 else if (option (OPTBRAILLEFRIENDLY))
820 move (menu->current - menu->top + menu->offset, SW);
822 move (menu->current - menu->top + menu->offset, COLS - 1);
827 /* try to catch dialog keys before ops */
828 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
831 i = km_dokey (menu->menu);
832 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
834 mvaddstr (LINES - 1, 0, "Tag-");
836 i = km_dokey (menu->menu);
838 CLEARLINE (LINES - 1);
840 else if (i == OP_TAG_PREFIX) {
841 mutt_error _("No tagged entries.");
845 else { /* None tagged, OP_TAG_PREFIX_COND */
849 while (UngetCount > 0) {
851 if (tmp.op == OP_END_COND)
854 mutt_message _("Nothing to do.");
859 else if (menu->tagged && option (OPTAUTOTAG))
866 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
868 mutt_resize_screen ();
869 menu->redraw = REDRAW_FULL;
871 clearok (stdscr, TRUE); /*force complete redraw */
881 /* Convert menubar movement to scrolling */
883 i = menu_dialog_translate_op (i);
887 menu_next_entry (menu);
890 menu_prev_entry (menu);
893 menu_half_down (menu);
899 menu_next_page (menu);
902 menu_prev_page (menu);
905 menu_next_line (menu);
908 menu_prev_line (menu);
911 menu_first_entry (menu);
914 menu_last_entry (menu);
917 menu_top_page (menu);
920 menu_middle_page (menu);
923 menu_bottom_page (menu);
926 menu_current_top (menu);
928 case OP_CURRENT_MIDDLE:
929 menu_current_middle (menu);
931 case OP_CURRENT_BOTTOM:
932 menu_current_bottom (menu);
935 case OP_SEARCH_REVERSE:
937 case OP_SEARCH_OPPOSITE:
938 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
939 menu->oldcurrent = menu->current;
940 if ((menu->current = menu_search (menu, i)) != -1)
941 menu->redraw = REDRAW_MOTION;
943 menu->current = menu->oldcurrent;
946 mutt_error _("Search is not implemented for this menu.");
951 mutt_error (_("Jumping is not implemented for dialogs."));
957 case OP_ENTER_COMMAND:
958 CurrentMenu = menu->menu;
959 mutt_enter_command ();
960 if (option (OPTFORCEREDRAWINDEX)) {
961 menu->redraw = REDRAW_FULL;
962 unset_option (OPTFORCEREDRAWINDEX);
963 unset_option (OPTFORCEREDRAWPAGER);
968 if (menu->tag && !menu->dialog) {
969 if (menu->tagprefix && !option (OPTAUTOTAG)) {
970 for (i = 0; i < menu->max; i++)
971 menu->tagged += menu->tag (menu, i, 0);
972 menu->redraw = REDRAW_INDEX;
974 else if (menu->max) {
975 int i = menu->tag (menu, menu->current, -1);
978 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1) {
980 menu->redraw = REDRAW_MOTION_RESYNCH;
983 menu->redraw = REDRAW_CURRENT;
986 mutt_error _("No entries.");
989 mutt_error _("Tagging is not supported.");
992 case OP_SHELL_ESCAPE:
993 mutt_shell_escape ();
994 MAYBE_REDRAW (menu->redraw);
1001 case OP_REBUILD_CACHE:
1002 mx_rebuild_cache ();
1006 clearok (stdscr, TRUE);
1007 menu->redraw = REDRAW_FULL;
1011 mutt_help (menu->menu);
1012 menu->redraw = REDRAW_FULL;
1016 km_error_key (menu->menu);