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)) > 0) {
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 shift = option (OPTARROWCURSOR) ? 3 : 0;
143 char *tmpbuf = p_new(char, n);
145 cols = COLS - SW - shift;
146 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, m_strlen(s), 1);
148 snprintf (s, n, "%s", tmpbuf); /* overkill */
152 void menu_redraw_full (MUTTMENU * menu)
154 SETCOLOR (MT_COLOR_NORMAL);
155 /* clear() doesn't optimize screen redraws */
159 if (option (OPTHELP)) {
160 SETCOLOR (MT_COLOR_STATUS);
161 move (option (OPTSTATUSONTOP) ? LINES - 2 : 0, SW);
162 mutt_paddstr (COLS-SW, menu->help);
163 SETCOLOR (MT_COLOR_NORMAL);
165 menu->pagelen = LINES - 3;
168 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
169 menu->pagelen = LINES - 2;
172 sidebar_draw_frames();
176 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
179 void menu_redraw_status (MUTTMENU * menu)
183 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
184 SETCOLOR (MT_COLOR_STATUS);
185 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, SW);
186 mutt_paddstr (COLS-SW, buf);
187 SETCOLOR (MT_COLOR_NORMAL);
188 menu->redraw &= ~REDRAW_STATUS;
189 sidebar_draw_frames();
192 void menu_redraw_index (MUTTMENU * menu)
197 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
199 menu_make_entry (buf, sizeof (buf), menu, i);
200 menu_pad_string (buf, sizeof (buf));
202 if (option (OPTARROWCURSOR)) {
203 attrset (menu->color (i));
204 CLEARLINE_WIN (i - menu->top + menu->offset);
206 if (i == menu->current) {
207 attrset (menu->color (i));
208 ADDCOLOR (MT_COLOR_INDICATOR);
209 BKGDSET (MT_COLOR_INDICATOR);
211 attrset (menu->color (i));
215 attrset (menu->color (i));
216 move (i - menu->top + menu->offset, SW);
220 print_enriched_string (menu->color (i), (unsigned char *) buf, 1);
221 SETCOLOR (MT_COLOR_NORMAL);
222 BKGDSET (MT_COLOR_NORMAL);
225 attrset (menu->color (i));
227 if (i == menu->current) {
228 ADDCOLOR (MT_COLOR_INDICATOR);
229 BKGDSET (MT_COLOR_INDICATOR);
232 CLEARLINE_WIN (i - menu->top + menu->offset);
234 move (i - menu->top + menu->offset, SW);
235 print_enriched_string (menu->color (i), (unsigned char *) buf,
237 SETCOLOR (MT_COLOR_NORMAL);
238 BKGDSET (MT_COLOR_NORMAL);
242 CLEARLINE_WIN (i - menu->top + menu->offset);
249 void menu_redraw_motion (MUTTMENU * menu)
254 menu->redraw &= ~REDRAW_MOTION;
258 move (menu->oldcurrent + menu->offset - menu->top, SW);
259 SETCOLOR (MT_COLOR_NORMAL);
260 BKGDSET (MT_COLOR_NORMAL);
262 if (option (OPTARROWCURSOR)) {
263 /* clear the pointer */
264 attrset (menu->color (menu->oldcurrent));
267 if (menu->redraw & REDRAW_MOTION_RESYNCH) {
269 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
270 menu_pad_string (buf, sizeof (buf));
271 move (menu->oldcurrent + menu->offset - menu->top, SW + 3);
272 print_enriched_string (menu->color (menu->oldcurrent),
273 (unsigned char *) buf, 1);
274 SETCOLOR (MT_COLOR_NORMAL);
277 /* now draw it in the new location */
278 move (menu->current + menu->offset - menu->top, SW);
279 attrset (menu->color (menu->current));
280 ADDCOLOR (MT_COLOR_INDICATOR);
282 SETCOLOR (MT_COLOR_NORMAL);
285 /* erase the current indicator */
286 attrset (menu->color (menu->oldcurrent));
288 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
289 menu_pad_string (buf, sizeof (buf));
290 print_enriched_string (menu->color (menu->oldcurrent),
291 (unsigned char *) buf, 1);
293 /* now draw the new one to reflect the change */
294 menu_make_entry (buf, sizeof (buf), menu, menu->current);
295 menu_pad_string (buf, sizeof (buf));
296 attrset (menu->color (menu->current));
297 ADDCOLOR (MT_COLOR_INDICATOR);
298 BKGDSET (MT_COLOR_INDICATOR);
299 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
300 move (menu->current + menu->offset - menu->top, SW);
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, SW);
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 static 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 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0) || (c < MenuContext)) {
374 if (menu->current < menu->top + c)
375 menu->top = menu->current - c;
376 else if (menu->current >= menu->top + menu->pagelen - c)
377 menu->top = menu->current - menu->pagelen + c + 1;
379 if (menu->current < menu->top + c)
380 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
381 else if ((menu->current >= menu->top + menu->pagelen - c))
382 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
386 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
387 menu->top = MIN (menu->top, menu->max - menu->pagelen);
388 menu->top = MAX (menu->top, 0);
390 if (menu->top != old_top)
391 menu->redraw |= REDRAW_INDEX;
394 void menu_jump (MUTTMENU * menu)
400 mutt_ungetch (LastKey, 0);
402 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
404 if (n >= 0 && n < menu->max) {
406 menu->redraw = REDRAW_MOTION;
409 mutt_error _("Invalid index number.");
413 mutt_error _("No entries.");
416 void menu_next_line (MUTTMENU * menu)
419 int c = MIN (MenuContext, menu->pagelen / 2);
421 if (menu->top + 1 < menu->max - c && (option (OPTMENUMOVEOFF)
422 || (menu->max > menu->pagelen
424 menu->max - menu->pagelen))) {
426 if (menu->current < menu->top + c && menu->current < menu->max - 1)
428 menu->redraw = REDRAW_INDEX;
431 mutt_error _("You cannot scroll down farther.");
434 mutt_error _("No entries.");
437 void menu_prev_line (MUTTMENU * menu)
440 int c = MIN (MenuContext, menu->pagelen / 2);
443 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
445 menu->redraw = REDRAW_INDEX;
448 mutt_error _("You cannot scroll up farther.");
452 * pageup: jumplen == -pagelen
453 * pagedown: jumplen == pagelen
454 * halfup: jumplen == -pagelen/2
455 * halfdown: jumplen == pagelen/2
457 #define DIRECTION ((neg * 2) + 1)
458 static void menu_length_jump (MUTTMENU *menu, int jumplen) {
459 int tmp, neg = (jumplen >= 0) ? 0 : -1;
460 int c = MIN (MenuContext, menu->pagelen / 2);
463 /* possible to scroll? */
464 if (DIRECTION * menu->top <
465 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
466 menu->top += jumplen;
468 /* jumped too long? */
469 if ((neg || !option (OPTMENUMOVEOFF)) && DIRECTION * menu->top > tmp)
472 /* need to move the cursor? */
474 (tmp = (menu->current - (menu->top +
475 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
476 menu->current -= tmp;
478 menu->redraw = REDRAW_INDEX;
480 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
481 menu->current += jumplen;
482 menu->redraw = REDRAW_MOTION;
485 mutt_error (neg ? _("You are on the first page.")
486 : _("You are on the last page."));
488 menu->current = MIN (menu->current, menu->max - 1);
489 menu->current = MAX (menu->current, 0);
492 mutt_error _("No entries.");
496 void menu_next_page (MUTTMENU *menu) {
497 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
500 void menu_prev_page (MUTTMENU *menu) {
501 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
504 void menu_half_down (MUTTMENU *menu) {
505 menu_length_jump (menu, menu->pagelen / 2);
508 void menu_half_up (MUTTMENU *menu) {
509 menu_length_jump (menu, 0 - menu->pagelen / 2);
512 void menu_top_page (MUTTMENU *menu) {
513 if (menu->current != menu->top) {
514 menu->current = menu->top;
515 menu->redraw = REDRAW_MOTION;
519 void menu_bottom_page (MUTTMENU *menu) {
521 menu->current = menu->top + menu->pagelen - 1;
522 if (menu->current > menu->max - 1)
523 menu->current = menu->max - 1;
524 menu->redraw = REDRAW_MOTION;
527 mutt_error _("No entries.");
530 void menu_middle_page (MUTTMENU *menu) {
534 i = menu->top + menu->pagelen;
535 if (i > menu->max - 1)
537 menu->current = menu->top + (i - menu->top) / 2;
538 menu->redraw = REDRAW_MOTION;
541 mutt_error _("No entries.");
544 void menu_first_entry (MUTTMENU *menu) {
547 menu->redraw = REDRAW_MOTION;
550 mutt_error _("No entries.");
553 void menu_last_entry (MUTTMENU *menu) {
555 menu->current = menu->max - 1;
556 menu->redraw = REDRAW_MOTION;
559 mutt_error _("No entries.");
562 void menu_current_top (MUTTMENU * menu)
565 menu->top = menu->current;
566 menu->redraw = REDRAW_INDEX;
569 mutt_error _("No entries.");
572 void menu_current_middle (MUTTMENU * menu)
575 menu->top = menu->current - menu->pagelen / 2;
578 menu->redraw = REDRAW_INDEX;
581 mutt_error _("No entries.");
584 void menu_current_bottom (MUTTMENU * menu)
587 menu->top = menu->current - menu->pagelen + 1;
590 menu->redraw = REDRAW_INDEX;
593 mutt_error _("No entries.");
596 static void menu_next_entry (MUTTMENU * menu)
598 if (menu->current < menu->max - 1) {
600 menu->redraw = REDRAW_MOTION;
603 mutt_error _("You are on the last entry.");
606 static void menu_prev_entry (MUTTMENU * menu)
610 menu->redraw = REDRAW_MOTION;
613 mutt_error _("You are on the first entry.");
616 static int default_color (int i __attribute__ ((unused)))
618 return ColorDefs[MT_COLOR_NORMAL];
621 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
623 char buf[LONG_STRING];
625 menu_make_entry (buf, sizeof (buf), m, n);
626 return (regexec (re, buf, 0, NULL, 0));
629 MUTTMENU *mutt_new_menu (void)
631 MUTTMENU *p = p_new(MUTTMENU, 1);
636 p->redraw = REDRAW_FULL;
637 p->pagelen = PAGELEN;
638 p->color = default_color;
639 p->search = menu_search_generic;
643 void mutt_menuDestroy (MUTTMENU ** p)
647 p_delete(&(*p)->searchBuf);
650 for (i = 0; i < (*p)->max; i++)
651 p_delete(&(*p)->dialog[i]);
653 p_delete(&(*p)->dialog);
659 #define M_SEARCH_UP 1
660 #define M_SEARCH_DOWN 2
662 static int menu_search (MUTTMENU * menu, int op)
669 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
670 m_strcpy(buf, sizeof(buf), NONULL(menu->searchBuf));
671 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
672 _("Reverse search for: "),
673 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
675 m_strreplace(&menu->searchBuf, buf);
676 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
679 if (!menu->searchBuf) {
680 mutt_error _("No search pattern.");
686 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
687 if (op == OP_SEARCH_OPPOSITE)
688 searchDir = -searchDir;
691 REGCOMP (&re, menu->searchBuf,
692 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
693 regerror (r, &re, buf, sizeof (buf));
695 mutt_error ("%s", buf);
699 r = menu->current + searchDir;
700 while (r >= 0 && r < menu->max) {
701 if (menu->search (menu, &re, r) == 0) {
710 mutt_error _("Not found.");
715 static int menu_dialog_translate_op (int i)
725 case OP_CURRENT_BOTTOM:
727 return OP_BOTTOM_PAGE;
728 case OP_CURRENT_MIDDLE:
729 return OP_MIDDLE_PAGE;
735 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
747 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
748 *ip = OP_MAX + (p - menu->keys + 1);
752 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
757 int menu_redraw (MUTTMENU * menu)
759 /* See if all or part of the screen needs to be updated. */
760 if (menu->redraw & REDRAW_FULL) {
761 menu_redraw_full (menu);
762 /* allow the caller to do any local configuration */
767 menu_check_recenter (menu);
769 if (menu->redraw & REDRAW_STATUS)
770 menu_redraw_status (menu);
771 if (menu->redraw & REDRAW_INDEX)
772 menu_redraw_index (menu);
773 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
774 menu_redraw_motion (menu);
775 else if (menu->redraw == REDRAW_CURRENT)
776 menu_redraw_current (menu);
779 menu_redraw_prompt (menu);
784 int mutt_menuLoop (MUTTMENU * menu)
789 if (option (OPTMENUCALLER)) {
790 unset_option (OPTMENUCALLER);
798 if (menu_redraw (menu) == OP_REDRAW)
801 menu->oldcurrent = menu->current;
803 if (option (OPTARROWCURSOR))
804 move (menu->current - menu->top + menu->offset, SW + 2);
805 else if (option (OPTBRAILLEFRIENDLY))
806 move (menu->current - menu->top + menu->offset, SW);
808 move (menu->current - menu->top + menu->offset, COLS - 1);
813 /* try to catch dialog keys before ops */
814 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
817 i = km_dokey (menu->menu);
818 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
820 mvaddstr (LINES - 1, 0, "Tag-");
822 i = km_dokey (menu->menu);
824 CLEARLINE (LINES - 1);
826 else if (i == OP_TAG_PREFIX) {
827 mutt_error _("No tagged entries.");
831 else { /* None tagged, OP_TAG_PREFIX_COND */
835 while (UngetCount > 0) {
837 if (tmp.op == OP_END_COND)
840 mutt_message _("Nothing to do.");
845 else if (menu->tagged && option (OPTAUTOTAG))
852 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
854 mutt_resize_screen ();
855 menu->redraw = REDRAW_FULL;
857 clearok (stdscr, TRUE); /*force complete redraw */
867 /* Convert menubar movement to scrolling */
869 i = menu_dialog_translate_op (i);
873 menu_next_entry (menu);
876 menu_prev_entry (menu);
879 menu_half_down (menu);
885 menu_next_page (menu);
888 menu_prev_page (menu);
891 menu_next_line (menu);
894 menu_prev_line (menu);
897 menu_first_entry (menu);
900 menu_last_entry (menu);
903 menu_top_page (menu);
906 menu_middle_page (menu);
909 menu_bottom_page (menu);
912 menu_current_top (menu);
914 case OP_CURRENT_MIDDLE:
915 menu_current_middle (menu);
917 case OP_CURRENT_BOTTOM:
918 menu_current_bottom (menu);
921 case OP_SEARCH_REVERSE:
923 case OP_SEARCH_OPPOSITE:
924 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
925 menu->oldcurrent = menu->current;
926 if ((menu->current = menu_search (menu, i)) != -1)
927 menu->redraw = REDRAW_MOTION;
929 menu->current = menu->oldcurrent;
932 mutt_error _("Search is not implemented for this menu.");
937 mutt_error (_("Jumping is not implemented for dialogs."));
943 case OP_ENTER_COMMAND:
944 CurrentMenu = menu->menu;
945 mutt_enter_command ();
946 if (option (OPTFORCEREDRAWINDEX)) {
947 menu->redraw = REDRAW_FULL;
948 unset_option (OPTFORCEREDRAWINDEX);
949 unset_option (OPTFORCEREDRAWPAGER);
954 if (menu->tag && !menu->dialog) {
955 if (menu->tagprefix && !option (OPTAUTOTAG)) {
956 for (i = 0; i < menu->max; i++)
957 menu->tagged += menu->tag (menu, i, 0);
958 menu->redraw = REDRAW_INDEX;
960 else if (menu->max) {
961 int t = menu->tag (menu, menu->current, -1);
964 if (t && option (OPTRESOLVE) && menu->current < menu->max - 1) {
966 menu->redraw = REDRAW_MOTION_RESYNCH;
969 menu->redraw = REDRAW_CURRENT;
972 mutt_error _("No entries.");
975 mutt_error _("Tagging is not supported.");
978 case OP_SHELL_ESCAPE:
979 mutt_shell_escape ();
980 MAYBE_REDRAW (menu->redraw);
987 case OP_REBUILD_CACHE:
992 clearok (stdscr, TRUE);
993 menu->redraw = REDRAW_FULL;
997 mutt_help (menu->menu);
998 menu->redraw = REDRAW_FULL;
1002 km_error_key (menu->menu);