<<< Date Index >>>     <<< Thread Index >>>

[PATCH] Manage last search pattern outside of menu lifecycle



# HG changeset patch
# User Rocco Rutte <pdmef@xxxxxxx>
# Date 1227815796 -3600
# Branch HEAD
# Node ID a93114dcaa86fca48cab8c8485d691f3edeac9a4
# Parent  fd555f9fcb031a1ca18b6a56c4a45fd382220951
Manage last search pattern outside of menu lifecycle

Previously, the pattern is thrown away during menu destruction. For
the next search, mutt then can't provide a good suggestion. The new
behaviour is to manage the pattern outside the lifecyle to always
provide the last pattern as suggestion.

diff --git a/addrbook.c b/addrbook.c
--- a/addrbook.c
+++ b/addrbook.c
@@ -149,10 +149,9 @@ void mutt_alias_menu (char *buf, size_t 
   /* tell whoever called me to redraw the screen when I return */
   set_option (OPTNEEDREDRAW);
   
-  menu = mutt_new_menu ();
+  menu = mutt_new_menu (MENU_ALIAS);
   menu->make_entry = alias_entry;
   menu->tag = alias_tag;
-  menu->menu = MENU_ALIAS;
   menu->title = _("Aliases");
   menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, 
AliasHelp);
 
