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 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
23 extern size_t UngetCount;
25 static void print_enriched_string (int attr, unsigned char *s, int do_color)
29 size_t n = m_strlen((char *) s);
34 if (*s < M_TREE_MAX) {
36 SETCOLOR (MT_COLOR_TREE);
37 while (*s && *s < M_TREE_MAX) {
40 waddch (stdscr, ACS_LLCORNER);
43 waddch (stdscr, ACS_ULCORNER);
46 waddch (stdscr, ACS_LTEE);
49 waddch (stdscr, ACS_HLINE);
52 waddch (stdscr, ACS_VLINE);
55 waddch (stdscr, ACS_TTEE);
58 waddch (stdscr, ACS_BTEE);
67 waddch (stdscr, '*'); /* fake thread indicator */
82 wattrset (stdscr, attr);
84 else if ((k = mbrtowc (&wc, (char *) s, n, &mbstate)) != (size_t)-1) {
85 waddnstr (stdscr, (char *) s, k);
93 static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i)
96 m_strcpy(s, l, menu->dialog[i]);
97 menu->current = -1; /* hide menubar */
100 menu->make_entry (s, l, menu, i);
103 static void menu_pad_string (char *s, size_t n)
105 int cols = COLS - SW;
106 char *tmpbuf = p_new(char, n);
108 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, m_strlen(s), 1);
110 snprintf (s, n, "%s", tmpbuf); /* overkill */
114 void menu_redraw_full (MUTTMENU * menu)
116 SETCOLOR (MT_COLOR_NORMAL);
117 /* wclear(stdscr) doesn't optimize screen redraws */
118 wmove (stdscr, 0, 0);
121 SETCOLOR (MT_COLOR_STATUS);
122 wmove (stdscr, option (OPTSTATUSONTOP) ? LINES - 2 : 0, SW);
123 mutt_paddstr (COLS-SW, "");
124 SETCOLOR (MT_COLOR_NORMAL);
126 menu->pagelen = LINES - 3;
129 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
132 void menu_redraw_status (MUTTMENU * menu)
136 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
137 SETCOLOR (MT_COLOR_STATUS);
138 wmove (stdscr, option (OPTSTATUSONTOP) ? 0 : LINES - 2, SW);
139 mutt_paddstr (COLS-SW, buf);
140 SETCOLOR (MT_COLOR_NORMAL);
141 menu->redraw &= ~REDRAW_STATUS;
144 void menu_redraw_index (MUTTMENU * menu)
149 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
151 menu_make_entry (buf, sizeof (buf), menu, i);
152 menu_pad_string (buf, sizeof (buf));
154 wattrset (stdscr, menu->color (i));
156 if (i == menu->current) {
157 ADDCOLOR (MT_COLOR_INDICATOR);
158 BKGDSET (MT_COLOR_INDICATOR);
161 CLEARLINE_WIN (i - menu->top + menu->offset);
163 wmove (stdscr, i - menu->top + menu->offset, SW);
164 print_enriched_string (menu->color (i), (unsigned char *) buf,
166 SETCOLOR (MT_COLOR_NORMAL);
167 BKGDSET (MT_COLOR_NORMAL);
169 CLEARLINE_WIN (i - menu->top + menu->offset);
177 void menu_redraw_motion (MUTTMENU * menu)
182 menu->redraw &= ~REDRAW_MOTION;
186 wmove (stdscr, menu->oldcurrent + menu->offset - menu->top, SW);
187 SETCOLOR (MT_COLOR_NORMAL);
188 BKGDSET (MT_COLOR_NORMAL);
190 /* erase the current indicator */
191 wattrset (stdscr, menu->color (menu->oldcurrent));
193 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
194 menu_pad_string (buf, sizeof (buf));
195 print_enriched_string (menu->color (menu->oldcurrent),
196 (unsigned char *) buf, 1);
198 /* now draw the new one to reflect the change */
199 menu_make_entry (buf, sizeof (buf), menu, menu->current);
200 menu_pad_string (buf, sizeof (buf));
201 wattrset (stdscr, menu->color (menu->current));
202 ADDCOLOR (MT_COLOR_INDICATOR);
203 BKGDSET (MT_COLOR_INDICATOR);
204 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
205 wmove (stdscr, menu->current + menu->offset - menu->top, SW);
206 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
208 SETCOLOR (MT_COLOR_NORMAL);
209 BKGDSET (MT_COLOR_NORMAL);
211 menu->redraw &= REDRAW_STATUS;
214 void menu_redraw_current (MUTTMENU * menu)
218 wmove (stdscr, menu->current + menu->offset - menu->top, SW);
219 menu_make_entry (buf, sizeof (buf), menu, menu->current);
220 menu_pad_string (buf, sizeof (buf));
222 wattrset (stdscr, menu->color (menu->current));
223 ADDCOLOR (MT_COLOR_INDICATOR);
224 BKGDSET (MT_COLOR_INDICATOR);
226 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
228 SETCOLOR (MT_COLOR_NORMAL);
229 BKGDSET (MT_COLOR_NORMAL);
231 menu->redraw &= REDRAW_STATUS;
234 static void menu_redraw_prompt (MUTTMENU * menu)
237 if (option (OPTMSGERR)) {
239 unset_option (OPTMSGERR);
245 SETCOLOR (MT_COLOR_NORMAL);
246 mvwaddstr (stdscr, LINES - 1, 0, menu->prompt);
251 void menu_check_recenter (MUTTMENU * menu)
253 int c = MIN (MenuContext, menu->pagelen / 2);
254 int old_top = menu->top;
256 if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) { /* less entries than lines */
257 if (menu->top != 0) {
259 set_option (OPTNEEDREDRAW);
262 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0) || (c < MenuContext)) {
263 if (menu->current < menu->top + c)
264 menu->top = menu->current - c;
265 else if (menu->current >= menu->top + menu->pagelen - c)
266 menu->top = menu->current - menu->pagelen + c + 1;
268 if (menu->current < menu->top + c)
269 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
270 else if ((menu->current >= menu->top + menu->pagelen - c))
271 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
275 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
276 menu->top = MIN (menu->top, menu->max - menu->pagelen);
277 menu->top = MAX (menu->top, 0);
279 if (menu->top != old_top)
280 menu->redraw |= REDRAW_INDEX;
283 void menu_jump (MUTTMENU * menu)
289 mutt_ungetch (LastKey, 0);
291 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
293 if (n >= 0 && n < menu->max) {
295 menu->redraw = REDRAW_MOTION;
298 mutt_error _("Invalid index number.");
302 mutt_error _("No entries.");
305 void menu_next_line (MUTTMENU * menu)
308 int c = MIN (MenuContext, menu->pagelen / 2);
310 if (menu->top + 1 < menu->max - c && (option (OPTMENUMOVEOFF)
311 || (menu->max > menu->pagelen
313 menu->max - menu->pagelen))) {
315 if (menu->current < menu->top + c && menu->current < menu->max - 1)
317 menu->redraw = REDRAW_INDEX;
320 mutt_error _("You cannot scroll down farther.");
323 mutt_error _("No entries.");
326 void menu_prev_line (MUTTMENU * menu)
329 int c = MIN (MenuContext, menu->pagelen / 2);
332 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
334 menu->redraw = REDRAW_INDEX;
337 mutt_error _("You cannot scroll up farther.");
341 * pageup: jumplen == -pagelen
342 * pagedown: jumplen == pagelen
343 * halfup: jumplen == -pagelen/2
344 * halfdown: jumplen == pagelen/2
346 #define DIRECTION ((neg * 2) + 1)
347 static void menu_length_jump (MUTTMENU *menu, int jumplen) {
348 int tmp, neg = (jumplen >= 0) ? 0 : -1;
349 int c = MIN (MenuContext, menu->pagelen / 2);
352 /* possible to scroll? */
353 if (DIRECTION * menu->top <
354 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
355 menu->top += jumplen;
357 /* jumped too long? */
358 if ((neg || !option (OPTMENUMOVEOFF)) && DIRECTION * menu->top > tmp)
361 /* need to move the cursor? */
363 (tmp = (menu->current - (menu->top +
364 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
365 menu->current -= tmp;
367 menu->redraw = REDRAW_INDEX;
369 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
370 menu->current += jumplen;
371 menu->redraw = REDRAW_MOTION;
374 mutt_error (neg ? _("You are on the first page.")
375 : _("You are on the last page."));
377 menu->current = MIN (menu->current, menu->max - 1);
378 menu->current = MAX (menu->current, 0);
381 mutt_error _("No entries.");
385 void menu_next_page (MUTTMENU *menu) {
386 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
389 void menu_prev_page (MUTTMENU *menu) {
390 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
393 void menu_half_down (MUTTMENU *menu) {
394 menu_length_jump (menu, menu->pagelen / 2);
397 void menu_half_up (MUTTMENU *menu) {
398 menu_length_jump (menu, 0 - menu->pagelen / 2);
401 void menu_top_page (MUTTMENU *menu) {
402 if (menu->current != menu->top) {
403 menu->current = menu->top;
404 menu->redraw = REDRAW_MOTION;
408 void menu_bottom_page (MUTTMENU *menu) {
410 menu->current = menu->top + menu->pagelen - 1;
411 if (menu->current > menu->max - 1)
412 menu->current = menu->max - 1;
413 menu->redraw = REDRAW_MOTION;
416 mutt_error _("No entries.");
419 void menu_middle_page (MUTTMENU *menu) {
423 i = menu->top + menu->pagelen;
424 if (i > menu->max - 1)
426 menu->current = menu->top + (i - menu->top) / 2;
427 menu->redraw = REDRAW_MOTION;
430 mutt_error _("No entries.");
433 void menu_first_entry (MUTTMENU *menu) {
436 menu->redraw = REDRAW_MOTION;
439 mutt_error _("No entries.");
442 void menu_last_entry (MUTTMENU *menu) {
444 menu->current = menu->max - 1;
445 menu->redraw = REDRAW_MOTION;
448 mutt_error _("No entries.");
451 void menu_current_top (MUTTMENU * menu)
454 menu->top = menu->current;
455 menu->redraw = REDRAW_INDEX;
458 mutt_error _("No entries.");
461 void menu_current_middle (MUTTMENU * menu)
464 menu->top = menu->current - menu->pagelen / 2;
467 menu->redraw = REDRAW_INDEX;
470 mutt_error _("No entries.");
473 void menu_current_bottom (MUTTMENU * menu)
476 menu->top = menu->current - menu->pagelen + 1;
479 menu->redraw = REDRAW_INDEX;
482 mutt_error _("No entries.");
485 static void menu_next_entry (MUTTMENU * menu)
487 if (menu->current < menu->max - 1) {
489 menu->redraw = REDRAW_MOTION;
492 mutt_error _("You are on the last entry.");
495 static void menu_prev_entry (MUTTMENU * menu)
499 menu->redraw = REDRAW_MOTION;
502 mutt_error _("You are on the first entry.");
505 static int default_color (int i __attribute__ ((unused)))
507 return ColorDefs[MT_COLOR_NORMAL];
510 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
512 char buf[LONG_STRING];
514 menu_make_entry (buf, sizeof (buf), m, n);
515 return (regexec (re, buf, 0, NULL, 0));
518 MUTTMENU *mutt_new_menu (void)
520 MUTTMENU *p = p_new(MUTTMENU, 1);
525 p->redraw = REDRAW_FULL;
526 p->pagelen = PAGELEN;
527 p->color = default_color;
528 p->search = menu_search_generic;
532 void mutt_menuDestroy (MUTTMENU ** p)
536 p_delete(&(*p)->searchBuf);
539 for (i = 0; i < (*p)->max; i++)
540 p_delete(&(*p)->dialog[i]);
542 p_delete(&(*p)->dialog);
548 #define M_SEARCH_UP 1
549 #define M_SEARCH_DOWN 2
551 static int menu_search (MUTTMENU * menu, int op)
558 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
559 m_strcpy(buf, sizeof(buf), NONULL(menu->searchBuf));
560 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
561 _("Reverse search for: "),
562 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
564 m_strreplace(&menu->searchBuf, buf);
565 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
568 if (!menu->searchBuf) {
569 mutt_error _("No search pattern.");
575 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
576 if (op == OP_SEARCH_OPPOSITE)
577 searchDir = -searchDir;
580 REGCOMP (&re, menu->searchBuf,
581 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
582 regerror (r, &re, buf, sizeof (buf));
584 mutt_error ("%s", buf);
588 r = menu->current + searchDir;
589 while (r >= 0 && r < menu->max) {
590 if (menu->search (menu, &re, r) == 0) {
599 mutt_error _("Not found.");
604 static int menu_dialog_translate_op (int i)
614 case OP_CURRENT_BOTTOM:
616 return OP_BOTTOM_PAGE;
617 case OP_CURRENT_MIDDLE:
618 return OP_MIDDLE_PAGE;
624 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
636 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
637 *ip = OP_MAX + (p - menu->keys + 1);
641 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
646 int menu_redraw (MUTTMENU * menu)
648 /* See if all or part of the screen needs to be updated. */
649 if (menu->redraw & REDRAW_FULL) {
650 menu_redraw_full (menu);
651 /* allow the caller to do any local configuration */
656 menu_check_recenter (menu);
658 if (menu->redraw & REDRAW_STATUS)
659 menu_redraw_status (menu);
660 if (menu->redraw & REDRAW_INDEX)
661 menu_redraw_index (menu);
662 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
663 menu_redraw_motion (menu);
664 else if (menu->redraw == REDRAW_CURRENT)
665 menu_redraw_current (menu);
668 menu_redraw_prompt (menu);
673 int mutt_menuLoop (MUTTMENU * menu)
678 if (option (OPTMENUCALLER)) {
679 unset_option (OPTMENUCALLER);
687 if (menu_redraw (menu) == OP_REDRAW)
690 menu->oldcurrent = menu->current;
692 if (option (OPTBRAILLEFRIENDLY))
693 wmove (stdscr, menu->current - menu->top + menu->offset, SW);
695 wmove (stdscr, menu->current - menu->top + menu->offset, COLS - 1);
700 /* try to catch dialog keys before ops */
701 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
704 i = km_dokey (menu->menu);
705 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
707 mvwaddstr (stdscr, LINES - 1, 0, "Tag-");
709 i = km_dokey (menu->menu);
711 CLEARLINE (LINES - 1);
713 else if (i == OP_TAG_PREFIX) {
714 mutt_error _("No tagged entries.");
718 else { /* None tagged, OP_TAG_PREFIX_COND */
722 while (UngetCount > 0) {
724 if (tmp.op == OP_END_COND)
727 mutt_message _("Nothing to do.");
732 else if (menu->tagged && option (OPTAUTOTAG))
741 menu->redraw = REDRAW_FULL;
750 /* Convert menubar movement to scrolling */
752 i = menu_dialog_translate_op (i);
756 menu_next_entry (menu);
759 menu_prev_entry (menu);
762 menu_half_down (menu);
768 menu_next_page (menu);
771 menu_prev_page (menu);
774 menu_next_line (menu);
777 menu_prev_line (menu);
780 menu_first_entry (menu);
783 menu_last_entry (menu);
786 menu_top_page (menu);
789 menu_middle_page (menu);
792 menu_bottom_page (menu);
795 menu_current_top (menu);
797 case OP_CURRENT_MIDDLE:
798 menu_current_middle (menu);
800 case OP_CURRENT_BOTTOM:
801 menu_current_bottom (menu);
804 case OP_SEARCH_REVERSE:
806 case OP_SEARCH_OPPOSITE:
807 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
808 menu->oldcurrent = menu->current;
809 if ((menu->current = menu_search (menu, i)) != -1)
810 menu->redraw = REDRAW_MOTION;
812 menu->current = menu->oldcurrent;
815 mutt_error _("Search is not implemented for this menu.");
820 mutt_error (_("Jumping is not implemented for dialogs."));
826 case OP_ENTER_COMMAND:
827 CurrentMenu = menu->menu;
828 mutt_enter_command ();
829 if (option (OPTFORCEREDRAWINDEX)) {
830 menu->redraw = REDRAW_FULL;
831 unset_option (OPTFORCEREDRAWINDEX);
832 unset_option (OPTFORCEREDRAWPAGER);
837 if (menu->tag && !menu->dialog) {
838 if (menu->tagprefix && !option (OPTAUTOTAG)) {
839 for (i = 0; i < menu->max; i++)
840 menu->tagged += menu->tag (menu, i, 0);
841 menu->redraw = REDRAW_INDEX;
843 else if (menu->max) {
844 int t = menu->tag (menu, menu->current, -1);
847 if (t && option (OPTRESOLVE) && menu->current < menu->max - 1) {
849 menu->redraw = REDRAW_MOTION_RESYNCH;
852 menu->redraw = REDRAW_CURRENT;
855 mutt_error _("No entries.");
858 mutt_error _("Tagging is not supported.");
861 case OP_SHELL_ESCAPE:
862 mutt_shell_escape ();
863 MAYBE_REDRAW (menu->redraw);
871 clearok (stdscr, TRUE);
872 menu->redraw = REDRAW_FULL;
876 mutt_help (menu->menu);
877 menu->redraw = REDRAW_FULL;
881 km_error_key (menu->menu);