2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_curses.h"
25 #include "mutt_menu.h"
36 extern int Charset_is_utf8; /* FIXME: bad modularisation */
38 extern size_t UngetCount;
40 static void print_enriched_string (int attr, unsigned char *s, int do_color)
44 size_t n = mutt_strlen ((char *)s);
47 memset (&mbstate, 0, sizeof (mbstate));
53 SETCOLOR (MT_COLOR_TREE);
54 while (*s && *s < M_TREE_MAX)
59 if (option (OPTASCIICHARS))
61 else if (Charset_is_utf8)
62 addstr ("\342\224\224"); /* WACS_LLCORNER */
67 if (option (OPTASCIICHARS))
69 else if (Charset_is_utf8)
70 addstr ("\342\224\214"); /* WACS_ULCORNER */
75 if (option (OPTASCIICHARS))
77 else if (Charset_is_utf8)
78 addstr ("\342\224\234"); /* WACS_LTEE */
83 if (option (OPTASCIICHARS))
85 else if (Charset_is_utf8)
86 addstr ("\342\224\200"); /* WACS_HLINE */
91 if (option (OPTASCIICHARS))
93 else if (Charset_is_utf8)
94 addstr ("\342\224\202"); /* WACS_VLINE */
99 if (option (OPTASCIICHARS))
101 else if (Charset_is_utf8)
102 addstr ("\342\224\254"); /* WACS_TTEE */
107 if (option (OPTASCIICHARS))
109 else if (Charset_is_utf8)
110 addstr ("\342\224\264"); /* WACS_BTEE */
121 addch ('*'); /* fake thread indicator */
135 if (do_color) attrset(attr);
137 else if ((k = mbrtowc (&wc, (char *)s, n, &mbstate)) > 0)
139 addnstr ((char *)s, k);
147 static void menu_make_entry (char *s, int l, MUTTMENU *menu, int i)
151 strncpy (s, menu->dialog[i], l);
152 menu->current = -1; /* hide menubar */
155 menu->make_entry (s, l, menu, i);
158 void menu_pad_string (char *s, size_t n)
160 int shift = option (OPTARROWCURSOR) ? 3 : 0;
161 int cols = COLS - shift - SidebarWidth;
162 char* tmpbuf = safe_malloc (n);
164 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, strlen (s), 1);
166 snprintf(s,n,"%s",tmpbuf); /* overkill */
170 void menu_redraw_full (MUTTMENU *menu)
172 SETCOLOR (MT_COLOR_NORMAL);
173 /* clear() doesn't optimize screen redraws */
177 if (option (OPTHELP))
179 SETCOLOR (MT_COLOR_STATUS);
180 move (option (OPTSTATUSONTOP) ? LINES-2 : 0, 0);
181 mutt_paddstr (COLS, menu->help);
182 SETCOLOR (MT_COLOR_NORMAL);
184 menu->pagelen = LINES - 3;
188 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
189 menu->pagelen = LINES - 2;
194 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
197 void menu_redraw_status (MUTTMENU *menu)
201 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
202 SETCOLOR (MT_COLOR_STATUS);
203 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
204 mutt_paddstr (COLS, buf);
205 SETCOLOR (MT_COLOR_NORMAL);
206 menu->redraw &= ~REDRAW_STATUS;
209 void menu_redraw_index (MUTTMENU *menu)
215 for (i = menu->top; i < menu->top + menu->pagelen; i++)
219 menu_make_entry (buf, sizeof (buf), menu, i);
220 menu_pad_string (buf, sizeof (buf));
222 if (option (OPTARROWCURSOR))
224 attrset (menu->color (i));
225 CLEARLINE_WIN (i - menu->top + menu->offset);
227 if (i == menu->current)
229 attrset (menu->color (i));
230 ADDCOLOR (MT_COLOR_INDICATOR);
232 attrset (menu->color (i));
236 move (i - menu->top + menu->offset, SidebarWidth + 3);
238 print_enriched_string (menu->color(i), (unsigned char *) buf, 1);
239 SETCOLOR (MT_COLOR_NORMAL);
243 attrset (menu->color (i));
245 if (i == menu->current)
247 ADDCOLOR (MT_COLOR_INDICATOR);
248 BKGDSET (MT_COLOR_INDICATOR);
251 CLEARLINE_WIN (i - menu->top + menu->offset);
252 print_enriched_string (menu->color(i), (unsigned char *) buf, i != menu->current);
253 SETCOLOR (MT_COLOR_NORMAL);
254 BKGDSET (MT_COLOR_NORMAL);
258 CLEARLINE_WIN (i - menu->top + menu->offset);
263 void menu_redraw_motion (MUTTMENU *menu)
269 menu->redraw &= ~REDRAW_MOTION;
273 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
274 SETCOLOR (MT_COLOR_NORMAL);
275 BKGDSET (MT_COLOR_NORMAL);
277 if (option (OPTARROWCURSOR))
279 /* clear the pointer */
280 attrset (menu->color (menu->oldcurrent));
283 if (menu->redraw & REDRAW_MOTION_RESYNCH)
286 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
287 menu_pad_string (buf, sizeof (buf));
288 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth + 3);
289 print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
290 SETCOLOR (MT_COLOR_NORMAL);
293 /* now draw it in the new location */
294 move (menu->current + menu->offset - menu->top, SidebarWidth);
295 attrset (menu->color (menu->current));
296 ADDCOLOR (MT_COLOR_INDICATOR);
298 SETCOLOR (MT_COLOR_NORMAL);
302 /* erase the current indicator */
303 attrset (menu->color (menu->oldcurrent));
305 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
306 menu_pad_string (buf, sizeof (buf));
307 print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
309 /* now draw the new one to reflect the change */
310 menu_make_entry (buf, sizeof (buf), menu, menu->current);
311 menu_pad_string (buf, sizeof (buf));
312 attrset (menu->color (menu->current));
313 ADDCOLOR (MT_COLOR_INDICATOR);
314 BKGDSET (MT_COLOR_INDICATOR);
315 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
316 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
317 SETCOLOR (MT_COLOR_NORMAL);
318 BKGDSET (MT_COLOR_NORMAL);
320 menu->redraw &= REDRAW_STATUS;
323 void menu_redraw_current (MUTTMENU *menu)
327 move (menu->current + menu->offset - menu->top, SidebarWidth);
328 menu_make_entry (buf, sizeof (buf), menu, menu->current);
329 menu_pad_string (buf, sizeof (buf));
331 if (option (OPTARROWCURSOR))
333 int attr = menu->color (menu->current);
336 attrset (menu->color (menu->current));
337 ADDCOLOR (MT_COLOR_INDICATOR);
341 menu_pad_string (buf, sizeof (buf));
342 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 1);
343 SETCOLOR (MT_COLOR_NORMAL);
347 attrset (menu->color (menu->current));
348 ADDCOLOR (MT_COLOR_INDICATOR);
349 BKGDSET (MT_COLOR_INDICATOR);
351 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
352 SETCOLOR (MT_COLOR_NORMAL);
353 BKGDSET (MT_COLOR_NORMAL);
355 menu->redraw &= REDRAW_STATUS;
358 void menu_redraw_prompt (MUTTMENU *menu)
362 if (option (OPTMSGERR))
365 unset_option (OPTMSGERR);
371 SETCOLOR (MT_COLOR_NORMAL);
372 mvaddstr (LINES - 1, 0, menu->prompt);
377 void menu_check_recenter (MUTTMENU *menu)
379 int c = MIN (MenuContext, menu->pagelen / 2);
380 int old_top = menu->top;
382 if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) /* less entries than lines */
384 if (menu->top != 0) {
386 set_option (OPTNEEDREDRAW);
389 else if (menu->current >= menu->top + menu->pagelen - c) /* indicator below bottom threshold */
391 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
392 menu->top = menu->current - menu->pagelen + c + 1;
394 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
396 else if (menu->current < menu->top + c) /* indicator above top threshold */
398 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
399 menu->top = menu->current - c;
401 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
404 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
405 menu->top = MIN (menu->top, menu->max - menu->pagelen);
406 menu->top = MAX (menu->top, 0);
408 if (menu->top != old_top)
409 menu->redraw |= REDRAW_INDEX;
412 void menu_jump (MUTTMENU *menu)
415 char buf[SHORT_STRING];
419 mutt_ungetch (LastKey, 0);
421 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0])
424 if (n >= 0 && n < menu->max)
427 menu->redraw = REDRAW_MOTION;
430 mutt_error _("Invalid index number.");
434 mutt_error _("No entries.");
437 void menu_next_line (MUTTMENU *menu)
441 int c = MIN (MenuContext, menu->pagelen / 2);
443 if (menu->top + 1 < menu->max - c
444 && (option(OPTMENUMOVEOFF) || (menu->max > menu->pagelen && menu->top < menu->max - menu->pagelen)))
447 if (menu->current < menu->top + c && menu->current < menu->max - 1)
449 menu->redraw = REDRAW_INDEX;
452 mutt_error _("You cannot scroll down farther.");
455 mutt_error _("No entries.");
458 void menu_prev_line (MUTTMENU *menu)
462 int c = MIN (MenuContext, menu->pagelen / 2);
465 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
467 menu->redraw = REDRAW_INDEX;
470 mutt_error _("You cannot scroll up farther.");
473 void menu_next_page (MUTTMENU *menu)
477 if (menu->top + menu->pagelen < menu->max)
479 menu->top += menu->pagelen;
480 if (menu->current < menu->top)
481 menu->current = menu->top;
482 menu->redraw = REDRAW_INDEX;
484 else if (menu->current != menu->max - 1 && !menu->dialog)
486 menu->current = menu->max - 1;
487 menu->redraw = REDRAW_MOTION;
490 mutt_error _("You are on the last page.");
493 mutt_error _("No entries.");
496 void menu_prev_page (MUTTMENU *menu)
498 int c = MIN (MenuContext, menu->pagelen / 2);
502 if ((menu->top -= menu->pagelen) < 0)
504 if (menu->current >= menu->top + menu->pagelen)
505 menu->current = menu->top + menu->pagelen - 1;
506 menu->redraw = REDRAW_INDEX;
508 else if (menu->current && !menu->dialog)
511 menu->redraw = REDRAW_MOTION;
514 mutt_error _("You are on the first page.");
517 void menu_top_page (MUTTMENU *menu)
519 if (menu->current != menu->top)
521 menu->current = menu->top;
522 menu->redraw = REDRAW_MOTION;
526 void menu_bottom_page (MUTTMENU *menu)
530 menu->current = menu->top + menu->pagelen - 1;
531 if (menu->current > menu->max - 1)
532 menu->current = menu->max - 1;
533 menu->redraw = REDRAW_MOTION;
536 mutt_error _("No entries.");
539 void menu_middle_page (MUTTMENU *menu)
545 i = menu->top + menu->pagelen;
546 if (i > menu->max - 1)
548 menu->current = menu->top + (i - menu->top) / 2;
549 menu->redraw = REDRAW_MOTION;
552 mutt_error _("No entries.");
555 void menu_first_entry (MUTTMENU *menu)
560 menu->redraw = REDRAW_MOTION;
563 mutt_error _("No entries.");
566 void menu_last_entry (MUTTMENU *menu)
570 menu->current = menu->max - 1;
571 menu->redraw = REDRAW_MOTION;
574 mutt_error _("No entries.");
577 void menu_half_up (MUTTMENU *menu)
581 if ((menu->top -= menu->pagelen / 2) < 0)
583 if (menu->current >= menu->top + menu->pagelen)
584 menu->current = menu->top + menu->pagelen - 1;
585 menu->redraw = REDRAW_INDEX;
587 else if (menu->current && !menu->dialog)
590 menu->redraw = REDRAW_MOTION;
593 mutt_error _("First entry is shown.");
596 void menu_half_down (MUTTMENU *menu)
600 if (menu->top + menu->pagelen < menu->max)
602 menu->top += menu->pagelen / 2;
603 if (menu->current < menu->top)
604 menu->current = menu->top;
605 menu->redraw = REDRAW_INDEX;
607 else if (menu->current != menu->max - 1 && !menu->dialog)
609 menu->current = menu->max - 1;
610 menu->redraw = REDRAW_INDEX;
613 mutt_error _("Last entry is shown.");
616 mutt_error _("No entries.");
619 void menu_current_top (MUTTMENU *menu)
623 menu->top = menu->current;
624 menu->redraw = REDRAW_INDEX;
627 mutt_error _("No entries.");
630 void menu_current_middle (MUTTMENU *menu)
634 menu->top = menu->current - menu->pagelen / 2;
637 menu->redraw = REDRAW_INDEX;
640 mutt_error _("No entries.");
643 void menu_current_bottom (MUTTMENU *menu)
647 menu->top = menu->current - menu->pagelen + 1;
650 menu->redraw = REDRAW_INDEX;
653 mutt_error _("No entries.");
656 void menu_next_entry (MUTTMENU *menu)
658 if (menu->current < menu->max - 1)
661 menu->redraw = REDRAW_MOTION;
664 mutt_error _("You are on the last entry.");
667 void menu_prev_entry (MUTTMENU *menu)
672 menu->redraw = REDRAW_MOTION;
675 mutt_error _("You are on the first entry.");
678 static int default_color (int i)
680 return ColorDefs[MT_COLOR_NORMAL];
683 static int menu_search_generic (MUTTMENU *m, regex_t *re, int n)
685 char buf[LONG_STRING];
687 menu_make_entry (buf, sizeof (buf), m, n);
688 return (regexec (re, buf, 0, NULL, 0));
691 MUTTMENU *mutt_new_menu (void)
693 MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
698 p->redraw = REDRAW_FULL;
699 p->pagelen = PAGELEN;
700 p->color = default_color;
701 p->search = menu_search_generic;
705 void mutt_menuDestroy (MUTTMENU **p)
709 FREE (&(*p)->searchBuf);
713 for (i=0; i < (*p)->max; i++)
714 FREE (&(*p)->dialog[i]);
716 FREE (& (*p)->dialog);
722 #define M_SEARCH_UP 1
723 #define M_SEARCH_DOWN 2
725 static int menu_search (MUTTMENU *menu, int op)
730 char buf[SHORT_STRING];
732 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
734 strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
735 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
736 _("Reverse search for: "),
737 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
739 mutt_str_replace (&menu->searchBuf, buf);
740 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
744 if (!menu->searchBuf)
746 mutt_error _("No search pattern.");
751 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
752 if (op == OP_SEARCH_OPPOSITE)
753 searchDir = -searchDir;
755 if ((r = REGCOMP (&re, menu->searchBuf, REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0)
757 regerror (r, &re, buf, sizeof (buf));
759 mutt_error ("%s", buf);
763 r = menu->current + searchDir;
764 while (r >= 0 && r < menu->max)
766 if (menu->search (menu, &re, r) == 0)
776 mutt_error _("Not found.");
780 static int menu_dialog_translate_op (int i)
788 case OP_CURRENT_TOP: case OP_FIRST_ENTRY:
790 case OP_CURRENT_BOTTOM: case OP_LAST_ENTRY:
791 return OP_BOTTOM_PAGE;
792 case OP_CURRENT_MIDDLE:
793 return OP_MIDDLE_PAGE;
799 static int menu_dialog_dokey (MUTTMENU *menu, int *ip)
812 if (ch.ch && (p = strchr (menu->keys, ch.ch)))
814 *ip = OP_MAX + (p - menu->keys + 1);
819 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
824 int menu_redraw (MUTTMENU *menu)
826 /* See if all or part of the screen needs to be updated. */
827 if (menu->redraw & REDRAW_FULL)
829 menu_redraw_full (menu);
830 /* allow the caller to do any local configuration */
835 menu_check_recenter (menu);
837 if (menu->redraw & REDRAW_STATUS)
838 menu_redraw_status (menu);
839 if (menu->redraw & REDRAW_INDEX)
840 menu_redraw_index (menu);
841 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
842 menu_redraw_motion (menu);
843 else if (menu->redraw == REDRAW_CURRENT)
844 menu_redraw_current (menu);
847 menu_redraw_prompt (menu);
852 int mutt_menuLoop (MUTTMENU *menu)
858 if (option (OPTMENUCALLER))
860 unset_option (OPTMENUCALLER);
871 if (menu_redraw (menu) == OP_REDRAW)
874 menu->oldcurrent = menu->current;
877 /* move the cursor out of the way */
878 move (menu->current - menu->top + menu->offset,
879 (option (OPTARROWCURSOR) ? 2 : COLS-1));
883 /* try to catch dialog keys before ops */
884 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
887 i = km_dokey (menu->menu);
888 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND)
892 mvaddstr (LINES - 1, 0, "Tag-");
894 i = km_dokey (menu->menu);
896 CLEARLINE (LINES - 1);
898 else if (i == OP_TAG_PREFIX)
900 mutt_error _("No tagged entries.");
903 else /* None tagged, OP_TAG_PREFIX_COND */
909 if(tmp.op==OP_END_COND)break;
911 mutt_message _("Nothing to do.");
915 else if (menu->tagged && option (OPTAUTOTAG))
922 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
925 mutt_resize_screen ();
926 menu->redraw = REDRAW_FULL;
928 clearok(stdscr,TRUE);/*force complete redraw*/
938 /* Convert menubar movement to scrolling */
940 i = menu_dialog_translate_op (i);
945 menu_next_entry (menu);
948 menu_prev_entry (menu);
951 menu_half_down (menu);
957 menu_next_page (menu);
960 menu_prev_page (menu);
963 menu_next_line (menu);
966 menu_prev_line (menu);
969 menu_first_entry (menu);
972 menu_last_entry (menu);
975 menu_top_page (menu);
978 menu_middle_page (menu);
981 menu_bottom_page (menu);
984 menu_current_top (menu);
986 case OP_CURRENT_MIDDLE:
987 menu_current_middle (menu);
989 case OP_CURRENT_BOTTOM:
990 menu_current_bottom (menu);
993 case OP_SEARCH_REVERSE:
995 case OP_SEARCH_OPPOSITE:
996 if (menu->search && !menu->dialog) /* Searching dialogs won't work */
998 menu->oldcurrent = menu->current;
999 if ((menu->current = menu_search (menu, i)) != -1)
1000 menu->redraw = REDRAW_MOTION;
1002 menu->current = menu->oldcurrent;
1005 mutt_error _("Search is not implemented for this menu.");
1010 mutt_error _("Jumping is not implemented for dialogs.");
1015 case OP_ENTER_COMMAND:
1016 CurrentMenu = menu->menu;
1017 mutt_enter_command ();
1018 if (option (OPTFORCEREDRAWINDEX))
1020 menu->redraw = REDRAW_FULL;
1021 unset_option (OPTFORCEREDRAWINDEX);
1022 unset_option (OPTFORCEREDRAWPAGER);
1027 if (menu->tag && !menu->dialog)
1029 if (menu->tagprefix && !option (OPTAUTOTAG))
1031 for (i = 0; i < menu->max; i++)
1032 menu->tagged += menu->tag (menu, i, 0);
1033 menu->redraw = REDRAW_INDEX;
1037 int i = menu->tag (menu, menu->current, -1);
1039 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1)
1042 menu->redraw = REDRAW_MOTION_RESYNCH;
1045 menu->redraw = REDRAW_CURRENT;
1048 mutt_error _("No entries.");
1051 mutt_error _("Tagging is not supported.");
1054 case OP_SHELL_ESCAPE:
1055 mutt_shell_escape ();
1056 MAYBE_REDRAW (menu->redraw);
1064 clearok (stdscr, TRUE);
1065 menu->redraw = REDRAW_FULL;
1069 mutt_help (menu->menu);
1070 menu->redraw = REDRAW_FULL;
1074 km_error_key (menu->menu);