Skip to content

Commit

Permalink
Track currently selected email by msgid
Browse files Browse the repository at this point in the history
  • Loading branch information
gahr authored and flatcap committed Jul 3, 2020
1 parent 900abfb commit 2dce94a
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 109 deletions.
11 changes: 5 additions & 6 deletions compmbox/compress.c
Expand Up @@ -557,7 +557,6 @@ static int comp_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
/**
* comp_mbox_check - Check for new mail - Implements MxOps::mbox_check()
* @param m Mailbox
* @param index_hint Currently selected mailbox
* @retval 0 Mailbox OK
* @retval #MUTT_REOPENED The mailbox was closed and reopened
* @retval -1 Mailbox bad
Expand All @@ -569,7 +568,7 @@ static int comp_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
*
* The return codes are picked to match mx_mbox_check().
*/
static int comp_mbox_check(struct Mailbox *m, int *index_hint)
static int comp_mbox_check(struct Mailbox *m)
{
if (!m || !m->compress_info)
return -1;
Expand All @@ -596,7 +595,7 @@ static int comp_mbox_check(struct Mailbox *m, int *index_hint)
if (rc == 0)
return -1;

return ops->mbox_check(m, index_hint);
return ops->mbox_check(m);
}

/**
Expand All @@ -605,7 +604,7 @@ static int comp_mbox_check(struct Mailbox *m, int *index_hint)
* Changes in NeoMutt only affect the tmp file.
* Calling comp_mbox_sync() will commit them to the compressed file.
*/
static int comp_mbox_sync(struct Mailbox *m, int *index_hint)
static int comp_mbox_sync(struct Mailbox *m)
{
if (!m || !m->compress_info)
return -1;
Expand All @@ -628,11 +627,11 @@ static int comp_mbox_sync(struct Mailbox *m, int *index_hint)
return -1;
}

int rc = comp_mbox_check(m, index_hint);
int rc = comp_mbox_check(m);
if (rc != 0)
goto sync_cleanup;

rc = ops->mbox_sync(m, index_hint);
rc = ops->mbox_sync(m);
if (rc != 0)
goto sync_cleanup;

Expand Down
5 changes: 2 additions & 3 deletions imap/imap.c
Expand Up @@ -2102,12 +2102,11 @@ static int imap_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)

