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_STATUS);
120 wmove (main_w, LINES - 2, 0);
121 mutt_paddstr (main_w, getmaxx(main_w), "");
122 SETCOLOR(main_w, MT_COLOR_NORMAL);
124 menu->pagelen = LINES - 3;
127 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
130 void menu_redraw_status (MUTTMENU * menu)
134 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
135 SETCOLOR(main_w, MT_COLOR_STATUS);
136 wmove (main_w, 0, 0);
137 mutt_paddstr (main_w, getmaxx(main_w), buf);
138 SETCOLOR(main_w, MT_COLOR_NORMAL);
139 menu->redraw &= ~REDRAW_STATUS;
142 void menu_redraw_index (MUTTMENU * menu)
147 for (i = menu->top; i < menu->top + menu->pagelen; i++) {
149 menu_make_entry (buf, sizeof (buf), menu, i);
150 menu_pad_string (buf, sizeof (buf));
152 wattrset (main_w, menu->color (i));
154 if (i == menu->current) {
155 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
156 BKGDSET(main_w, MT_COLOR_INDICATOR);
159 CLEARLINE(main_w, i - menu->top + menu->offset);
161 wmove (main_w, i - menu->top + menu->offset, 0);
162 print_enriched_string (menu->color (i), (unsigned char *) buf,
164 SETCOLOR(main_w, MT_COLOR_NORMAL);
165 BKGDSET(main_w, MT_COLOR_NORMAL);
167 CLEARLINE(main_w, i - menu->top + menu->offset);
175 void menu_redraw_motion (MUTTMENU * menu)
180 menu->redraw &= ~REDRAW_MOTION;
184 wmove (main_w, menu->oldcurrent + menu->offset - menu->top, 0);
185 SETCOLOR(main_w, MT_COLOR_NORMAL);
186 BKGDSET(main_w, MT_COLOR_NORMAL);
188 /* erase the current indicator */
189 wattrset (main_w, menu->color (menu->oldcurrent));
191 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
192 menu_pad_string (buf, sizeof (buf));
193 print_enriched_string (menu->color (menu->oldcurrent),
194 (unsigned char *) buf, 1);
196 /* now draw the new one to reflect the change */
197 menu_make_entry (buf, sizeof (buf), menu, menu->current);
198 menu_pad_string (buf, sizeof (buf));
199 wattrset (main_w, menu->color (menu->current));
200 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
201 BKGDSET(main_w, MT_COLOR_INDICATOR);
202 CLEARLINE(main_w, menu->current - menu->top + menu->offset);
203 wmove (main_w, menu->current + menu->offset - menu->top, 0);
204 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
206 SETCOLOR(main_w, MT_COLOR_NORMAL);
207 BKGDSET(main_w, MT_COLOR_NORMAL);
209 menu->redraw &= REDRAW_STATUS;
212 void menu_redraw_current (MUTTMENU * menu)
216 wmove (main_w, menu->current + menu->offset - menu->top, 0);
217 menu_make_entry (buf, sizeof (buf), menu, menu->current);
218 menu_pad_string (buf, sizeof (buf));
220 wattrset (main_w, menu->color (menu->current));
221 ADDCOLOR(main_w, MT_COLOR_INDICATOR);
222 BKGDSET(main_w, MT_COLOR_INDICATOR);
224 print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
226 SETCOLOR(main_w, MT_COLOR_NORMAL);
227 BKGDSET(main_w, MT_COLOR_NORMAL);
229 menu->redraw &= REDRAW_STATUS;
232 static void menu_redraw_prompt (MUTTMENU * menu)
235 if (option (OPTMSGERR)) {
237 unset_option (OPTMSGERR);
243 SETCOLOR(main_w, MT_COLOR_NORMAL);
244 mvwaddstr (main_w, LINES - 1, 0, menu->prompt);
249 void menu_check_recenter (MUTTMENU * menu)
251 int c = MIN (MenuContext, menu->pagelen / 2);
252 int old_top = menu->top;
254 if ((menu->pagelen <= 0) || (c < MenuContext)) {
255 if (menu->current < menu->top + c)
256 menu->top = menu->current - c;
257 else if (menu->current >= menu->top + menu->pagelen - c)
258 menu->top = menu->current - menu->pagelen + c + 1;
260 if (menu->current < menu->top + c)
261 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
262 else if ((menu->current >= menu->top + menu->pagelen - c))
263 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
266 menu->top = MAX (menu->top, 0);
268 if (menu->top != old_top)
269 menu->redraw |= REDRAW_INDEX;
272 void menu_jump (MUTTMENU * menu)
278 mutt_ungetch (LastKey, 0);
280 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0]) {
282 if (n >= 0 && n < menu->max) {
284 menu->redraw = REDRAW_MOTION;
287 mutt_error _("Invalid index number.");
291 mutt_error _("No entries.");
294 void menu_next_line (MUTTMENU * menu)
297 int c = MIN (MenuContext, menu->pagelen / 2);
299 if (menu->top + 1 < menu->max - c) {
301 if (menu->current < menu->top + c && menu->current < menu->max - 1)
303 menu->redraw = REDRAW_INDEX;
305 mutt_error _("You cannot scroll down farther.");
308 mutt_error _("No entries.");
312 void menu_prev_line (MUTTMENU * menu)
315 int c = MIN (MenuContext, menu->pagelen / 2);
318 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
320 menu->redraw = REDRAW_INDEX;
323 mutt_error _("You cannot scroll up farther.");
327 * pageup: jumplen == -pagelen
328 * pagedown: jumplen == pagelen
329 * halfup: jumplen == -pagelen/2
330 * halfdown: jumplen == pagelen/2
332 #define DIRECTION ((neg * 2) + 1)
333 static void menu_length_jump (MUTTMENU *menu, int jumplen) {
334 int tmp, neg = (jumplen >= 0) ? 0 : -1;
335 int c = MIN (MenuContext, menu->pagelen / 2);
338 /* possible to scroll? */
339 if (DIRECTION * menu->top <
340 (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
341 menu->top += jumplen;
343 /* jumped too long? */
344 if (neg && DIRECTION * menu->top > tmp)
347 /* need to move the cursor? */
349 (tmp = (menu->current - (menu->top +
350 (neg ? (menu->pagelen - 1) - c : c))))) < 0)
351 menu->current -= tmp;
353 menu->redraw = REDRAW_INDEX;
355 else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
356 menu->current += jumplen;
357 menu->redraw = REDRAW_MOTION;
360 mutt_error (neg ? _("You are on the first page.")
361 : _("You are on the last page."));
363 menu->current = MIN (menu->current, menu->max - 1);
364 menu->current = MAX (menu->current, 0);
367 mutt_error _("No entries.");
371 void menu_next_page (MUTTMENU *menu) {
372 menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
375 void menu_prev_page (MUTTMENU *menu) {
376 menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
379 void menu_half_down (MUTTMENU *menu) {
380 menu_length_jump (menu, menu->pagelen / 2);
383 void menu_half_up (MUTTMENU *menu) {
384 menu_length_jump (menu, 0 - menu->pagelen / 2);
387 void menu_top_page (MUTTMENU *menu) {
388 if (menu->current != menu->top) {
389 menu->current = menu->top;
390 menu->redraw = REDRAW_MOTION;
394 void menu_bottom_page (MUTTMENU *menu) {
396 menu->current = menu->top + menu->pagelen - 1;
397 if (menu->current > menu->max - 1)
398 menu->current = menu->max - 1;
399 menu->redraw = REDRAW_MOTION;
402 mutt_error _("No entries.");
405 void menu_middle_page (MUTTMENU *menu) {
409 i = menu->top + menu->pagelen;
410 if (i > menu->max - 1)
412 menu->current = menu->top + (i - menu->top) / 2;
413 menu->redraw = REDRAW_MOTION;
416 mutt_error _("No entries.");
419 void menu_first_entry (MUTTMENU *menu) {
422 menu->redraw = REDRAW_MOTION;
425 mutt_error _("No entries.");
428 void menu_last_entry (MUTTMENU *menu) {
430 menu->current = menu->max - 1;
431 menu->redraw = REDRAW_MOTION;
434 mutt_error _("No entries.");
437 void menu_current_top (MUTTMENU * menu)
440 menu->top = menu->current;
441 menu->redraw = REDRAW_INDEX;
444 mutt_error _("No entries.");
447 void menu_current_middle (MUTTMENU * menu)
450 menu->top = menu->current - menu->pagelen / 2;
453 menu->redraw = REDRAW_INDEX;
456 mutt_error _("No entries.");
459 void menu_current_bottom (MUTTMENU * menu)
462 menu->top = menu->current - menu->pagelen + 1;
465 menu->redraw = REDRAW_INDEX;
468 mutt_error _("No entries.");
471 static void menu_next_entry (MUTTMENU * menu)
473 if (menu->current < menu->max - 1) {
475 menu->redraw = REDRAW_MOTION;
478 mutt_error _("You are on the last entry.");
481 static void menu_prev_entry (MUTTMENU * menu)
485 menu->redraw = REDRAW_MOTION;
488 mutt_error _("You are on the first entry.");
491 static int default_color (int i __attribute__ ((unused)))
493 return ColorDefs[MT_COLOR_NORMAL];
496 static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
498 char buf[LONG_STRING];
500 menu_make_entry (buf, sizeof (buf), m, n);
501 return (regexec (re, buf, 0, NULL, 0));
504 MUTTMENU *mutt_new_menu (void)
506 MUTTMENU *p = p_new(MUTTMENU, 1);
511 p->redraw = REDRAW_FULL;
512 p->pagelen = LINES - 3;
513 p->color = default_color;
514 p->search = menu_search_generic;
518 void mutt_menuDestroy (MUTTMENU ** p)
522 p_delete(&(*p)->searchBuf);
525 for (i = 0; i < (*p)->max; i++)
526 p_delete(&(*p)->dialog[i]);
528 p_delete(&(*p)->dialog);
534 #define M_SEARCH_UP 1
535 #define M_SEARCH_DOWN 2
537 static int menu_search (MUTTMENU * menu, int op)
544 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
545 m_strcpy(buf, sizeof(buf), NONULL(menu->searchBuf));
546 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
547 _("Reverse search for: "),
548 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
550 m_strreplace(&menu->searchBuf, buf);
551 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
554 if (!menu->searchBuf) {
555 mutt_error _("No search pattern.");
561 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
562 if (op == OP_SEARCH_OPPOSITE)
563 searchDir = -searchDir;
566 REGCOMP (&re, menu->searchBuf,
567 REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0) {
568 regerror (r, &re, buf, sizeof (buf));
570 mutt_error ("%s", buf);
574 r = menu->current + searchDir;
575 while (r >= 0 && r < menu->max) {
576 if (menu->search (menu, &re, r) == 0) {
585 mutt_error _("Not found.");
590 static int menu_dialog_translate_op (int i)
600 case OP_CURRENT_BOTTOM:
602 return OP_BOTTOM_PAGE;
603 case OP_CURRENT_MIDDLE:
604 return OP_MIDDLE_PAGE;
610 static int menu_dialog_dokey (MUTTMENU * menu, int *ip)
622 if (ch.ch && (p = strchr (menu->keys, ch.ch))) {
623 *ip = OP_MAX + (p - menu->keys + 1);
627 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
632 int menu_redraw (MUTTMENU * menu)
634 /* See if all or part of the screen needs to be updated. */
635 if (menu->redraw & REDRAW_FULL) {
636 menu_redraw_full (menu);
637 /* allow the caller to do any local configuration */
642 menu_check_recenter (menu);
644 if (menu->redraw & REDRAW_STATUS)
645 menu_redraw_status (menu);
646 if (menu->redraw & REDRAW_INDEX)
647 menu_redraw_index (menu);
648 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
649 menu_redraw_motion (menu);
650 else if (menu->redraw == REDRAW_CURRENT)
651 menu_redraw_current (menu);
654 menu_redraw_prompt (menu);
659 int mutt_menuLoop (MUTTMENU * menu)
664 if (option (OPTMENUCALLER)) {
665 unset_option (OPTMENUCALLER);
673 if (menu_redraw (menu) == OP_REDRAW)
676 menu->oldcurrent = menu->current;
679 /* try to catch dialog keys before ops */
680 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
683 i = km_dokey (menu->menu);
684 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND) {
686 mvwaddstr (main_w, LINES - 1, 0, "Tag-");
688 i = km_dokey (menu->menu);
690 CLEARLINE(main_w, LINES - 1);
692 else if (i == OP_TAG_PREFIX) {
693 mutt_error _("No tagged entries.");
697 else { /* None tagged, OP_TAG_PREFIX_COND */
701 while (UngetCount > 0) {
703 if (tmp.op == OP_END_COND)
706 mutt_message _("Nothing to do.");
711 else if (menu->tagged && option (OPTAUTOTAG))
720 menu->redraw = REDRAW_FULL;
729 /* Convert menubar movement to scrolling */
731 i = menu_dialog_translate_op (i);
735 menu_next_entry (menu);
738 menu_prev_entry (menu);
741 menu_half_down (menu);
747 menu_next_page (menu);
750 menu_prev_page (menu);
753 menu_next_line (menu);
756 menu_prev_line (menu);
759 menu_first_entry (menu);
762 menu_last_entry (menu);
765 menu_top_page (menu);
768 menu_middle_page (menu);
771 menu_bottom_page (menu);
774 menu_current_top (menu);
776 case OP_CURRENT_MIDDLE:
777 menu_current_middle (menu);
779 case OP_CURRENT_BOTTOM:
780 menu_current_bottom (menu);
783 case OP_SEARCH_REVERSE:
785 case OP_SEARCH_OPPOSITE:
786 if (menu->search && !menu->dialog) { /* Searching dialogs won't work */
787 menu->oldcurrent = menu->current;
788 if ((menu->current = menu_search (menu, i)) != -1)
789 menu->redraw = REDRAW_MOTION;
791 menu->current = menu->oldcurrent;
794 mutt_error _("Search is not implemented for this menu.");
799 mutt_error (_("Jumping is not implemented for dialogs."));
805 case OP_ENTER_COMMAND:
806 CurrentMenu = menu->menu;
807 mutt_enter_command ();
808 if (option (OPTFORCEREDRAWINDEX)) {
809 menu->redraw = REDRAW_FULL;
810 unset_option (OPTFORCEREDRAWINDEX);
811 unset_option (OPTFORCEREDRAWPAGER);
816 if (menu->tag && !menu->dialog) {
817 if (menu->tagprefix && !option (OPTAUTOTAG)) {
818 for (i = 0; i < menu->max; i++)
819 menu->tagged += menu->tag (menu, i, 0);
820 menu->redraw = REDRAW_INDEX;
822 else if (menu->max) {
823 int t = menu->tag (menu, menu->current, -1);
826 if (t && option (OPTRESOLVE) && menu->current < menu->max - 1) {
828 menu->redraw = REDRAW_MOTION_RESYNCH;
831 menu->redraw = REDRAW_CURRENT;
834 mutt_error _("No entries.");
837 mutt_error _("Tagging is not supported.");
840 case OP_SHELL_ESCAPE:
841 mutt_shell_escape ();
842 MAYBE_REDRAW (menu->redraw);
850 clearok (main_w, TRUE);
851 menu->redraw = REDRAW_FULL;
855 mutt_help (menu->menu);
856 menu->redraw = REDRAW_FULL;
860 km_error_key (menu->menu);