Fix compilation warnings in menu.c
[apps/madmutt.git] / menu.c
diff --git a/menu.c b/menu.c
index ab3ce23..90e2bdc 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -1,9 +1,7 @@
 /*
  * Copyright notice from original mutt:
  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
- * * Parts of it were written/modified by:
- *   Nico Golde <nico@ngolde.de>
- * 
+ *
  * This file is part of mutt-ng, see http://www.muttng.org/.
  * It's licensed under the GNU General Public License,
  * please see the file GPL in the top level source directory.
 # include "config.h"
 #endif
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/macros.h>
+
 #include "mutt.h"
+#include "enter.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
 #include "mbyte.h"
 #include "sidebar.h"
 
-#ifdef USE_IMAP
-#include "imap.h"
-#endif
-
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
+#include <imap/imap.h>
 
 #include <string.h>
 #include <stdlib.h>
 
-extern int Charset_is_utf8;     /* FIXME: bad modularisation */
+#define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
 
 extern size_t UngetCount;
 
@@ -38,10 +35,10 @@ static void print_enriched_string (int attr, unsigned char *s, int do_color)
 {
   wchar_t wc;
   size_t k;
-  size_t n = mutt_strlen ((char *) s);
+  size_t n = m_strlen((char *) s);
   mbstate_t mbstate;
 
-  memset (&mbstate, 0, sizeof (mbstate));
+  p_clear(&mbstate, 1);
   while (*s) {
     if (*s < M_TREE_MAX) {
       if (do_color)
@@ -140,7 +137,7 @@ static void print_enriched_string (int attr, unsigned char *s, int do_color)
 static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i)
 {
   if (menu->dialog) {
-    strncpy (s, menu->dialog[i], l);
+    m_strcpy(s, l, menu->dialog[i]);
     menu->current = -1;         /* hide menubar */
   }
   else
@@ -151,16 +148,16 @@ void menu_pad_string (char *s, size_t n)
 {
   int shift = option (OPTARROWCURSOR) ? 3 : 0;
   int cols;
-  char *tmpbuf = safe_malloc (n);
+  char *tmpbuf = p_new(char, n);
 
   if (option (OPTMBOXPANE))
     cols = COLS - shift - SidebarWidth;
   else
     cols = COLS - shift;
-  mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, mutt_strlen (s), 1);
+  mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, m_strlen(s), 1);
   tmpbuf[n - 1] = 0;
   snprintf (s, n, "%s", tmpbuf);        /* overkill */
-  FREE (&tmpbuf);
+  p_delete(&tmpbuf);
 }
 
 void menu_redraw_full (MUTTMENU * menu)
@@ -169,13 +166,11 @@ void menu_redraw_full (MUTTMENU * menu)
   /* clear() doesn't optimize screen redraws */
   move (0, 0);
   clrtobot ();
-  
-  if (!option(OPTMBOXPANE)) SidebarWidth=0;
 
   if (option (OPTHELP)) {
     SETCOLOR (MT_COLOR_STATUS);
-    move (option (OPTSTATUSONTOP) ? LINES - 2 : 0, 0);
-    mutt_paddstr (COLS, menu->help);
+    move (option (OPTSTATUSONTOP) ? LINES - 2 : 0, SW);
+    mutt_paddstr (COLS-SW, menu->help);
     SETCOLOR (MT_COLOR_NORMAL);
     menu->offset = 1;
     menu->pagelen = LINES - 3;
@@ -185,6 +180,8 @@ void menu_redraw_full (MUTTMENU * menu)
     menu->pagelen = LINES - 2;
   }
 
+  sidebar_draw_frames();
+
   mutt_show_error ();
 
   menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
@@ -196,10 +193,11 @@ void menu_redraw_status (MUTTMENU * menu)
 
   snprintf (buf, sizeof (buf), M_MODEFMT, menu->title);
   SETCOLOR (MT_COLOR_STATUS);
-  move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, 0);
-  mutt_paddstr (COLS, buf);
+  move (option (OPTSTATUSONTOP) ? 0 : LINES - 2, SW);
+  mutt_paddstr (COLS-SW, buf);
   SETCOLOR (MT_COLOR_NORMAL);
   menu->redraw &= ~REDRAW_STATUS;