diff --git a/browser.c b/browser.c
--- a/browser.c
+++ b/browser.c
@@ -645,8 +645,7 @@ void _mutt_select_file (char *f, size_t 
   if (examine_directory (NULL, &state, LastDir, prefix) == -1)
     goto bail;
 
-  menu = mutt_new_menu ();
-  menu->menu = MENU_FOLDER;
+  menu = mutt_new_menu (MENU_FOLDER);
   menu->make_entry = folder_entry;
   menu->search = select_file_search;
   menu->title = title;
diff --git a/compose.c b/compose.c
--- a/compose.c
+++ b/compose.c
@@ -511,8 +511,7 @@ int mutt_compose_menu (HEADER *msg,   /*
   mutt_attach_init (msg->content);
   idx = mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0, 1);
 
-  menu = mutt_new_menu ();
-  menu->menu = MENU_COMPOSE;
+  menu = mutt_new_menu (MENU_COMPOSE);
   menu->offset = HDR_ATTACH;
   menu->max = idxlen;
   menu->make_entry = snd_entry;
diff --git a/crypt-gpgme.c b/crypt-gpgme.c
--- a/crypt-gpgme.c
+++ b/crypt-gpgme.c
@@ -3832,10 +3832,9 @@ static crypt_key_t *crypt_select_key (cr
   mutt_make_help (buf, sizeof (buf), _("Help"), menu_to_use, OP_HELP);
   strcat (helpstr, buf);       /* __STRCAT_CHECKED__ */
 
-  menu = mutt_new_menu ();
+  menu = mutt_new_menu (menu_to_use);
   menu->max = i;
   menu->make_entry = crypt_entry;
-  menu->menu = menu_to_use;
   menu->help = helpstr;
   menu->data = key_table;
 
diff --git a/curs_main.c b/curs_main.c
--- a/curs_main.c
+++ b/curs_main.c
@@ -435,8 +435,7 @@ int mutt_index_menu (void)
   int close = 0; /* did we OP_QUIT or OP_EXIT out of this menu? */
   int attach_msg = option(OPTATTACHMSG);
   
-  menu = mutt_new_menu ();
-  menu->menu = MENU_MAIN;
+  menu = mutt_new_menu (MENU_MAIN);
   menu->offset = 1;
   menu->pagelen = LINES - 3;
   menu->make_entry = index_make_entry;
diff --git a/init.c b/init.c
--- a/init.c
+++ b/init.c
@@ -2884,6 +2884,8 @@ void mutt_init (int skip_sys_rc, LIST *c
   Groups = hash_create (1031);
   ReverseAlias = hash_create (1031);
   
+  mutt_menu_init ();
+
   /* 
    * XXX - use something even more difficult to predict?
    */
diff --git a/menu.c b/menu.c
--- a/menu.c
+++ b/menu.c
@@ -32,6 +32,8 @@ extern int Charset_is_utf8; /* FIXME: ba
 
 extern size_t UngetCount;
 
+char* SearchBuffers[MENU_MAX];
+
 static void print_enriched_string (int attr, unsigned char *s, int do_color)
 {
   wchar_t wc;
@@ -673,10 +675,19 @@ static int menu_search_generic (MUTTMENU
   return (regexec (re, buf, 0, NULL, 0));
 }
 
-MUTTMENU *mutt_new_menu (void)
+void mutt_menu_init (void)
+{
+  int i;
+
+  for (i = 0; i < MENU_MAX; i++)
+    SearchBuffers[i] = NULL;
+}
+
+MUTTMENU *mutt_new_menu (int menu)
 {
   MUTTMENU *p = (MUTTMENU *) safe_calloc (1, sizeof (MUTTMENU));
 
+  p->menu = menu;
   p->current = 0;
   p->top = 0;
   p->offset = 1;
@@ -691,9 +702,7 @@ void mutt_menuDestroy (MUTTMENU **p)
 {
   int i;
 
-  FREE (&(*p)->searchBuf);
-
-  if ((*p)->dialog) 
+  if ((*p)->dialog)
   {
     for (i=0; i < (*p)->max; i++)
       FREE (&(*p)->dialog[i]);
@@ -713,20 +722,26 @@ static int menu_search (MUTTMENU *menu, 
   int searchDir;
   regex_t re;
   char buf[SHORT_STRING];
+  char* searchBuf = menu->menu >= 0 && menu->menu < MENU_MAX ?
+                    SearchBuffers[menu->menu] : NULL;
 
   if (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE)
   {
-    strfcpy (buf, menu->searchBuf ? menu->searchBuf : "", sizeof (buf));
+    strfcpy (buf, searchBuf ? searchBuf : "", sizeof (buf));
     if (mutt_get_field ((op == OP_SEARCH) ? _("Search for: ") : 
                                             _("Reverse search for: "),
                         buf, sizeof (buf), M_CLEAR) != 0 || !buf[0])
       return (-1);
-    mutt_str_replace (&menu->searchBuf, buf);
+    if (menu->menu >= 0 && menu->menu < MENU_MAX)
+    {
+      mutt_str_replace (&SearchBuffers[menu->menu], buf);
+      searchBuf = SearchBuffers[menu->menu];
+    }
     menu->searchDir = (op == OP_SEARCH) ? M_SEARCH_DOWN : M_SEARCH_UP;
   }
   else 
   {
-    if (!menu->searchBuf)
+    if (!searchBuf || !*searchBuf)
     {
       mutt_error _("No search pattern.");
       return (-1);
@@ -737,7 +752,7 @@ static int menu_search (MUTTMENU *menu, 
   if (op == OP_SEARCH_OPPOSITE)
     searchDir = -searchDir;
 
-  if ((r = REGCOMP (&re, menu->searchBuf, REG_NOSUB | mutt_which_case 
(menu->searchBuf))) != 0)
+  if ((r = REGCOMP (&re, searchBuf, REG_NOSUB | mutt_which_case (searchBuf))) 
!= 0)
   {
     regerror (r, &re, buf, sizeof (buf));
     regfree (&re);
diff --git a/mutt_menu.h b/mutt_menu.h
--- a/mutt_menu.h
+++ b/mutt_menu.h
@@ -75,11 +75,11 @@ typedef struct menu_t
   /* the following are used only by mutt_menuLoop() */
   int top;             /* entry that is the top of the current page */
   int oldcurrent;      /* for driver use only. */
-  char *searchBuf;     /* last search pattern */
   int searchDir;       /* direction of search */
   int tagged;          /* number of tagged entries */
 } MUTTMENU;
 
+void mutt_menu_init (void);
 void menu_jump (MUTTMENU *);
 void menu_redraw_full (MUTTMENU *);
 void menu_redraw_index (MUTTMENU *);
@@ -104,7 +104,7 @@ void menu_check_recenter (MUTTMENU *);
 void menu_check_recenter (MUTTMENU *);
 void menu_status_line (char *, size_t, MUTTMENU *, const char *);
 
-MUTTMENU *mutt_new_menu (void);
+MUTTMENU *mutt_new_menu (int);
 void mutt_menuDestroy (MUTTMENU **);
 int mutt_menuLoop (MUTTMENU *);
 
diff --git a/mutt_ssl.c b/mutt_ssl.c
--- a/mutt_ssl.c
+++ b/mutt_ssl.c
@@ -761,7 +761,7 @@ static int ssl_check_certificate (CONNEC
   }
 
   /* interactive check from user */
-  menu = mutt_new_menu ();
+  menu = mutt_new_menu (-1);
   menu->max = 19;
   menu->dialog = (char **) safe_calloc (1, menu->max * sizeof (char *));
   for (i = 0; i < menu->max; i++)
diff --git a/mutt_ssl_gnutls.c b/mutt_ssl_gnutls.c
--- a/mutt_ssl_gnutls.c
+++ b/mutt_ssl_gnutls.c
@@ -700,7 +700,7 @@ static int tls_check_certificate (CONNEC
 
 
   /* interactive check from user */
-  menu = mutt_new_menu ();
+  menu = mutt_new_menu (-1);
   menu->max = 25;
   menu->dialog = (char **) safe_calloc (1, menu->max * sizeof (char *));
   for (i = 0; i < menu->max; i++)
diff --git a/pager.c b/pager.c
--- a/pager.c
+++ b/pager.c
@@ -1652,8 +1652,7 @@ mutt_pager (const char *banner, const ch
        {
          /* only allocate the space if/when we need the index.
             Initialise the menu as per the main index */
-         index = mutt_new_menu();
-         index->menu = MENU_MAIN;
+         index = mutt_new_menu(MENU_MAIN);
          index->make_entry = index_make_entry;
          index->color = index_color;
          index->max = Context->vcount;
diff --git a/pgpkey.c b/pgpkey.c
--- a/pgpkey.c
+++ b/pgpkey.c
@@ -522,10 +522,9 @@ static pgp_key_t pgp_select_key (pgp_key
   mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
   strcat (helpstr, buf);       /* __STRCAT_CHECKED__ */
 
-  menu = mutt_new_menu ();
+  menu = mutt_new_menu (MENU_PGP);
   menu->max = i;
   menu->make_entry = pgp_entry;
-  menu->menu = MENU_PGP;
   menu->help = helpstr;
   menu->data = KeyTable;
 
diff --git a/postpone.c b/postpone.c
--- a/postpone.c
+++ b/postpone.c
@@ -160,9 +160,8 @@ static HEADER *select_msg (void)
   char helpstr[LONG_STRING];
   short orig_sort;
 
-  menu = mutt_new_menu ();
+  menu = mutt_new_menu (MENU_POST);
   menu->make_entry = post_entry;
-  menu->menu = MENU_POST;
   menu->max = PostContext->msgcount;
   menu->title = _("Postponed Messages");
   menu->data = PostContext;
diff --git a/query.c b/query.c
--- a/query.c
+++ b/query.c
@@ -301,11 +301,10 @@ static void query_menu (char *buf, size_
 
   snprintf (title, sizeof (title), _("Query")); /* FIXME */
 
-  menu = mutt_new_menu ();
+  menu = mutt_new_menu (MENU_QUERY);
   menu->make_entry = query_entry;
   menu->search = query_search;
   menu->tag = query_tag;
-  menu->menu = MENU_QUERY;
   menu->title = title;
   menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_QUERY, 
QueryHelp);
 
@@ -374,11 +373,10 @@ static void query_menu (char *buf, size_
 
              menu->current = 0;
              mutt_menuDestroy (&menu);
-             menu = mutt_new_menu ();
+             menu = mutt_new_menu (MENU_QUERY);
              menu->make_entry = query_entry;
              menu->search = query_search;
              menu->tag = query_tag;
-             menu->menu = MENU_QUERY;
              menu->title = title;
              menu->help = mutt_compile_help (helpstr, sizeof (helpstr), 
MENU_QUERY, QueryHelp);
 
diff --git a/recvattach.c b/recvattach.c
--- a/recvattach.c
+++ b/recvattach.c
@@ -1005,8 +1005,7 @@ void mutt_view_attachments (HEADER *hdr)
     cur = hdr->content;
   }
 
-  menu = mutt_new_menu ();
-  menu->menu = MENU_ATTACH;
+  menu = mutt_new_menu (MENU_ATTACH);
   menu->title = _("Attachments");
   menu->make_entry = attach_entry;
   menu->tag = mutt_tag_attach;
diff --git a/remailer.c b/remailer.c
--- a/remailer.c
+++ b/remailer.c
@@ -535,8 +535,7 @@ void mix_make_chain (LIST **chainp, int 
   
   mix_screen_coordinates (type2_list, &coords, chain, 0);
   
-  menu = mutt_new_menu ();
-  menu->menu = MENU_MIX;
+  menu = mutt_new_menu (MENU_MIX);
   menu->max = ttll;
   menu->make_entry = mix_entry;
   menu->tag = NULL;
diff --git a/smime.c b/smime.c
--- a/smime.c
+++ b/smime.c
@@ -438,10 +438,9 @@ char* smime_ask_for_key (char *prompt, c
     strcat (helpstr, buf);     /* __STRCAT_CHECKED__ */
   
     /* Create the menu */
-    menu = mutt_new_menu();
+    menu = mutt_new_menu(MENU_SMIME);
     menu->max = cur;
     menu->make_entry = smime_entry;
-    menu->menu = MENU_SMIME;
     menu->help = helpstr;
     menu->data = Table;
     menu->title = title;