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);
115 /* wclear() doesn't optimize screen redraws */
116 wmove (main_w, 0, 0);
119 SETCOLOR(main_w, MT_COLOR_SIDEBAR);
120 mvwhline(main_w, LINES - 2, 0, ACS_HLINE, getmaxx(main_w));
121 SETCOLOR(main_w, MT_COLOR_NORMAL);
123 menu->pagelen = LINES - 3;
126 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
129 void menu_redraw_status (MUTTMENU * menu)
133 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
134 SETCOLOR(main_w, MT_COLOR_STATUS);
135 wmove (main_w, 0, 0);
136 mutt_paddstr (main_w, getmaxx(main_w), buf);
137 SETCOLOR(main_w, MT_COLOR_NORMAL);
138 menu->redraw &= ~REDRAW_STATUS;
141 void menu_redraw_index (MUTTMENU * menu)
146 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
148 menu_make_entry (buf, sizeof (buf), menu, i);
149 menu_pad_string (buf, sizeof (buf));
151 wattrset (main_w, menu->color (i));
153 if (i == menu->current) {
154 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
155 BKGDSET(main_w, MT_COLOR_INDICATOR);
158 CLEARLINE(main_w, i - menu->top + menu->offset);
160 wmove (main_w, i - menu->top + menu->offset, 0);
161 print_enriched_string (menu->color (i), (unsigned char *) buf,
163 SETCOLOR(main_w, MT_COLOR_NORMAL);
164 BKGDSET(main_w, MT_COLOR_NORMAL);
166 CLEARLINE(main_w, i - menu->top + menu->offset);
174 void menu_redraw_motion (MUTTMENU * menu)
179 menu->redraw &= ~REDRAW_MOTION;
183 SETCOLOR(main_w, MT_COLOR_NORMAL);
184 BKGDSET(main_w, MT_COLOR_NORMAL);
186 /* erase the current indicator */
187 wattrset(main_w, menu->color(menu->oldcurrent));
188 wmove (main_w, menu->oldcurrent + menu->offset - menu->top, 0);
190 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
191 menu_pad_string (buf, sizeof (buf));
192 print_enriched_string (menu->color (menu->oldcurrent),
193 (unsigned char *) buf, 1);
195 /* now draw the new one to reflect the change */
196 menu_make_entry (buf, sizeof (buf), menu, menu->current);
197 menu_pad_string (buf, sizeof (buf));
198 wattrset (main_w, menu->color (menu->current));
199 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
200 BKGDSET(main_w, MT_COLOR_INDICATOR);
201 CLEARLINE(main_w, menu->current - menu->top + menu->offset);
202 wmove (main_w, menu->current + menu->offset - menu->top, 0);
203 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
205 SETCOLOR(main_w, MT_COLOR_NORMAL);
206 BKGDSET(main_w, MT_COLOR_NORMAL);
208 menu->redraw &= REDRAW_STATUS;
211 void menu_redraw_current (MUTTMENU * menu)
215 menu_make_entry (buf, sizeof (buf), menu, menu->current);
216 menu_pad_string (buf, sizeof (buf));
218 wattrset (main_w, menu->color (menu->current));
219 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
220 BKGDSET(main_w, MT_COLOR_INDICATOR);
221 wmove (main_w, menu->current + menu->offset - menu->top, 0);
223 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
225 SETCOLOR(main_w, MT_COLOR_NORMAL);
226 BKGDSET(main_w, MT_COLOR_NORMAL);
228 menu->redraw &= REDRAW_STATUS;
231 static void menu_redraw_prompt (MUTTMENU * menu)
234 if (option (OPTMSGERR)) {
236 unset_option (OPTMSGERR);
242 SETCOLOR(stdscr, MT_COLOR_NORMAL);
243 mvwaddstr(stdscr, LINES - 1, 0, menu->prompt);
248 void menu_check_recenter (MUTTMENU * menu)
250 int c = MIN (MenuContext, menu->pagelen / 2);
251 int old_top = menu->top;
253 if ((menu->pagelen <= 0) || (c < MenuContext)) {
254 if (menu->current < menu->top + c)
255 menu->top = menu->current - c;
256 else if (menu->current >= menu->top + menu->pagelen - c)
257 menu->top = menu->current - menu->pagelen + c + 1;
259 if (menu->current < menu->top + c)
260 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
261 else if ((menu->current >= menu->top + menu->pagelen - c))
262 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
265 menu->top = MAX (menu->top, 0);
267 if (menu->top != old_top)
268 menu->redraw |= REDRAW_INDEX;
271 void menu_jump (MUTTMENU * menu)
277 mutt_ungetch (LastKey, 0);
279 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
281 if (n >= 0 && n < menu->max) {
283 menu->redraw = REDRAW_MOTION;
286 mutt_error _("Invalid index number.");
290 mutt_error _("No entries.");
293 void menu_next_line (MUTTMENU * menu)
296 int c = MIN (MenuContext, menu->pagelen / 2);
298 if (menu->top + 1 < menu->max - c) {
300 if (menu->current < menu->top + c && menu->current < menu->max - 1)
302 menu->redraw = REDRAW_INDEX;
304 mutt_error _("You cannot scroll down farther.");
307 mutt_error _("No entries.");
311 void menu_prev_line (MUTTMENU * menu)
314 int c = MIN (MenuContext, menu->pagelen / 2);
317 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
319 menu->redraw = REDRAW_INDEX;
322 mutt_error _("You cannot scroll up farther.");
326 * pageup: jumplen == -pagelen
327 * pagedown: jumplen == pagelen
328 * halfup: jumplen == -pagelen/2
329 * halfdown: jumplen == pagelen/2
331 #define DIRECTION ((neg * 2) + 1)
332 static void menu_length_jump (MUTTMENU *menu, int jumplen) {
333 int tmp, neg = (jumplen >= 0) ? 0 : -1;
334 int c = MIN (MenuContext, menu->pagelen / 2);
337 /* possible to scroll? */
338 if (DIRECTION * menu->top <
339 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
340 menu->top += jumplen;
342 /* jumped too long? */
343 if (neg && DIRECTION * menu->top > tmp)
346 /* need to move the cursor? */
348 (tmp = (menu->current - (menu->top +
349 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
350 menu->current -= tmp;
352 menu->redraw = REDRAW_INDEX;
354 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
355 menu->current += jumplen;
356 menu->redraw = REDRAW_MOTION;
359 mutt_error (neg ? _("You are on the first page.")
360 : _("You are on the last page."));
362 menu->current = MIN (menu->current, menu->max - 1);
363 menu->current = MAX (menu->current, 0);
366 mutt_error _("No entries.");
370 void menu_next_page (MUTTMENU *menu) {
371 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
374 void menu_prev_page (MUTTMENU *menu) {
375 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
378 void menu_half_down (MUTTMENU *menu) {
379 menu_length_jump (menu, menu->pagelen / 2);
382 void menu_half_up (MUTTMENU *menu) {
383 menu_length_jump (menu, 0 - menu->pagelen / 2);
386 void menu_top_page (MUTTMENU *menu) {
387 if (menu->current != menu->top) {
388 menu->current = menu->top;
389 menu->redraw = REDRAW_MOTION;
393 void menu_bottom_page (MUTTMENU *menu) {
395 menu->current = menu->top + menu->pagelen - 1;
396 if (menu->current > menu->max - 1)
397 menu->current = menu->max - 1;
398 menu->redraw = REDRAW_MOTION;
401 mutt_error _("No entries.");
404 void menu_middle_page (MUTTMENU *menu) {
408 i = menu->top + menu->pagelen;
409 if (i > menu->max - 1)
411 menu->current = menu->top + (i - menu->top) / 2;
412 menu->redraw = REDRAW_MOTION;
415 mutt_error _("No entries.");
418 void menu_first_entry (MUTTMENU *menu) {
421 menu->redraw = REDRAW_MOTION;
424 mutt_error _("No entries.");
427 void menu_last_entry (MUTTMENU *menu) {
429 menu->current = menu->max - 1;
430 menu->redraw = REDRAW_MOTION;
433 mutt_error _("No entries.");
436 void menu_current_top (MUTTMENU * menu)
439 menu->top = menu->current;
440 menu->redraw = REDRAW_INDEX;
443 mutt_error _("No entries.");
446 void menu_current_middle (MUTTMENU * menu)
449 menu->top = menu->current - menu->pagelen / 2;
452 menu->redraw = REDRAW_INDEX;
455 mutt_error _("No entries.");
458 void menu_current_bottom (MUTTMENU * menu)
461 menu->top = menu->current - menu->pagelen + 1;
464 menu->redraw = REDRAW_INDEX;
467 mutt_error _("No entries.");
470 static void menu_next_entry (MUTTMENU * menu)
472 if (menu->current < menu->max - 1) {
474 menu->redraw = REDRAW_MOTION;
477 mutt_error _("You are on the last entry.");
480 static void menu_prev_entry (MUTTMENU * menu)
484 menu->redraw = REDRAW_MOTION;
487 mutt_error _("You are on the first entry.");
490 static int default_color (int i __attribute__ ((unused)))
492 return ColorDefs[MT_COLOR_NORMAL];
495 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
497 char buf[LONG_STRING];
499 menu_make_entry (buf, sizeof (buf), m, n);
500 return (regexec (re, buf, 0, NULL, 0));
503 MUTTMENU *mutt_new_menu (void)
505 MUTTMENU *p = p_new(MUTTMENU, 1);
510 p->redraw = REDRAW_FULL;
511 p->pagelen = LINES - 3;
512 p->color = default_color;
513 p->search = menu_search_generic;
517 void mutt_menuDestroy (MUTTMENU ** p)
521 p_delete(&(*p)->searchBuf);
524 for (i = 0; i < (*p)->max; i++)
525 p_delete(&(*p)->dialog[i]);
527 p_delete(&(*p)->dialog);
533 #define M_SEARCH_UP 1
534 #define M_SEARCH_DOWN 2
536 static int menu_search (MUTTMENU * menu, int op)
543 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
544 m_strcpy(buf, sizeof(buf), NONULL(menu->searchBuf));
545 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
546 _("Reverse search for: "),
547 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
549 m_strreplace(&menu->searchBuf, buf);
550 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
553 if (!menu->searchBuf) {
554 mutt_error _("No search pattern.");
560 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
561 if (op == OP_SEARCH_OPPOSITE)
562 searchDir = -searchDir;
565 REGCOMP (&re, menu->searchBuf,
566 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
567 regerror (r, &re, buf, sizeof (buf));
569 mutt_error ("%s", buf);
573 r = menu->current + searchDir;
574 while (r >= 0 && r < menu->max) {
575 if (menu->search (menu, &re, r) == 0) {
584 mutt_error _("Not found.");
589 static int menu_dialog_translate_op (int i)
599 case OP_CURRENT_BOTTOM:
601 return OP_BOTTOM_PAGE;
602 case OP_CURRENT_MIDDLE:
603 return OP_MIDDLE_PAGE;
609 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
621 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
622 *ip = OP_MAX + (p - menu->keys + 1);
626 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
631 int menu_redraw (MUTTMENU * menu)
633 /* See if all or part of the screen needs to be updated. */
634 if (menu->redraw & REDRAW_FULL) {
635 menu_redraw_full (menu);
636 /* allow the caller to do any local configuration */
641 menu_check_recenter (menu);
643 if (menu->redraw & REDRAW_STATUS)
644 menu_redraw_status (menu);
645 if (menu->redraw & REDRAW_INDEX)
646 menu_redraw_index (menu);
647 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
648 menu_redraw_motion (menu);
649 else if (menu->redraw == REDRAW_CURRENT)
650 menu_redraw_current (menu);
653 menu_redraw_prompt (menu);
658 int mutt_menuLoop (MUTTMENU * menu)
663 if (option (OPTMENUCALLER)) {
664 unset_option (OPTMENUCALLER);
672 if (menu_redraw (menu) == OP_REDRAW)
675 menu->oldcurrent = menu->current;
678 /* try to catch dialog keys before ops */
679 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
682 i = km_dokey (menu->menu);
683 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
685 mvwaddstr(stdscr, LINES - 1, 0, "Tag-");
687 i = km_dokey (menu->menu);
689 CLEARLINE(stdscr, LINES - 1);
691 else if (i == OP_TAG_PREFIX) {
692 mutt_error _("No tagged entries.");
696 else { /* None tagged, OP_TAG_PREFIX_COND */
700 while (UngetCount > 0) {
702 if (tmp.op == OP_END_COND)
705 mutt_message _("Nothing to do.");
710 else if (menu->tagged && option (OPTAUTOTAG))
719 menu->redraw = REDRAW_FULL;
728 /* Convert menubar movement to scrolling */
730 i = menu_dialog_translate_op (i);
734 menu_next_entry (menu);
737 menu_prev_entry (menu);
740 menu_half_down (menu);
746 menu_next_page (menu);
749 menu_prev_page (menu);
752 menu_next_line (menu);
755 menu_prev_line (menu);
758 menu_first_entry (menu);
761 menu_last_entry (menu);
764 menu_top_page (menu);
767 menu_middle_page (menu);
770 menu_bottom_page (menu);
773 menu_current_top (menu);
775 case OP_CURRENT_MIDDLE:
776 menu_current_middle (menu);
778 case OP_CURRENT_BOTTOM:
779 menu_current_bottom (menu);
782 case OP_SEARCH_REVERSE:
784 case OP_SEARCH_OPPOSITE:
785 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
786 menu->oldcurrent = menu->current;
787 if ((menu->current = menu_search (menu, i)) != -1)
788 menu->redraw = REDRAW_MOTION;
790 menu->current = menu->oldcurrent;
793 mutt_error _("Search is not implemented for this menu.");
798 mutt_error (_("Jumping is not implemented for dialogs."));
804 case OP_ENTER_COMMAND:
805 CurrentMenu = menu->menu;
806 mutt_enter_command ();
807 if (option (OPTFORCEREDRAWINDEX)) {
808 menu->redraw = REDRAW_FULL;
809 unset_option (OPTFORCEREDRAWINDEX);
810 unset_option (OPTFORCEREDRAWPAGER);
815 if (menu->tag && !menu->dialog) {
816 if (menu->tagprefix && !option (OPTAUTOTAG)) {
817 for (i = 0; i < menu->max; i++)
818 menu->tagged += menu->tag (menu, i, 0);
819 menu->redraw = REDRAW_INDEX;
821 else if (menu->max) {
822 int t = menu->tag (menu, menu->current, -1);
825 if (t && option (OPTRESOLVE) && menu->current < menu->max - 1) {
827 menu->redraw = REDRAW_MOTION_RESYNCH;
830 menu->redraw = REDRAW_CURRENT;
833 mutt_error _("No entries.");
836 mutt_error _("Tagging is not supported.");
839 case OP_SHELL_ESCAPE:
840 mutt_shell_escape ();
841 MAYBE_REDRAW (menu->redraw);
849 clearok (main_w, TRUE);
850 menu->redraw = REDRAW_FULL;
854 mutt_help (menu->menu);
855 menu->redraw = REDRAW_FULL;
859 km_error_key (menu->menu);