+  sidebar_draw_frames();
 }
 
 void menu_redraw_index (MUTTMENU * menu)
@@ -207,8 +205,6 @@ void menu_redraw_index (MUTTMENU * menu)
   char buf[STRING];
   int i;
 
-  if (!option(OPTMBOXPANE)) SidebarWidth=0;
-  sidebar_draw (1);
   for (i = menu->top; i < menu->top + menu->pagelen; i++) {
     if (i < menu->max) {
       menu_make_entry (buf, sizeof (buf), menu, i);
@@ -221,18 +217,20 @@ void menu_redraw_index (MUTTMENU * menu)
         if (i == menu->current) {
           attrset (menu->color (i));
           ADDCOLOR (MT_COLOR_INDICATOR);
+          BKGDSET (MT_COLOR_INDICATOR);
           addstr ("->");
           attrset (menu->color (i));
           addch (' ');
         }
         else {
           attrset (menu->color (i));
-          move (i - menu->top + menu->offset, SidebarWidth);
+          move (i - menu->top + menu->offset, SW);
           addstr ("   ");
         }
 
         print_enriched_string (menu->color (i), (unsigned char *) buf, 1);
         SETCOLOR (MT_COLOR_NORMAL);
+        BKGDSET (MT_COLOR_NORMAL);
       }
       else {
         attrset (menu->color (i));
@@ -243,6 +241,8 @@ void menu_redraw_index (MUTTMENU * menu)
         }
 
         CLEARLINE_WIN (i - menu->top + menu->offset);
+
+        move (i - menu->top + menu->offset, SW);
         print_enriched_string (menu->color (i), (unsigned char *) buf,
                                i != menu->current);
         SETCOLOR (MT_COLOR_NORMAL);
@@ -252,6 +252,9 @@ void menu_redraw_index (MUTTMENU * menu)
     else
       CLEARLINE_WIN (i - menu->top + menu->offset);
   }
+  sidebar_draw (1);
+/*  sidebar_draw_frames(); */
+
   menu->redraw = 0;
 }
 
@@ -264,7 +267,7 @@ void menu_redraw_motion (MUTTMENU * menu)
     return;
   }
 
-  move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth);
+  move (menu->oldcurrent + menu->offset - menu->top, SW);
   SETCOLOR (MT_COLOR_NORMAL);
   BKGDSET (MT_COLOR_NORMAL);
 
@@ -277,14 +280,14 @@ void menu_redraw_motion (MUTTMENU * menu)
       clrtoeol ();
       menu_make_entry (buf, sizeof (buf), menu, menu->oldcurrent);
       menu_pad_string (buf, sizeof (buf));
-      move (menu->oldcurrent + menu->offset - menu->top, SidebarWidth + 3);
+      move (menu->oldcurrent + menu->offset - menu->top, SW + 3);
       print_enriched_string (menu->color (menu->oldcurrent),
                              (unsigned char *) buf, 1);
       SETCOLOR (MT_COLOR_NORMAL);
     }
 
     /* now draw it in the new location */
-    move (menu->current + menu->offset - menu->top, SidebarWidth);
+    move (menu->current + menu->offset - menu->top, SW);
     attrset (menu->color (menu->current));
     ADDCOLOR (MT_COLOR_INDICATOR);
     addstr ("->");
@@ -306,6 +309,7 @@ void menu_redraw_motion (MUTTMENU * menu)
     ADDCOLOR (MT_COLOR_INDICATOR);
     BKGDSET (MT_COLOR_INDICATOR);
     CLEARLINE_WIN (menu->current - menu->top + menu->offset);
+    move (menu->current + menu->offset - menu->top, SW);
     print_enriched_string (menu->color (menu->current), (unsigned char *) buf,
                            0);
     SETCOLOR (MT_COLOR_NORMAL);