/**
* imap_mbox_check - Check for new mail - Implements MxOps::mbox_check()
* @param m Mailbox
* @param index_hint Remember our place in the index
* @param m Mailbox
* @retval >0 Success, e.g. #MUTT_REOPENED
* @retval -1 Failure
*/
static int imap_mbox_check(struct Mailbox *m, int *index_hint)
static int imap_mbox_check(struct Mailbox *m)
{
if (!m)
return -1;
Expand Down
85 changes: 40 additions & 45 deletions index.c
Expand Up @@ -608,22 +608,14 @@ static void update_index_unthreaded(struct Context *ctx, int check, int oldcount
* @param ctx Mailbox
* @param check Flags, e.g. #MUTT_REOPENED
* @param oldcount How many items are currently in the index
* @param index_hint Remember our place in the index
* @param curr_msgid Remember our place in the index
*/
void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcount, int index_hint)
void update_index(struct Menu *menu, struct Context *ctx, int check,
int oldcount, const char *curr_msgid)
{
if (!menu || !ctx)
return;

/* take note of the current message */
if (oldcount)
{
if (menu->current < ctx->mailbox->vcount)
menu->oldcurrent = index_hint;
else
oldcount = 0; /* invalid message number! */
}

if ((C_Sort & SORT_MASK) == SORT_THREADS)
update_index_threaded(ctx, check, oldcount);
else
Expand All @@ -638,7 +630,7 @@ void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcoun
struct Email *e = mutt_get_virt_email(ctx->mailbox, i);
if (!e)
continue;
if (e->index == menu->oldcurrent)
if (mutt_str_equal(e->env->message_id, curr_msgid))
{
menu->current = i;
break;
Expand Down Expand Up @@ -675,11 +667,11 @@ static int mailbox_index_observer(struct NotifyCallback *nc)
* @param menu Current Menu
* @param m Mailbox
* @param oldcount How many items are currently in the index
* @param index_hint Remember our place in the index
* @param curr_msgid Remember our place in the index
* @param read_only Open Mailbox in read-only mode
*/
static void change_folder_mailbox(struct Menu *menu, struct Mailbox *m,
int *oldcount, int *index_hint, bool read_only)
static void change_folder_mailbox(struct Menu *menu, struct Mailbox *m, int *oldcount,
const char *curr_msgid, bool read_only)
{
if (!m)
return;
Expand Down Expand Up @@ -710,7 +702,7 @@ static void change_folder_mailbox(struct Menu *menu, struct Mailbox *m,
mutt_monitor_add(NULL);
#endif
if ((check == MUTT_NEW_MAIL) || (check == MUTT_REOPENED))
update_index(menu, Context, check, *oldcount, *index_hint);
update_index(menu, Context, check, *oldcount, curr_msgid);

FREE(&new_last_folder);
OptSearchInvalid = true;
Expand Down Expand Up @@ -779,11 +771,12 @@ static void change_folder_mailbox(struct Menu *menu, struct Mailbox *m,
* @param buf Folder to change to
* @param buflen Length of buffer
* @param oldcount How many items are currently in the index
* @param index_hint Remember our place in the index
* @param curr_msgid Remember our place in the index
* @param read_only Open Mailbox in read-only mode
*/
static struct Mailbox *change_folder_notmuch(struct Menu *menu, char *buf, int buflen,
int *oldcount, int *index_hint, bool read_only)
static struct Mailbox *change_folder_notmuch(struct Menu *menu, char *buf,
int buflen, int *oldcount,
const char *curr_msgid, bool read_only)
{
if (!nm_url_from_query(NULL, buf, buflen))
{
Expand All @@ -792,7 +785,7 @@ static struct Mailbox *change_folder_notmuch(struct Menu *menu, char *buf, int b
}

struct Mailbox *m_query = mx_path_resolve(buf);
change_folder_mailbox(menu, m_query, oldcount, index_hint, read_only);
change_folder_mailbox(menu, m_query, oldcount, curr_msgid, read_only);
return m_query;
}
#endif
Expand All @@ -803,12 +796,13 @@ static struct Mailbox *change_folder_notmuch(struct Menu *menu, char *buf, int b
* @param buf Folder to change to
* @param buflen Length of buffer
* @param oldcount How many items are currently in the index
* @param index_hint Remember our place in the index
* @param curr_msgid Remember our place in the index
* @param pager_return Return to the pager afterwards
* @param read_only Open Mailbox in read-only mode
*/
static void change_folder_string(struct Menu *menu, char *buf, size_t buflen, int *oldcount,
int *index_hint, bool *pager_return, bool read_only)
static void change_folder_string(struct Menu *menu, char *buf, size_t buflen,
int *oldcount, const char *curr_msgid,
bool *pager_return, bool read_only)
{
#ifdef USE_NNTP
if (OptNews)
Expand All @@ -829,7 +823,7 @@ static void change_folder_string(struct Menu *menu, char *buf, size_t buflen, in
struct Mailbox *m = mailbox_find_name(buf);
if (m)
{
change_folder_mailbox(menu, m, oldcount, index_hint, read_only);
change_folder_mailbox(menu, m, oldcount, curr_msgid, read_only);
*pager_return = false;
}
else
Expand All @@ -841,7 +835,7 @@ static void change_folder_string(struct Menu *menu, char *buf, size_t buflen, in
*pager_return = false;

struct Mailbox *m = mx_path_resolve(buf);
change_folder_mailbox(menu, m, oldcount, index_hint, read_only);
change_folder_mailbox(menu, m, oldcount, curr_msgid, read_only);
}

/**
Expand Down Expand Up @@ -1143,7 +1137,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
bool tag = false; /* has the tag-prefix command been pressed? */
int newcount = -1;
int oldcount = -1;
int index_hint = 0; /* used to restore cursor position */
char *curr_msgid = NULL; /* used to restore cursor position */
bool do_mailbox_notify = true;
int close = 0; /* did we OP_QUIT or OP_EXIT out of this menu? */
int attach_msg = OptAttachMsg;
Expand Down Expand Up @@ -1225,9 +1219,9 @@ int mutt_index_menu(struct MuttWindow *dlg)
* modified underneath us.) */

struct Email *e_cur = get_cur_email(Context, menu);
index_hint = e_cur ? e_cur->index : 0;
mutt_str_replace(&curr_msgid, e_cur ? e_cur->env->message_id : NULL);

int check = mx_mbox_check(Context->mailbox, &index_hint);
int check = mx_mbox_check(Context->mailbox);
if (check < 0)
{
if (!Context->mailbox || (mutt_buffer_is_empty(&Context->mailbox->pathbuf)))
Expand Down Expand Up @@ -1278,7 +1272,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
{
bool verbose = Context->mailbox->verbose;
Context->mailbox->verbose = false;
update_index(menu, Context, check, oldcount, index_hint);
update_index(menu, Context, check, oldcount, curr_msgid);
Context->mailbox->verbose = verbose;
menu->max = Context->mailbox->vcount;
}
Expand Down Expand Up @@ -1867,7 +1861,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
else
{
if ((check == MUTT_NEW_MAIL) || (check == MUTT_REOPENED))
update_index(menu, Context, check, oldcount, index_hint);
update_index(menu, Context, check, oldcount, curr_msgid);

menu->redraw = REDRAW_FULL; /* new mail arrived? */
OptSearchInvalid = true;
Expand Down Expand Up @@ -2015,7 +2009,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
if (check != 0)
{
if ((check == MUTT_NEW_MAIL) || (check == MUTT_REOPENED))
update_index(menu, Context, check, oldcount, index_hint);
update_index(menu, Context, check, oldcount, curr_msgid);
OptSearchInvalid = true;
menu->redraw = REDRAW_FULL;
break;
Expand Down Expand Up @@ -2056,7 +2050,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
e = mutt_get_virt_email(Context->mailbox, newidx);
}

int check = mx_mbox_sync(Context->mailbox, &index_hint);
int check = mx_mbox_sync(Context->mailbox);
if (check == 0)
{
if (e && (Context->mailbox->vcount != ovc))
Expand All @@ -2074,7 +2068,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
OptSearchInvalid = true;
}
else if ((check == MUTT_NEW_MAIL) || (check == MUTT_REOPENED))
update_index(menu, Context, check, oc, index_hint);
update_index(menu, Context, check, oc, curr_msgid);

/* do a sanity check even if mx_mbox_sync failed. */

Expand Down Expand Up @@ -2150,7 +2144,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
if (buf[strlen(buf) - 1] == '>')
buf[strlen(buf) - 1] = '\0';

change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, &index_hint, false);
change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, curr_msgid, false);

// If notmuch doesn't contain the message, we're left in an empty
// vfolder. No messages are found, but nm_read_entire_thread assumes
Expand Down Expand Up @@ -2324,7 +2318,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
char *query_unencoded = mutt_str_dup(buf);

struct Mailbox *m_query =
change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, &index_hint,
change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, curr_msgid,
(op == OP_MAIN_VFOLDER_FROM_QUERY_READONLY));
if (m_query)
{
Expand Down Expand Up @@ -2355,7 +2349,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
}
nm_query_window_backward();
mutt_str_copy(buf, C_NmQueryWindowCurrentSearch, sizeof(buf));
change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, &index_hint, false);
change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, curr_msgid, false);
break;
}

Expand All @@ -2375,14 +2369,14 @@ int mutt_index_menu(struct MuttWindow *dlg)
}
nm_query_window_forward();
mutt_str_copy(buf, C_NmQueryWindowCurrentSearch, sizeof(buf));
change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, &index_hint, false);
change_folder_notmuch(menu, buf, sizeof(buf), &oldcount, curr_msgid, false);
break;
}
#endif

#ifdef USE_SIDEBAR
case OP_SIDEBAR_OPEN:
change_folder_mailbox(menu, sb_get_highlight(), &oldcount, &index_hint, false);
change_folder_mailbox(menu, sb_get_highlight(), &oldcount, curr_msgid, false);
break;
#endif

Expand All @@ -2404,7 +2398,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
break;
}

change_folder_mailbox(menu, m, &oldcount, &index_hint, false);
change_folder_mailbox(menu, m, &oldcount, curr_msgid, false);
break;
}

