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.");
461 * pageup: jumplen == -pagelen
462 * pagedown: jumplen == pagelen
463 * halfup: jumplen == -pagelen/2
464 * halfdown: jumplen == pagelen/2
466 #define DIRECTION ((neg * 2) + 1)
467 void menu_length_jump (MUTTMENU *menu, int jumplen) {
468 int tmp, neg = (jumplen >= 0) ? 0 : -1;
469 int c = MIN (MenuContext, menu->pagelen / 2);
472 /* possible to scroll? */
473 if (DIRECTION * menu->top <
474 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
475 menu->top += jumplen;
477 /* jumped too long? */
478 if ((neg || !option (OPTMENUMOVEOFF)) && DIRECTION * menu->top > tmp)
481 /* need to move the cursor? */
483 (tmp = (menu->current - (menu->top +
484 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
485 menu->current -= tmp;
487 menu->redraw = REDRAW_INDEX;
489 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
490 menu->current += jumplen;
491 menu->redraw = REDRAW_MOTION;
494 mutt_error (neg ? _("You are on the first page.")
495 : _("You are on the last page."));
497 menu->current = MIN (menu->current, menu->max - 1);
498 menu->current = MAX (menu->current, 0);
501 mutt_error _("No entries.");
505 void menu_next_page (MUTTMENU *menu) {
506 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
509 void menu_prev_page (MUTTMENU *menu) {
510 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
513 void menu_half_down (MUTTMENU *menu) {
514 menu_length_jump (menu, menu->pagelen / 2);
517 void menu_half_up (MUTTMENU *menu) {
518 menu_length_jump (menu, 0 - menu->pagelen / 2);
521 void menu_top_page (MUTTMENU *menu) {
522 if (menu->current != menu->top) {
523 menu->current = menu->top;
524 menu->redraw = REDRAW_MOTION;
528 void menu_bottom_page (MUTTMENU *menu) {
530 menu->current = menu->top + menu->pagelen - 1;
531 if (menu->current > menu->max - 1)
532 menu->current = menu->max - 1;
533 menu->redraw = REDRAW_MOTION;
536 mutt_error _("No entries.");
539 void menu_middle_page (MUTTMENU *menu) {
543 i = menu->top + menu->pagelen;
544 if (i > menu->max - 1)
546 menu->current = menu->top + (i - menu->top) / 2;
547 menu->redraw = REDRAW_MOTION;
550 mutt_error _("No entries.");
553 void menu_first_entry (MUTTMENU *menu) {
556 menu->redraw = REDRAW_MOTION;
559 mutt_error _("No entries.");
562 void menu_last_entry (MUTTMENU *menu) {
564 menu->current = menu->max - 1;
565 menu->redraw = REDRAW_MOTION;
568 mutt_error _("No entries.");
571 void menu_current_top (MUTTMENU * menu)
574 menu->top = menu->current;
575 menu->redraw = REDRAW_INDEX;
578 mutt_error _("No entries.");
581 void menu_current_middle (MUTTMENU * menu)
584 menu->top = menu->current - menu->pagelen / 2;
587 menu->redraw = REDRAW_INDEX;
590 mutt_error _("No entries.");
593 void menu_current_bottom (MUTTMENU * menu)
596 menu->top = menu->current - menu->pagelen + 1;
599 menu->redraw = REDRAW_INDEX;
602 mutt_error _("No entries.");
605 void menu_next_entry (MUTTMENU * menu)
607 if (menu->current < menu->max - 1) {
609 menu->redraw = REDRAW_MOTION;
612 mutt_error _("You are on the last entry.");
615 void menu_prev_entry (MUTTMENU * menu)
619 menu->redraw = REDRAW_MOTION;
622 mutt_error _("You are on the first entry.");
625 static int default_color (int i)
627 return ColorDefs[MT_COLOR_NORMAL];
630 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
632 char buf[LONG_STRING];
634 menu_make_entry (buf, sizeof (buf), m, n);
635 return (regexec (re, buf, 0, NULL, 0));
638 MUTTMENU *mutt_new_menu (void)
640 MUTTMENU *p = (MUTTMENU *) mem_calloc (1, sizeof (MUTTMENU));
645 p->redraw = REDRAW_FULL;
646 p->pagelen = PAGELEN;
647 p->color = default_color;
648 p->search = menu_search_generic;
652 void mutt_menuDestroy (MUTTMENU ** p)
656 mem_free (&(*p)->searchBuf);
659 for (i = 0; i < (*p)->max; i++)
660 mem_free (&(*p)->dialog[i]);
662 mem_free (&(*p)->dialog);
668 #define M_SEARCH_UP 1
669 #define M_SEARCH_DOWN 2
671 static int menu_search (MUTTMENU * menu, int op)
676 char buf[SHORT_STRING];
678 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
679 strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
680 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
681 _("Reverse search for: "),
682 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
684 str_replace (&menu->searchBuf, buf);
685 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
688 if (!menu->searchBuf) {
689 mutt_error _("No search pattern.");
695 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
696 if (op == OP_SEARCH_OPPOSITE)
697 searchDir = -searchDir;
700 REGCOMP (&re, menu->searchBuf,
701 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
702 regerror (r, &re, buf, sizeof (buf));
704 mutt_error ("%s", buf);
708 r = menu->current + searchDir;
709 while (r >= 0 && r < menu->max) {
710 if (menu->search (menu, &re, r) == 0) {
719 mutt_error _("Not found.");
724 static int menu_dialog_translate_op (int i)
734 case OP_CURRENT_BOTTOM:
736 return OP_BOTTOM_PAGE;
737 case OP_CURRENT_MIDDLE:
738 return OP_MIDDLE_PAGE;
744 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
756 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
757 *ip = OP_MAX + (p - menu->keys + 1);
761 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
766 int menu_redraw (MUTTMENU * menu)
768 /* See if all or part of the screen needs to be updated. */
769 if (menu->redraw & REDRAW_FULL) {
770 menu_redraw_full (menu);
771 /* allow the caller to do any local configuration */
776 menu_check_recenter (menu);
778 if (menu->redraw & REDRAW_STATUS)
779 menu_redraw_status (menu);
780 if (menu->redraw & REDRAW_INDEX)
781 menu_redraw_index (menu);
782 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
783 menu_redraw_motion (menu);
784 else if (menu->redraw == REDRAW_CURRENT)
785 menu_redraw_current (menu);
788 menu_redraw_prompt (menu);
793 int mutt_menuLoop (MUTTMENU * menu)
798 if (option (OPTMENUCALLER)) {
799 unset_option (OPTMENUCALLER);
810 if (menu_redraw (menu) == OP_REDRAW)
813 menu->oldcurrent = menu->current;
815 if (option (OPTARROWCURSOR))
816 move (menu->current - menu->top + menu->offset, 2);
817 else if (option (OPTBRAILLEFRIENDLY))
818 move (menu->current - menu->top + menu->offset, 0);
820 move (menu->current - menu->top + menu->offset, COLS - 1);
825 /* try to catch dialog keys before ops */
826 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
829 i = km_dokey (menu->menu);
830 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
832 mvaddstr (LINES - 1, 0, "Tag-");
834 i = km_dokey (menu->menu);
836 CLEARLINE (LINES - 1);
838 else if (i == OP_TAG_PREFIX) {
839 mutt_error _("No tagged entries.");
843 else { /* None tagged, OP_TAG_PREFIX_COND */
847 while (UngetCount > 0) {
849 if (tmp.op == OP_END_COND)
852 mutt_message _("Nothing to do.");
857 else if (menu->tagged && option (OPTAUTOTAG))
864 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
866 mutt_resize_screen ();
867 menu->redraw = REDRAW_FULL;
869 clearok (stdscr, TRUE); /*force complete redraw */
879 /* Convert menubar movement to scrolling */
881 i = menu_dialog_translate_op (i);
885 menu_next_entry (menu);
888 menu_prev_entry (menu);
891 menu_half_down (menu);
897 menu_next_page (menu);
900 menu_prev_page (menu);
903 menu_next_line (menu);
906 menu_prev_line (menu);
909 menu_first_entry (menu);
912 menu_last_entry (menu);
915 menu_top_page (menu);
918 menu_middle_page (menu);
921 menu_bottom_page (menu);
924 menu_current_top (menu);
926 case OP_CURRENT_MIDDLE:
927 menu_current_middle (menu);
929 case OP_CURRENT_BOTTOM:
930 menu_current_bottom (menu);
933 case OP_SEARCH_REVERSE:
935 case OP_SEARCH_OPPOSITE:
936 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
937 menu->oldcurrent = menu->current;
938 if ((menu->current = menu_search (menu, i)) != -1)
939 menu->redraw = REDRAW_MOTION;
941 menu->current = menu->oldcurrent;
944 mutt_error _("Search is not implemented for this menu.");
949 mutt_error (_("Jumping is not implemented for dialogs."));
955 case OP_ENTER_COMMAND:
956 CurrentMenu = menu->menu;
957 mutt_enter_command ();
958 if (option (OPTFORCEREDRAWINDEX)) {
959 menu->redraw = REDRAW_FULL;
960 unset_option (OPTFORCEREDRAWINDEX);
961 unset_option (OPTFORCEREDRAWPAGER);
966 if (menu->tag && !menu->dialog) {
967 if (menu->tagprefix && !option (OPTAUTOTAG)) {
968 for (i = 0; i < menu->max; i++)
969 menu->tagged += menu->tag (menu, i, 0);
970 menu->redraw = REDRAW_INDEX;
972 else if (menu->max) {
973 int i = menu->tag (menu, menu->current, -1);
976 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1) {
978 menu->redraw = REDRAW_MOTION_RESYNCH;
981 menu->redraw = REDRAW_CURRENT;
984 mutt_error _("No entries.");
987 mutt_error _("Tagging is not supported.");
990 case OP_SHELL_ESCAPE:
991 mutt_shell_escape ();
992 MAYBE_REDRAW (menu->redraw);
999 case OP_REBUILD_CACHE:
1000 mx_rebuild_cache ();
1004 clearok (stdscr, TRUE);
1005 menu->redraw = REDRAW_FULL;
1009 mutt_help (menu->menu);
1010 menu->redraw = REDRAW_FULL;
1014 km_error_key (menu->menu);