@@ -318,12 +322,10 @@ void menu_redraw_current (MUTTMENU * menu)
 {
   char buf[STRING];
 
-  move (menu->current + menu->offset - menu->top, SidebarWidth);
+  move (menu->current + menu->offset - menu->top, SW);
   menu_make_entry (buf, sizeof (buf), menu, menu->current);
   menu_pad_string (buf, sizeof (buf));
 
-  if (!option(OPTMBOXPANE)) SidebarWidth=0;
-
   if (option (OPTARROWCURSOR)) {
     int attr = menu->color (menu->current);
 
@@ -379,23 +381,18 @@ void menu_check_recenter (MUTTMENU * menu)
       menu->top = 0;
       set_option (OPTNEEDREDRAW);
     }
-  }
-  else if (menu->current >= menu->top + menu->pagelen - c) {    /* indicator below bottom threshold */
-    if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
-      menu->top = menu->current - menu->pagelen + c + 1;
-    else
-      menu->top +=
-        (menu->pagelen -
-         c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
-  }
-  else if (menu->current < menu->top + c) {     /* indicator above top threshold */
-    if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
-      menu->top = menu->current - c;
-    else
-      menu->top -=
-        (menu->pagelen -
-         c) * ((menu->top + menu->pagelen - 1 -
-                menu->current) / (menu->pagelen - c)) - c;
+  } else {
+    if (option (OPTMENUSCROLL) || (menu->pagelen <= 0) || (c < MenuContext)) {
+      if (menu->current < menu->top + c)
+        menu->top = menu->current - c;
+      else if (menu->current >= menu->top + menu->pagelen - c)
+        menu->top = menu->current - menu->pagelen + c + 1;
+    } else {
+      if (menu->current < menu->top + c)
+        menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
+      else if ((menu->current >= menu->top + menu->pagelen - c))
+        menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
+    }
   }
 
   if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
@@ -463,55 +460,75 @@ void menu_prev_line (MUTTMENU * menu)
     mutt_error _("You cannot scroll up farther.");
 }
 
