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-ui/lib-ui.h>
18 #include <imap/imap.h>
19 #include <lib-ui/sidebar.h>
21 extern size_t UngetCount;
23 static void print_enriched_string (int attr, unsigned char *s, int do_color)
27 size_t n = m_strlen((char *) s);
32 if (*s < M_TREE_MAX) {
34 SETCOLOR(main_w, MT_COLOR_TREE);
35 while (*s && *s < M_TREE_MAX) {
38 waddch (main_w, ACS_LLCORNER);
41 waddch (main_w, ACS_ULCORNER);
44 waddch (main_w, ACS_LTEE);
47 waddch (main_w, ACS_HLINE);
50 waddch (main_w, ACS_VLINE);
53 waddch (main_w, ACS_TTEE);
56 waddch (main_w, ACS_BTEE);
65 waddch (main_w, '*'); /* fake thread indicator */
80 wattrset (main_w, attr);
82 else if ((k = mbrtowc (&wc, (char *) s, n, &mbstate)) != (size_t)-1) {
83 waddnstr (main_w, (char *) s, k);
91 static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i)
94 m_strcpy(s, l, menu->dialog[i]);
95 menu->current = -1; /* hide menubar */
98 menu->make_entry (s, l, menu, i);
101 static void menu_pad_string (char *s, size_t n)
103 int cols = getmaxx(main_w);
104 char *tmpbuf = p_new(char, n);
106 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, m_strlen(s), 1);
108 snprintf (s, n, "%s", tmpbuf); /* overkill */
112 void menu_redraw_full (MUTTMENU * menu)
114 SETCOLOR(main_w, MT_COLOR_NORMAL);
116 SETCOLOR(main_w, MT_COLOR_SIDEBAR);
117 mvwhline(main_w, LINES - 2, 0, ACS_HLINE, getmaxx(main_w));
118 SETCOLOR(main_w, MT_COLOR_NORMAL);
120 menu->pagelen = LINES - 3;
123 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
126 void menu_redraw_status (MUTTMENU * menu)
130 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
131 SETCOLOR(main_w, MT_COLOR_STATUS);
132 wmove (main_w, 0, 0);
133 mutt_paddstr (main_w, getmaxx(main_w), buf);
134 SETCOLOR(main_w, MT_COLOR_NORMAL);
135 menu->redraw &= ~REDRAW_STATUS;
138 void menu_redraw_index (MUTTMENU * menu)
143 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
145 menu_make_entry (buf, sizeof (buf), menu, i);
146 menu_pad_string (buf, sizeof (buf));
148 wattrset (main_w, menu->color (i));
150 if (i == menu->current) {
151 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
152 BKGDSET(main_w, MT_COLOR_INDICATOR);
155 CLEARLINE(main_w, i - menu->top + menu->offset);
157 wmove (main_w, i - menu->top + menu->offset, 0);
158 print_enriched_string (menu->color (i), (unsigned char *) buf,
160 SETCOLOR(main_w, MT_COLOR_NORMAL);
161 BKGDSET(main_w, MT_COLOR_NORMAL);
163 CLEARLINE(main_w, i - menu->top + menu->offset);
171 void menu_redraw_motion (MUTTMENU * menu)
176 menu->redraw &= ~REDRAW_MOTION;
180 SETCOLOR(main_w, MT_COLOR_NORMAL);
181 BKGDSET(main_w, MT_COLOR_NORMAL);
183 /* erase the current indicator */
184 wattrset(main_w, menu->color(menu->oldcurrent));
185 wmove (main_w, menu->oldcurrent + menu->offset - menu->top, 0);
187 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
188 menu_pad_string (buf, sizeof (buf));
189 print_enriched_string (menu->color (menu->oldcurrent),
190 (unsigned char *) buf, 1);
192 /* now draw the new one to reflect the change */
193 menu_make_entry (buf, sizeof (buf), menu, menu->current);
194 menu_pad_string (buf, sizeof (buf));
195 wattrset (main_w, menu->color (menu->current));
196 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
197 BKGDSET(main_w, MT_COLOR_INDICATOR);
198 CLEARLINE(main_w, menu->current - menu->top + menu->offset);
199 wmove (main_w, menu->current + menu->offset - menu->top, 0);
200 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
202 SETCOLOR(main_w, MT_COLOR_NORMAL);
203 BKGDSET(main_w, MT_COLOR_NORMAL);
205 menu->redraw &= REDRAW_STATUS;
208 void menu_redraw_current (MUTTMENU * menu)
212 menu_make_entry (buf, sizeof (buf), menu, menu->current);
213 menu_pad_string (buf, sizeof (buf));
215 wattrset (main_w, menu->color (menu->current));
216 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
217 BKGDSET(main_w, MT_COLOR_INDICATOR);
218 wmove (main_w, menu->current + menu->offset - menu->top, 0);
220 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
222 SETCOLOR(main_w, MT_COLOR_NORMAL);
223 BKGDSET(main_w, MT_COLOR_NORMAL);
225 menu->redraw &= REDRAW_STATUS;
228 static void menu_redraw_prompt (MUTTMENU * menu)
231 if (option (OPTMSGERR)) {
233 unset_option (OPTMSGERR);
239 SETCOLOR(stdscr, MT_COLOR_NORMAL);
240 mvwaddstr(stdscr, LINES - 1, 0, menu->prompt);
245 void menu_check_recenter (MUTTMENU * menu)
247 int c = MIN (MenuContext, menu->pagelen / 2);
248 int old_top = menu->top;
250 if ((menu->pagelen <= 0) || (c < MenuContext)) {
251 if (menu->current < menu->top + c)
252 menu->top = menu->current - c;
253 else if (menu->current >= menu->top + menu->pagelen - c)
254 menu->top = menu->current - menu->pagelen + c + 1;
256 if (menu->current < menu->top + c)
257 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
258 else if ((menu->current >= menu->top + menu->pagelen - c))
259 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
262 menu->top = MAX (menu->top, 0);
264 if (menu->top != old_top)
265 menu->redraw |= REDRAW_INDEX;
268 void menu_jump (MUTTMENU * menu)
274 mutt_ungetch (LastKey, 0);
276 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
278 if (n >= 0 && n < menu->max) {
280 menu->redraw = REDRAW_MOTION;
283 mutt_error _("Invalid index number.");
287 mutt_error _("No entries.");
290 void menu_next_line (MUTTMENU * menu)
293 int c = MIN (MenuContext, menu->pagelen / 2);
295 if (menu->top + 1 < menu->max - c) {
297 if (menu->current < menu->top + c && menu->current < menu->max - 1)
299 menu->redraw = REDRAW_INDEX;
301 mutt_error _("You cannot scroll down farther.");
304 mutt_error _("No entries.");
308 void menu_prev_line (MUTTMENU * menu)
311 int c = MIN (MenuContext, menu->pagelen / 2);
314 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
316 menu->redraw = REDRAW_INDEX;
319 mutt_error _("You cannot scroll up farther.");
323 * pageup: jumplen == -pagelen
324 * pagedown: jumplen == pagelen
325 * halfup: jumplen == -pagelen/2
326 * halfdown: jumplen == pagelen/2
328 #define DIRECTION ((neg * 2) + 1)
329 static void menu_length_jump (MUTTMENU *menu, int jumplen) {
330 int tmp, neg = (jumplen >= 0) ? 0 : -1;
331 int c = MIN (MenuContext, menu->pagelen / 2);
334 /* possible to scroll? */
335 if (DIRECTION * menu->top <
336 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
337 menu->top += jumplen;
339 /* jumped too long? */
340 if (neg && DIRECTION * menu->top > tmp)
343 /* need to move the cursor? */
345 (tmp = (menu->current - (menu->top +
346 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
347 menu->current -= tmp;
349 menu->redraw = REDRAW_INDEX;
351 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
352 menu->current += jumplen;
353 menu->redraw = REDRAW_MOTION;
356 mutt_error (neg ? _("You are on the first page.")
357 : _("You are on the last page."));
359 menu->current = MIN (menu->current, menu->max - 1);
360 menu->current = MAX (menu->current, 0);
363 mutt_error _("No entries.");
367 void menu_next_page (MUTTMENU *menu) {
368 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
371 void menu_prev_page (MUTTMENU *menu) {
372 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
375 void menu_half_down (MUTTMENU *menu) {
376 menu_length_jump (menu, menu->pagelen / 2);
379 void menu_half_up (MUTTMENU *menu) {
380 menu_length_jump (menu, 0 - menu->pagelen / 2);
383 void menu_top_page (MUTTMENU *menu) {
384 if (menu->current != menu->top) {
385 menu->current = menu->top;
386 menu->redraw = REDRAW_MOTION;
390 void menu_bottom_page (MUTTMENU *menu) {
392 menu->current = menu->top + menu->pagelen - 1;
393 if (menu->current > menu->max - 1)
394 menu->current = menu->max - 1;
395 menu->redraw = REDRAW_MOTION;
398 mutt_error _("No entries.");
401 void menu_middle_page (MUTTMENU *menu) {
405 i = menu->top + menu->pagelen;
406 if (i > menu->max - 1)
408 menu->current = menu->top + (i - menu->top) / 2;
409 menu->redraw = REDRAW_MOTION;
412 mutt_error _("No entries.");
415 void menu_first_entry (MUTTMENU *menu) {
418 menu->redraw = REDRAW_MOTION;
421 mutt_error _("No entries.");
424 void menu_last_entry (MUTTMENU *menu) {
426 menu->current = menu->max - 1;
427 menu->redraw = REDRAW_MOTION;
430 mutt_error _("No entries.");
433 void menu_current_top (MUTTMENU * menu)
436 menu->top = menu->current;
437 menu->redraw = REDRAW_INDEX;
440 mutt_error _("No entries.");
443 void menu_current_middle (MUTTMENU * menu)
446 menu->top = menu->current - menu->pagelen / 2;
449 menu->redraw = REDRAW_INDEX;
452 mutt_error _("No entries.");
455 void menu_current_bottom (MUTTMENU * menu)
458 menu->top = menu->current - menu->pagelen + 1;
461 menu->redraw = REDRAW_INDEX;
464 mutt_error _("No entries.");
467 static void menu_next_entry (MUTTMENU * menu)
469 if (menu->current < menu->max - 1) {
471 menu->redraw = REDRAW_MOTION;
474 mutt_error _("You are on the last entry.");
477 static void menu_prev_entry (MUTTMENU * menu)
481 menu->redraw = REDRAW_MOTION;
484 mutt_error _("You are on the first entry.");
487 static int default_color (int i __attribute__ ((unused)))
489 return ColorDefs[MT_COLOR_NORMAL];
492 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
494 char buf[LONG_STRING];
496 menu_make_entry (buf, sizeof (buf), m, n);
497 return (regexec (re, buf, 0, NULL, 0));
500 MUTTMENU *mutt_new_menu (void)
502 MUTTMENU *p = p_new(MUTTMENU, 1);
507 p->redraw = REDRAW_FULL;
508 p->pagelen = LINES - 3;
509 p->color = default_color;
510 p->search = menu_search_generic;
514 void mutt_menuDestroy (MUTTMENU ** p)
518 p_delete(&(*p)->searchBuf);
521 for (i = 0; i < (*p)->max; i++)
522 p_delete(&(*p)->dialog[i]);
524 p_delete(&(*p)->dialog);
530 #define M_SEARCH_UP 1
531 #define M_SEARCH_DOWN 2
533 static int menu_search (MUTTMENU * menu, int op)
540 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
541 m_strcpy(buf, sizeof(buf), NONULL(menu->searchBuf));
542 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
543 _("Reverse search for: "),
544 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
546 m_strreplace(&menu->searchBuf, buf);
547 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
550 if (!menu->searchBuf) {
551 mutt_error _("No search pattern.");
557 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
558 if (op == OP_SEARCH_OPPOSITE)
559 searchDir = -searchDir;
562 REGCOMP (&re, menu->searchBuf,
563 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
564 regerror (r, &re, buf, sizeof (buf));
566 mutt_error ("%s", buf);
570 r = menu->current + searchDir;
571 while (r >= 0 && r < menu->max) {
572 if (menu->search (menu, &re, r) == 0) {
581 mutt_error _("Not found.");
586 static int menu_dialog_translate_op (int i)
596 case OP_CURRENT_BOTTOM:
598 return OP_BOTTOM_PAGE;
599 case OP_CURRENT_MIDDLE:
600 return OP_MIDDLE_PAGE;
606 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
618 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
619 *ip = OP_MAX + (p - menu->keys + 1);
623 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
628 int menu_redraw (MUTTMENU * menu)
630 /* See if all or part of the screen needs to be updated. */
631 if (menu->redraw & REDRAW_FULL) {
632 menu_redraw_full (menu);
633 /* allow the caller to do any local configuration */
638 menu_check_recenter (menu);
640 if (menu->redraw & REDRAW_STATUS)
641 menu_redraw_status (menu);
642 if (menu->redraw & REDRAW_INDEX)
643 menu_redraw_index (menu);
644 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
645 menu_redraw_motion (menu);
646 else if (menu->redraw == REDRAW_CURRENT)
647 menu_redraw_current (menu);
650 menu_redraw_prompt (menu);
655 int mutt_menuLoop (MUTTMENU * menu)
660 if (option (OPTMENUCALLER)) {
661 unset_option (OPTMENUCALLER);
669 if (menu_redraw (menu) == OP_REDRAW)
672 menu->oldcurrent = menu->current;
675 /* try to catch dialog keys before ops */
676 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
679 i = km_dokey (menu->menu);
680 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
682 mvwaddstr(stdscr, LINES - 1, 0, "Tag-");
684 i = km_dokey (menu->menu);
686 CLEARLINE(stdscr, LINES - 1);
688 else if (i == OP_TAG_PREFIX) {
689 mutt_error _("No tagged entries.");
693 else { /* None tagged, OP_TAG_PREFIX_COND */
697 while (UngetCount > 0) {
699 if (tmp.op == OP_END_COND)
702 mutt_message _("Nothing to do.");
707 else if (menu->tagged && option (OPTAUTOTAG))
716 menu->redraw = REDRAW_FULL;
725 /* Convert menubar movement to scrolling */
727 i = menu_dialog_translate_op (i);
731 menu_next_entry (menu);
734 menu_prev_entry (menu);
737 menu_half_down (menu);
743 menu_next_page (menu);
746 menu_prev_page (menu);
749 menu_next_line (menu);
752 menu_prev_line (menu);
755 menu_first_entry (menu);
758 menu_last_entry (menu);
761 menu_top_page (menu);
764 menu_middle_page (menu);
767 menu_bottom_page (menu);
770 menu_current_top (menu);
772 case OP_CURRENT_MIDDLE:
773 menu_current_middle (menu);
775 case OP_CURRENT_BOTTOM:
776 menu_current_bottom (menu);
779 case OP_SEARCH_REVERSE:
781 case OP_SEARCH_OPPOSITE:
782 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
783 menu->oldcurrent = menu->current;
784 if ((menu->current = menu_search (menu, i)) != -1)
785 menu->redraw = REDRAW_MOTION;
787 menu->current = menu->oldcurrent;
790 mutt_error _("Search is not implemented for this menu.");
795 mutt_error (_("Jumping is not implemented for dialogs."));
801 case OP_ENTER_COMMAND:
802 CurrentMenu = menu->menu;
803 mutt_enter_command ();
804 if (option (OPTFORCEREDRAWINDEX)) {
805 menu->redraw = REDRAW_FULL;
806 unset_option (OPTFORCEREDRAWINDEX);
807 unset_option (OPTFORCEREDRAWPAGER);
812 if (menu->tag && !menu->dialog) {
813 if (menu->tagprefix && !option (OPTAUTOTAG)) {
814 for (i = 0; i < menu->max; i++)
815 menu->tagged += menu->tag (menu, i, 0);
816 menu->redraw = REDRAW_INDEX;
818 else if (menu->max) {
819 int t = menu->tag (menu, menu->current, -1);
822 if (t && option (OPTRESOLVE) && menu->current < menu->max - 1) {
824 menu->redraw = REDRAW_MOTION_RESYNCH;
827 menu->redraw = REDRAW_CURRENT;
830 mutt_error _("No entries.");
833 mutt_error _("Tagging is not supported.");
836 case OP_SHELL_ESCAPE:
837 mutt_shell_escape ();
838 MAYBE_REDRAW (menu->redraw);
846 clearok (main_w, TRUE);
847 menu->redraw = REDRAW_FULL;
851 mutt_help (menu->menu);
852 menu->redraw = REDRAW_FULL;
856 km_error_key (menu->menu);