Expand Down Expand Up @@ -2455,13 +2449,13 @@ int mutt_index_menu(struct MuttWindow *dlg)
struct Mailbox *m = mx_mbox_find2(mutt_b2s(folderbuf));
if (m)
{
change_folder_mailbox(menu, m, &oldcount, &index_hint, read_only);
change_folder_mailbox(menu, m, &oldcount, curr_msgid, read_only);
pager_return = false;
}
else
{
change_folder_string(menu, folderbuf->data, folderbuf->dsize,
&oldcount, &index_hint, &pager_return, read_only);
&oldcount, curr_msgid, &pager_return, read_only);
}

changefoldercleanup:
Expand Down Expand Up @@ -2527,13 +2521,13 @@ int mutt_index_menu(struct MuttWindow *dlg)
struct Mailbox *m = mx_mbox_find2(mutt_b2s(folderbuf));
if (m)
{
change_folder_mailbox(menu, m, &oldcount, &index_hint, read_only);
change_folder_mailbox(menu, m, &oldcount, curr_msgid, read_only);
pager_return = false;
}
else
{
change_folder_string(menu, folderbuf->data, folderbuf->dsize,
&oldcount, &index_hint, &pager_return, read_only);
&oldcount, curr_msgid, &pager_return, read_only);
}
menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_MAIN, IndexNewsHelp);