-void menu_next_page (MUTTMENU * menu)
-{
+/* 
+ * pageup:   jumplen == -pagelen
+ * pagedown: jumplen == pagelen
+ * halfup:   jumplen == -pagelen/2
+ * halfdown: jumplen == pagelen/2
+ */
+#define DIRECTION ((neg * 2) + 1)
+void menu_length_jump (MUTTMENU *menu, int jumplen) {
+  int tmp, neg = (jumplen >= 0) ? 0 : -1;
+  int c = MIN (MenuContext, menu->pagelen / 2);
+
   if (menu->max) {
-    if (menu->top + menu->pagelen < menu->max) {
-      menu->top += menu->pagelen;
-      if (menu->current < menu->top)
-        menu->current = menu->top;
+    /* possible to scroll? */
+    if (DIRECTION * menu->top < 
+        (tmp = (neg ? 0 : (menu->max /*-1*/) - (menu->pagelen /*-1*/)))) {
+      menu->top += jumplen;
+
+      /* jumped too long? */
+      if ((neg || !option (OPTMENUMOVEOFF)) && DIRECTION * menu->top > tmp)
+        menu->top = tmp;
+
+      /* need to move the cursor? */
+      if ((DIRECTION * 
+           (tmp = (menu->current - (menu->top + 
+                                    (neg ? (menu->pagelen - 1) - c : c))))) < 0)
+        menu->current -= tmp;
+
       menu->redraw = REDRAW_INDEX;
     }
-    else if (menu->current != menu->max - 1 && !menu->dialog) {
-      menu->current = menu->max - 1;
+    else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog) {
+      menu->current += jumplen;
       menu->redraw = REDRAW_MOTION;
     }
     else
-      mutt_error _("You are on the last page.");
+      mutt_error (neg ? _("You are on the first page.")
+                  : _("You are on the last page."));
+
+    menu->current = MIN (menu->current, menu->max - 1);
+    menu->current = MAX (menu->current, 0);
   }
   else
     mutt_error _("No entries.");
 }
+#undef DIRECTION
 
-void menu_prev_page (MUTTMENU * menu)
-{
-  int c = MIN (MenuContext, menu->pagelen / 2);
+void menu_next_page (MUTTMENU *menu) {
+  menu_length_jump (menu, MAX (menu->pagelen /* - MenuOverlap */, 0));
+}
 
-  if (menu->top > c) {
-    if ((menu->top -= menu->pagelen) < 0)
-      menu->top = 0;
-    if (menu->current >= menu->top + menu->pagelen)
-      menu->current = menu->top + menu->pagelen - 1;
-    menu->redraw = REDRAW_INDEX;
-  }
-  else if (menu->current && !menu->dialog) {
-    menu->current = 0;
-    menu->redraw = REDRAW_MOTION;
-  }
-  else
-    mutt_error _("You are on the first page.");
+void menu_prev_page (MUTTMENU *menu) {
+  menu_length_jump (menu, 0 - MAX (menu->pagelen /* - MenuOverlap */, 0));
 }
 
-void menu_top_page (MUTTMENU * menu)
-{
+void menu_half_down (MUTTMENU *menu) {
+  menu_length_jump (menu, menu->pagelen / 2);
+}
+
+void menu_half_up (MUTTMENU *menu) {
+  menu_length_jump (menu, 0 - menu->pagelen / 2);
+}
+
+void menu_top_page (MUTTMENU *menu) {
   if (menu->current != menu->top) {
     menu->current = menu->top;
     menu->redraw = REDRAW_MOTION;
   }
 }
 
-void menu_bottom_page (MUTTMENU * menu)
-{
+void menu_bottom_page (MUTTMENU *menu) {
   if (menu->max) {
     menu->current = menu->top + menu->pagelen - 1;
     if (menu->current > menu->max - 1)
@@ -522,8 +539,7 @@ void menu_bottom_page (MUTTMENU * menu)
     mutt_error _("No entries.");
 }
 
-void menu_middle_page (MUTTMENU * menu)
-{
+void menu_middle_page (MUTTMENU *menu) {
   int i;
 
   if (menu->max) {
@@ -537,8 +553,7 @@ void menu_middle_page (MUTTMENU * menu)
     mutt_error _("No entries.");
 }
 
-void menu_first_entry (MUTTMENU * menu)
-{
+void menu_first_entry (MUTTMENU *menu) {
   if (menu->max) {
     menu->current = 0;
     menu->redraw = REDRAW_MOTION;
@@ -547,8 +562,7 @@ void menu_first_entry (MUTTMENU * menu)
     mutt_error _("No entries.");
 }
 
-void menu_last_entry (MUTTMENU * menu)
-{
+void menu_last_entry (MUTTMENU *menu) {
   if (menu->max) {
     menu->current = menu->max - 1;
     menu->redraw = REDRAW_MOTION;
@@ -557,43 +571,6 @@ void menu_last_entry (MUTTMENU * menu)
     mutt_error _("No entries.");
 }
 
-void menu_half_up (MUTTMENU * menu)
-{
-  if (menu->top > 0) {
-    if ((menu->top -= menu->pagelen / 2) < 0)
-      menu->top = 0;
-    if (menu->current >= menu->top + menu->pagelen)
-      menu->current = menu->top + menu->pagelen - 1;
-    menu->redraw = REDRAW_INDEX;
-  }
-  else if (menu->current && !menu->dialog) {
-    menu->current = 0;
-    menu->redraw = REDRAW_MOTION;
-  }
-  else
-    mutt_error _("First entry is shown.");
-}
-
-void menu_half_down (MUTTMENU * menu)
-{
-  if (menu->max) {
-    if (menu->top + menu->pagelen < menu->max) {
-      menu->top += menu->pagelen / 2;
-      if (menu->current < menu->top)
-        menu->current = menu->top;
-      menu->redraw = REDRAW_INDEX;
-    }
-    else if (menu->current != menu->max - 1 && !menu->dialog) {
-      menu->current = menu->max - 1;
-      menu->redraw = REDRAW_INDEX;
-    }
-    else
-      mutt_error _("Last entry is shown.");
-  }
-  else
-    mutt_error _("No entries.");
-}
-
 void menu_current_top (MUTTMENU * menu)
 {
   if (menu->max) {
@@ -648,7 +625,7 @@ void menu_prev_entry (MUTTMENU * menu)
     mutt_error _("You are on the first entry.");
 }
 
-static int default_color (int i)
+static int default_color (int i __attribute__ ((unused)))
 {
   return ColorDefs[MT_COLOR_NORMAL];
 }
@@ -663,7 +640,7 @@ static int menu_search_generic (MUTTMENU * m, regex_t * re, int n)
 
 MUTTMENU *mutt_new_menu (void)
 {
-  MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
+  MUTTMENU *p = p_new(MUTTMENU, 1);
 
   p->current = 0;
   p->top = 0;
@@ -679,16 +656,16 @@ void mutt_menuDestroy (MUTTMENU ** p)
 {
   int i;
 
-  FREE (&(*p)->searchBuf);
+  p_delete(&(*p)->searchBuf);
 
   if ((*p)->dialog) {
     for (i = 0; i < (*p)->max; i++)
-      FREE (&(*p)->dialog[i]);
+      p_delete(&(*p)->dialog[i]);
 
-    FREE (&(*p)->dialog);
+    p_delete(&(*p)->dialog);
   }
 
-  FREE (p);
+  p_delete(p);
 }
 
 #define M_SEARCH_UP   1
@@ -702,12 +679,12 @@ static int menu_search (MUTTMENU * menu, int op)
   char buf[SHORT_STRING];
 
   if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE) {
-    strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
+    m_strcpy(buf, sizeof(buf), NONULL(menu->searchBuf));
     if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") :
                         _("Reverse search for: "),
                         buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
       return (-1);
-    str_replace (&menu->searchBuf, buf);
+    m_strreplace(&menu->searchBuf, buf);
     menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
   }
   else {
@@ -820,7 +797,7 @@ int mutt_menuLoop (MUTTMENU * menu)
 {
   int i = OP_NULL;
 
-  FOREVER {
+  for (;;) {
     if (option (OPTMENUCALLER)) {
       unset_option (OPTMENUCALLER);
       return OP_NULL;
@@ -828,10 +805,7 @@ int mutt_menuLoop (MUTTMENU * menu)
 
 
     mutt_curs_set (0);
-
-#ifdef USE_IMAP
     imap_keepalive ();
-#endif
 
     if (menu_redraw (menu) == OP_REDRAW)
       return OP_REDRAW;
@@ -839,9 +813,9 @@ int mutt_menuLoop (MUTTMENU * menu)
     menu->oldcurrent = menu->current;
 
     if (option (OPTARROWCURSOR))
-      move (menu->current - menu->top + menu->offset, 2);
+      move (menu->current - menu->top + menu->offset, SW + 2);
     else if (option (OPTBRAILLEFRIENDLY))
-      move (menu->current - menu->top + menu->offset, 0);
+      move (menu->current - menu->top + menu->offset, SW);
     else
       move (menu->current - menu->top + menu->offset, COLS - 1);
 
@@ -996,10 +970,10 @@ int mutt_menuLoop (MUTTMENU * menu)
           menu->redraw = REDRAW_INDEX;
         }
         else if (menu->max) {
-          int i = menu->tag (menu, menu->current, -1);
+          int t = menu->tag (menu, menu->current, -1);
 
-          menu->tagged += i;
-          if (i && option (OPTRESOLVE) && menu->current < menu->max - 1) {
+          menu->tagged += t;
+          if (t && option (OPTRESOLVE) && menu->current < menu->max - 1) {
             menu->current++;
             menu->redraw = REDRAW_MOTION_RESYNCH;
           }
@@ -1022,6 +996,10 @@ int mutt_menuLoop (MUTTMENU * menu)
       mutt_what_key ();
       break;
 
+    case OP_REBUILD_CACHE:
+      mx_rebuild_cache ();
+      break;
+
     case OP_REDRAW:
       clearok (stdscr, TRUE);
       menu->redraw = REDRAW_FULL;