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;
165 mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, strlen (s), 1);
168 snprintf(s,n,"%s",tmpbuf); /* overkill */
171 void menu_redraw_full (MUTTMENU *menu)
173 SETCOLOR (MT_COLOR_NORMAL);
174 /* clear() doesn't optimize screen redraws */
178 if (option (OPTHELP))
180 SETCOLOR (MT_COLOR_STATUS);
181 move (option (OPTSTATUSONTOP) ? LINES-2 : 0, 0);
182 mutt_paddstr (COLS, menu->help);
183 SETCOLOR (MT_COLOR_NORMAL);
185 menu->pagelen = LINES - 3;
189 menu->offset = option (OPTSTATUSONTOP) ? 1 : 0;
190 menu->pagelen = LINES - 2;
195 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
198 void menu_redraw_status (MUTTMENU *menu)
202 snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
203 SETCOLOR (MT_COLOR_STATUS);
204 move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
205 mutt_paddstr (COLS, buf);
206 SETCOLOR (MT_COLOR_NORMAL);
207 menu->redraw &= ~REDRAW_STATUS;
210 void menu_redraw_index (MUTTMENU *menu)
216 for (i = menu->top; i < menu->top + menu->pagelen; i++)
220 menu_make_entry (buf, sizeof (buf), menu, i);
221 menu_pad_string (buf, sizeof (buf));
223 if (option (OPTARROWCURSOR))
225 attrset (menu->color (i));
226 CLEARLINE_WIN (i - menu->top + menu->offset);
228 if (i == menu->current)
230 attrset (menu->color (i));
231 ADDCOLOR (MT_COLOR_INDICATOR);
233 attrset (menu->color (i));
237 move (i - menu->top + menu->offset, SidebarWidth + 3);
239 print_enriched_string (menu->color(i), (unsigned char *) buf, 1);
240 SETCOLOR (MT_COLOR_NORMAL);
244 attrset (menu->color (i));
246 if (i == menu->current)
248 ADDCOLOR (MT_COLOR_INDICATOR);
249 BKGDSET (MT_COLOR_INDICATOR);
252 CLEARLINE_WIN (i - menu->top + menu->offset);
253 print_enriched_string (menu->color(i), (unsigned char *) buf, i != menu->current);
254 SETCOLOR (MT_COLOR_NORMAL);
255 BKGDSET (MT_COLOR_NORMAL);
259 CLEARLINE_WIN (i - menu->top + menu->offset);
264 void menu_redraw_motion (MUTTMENU *menu)
270 menu->redraw &= ~REDRAW_MOTION;
274 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
275 SETCOLOR (MT_COLOR_NORMAL);
276 BKGDSET (MT_COLOR_NORMAL);
278 if (option (OPTARROWCURSOR))
280 /* clear the pointer */
281 attrset (menu->color (menu->oldcurrent));
284 if (menu->redraw & REDRAW_MOTION_RESYNCH)
287 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
288 menu_pad_string (buf, sizeof (buf));
289 move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth + 3);
290 print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
291 SETCOLOR (MT_COLOR_NORMAL);
294 /* now draw it in the new location */
295 move (menu->current + menu->offset - menu->top, SidebarWidth);
296 attrset (menu->color (menu->current));
297 ADDCOLOR (MT_COLOR_INDICATOR);
299 SETCOLOR (MT_COLOR_NORMAL);
303 /* erase the current indicator */
304 attrset (menu->color (menu->oldcurrent));
306 menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
307 menu_pad_string (buf, sizeof (buf));
308 print_enriched_string (menu->color(menu->oldcurrent), (unsigned char *) buf, 1);
310 /* now draw the new one to reflect the change */
311 menu_make_entry (buf, sizeof (buf), menu, menu->current);
312 menu_pad_string (buf, sizeof (buf));
313 attrset (menu->color (menu->current));
314 ADDCOLOR (MT_COLOR_INDICATOR);
315 BKGDSET (MT_COLOR_INDICATOR);
316 CLEARLINE_WIN (menu->current - menu->top + menu->offset);
317 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
318 SETCOLOR (MT_COLOR_NORMAL);
319 BKGDSET (MT_COLOR_NORMAL);
321 menu->redraw &= REDRAW_STATUS;
324 void menu_redraw_current (MUTTMENU *menu)
328 move (menu->current + menu->offset - menu->top, SidebarWidth);
329 menu_make_entry (buf, sizeof (buf), menu, menu->current);
330 menu_pad_string (buf, sizeof (buf));
332 if (option (OPTARROWCURSOR))
334 int attr = menu->color (menu->current);
337 attrset (menu->color (menu->current));
338 ADDCOLOR (MT_COLOR_INDICATOR);
342 menu_pad_string (buf, sizeof (buf));
343 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 1);
344 SETCOLOR (MT_COLOR_NORMAL);
348 attrset (menu->color (menu->current));
349 ADDCOLOR (MT_COLOR_INDICATOR);
350 BKGDSET (MT_COLOR_INDICATOR);
352 print_enriched_string (menu->color(menu->current), (unsigned char *) buf, 0);
353 SETCOLOR (MT_COLOR_NORMAL);
354 BKGDSET (MT_COLOR_NORMAL);
356 menu->redraw &= REDRAW_STATUS;
359 void menu_redraw_prompt (MUTTMENU *menu)
363 if (option (OPTMSGERR))
366 unset_option (OPTMSGERR);
372 SETCOLOR (MT_COLOR_NORMAL);
373 mvaddstr (LINES - 1, 0, menu->prompt);
378 void menu_check_recenter (MUTTMENU *menu)
380 int c = MIN (MenuContext, menu->pagelen / 2);
381 int old_top = menu->top;
383 if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) /* less entries than lines */
385 if (menu->top != 0) {
387 set_option (OPTNEEDREDRAW);
390 else if (menu->current >= menu->top + menu->pagelen - c) /* indicator below bottom threshold */
392 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
393 menu->top = menu->current - menu->pagelen + c + 1;
395 menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
397 else if (menu->current < menu->top + c) /* indicator above top threshold */
399 if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
400 menu->top = menu->current - c;
402 menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
405 if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
406 menu->top = MIN (menu->top, menu->max - menu->pagelen);
407 menu->top = MAX (menu->top, 0);
409 if (menu->top != old_top)
410 menu->redraw |= REDRAW_INDEX;
413 void menu_jump (MUTTMENU *menu)
416 char buf[SHORT_STRING];
420 mutt_ungetch (LastKey, 0);
422 if (mutt_get_field (_("Jump to: "), buf, sizeof (buf), 0) == 0 && buf[0])
425 if (n >= 0 && n < menu->max)
428 menu->redraw = REDRAW_MOTION;
431 mutt_error _("Invalid index number.");
435 mutt_error _("No entries.");
438 void menu_next_line (MUTTMENU *menu)
442 int c = MIN (MenuContext, menu->pagelen / 2);
444 if (menu->top + 1 < menu->max - c
445 && (option(OPTMENUMOVEOFF) || (menu->max > menu->pagelen && menu->top < menu->max - menu->pagelen)))
448 if (menu->current < menu->top + c && menu->current < menu->max - 1)
450 menu->redraw = REDRAW_INDEX;
453 mutt_error _("You cannot scroll down farther.");
456 mutt_error _("No entries.");
459 void menu_prev_line (MUTTMENU *menu)
463 int c = MIN (MenuContext, menu->pagelen / 2);
466 if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
468 menu->redraw = REDRAW_INDEX;
471 mutt_error _("You cannot scroll up farther.");
474 void menu_next_page (MUTTMENU *menu)
478 if (menu->top + menu->pagelen < menu->max)
480 menu->top += menu->pagelen;
481 if (menu->current < menu->top)
482 menu->current = menu->top;
483 menu->redraw = REDRAW_INDEX;
485 else if (menu->current != menu->max - 1 && !menu->dialog)
487 menu->current = menu->max - 1;
488 menu->redraw = REDRAW_MOTION;
491 mutt_error _("You are on the last page.");
494 mutt_error _("No entries.");
497 void menu_prev_page (MUTTMENU *menu)
499 int c = MIN (MenuContext, menu->pagelen / 2);
503 if ((menu->top -= menu->pagelen) < 0)
505 if (menu->current >= menu->top + menu->pagelen)
506 menu->current = menu->top + menu->pagelen - 1;
507 menu->redraw = REDRAW_INDEX;
509 else if (menu->current && !menu->dialog)
512 menu->redraw = REDRAW_MOTION;
515 mutt_error _("You are on the first page.");
518 void menu_top_page (MUTTMENU *menu)
520 if (menu->current != menu->top)
522 menu->current = menu->top;
523 menu->redraw = REDRAW_MOTION;
527 void menu_bottom_page (MUTTMENU *menu)
531 menu->current = menu->top + menu->pagelen - 1;
532 if (menu->current > menu->max - 1)
533 menu->current = menu->max - 1;
534 menu->redraw = REDRAW_MOTION;
537 mutt_error _("No entries.");
540 void menu_middle_page (MUTTMENU *menu)
546 i = menu->top + menu->pagelen;
547 if (i > menu->max - 1)
549 menu->current = menu->top + (i - menu->top) / 2;
550 menu->redraw = REDRAW_MOTION;
553 mutt_error _("No entries.");
556 void menu_first_entry (MUTTMENU *menu)
561 menu->redraw = REDRAW_MOTION;
564 mutt_error _("No entries.");
567 void menu_last_entry (MUTTMENU *menu)
571 menu->current = menu->max - 1;
572 menu->redraw = REDRAW_MOTION;
575 mutt_error _("No entries.");
578 void menu_half_up (MUTTMENU *menu)
582 if ((menu->top -= menu->pagelen / 2) < 0)
584 if (menu->current >= menu->top + menu->pagelen)
585 menu->current = menu->top + menu->pagelen - 1;
586 menu->redraw = REDRAW_INDEX;
588 else if (menu->current && !menu->dialog)
591 menu->redraw = REDRAW_MOTION;
594 mutt_error _("First entry is shown.");
597 void menu_half_down (MUTTMENU *menu)
601 if (menu->top + menu->pagelen < menu->max)
603 menu->top += menu->pagelen / 2;
604 if (menu->current < menu->top)
605 menu->current = menu->top;
606 menu->redraw = REDRAW_INDEX;
608 else if (menu->current != menu->max - 1 && !menu->dialog)
610 menu->current = menu->max - 1;
611 menu->redraw = REDRAW_INDEX;
614 mutt_error _("Last entry is shown.");
617 mutt_error _("No entries.");
620 void menu_current_top (MUTTMENU *menu)
624 menu->top = menu->current;
625 menu->redraw = REDRAW_INDEX;
628 mutt_error _("No entries.");
631 void menu_current_middle (MUTTMENU *menu)
635 menu->top = menu->current - menu->pagelen / 2;
638 menu->redraw = REDRAW_INDEX;
641 mutt_error _("No entries.");
644 void menu_current_bottom (MUTTMENU *menu)
648 menu->top = menu->current - menu->pagelen + 1;
651 menu->redraw = REDRAW_INDEX;
654 mutt_error _("No entries.");
657 void menu_next_entry (MUTTMENU *menu)
659 if (menu->current < menu->max - 1)
662 menu->redraw = REDRAW_MOTION;
665 mutt_error _("You are on the last entry.");
668 void menu_prev_entry (MUTTMENU *menu)
673 menu->redraw = REDRAW_MOTION;
676 mutt_error _("You are on the first entry.");
679 static int default_color (int i)
681 return ColorDefs[MT_COLOR_NORMAL];
684 static int menu_search_generic (MUTTMENU *m, regex_t *re, int n)
686 char buf[LONG_STRING];
688 menu_make_entry (buf, sizeof (buf), m, n);
689 return (regexec (re, buf, 0, NULL, 0));
692 MUTTMENU *mutt_new_menu (void)
694 MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
699 p->redraw = REDRAW_FULL;
700 p->pagelen = PAGELEN;
701 p->color = default_color;
702 p->search = menu_search_generic;
706 void mutt_menuDestroy (MUTTMENU **p)
710 FREE (&(*p)->searchBuf);
714 for (i=0; i < (*p)->max; i++)
715 FREE (&(*p)->dialog[i]);
717 FREE (& (*p)->dialog);
723 #define M_SEARCH_UP 1
724 #define M_SEARCH_DOWN 2
726 static int menu_search (MUTTMENU *menu, int op)
731 char buf[SHORT_STRING];
733 if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
735 strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
736 if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
737 _("Reverse search for: "),
738 buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
740 mutt_str_replace (&menu->searchBuf, buf);
741 menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
745 if (!menu->searchBuf)
747 mutt_error _("No search pattern.");
752 searchDir = (menu->searchDir == M_SEARCH_UP) ? -1 : 1;
753 if (op == OP_SEARCH_OPPOSITE)
754 searchDir = -searchDir;
756 if ((r = REGCOMP (&re, menu->searchBuf, REG_NOSUB | mutt_which_case (menu->searchBuf))) != 0)
758 regerror (r, &re, buf, sizeof (buf));
760 mutt_error ("%s", buf);
764 r = menu->current + searchDir;
765 while (r >= 0 && r < menu->max)
767 if (menu->search (menu, &re, r) == 0)
777 mutt_error _("Not found.");
781 static int menu_dialog_translate_op (int i)
789 case OP_CURRENT_TOP: case OP_FIRST_ENTRY:
791 case OP_CURRENT_BOTTOM: case OP_LAST_ENTRY:
792 return OP_BOTTOM_PAGE;
793 case OP_CURRENT_MIDDLE:
794 return OP_MIDDLE_PAGE;
800 static int menu_dialog_dokey (MUTTMENU *menu, int *ip)
813 if (ch.ch && (p = strchr (menu->keys, ch.ch)))
815 *ip = OP_MAX + (p - menu->keys + 1);
820 mutt_ungetch (ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
825 int menu_redraw (MUTTMENU *menu)
827 /* See if all or part of the screen needs to be updated. */
828 if (menu->redraw & REDRAW_FULL)
830 menu_redraw_full (menu);
831 /* allow the caller to do any local configuration */
836 menu_check_recenter (menu);
838 if (menu->redraw & REDRAW_STATUS)
839 menu_redraw_status (menu);
840 if (menu->redraw & REDRAW_INDEX)
841 menu_redraw_index (menu);
842 else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNCH))
843 menu_redraw_motion (menu);
844 else if (menu->redraw == REDRAW_CURRENT)
845 menu_redraw_current (menu);
848 menu_redraw_prompt (menu);
853 int mutt_menuLoop (MUTTMENU *menu)
859 if (option (OPTMENUCALLER))
861 unset_option (OPTMENUCALLER);
872 if (menu_redraw (menu) == OP_REDRAW)
875 menu->oldcurrent = menu->current;
878 /* move the cursor out of the way */
879 move (menu->current - menu->top + menu->offset,
880 (option (OPTARROWCURSOR) ? 2 : COLS-1));
884 /* try to catch dialog keys before ops */
885 if (menu->dialog && menu_dialog_dokey (menu, &i) == 0)
888 i = km_dokey (menu->menu);
889 if (i == OP_TAG_PREFIX || i == OP_TAG_PREFIX_COND)
893 mvaddstr (LINES - 1, 0, "Tag-");
895 i = km_dokey (menu->menu);
897 CLEARLINE (LINES - 1);
899 else if (i == OP_TAG_PREFIX)
901 mutt_error _("No tagged entries.");
904 else /* None tagged, OP_TAG_PREFIX_COND */
910 if(tmp.op==OP_END_COND)break;
912 mutt_message _("Nothing to do.");
916 else if (menu->tagged && option (OPTAUTOTAG))
923 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
926 mutt_resize_screen ();
927 menu->redraw = REDRAW_FULL;
929 clearok(stdscr,TRUE);/*force complete redraw*/
939 /* Convert menubar movement to scrolling */
941 i = menu_dialog_translate_op (i);
946 menu_next_entry (menu);
949 menu_prev_entry (menu);
952 menu_half_down (menu);
958 menu_next_page (menu);
961 menu_prev_page (menu);
964 menu_next_line (menu);
967 menu_prev_line (menu);
970 menu_first_entry (menu);
973 menu_last_entry (menu);
976 menu_top_page (menu);
979 menu_middle_page (menu);
982 menu_bottom_page (menu);
985 menu_current_top (menu);
987 case OP_CURRENT_MIDDLE:
988 menu_current_middle (menu);
990 case OP_CURRENT_BOTTOM:
991 menu_current_bottom (menu);
994 case OP_SEARCH_REVERSE:
996 case OP_SEARCH_OPPOSITE:
997 if (menu->search && !menu->dialog) /* Searching dialogs won't work */
999 menu->oldcurrent = menu->current;
1000 if ((menu->current = menu_search (menu, i)) != -1)
1001 menu->redraw = REDRAW_MOTION;
1003 menu->current = menu->oldcurrent;
1006 mutt_error _("Search is not implemented for this menu.");
1011 mutt_error _("Jumping is not implemented for dialogs.");
1016 case OP_ENTER_COMMAND:
1017 CurrentMenu = menu->menu;
1018 mutt_enter_command ();
1019 if (option (OPTFORCEREDRAWINDEX))
1021 menu->redraw = REDRAW_FULL;
1022 unset_option (OPTFORCEREDRAWINDEX);
1023 unset_option (OPTFORCEREDRAWPAGER);
1028 if (menu->tag && !menu->dialog)
1030 if (menu->tagprefix && !option (OPTAUTOTAG))
1032 for (i = 0; i < menu->max; i++)
1033 menu->tagged += menu->tag (menu, i, 0);
1034 menu->redraw = REDRAW_INDEX;
1038 int i = menu->tag (menu, menu->current, -1);
1040 if (i && option (OPTRESOLVE) && menu->current < menu->max - 1)
1043 menu->redraw = REDRAW_MOTION_RESYNCH;
1046 menu->redraw = REDRAW_CURRENT;
1049 mutt_error _("No entries.");
1052 mutt_error _("Tagging is not supported.");
1055 case OP_SHELL_ESCAPE:
1056 mutt_shell_escape ();
1057 MAYBE_REDRAW (menu->redraw);
1065 clearok (stdscr, TRUE);
1066 menu->redraw = REDRAW_FULL;
1070 mutt_help (menu->menu);
1071 menu->redraw = REDRAW_FULL;
1075 km_error_key (menu->menu);