Expand Down Expand Up @@ -2581,7 +2575,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
e_cur = get_cur_email(Context, menu);
if (!e_cur)
break;
int hint = e_cur->index;
mutt_str_replace(&curr_msgid, e_cur->env->message_id);

op = mutt_display_message(win_index, win_ibar, win_pager, win_pbar,
Context->mailbox, e_cur);
Expand All @@ -2597,7 +2591,7 @@ int mutt_index_menu(struct MuttWindow *dlg)
in_pager = true;
menu->oldcurrent = menu->current;
if (Context && Context->mailbox)
update_index(menu, Context, MUTT_NEW_MAIL, Context->mailbox->msg_count, hint);
update_index(menu, Context, MUTT_NEW_MAIL, Context->mailbox->msg_count, curr_msgid);
continue;
}

Expand Down Expand Up @@ -3980,6 +3974,7 @@ int mutt_index_menu(struct MuttWindow *dlg)

mutt_menu_pop_current(menu);
mutt_menu_free(&menu);
FREE(&curr_msgid);
return close;
}

Expand Down
2 changes: 1 addition & 1 deletion index.h
Expand Up @@ -48,7 +48,7 @@ void index_make_entry(char *buf, size_t buflen, struct Menu *menu, int line);
void mutt_draw_statusline(int cols, const char *buf, size_t buflen);
int mutt_index_menu(struct MuttWindow *dlg);
void mutt_set_header_color(struct Mailbox *m, struct Email *e);
void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcount, int index_hint);
void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcount, const char *curr_msgid);
struct MuttWindow *index_pager_init(void);
void index_pager_shutdown(struct MuttWindow *dlg);
int mutt_dlgindex_observer(struct NotifyCallback *nc);
Expand Down
2 changes: 1 addition & 1 deletion maildir/maildir.c
Expand Up @@ -364,7 +364,7 @@ static int maildir_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
* already knew about. We don't treat either subdirectory differently, as mail
* could be copied directly into the cur directory from another agent.
*/
int maildir_mbox_check(struct Mailbox *m, int *index_hint)
int maildir_mbox_check(struct Mailbox *m)
{
if (!m)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion maildir/mh.c
Expand Up @@ -590,7 +590,7 @@ static int mh_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
*
* Don't change this code unless you _really_ understand what happens.
*/
int mh_mbox_check(struct Mailbox *m, int *index_hint)
int mh_mbox_check(struct Mailbox *m)
{
if (!m)
return -1;
Expand Down

0 comments on commit 2dce94a

Please sign in to comment.