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.
10 #include <lib-lib/lib-lib.h>
19 #include <imap/imap.h>
20 #include <lib-ui/sidebar.h>
22 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
24 extern size_t UngetCount;
26 static void print_enriched_string (int attr, unsigned char *s, int do_color)
30 size_t n = m_strlen((char *) s);
35 if (*s < M_TREE_MAX) {
37 SETCOLOR (MT_COLOR_TREE);
38 while (*s && *s < M_TREE_MAX) {
41 if (option (OPTASCIICHARS))
43 else if (Charset_is_utf8)
44 addstr ("\342\224\224"); /* WACS_LLCORNER */
49 if (option (OPTASCIICHARS))
51 else if (Charset_is_utf8)
52 addstr ("\342\224\214"); /* WACS_ULCORNER */
57 if (option (OPTASCIICHARS))
59 else if (Charset_is_utf8)
60 addstr ("\342\224\234"); /* WACS_LTEE */
65 if (option (OPTASCIICHARS))
67 else if (Charset_is_utf8)
68 addstr ("\342\224\200"); /* WACS_HLINE */
73 if (option (OPTASCIICHARS))
75 else if (Charset_is_utf8)
76 addstr ("\342\224\202"); /* WACS_VLINE */
81 if (option (OPTASCIICHARS))
83 else if (Charset_is_utf8)
84 addstr ("\342\224\254"); /* WACS_TTEE */
89 if (option (OPTASCIICHARS))
91 else if (Charset_is_utf8)
92 addstr ("\342\224\264"); /* WACS_BTEE */
103 addch ('*'); /* fake thread indicator */
120 else if ((k = mbrtowc (&wc, (char *) s, n, &mbstate)) != (size_t)-1) {
121 addnstr ((char *) s, k);
129 static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i)
132 m_strcpy(s, l, menu->dialog[i]);
133 menu->current = -1; /* hide menubar */
136 menu->make_entry (s, l, menu, i);
139 static void menu_pad_string (char *s, size_t n)
141 int cols = COLS - SW;
142 char *tmpbuf = p_new(char, n);
144 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, m_strlen(s), 1);
146 snprintf (s, n, "%s", tmpbuf); /* overkill */
150 void menu_redraw_full (MUTTMENU * menu)
152 SETCOLOR (MT_COLOR_NORMAL);
153 /* clear() doesn't optimize screen redraws */
157 if (option (OPTHELP)) {
158 SETCOLOR (MT_COLOR_STATUS);
159 move (option (OPTSTATUSONTOP) ? LINES - 2 : 0, SW);
160 mutt_paddstr (COLS-SW, "");
161 SETCOLOR (MT_COLOR_NORMAL);
163 menu->pagelen = LINES - 3;
166 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
167 menu->pagelen = LINES - 2;
170 sidebar_draw_frames();
174 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
177 void menu_redraw_status (MUTTMENU * menu)
181 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
182 SETCOLOR (MT_COLOR_STATUS);
183 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, SW);
184 mutt_paddstr (COLS-SW, buf);
185 SETCOLOR (MT_COLOR_NORMAL);
186 menu->redraw &= ~REDRAW_STATUS;
187 sidebar_draw_frames();
190 void menu_redraw_index (MUTTMENU * menu)
195 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
197 menu_make_entry (buf, sizeof (buf), menu, i);
198 menu_pad_string (buf, sizeof (buf));
200 attrset (menu->color (i));
202 if (i == menu->current) {
203 ADDCOLOR (MT_COLOR_INDICATOR);
204 BKGDSET (MT_COLOR_INDICATOR);
207 CLEARLINE_WIN (i - menu->top + menu->offset);
209 move (i - menu->top + menu->offset, SW);
210 print_enriched_string (menu->color (i), (unsigned char *) buf,
212 SETCOLOR (MT_COLOR_NORMAL);
213 BKGDSET (MT_COLOR_NORMAL);
215 CLEARLINE_WIN (i - menu->top + menu->offset);
223 void menu_redraw_motion (MUTTMENU * menu)
228 menu->redraw &= ~REDRAW_MOTION;
232 move (menu->oldcurrent + menu->offset - menu->top, SW);
233 SETCOLOR (MT_COLOR_NORMAL);
234 BKGDSET (MT_COLOR_NORMAL);
236 /* erase the current indicator */
237 attrset (menu->color (menu->oldcurrent));
239 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
240 menu_pad_string (buf, sizeof (buf));
241 print_enriched_string (menu->color (menu->oldcurrent),
242 (unsigned char *) buf, 1);
244 /* now draw the new one to reflect the change */
245 menu_make_entry (buf, sizeof (buf), menu, menu->current);
246 menu_pad_string (buf, sizeof (buf));
247 attrset (menu->color (menu->current));
248 ADDCOLOR (MT_COLOR_INDICATOR);
249 BKGDSET (MT_COLOR_INDICATOR);
250 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
251 move (menu->current + menu->offset - menu->top, SW);
252 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
254 SETCOLOR (MT_COLOR_NORMAL);
255 BKGDSET (MT_COLOR_NORMAL);
257 menu->redraw &= REDRAW_STATUS;
260 void menu_redraw_current (MUTTMENU * menu)
264 move (menu->current + menu->offset - menu->top, SW);
265 menu_make_entry (buf, sizeof (buf), menu, menu->current);
266 menu_pad_string (buf, sizeof (buf));
268 attrset (menu->color (menu->current));
269 ADDCOLOR (MT_COLOR_INDICATOR);
270 BKGDSET (MT_COLOR_INDICATOR);
272 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
274 SETCOLOR (MT_COLOR_NORMAL);
275 BKGDSET (MT_COLOR_NORMAL);
277 menu->redraw &= REDRAW_STATUS;
280 static void menu_redraw_prompt (MUTTMENU * menu)
283 if (option (OPTMSGERR)) {
285 unset_option (OPTMSGERR);
291 SETCOLOR (MT_COLOR_NORMAL);
292 mvaddstr (LINES - 1, 0, menu->prompt);
297 void menu_check_recenter (MUTTMENU * menu)
299 int c = MIN (MenuContext, menu->pagelen / 2);
300 int old_top = menu->top;
302 if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) { /* less entries than lines */
303 if (menu->top != 0) {
305 set_option (OPTNEEDREDRAW);
308 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0) || (c < MenuContext)) {
309 if (menu->current < menu->top + c)
310 menu->top = menu->current - c;
311 else if (menu->current >= menu->top + menu->pagelen - c)
312 menu->top = menu->current - menu->pagelen + c + 1;
314 if (menu->current < menu->top + c)
315 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
316 else if ((menu->current >= menu->top + menu->pagelen - c))
317 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
321 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
322 menu->top = MIN (menu->top, menu->max - menu->pagelen);
323 menu->top = MAX (menu->top, 0);
325 if (menu->top != old_top)
326 menu->redraw |= REDRAW_INDEX;
329 void menu_jump (MUTTMENU * menu)
335 mutt_ungetch (LastKey, 0);
337 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
339 if (n >= 0 && n < menu->max) {
341 menu->redraw = REDRAW_MOTION;
344 mutt_error _("Invalid index number.");
348 mutt_error _("No entries.");
351 void menu_next_line (MUTTMENU * menu)
354 int c = MIN (MenuContext, menu->pagelen / 2);
356 if (menu->top + 1 < menu->max - c && (option (OPTMENUMOVEOFF)
357 || (menu->max > menu->pagelen
359 menu->max - menu->pagelen))) {
361 if (menu->current < menu->top + c && menu->current < menu->max - 1)
363 menu->redraw = REDRAW_INDEX;
366 mutt_error _("You cannot scroll down farther.");
369 mutt_error _("No entries.");
372 void menu_prev_line (MUTTMENU * menu)
375 int c = MIN (MenuContext, menu->pagelen / 2);
378 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
380 menu->redraw = REDRAW_INDEX;
383 mutt_error _("You cannot scroll up farther.");
387 * pageup: jumplen == -pagelen
388 * pagedown: jumplen == pagelen
389 * halfup: jumplen == -pagelen/2
390 * halfdown: jumplen == pagelen/2
392 #define DIRECTION ((neg * 2) + 1)
393 static void menu_length_jump (MUTTMENU *menu, int jumplen) {
394 int tmp, neg = (jumplen >= 0) ? 0 : -1;
395 int c = MIN (MenuContext, menu->pagelen / 2);
398 /* possible to scroll? */
399 if (DIRECTION * menu->top <
400 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
401 menu->top += jumplen;
403 /* jumped too long? */
404 if ((neg || !option (OPTMENUMOVEOFF)) && DIRECTION * menu->top > tmp)
407 /* need to move the cursor? */
409 (tmp = (menu->current - (menu->top +
410 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
411 menu->current -= tmp;
413 menu->redraw = REDRAW_INDEX;
415 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
416 menu->current += jumplen;
417 menu->redraw = REDRAW_MOTION;
420 mutt_error (neg ? _("You are on the first page.")
421 : _("You are on the last page."));
423 menu->current = MIN (menu->current, menu->max - 1);
424 menu->current = MAX (menu->current, 0);
427 mutt_error _("No entries.");
431 void menu_next_page (MUTTMENU *menu) {
432 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
435 void menu_prev_page (MUTTMENU *menu) {
436 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
439 void menu_half_down (MUTTMENU *menu) {
440 menu_length_jump (menu, menu->pagelen / 2);
443 void menu_half_up (MUTTMENU *menu) {
444 menu_length_jump (menu, 0 - menu->pagelen / 2);
447 void menu_top_page (MUTTMENU *menu) {
448 if (menu->current != menu->top) {
449 menu->current = menu->top;
450 menu->redraw = REDRAW_MOTION;
454 void menu_bottom_page (MUTTMENU *menu) {
456 menu->current = menu->top + menu->pagelen - 1;
457 if (menu->current > menu->max - 1)
458 menu->current = menu->max - 1;
459 menu->redraw = REDRAW_MOTION;
462 mutt_error _("No entries.");
465 void menu_middle_page (MUTTMENU *menu) {
469 i = menu->top + menu->pagelen;
470 if (i > menu->max - 1)
472 menu->current = menu->top + (i - menu->top) / 2;
473 menu->redraw = REDRAW_MOTION;
476 mutt_error _("No entries.");
479 void menu_first_entry (MUTTMENU *menu) {
482 menu->redraw = REDRAW_MOTION;
485 mutt_error _("No entries.");
488 void menu_last_entry (MUTTMENU *menu) {
490 menu->current = menu->max - 1;
491 menu->redraw = REDRAW_MOTION;
494 mutt_error _("No entries.");
497 void menu_current_top (MUTTMENU * menu)
500 menu->top = menu->current;
501 menu->redraw = REDRAW_INDEX;
504 mutt_error _("No entries.");
507 void menu_current_middle (MUTTMENU * menu)
510 menu->top = menu->current - menu->pagelen / 2;
513 menu->redraw = REDRAW_INDEX;
516 mutt_error _("No entries.");
519 void menu_current_bottom (MUTTMENU * menu)
522 menu->top = menu->current - menu->pagelen + 1;
525 menu->redraw = REDRAW_INDEX;
528 mutt_error _("No entries.");
531 static void menu_next_entry (MUTTMENU * menu)
533 if (menu->current < menu->max - 1) {
535 menu->redraw = REDRAW_MOTION;
538 mutt_error _("You are on the last entry.");
541 static void menu_prev_entry (MUTTMENU * menu)
545 menu->redraw = REDRAW_MOTION;
548 mutt_error _("You are on the first entry.");
551 static int default_color (int i __attribute__ ((unused)))
553 return ColorDefs[MT_COLOR_NORMAL];
556 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
558 char buf[LONG_STRING];
560 menu_make_entry (buf, sizeof (buf), m, n);
561 return (regexec (re, buf, 0, NULL, 0));
564 MUTTMENU *mutt_new_menu (void)
566 MUTTMENU *p = p_new(MUTTMENU, 1);
571 p->redraw = REDRAW_FULL;
572 p->pagelen = PAGELEN;
573 p->color = default_color;
574 p->search = menu_search_generic;
578 void mutt_menuDestroy (MUTTMENU ** p)
582 p_delete(&(*p)->searchBuf);
585 for (i = 0; i < (*p)->max; i++)
586 p_delete(&(*p)->dialog[i]);
588 p_delete(&(*p)->dialog);
594 #define M_SEARCH_UP 1
595 #define M_SEARCH_DOWN 2
597 static int menu_search (MUTTMENU * menu, int op)
604 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
605 m_strcpy(buf, sizeof(buf), NONULL(menu->searchBuf));
606 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
607 _("Reverse search for: "),
608 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
610 m_strreplace(&menu->searchBuf, buf);
611 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
614 if (!menu->searchBuf) {
615 mutt_error _("No search pattern.");
621 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
622 if (op == OP_SEARCH_OPPOSITE)
623 searchDir = -searchDir;
626 REGCOMP (&re, menu->searchBuf,
627 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
628 regerror (r, &re, buf, sizeof (buf));
630 mutt_error ("%s", buf);
634 r = menu->current + searchDir;
635 while (r >= 0 && r < menu->max) {
636 if (menu->search (menu, &re, r) == 0) {
645 mutt_error _("Not found.");
650 static int menu_dialog_translate_op (int i)
660 case OP_CURRENT_BOTTOM:
662 return OP_BOTTOM_PAGE;
663 case OP_CURRENT_MIDDLE:
664 return OP_MIDDLE_PAGE;
670 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
682 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
683 *ip = OP_MAX + (p - menu->keys + 1);
687 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
692 int menu_redraw (MUTTMENU * menu)
694 /* See if all or part of the screen needs to be updated. */
695 if (menu->redraw & REDRAW_FULL) {
696 menu_redraw_full (menu);
697 /* allow the caller to do any local configuration */
702 menu_check_recenter (menu);
704 if (menu->redraw & REDRAW_STATUS)
705 menu_redraw_status (menu);
706 if (menu->redraw & REDRAW_INDEX)
707 menu_redraw_index (menu);
708 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
709 menu_redraw_motion (menu);
710 else if (menu->redraw == REDRAW_CURRENT)
711 menu_redraw_current (menu);
714 menu_redraw_prompt (menu);
719 int mutt_menuLoop (MUTTMENU * menu)
724 if (option (OPTMENUCALLER)) {
725 unset_option (OPTMENUCALLER);
733 if (menu_redraw (menu) == OP_REDRAW)
736 menu->oldcurrent = menu->current;
738 if (option (OPTBRAILLEFRIENDLY))
739 move (menu->current - menu->top + menu->offset, SW);
741 move (menu->current - menu->top + menu->offset, COLS - 1);
746 /* try to catch dialog keys before ops */
747 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
750 i = km_dokey (menu->menu);
751 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
753 mvaddstr (LINES - 1, 0, "Tag-");
755 i = km_dokey (menu->menu);
757 CLEARLINE (LINES - 1);
759 else if (i == OP_TAG_PREFIX) {
760 mutt_error _("No tagged entries.");
764 else { /* None tagged, OP_TAG_PREFIX_COND */
768 while (UngetCount > 0) {
770 if (tmp.op == OP_END_COND)
773 mutt_message _("Nothing to do.");
778 else if (menu->tagged && option (OPTAUTOTAG))
786 mutt_resize_screen ();
787 menu->redraw = REDRAW_FULL;
789 clearok (stdscr, TRUE); /*force complete redraw */
798 /* Convert menubar movement to scrolling */
800 i = menu_dialog_translate_op (i);
804 menu_next_entry (menu);
807 menu_prev_entry (menu);
810 menu_half_down (menu);
816 menu_next_page (menu);
819 menu_prev_page (menu);
822 menu_next_line (menu);
825 menu_prev_line (menu);
828 menu_first_entry (menu);
831 menu_last_entry (menu);
834 menu_top_page (menu);
837 menu_middle_page (menu);
840 menu_bottom_page (menu);
843 menu_current_top (menu);
845 case OP_CURRENT_MIDDLE:
846 menu_current_middle (menu);
848 case OP_CURRENT_BOTTOM:
849 menu_current_bottom (menu);
852 case OP_SEARCH_REVERSE:
854 case OP_SEARCH_OPPOSITE:
855 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
856 menu->oldcurrent = menu->current;
857 if ((menu->current = menu_search (menu, i)) != -1)
858 menu->redraw = REDRAW_MOTION;
860 menu->current = menu->oldcurrent;
863 mutt_error _("Search is not implemented for this menu.");
868 mutt_error (_("Jumping is not implemented for dialogs."));
874 case OP_ENTER_COMMAND:
875 CurrentMenu = menu->menu;
876 mutt_enter_command ();
877 if (option (OPTFORCEREDRAWINDEX)) {
878 menu->redraw = REDRAW_FULL;
879 unset_option (OPTFORCEREDRAWINDEX);
880 unset_option (OPTFORCEREDRAWPAGER);
885 if (menu->tag && !menu->dialog) {
886 if (menu->tagprefix && !option (OPTAUTOTAG)) {
887 for (i = 0; i < menu->max; i++)
888 menu->tagged += menu->tag (menu, i, 0);
889 menu->redraw = REDRAW_INDEX;
891 else if (menu->max) {
892 int t = menu->tag (menu, menu->current, -1);
895 if (t && option (OPTRESOLVE) && menu->current < menu->max - 1) {
897 menu->redraw = REDRAW_MOTION_RESYNCH;
900 menu->redraw = REDRAW_CURRENT;
903 mutt_error _("No entries.");
906 mutt_error _("Tagging is not supported.");
909 case OP_SHELL_ESCAPE:
910 mutt_shell_escape ();
911 MAYBE_REDRAW (menu->redraw);
919 clearok (stdscr, TRUE);
920 menu->redraw = REDRAW_FULL;
924 mutt_help (menu->menu);
925 menu->redraw = REDRAW_FULL;
929 km_error_key (menu->menu);