# vim:ft=diff: This is the trash folder patch by Cedric Duval . With this patch, if the trash variable is set to a path (unset by default), the deleted mails will be moved to a trash folder instead of being irremediably purged when syncing the mailbox. For instance, set trash="~/Mail/trash" will cause every deleted mail to go to this folder. Note that the append to the trash folder doesn't occur until the resync is done. This allows you to change your mind and undo deletes, and thus the moves to the trash folder are unnecessary. Notes * You might also want to have a look at the purge message feature below which is related to this patch. * IMAP is now supported. To retain the previous behavior, add this to your muttrc: folder-hook ^imap:// 'unset trash' FAQ Every once in a while, someone asks what are the advantages of this patch over a macro based solution. Here's an attempt to answer this question: * The folder history doesn't clutter up with unwanted trash entries. * Delayed move to the trash allows to change one's mind. * No need to treat the case of "normal folders" and trash folders separately with folder-hooks, and to create two sets of macros (one for the index, one for the pager). * Works not only with delete-message, but also with every deletion functions like delete-pattern, delete-thread or delete-subthread. To sum up, it's more integrated and transparent to the user. * Patch last synced with upstream: - Date: 2007-02-15 - File: http://cedricduval.free.fr/mutt/patches/download/patch-1.5.5.1.cd.trash_folder.3.4 * Changes made: - Updated to 1.5.13: - structure of _mutt_save_message changed (commands.c) - context of option (OPTCONFIRMAPPEND) changed (muttlib.c) - Fixed indentation of "appended" in mutt.h. == END PATCH Index: mutt/commands.c =================================================================== --- mutt.orig/commands.c 2009-06-25 12:35:37.000000000 +0200 +++ mutt/commands.c 2009-06-25 12:35:48.000000000 +0200 @@ -716,6 +716,7 @@ if (option (OPTDELETEUNTAG)) mutt_set_flag (Context, h, M_TAG, 0); } + mutt_set_flag (Context, h, M_APPENDED, 1); return 0; } Index: mutt/flags.c =================================================================== --- mutt.orig/flags.c 2009-06-25 12:35:37.000000000 +0200 +++ mutt/flags.c 2009-06-25 12:35:48.000000000 +0200 @@ -65,7 +65,13 @@ { h->deleted = 0; update = 1; - if (upd_ctx) ctx->deleted--; + if (upd_ctx) + { + ctx->deleted--; + if (h->appended) + ctx->appended--; + } + h->appended = 0; /* when undeleting, also reset the appended flag */ #ifdef USE_IMAP /* see my comment above */ if (ctx->magic == M_IMAP) @@ -87,6 +93,17 @@ } break; + case M_APPENDED: + if (bf) + { + if (!h->appended) + { + h->appended = 1; + if (upd_ctx) ctx->appended++; + } + } + break; + case M_NEW: if (!mutt_bit_isset(ctx->rights,M_ACL_SEEN)) Index: mutt/globals.h =================================================================== --- mutt.orig/globals.h 2009-06-25 12:35:44.000000000 +0200 +++ mutt/globals.h 2009-06-25 12:35:48.000000000 +0200 @@ -139,6 +139,7 @@ WHERE char *Status; WHERE char *Tempdir; WHERE char *Tochars; +WHERE char *TrashPath; WHERE char *Username; WHERE char *Visual; WHERE char *XtermTitle; Index: mutt/imap/message.c =================================================================== --- mutt.orig/imap/message.c 2009-06-25 12:35:37.000000000 +0200 +++ mutt/imap/message.c 2009-06-25 12:35:48.000000000 +0200 @@ -867,6 +867,7 @@ if (ctx->hdrs[n]->tagged) { mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1); + mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1); if (option (OPTDELETEUNTAG)) mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0); } @@ -874,6 +875,7 @@ else { mutt_set_flag (ctx, h, M_DELETE, 1); + mutt_set_flag (ctx, h, M_APPENDED, 1); if (option (OPTDELETEUNTAG)) mutt_set_flag (ctx, h, M_TAG, 0); } Index: mutt/init.h =================================================================== --- mutt.orig/init.h 2009-06-25 12:35:44.000000000 +0200 +++ mutt/init.h 2009-06-25 12:35:48.000000000 +0200 @@ -3180,6 +3180,16 @@ ** by \fIyou\fP. The sixth character is used to indicate when a mail ** was sent to a mailing-list you subscribe to. */ + { "trash", DT_PATH, R_NONE, UL &TrashPath, 0 }, + /* + ** .pp + ** If set, this variable specifies the path of the trash folder where the + ** mails marked for deletion will be moved, instead of being irremediably + ** purged. + ** .pp + ** NOTE: When you delete a message in the trash folder, it is really + ** deleted, so that you have a way to clean the trash. + */ #ifdef USE_SOCKET { "tunnel", DT_STR, R_NONE, UL &Tunnel, UL 0 }, /* Index: mutt/mutt.h =================================================================== --- mutt.orig/mutt.h 2009-06-25 12:35:44.000000000 +0200 +++ mutt/mutt.h 2009-06-25 12:35:48.000000000 +0200 @@ -187,6 +187,7 @@ M_DELETE, M_UNDELETE, M_DELETED, + M_APPENDED, M_FLAG, M_TAG, M_UNTAG, @@ -702,6 +703,7 @@ unsigned int mime : 1; /* has a MIME-Version header? */ unsigned int flagged : 1; /* marked important? */ unsigned int tagged : 1; + unsigned int appended : 1; /* has been saved */ unsigned int deleted : 1; unsigned int changed : 1; unsigned int attach_del : 1; /* has an attachment marked for deletion */ @@ -874,6 +876,7 @@ int new; /* how many new messages? */ int unread; /* how many unread messages? */ int deleted; /* how many deleted messages */ + int appended; /* how many saved messages? */ int flagged; /* how many flagged messages */ int msgnotreadyet; /* which msg "new" in pager, -1 if none */ Index: mutt/muttlib.c =================================================================== --- mutt.orig/muttlib.c 2009-06-25 12:35:37.000000000 +0200 +++ mutt/muttlib.c 2009-06-25 12:35:48.000000000 +0200 @@ -1460,7 +1460,9 @@ if (magic > 0 && !mx_access (s, W_OK)) { - if (option (OPTCONFIRMAPPEND)) + if (option (OPTCONFIRMAPPEND) && + (!TrashPath || (mutt_strcmp (s, TrashPath) != 0))) + /* if we're appending to the trash, there's no point in asking */ { snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s); if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO) Index: mutt/mx.c =================================================================== --- mutt.orig/mx.c 2009-06-25 12:35:37.000000000 +0200 +++ mutt/mx.c 2009-06-25 12:35:48.000000000 +0200 @@ -773,6 +773,53 @@ return rc; } +/* move deleted mails to the trash folder */ +static int trash_append (CONTEXT *ctx) +{ + CONTEXT *ctx_trash; + int i = 0; + struct stat st, stc; + + if (!TrashPath || !ctx->deleted || + (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH))) + return 0; + + for (;i < ctx->msgcount && (!ctx->hdrs[i]->deleted || + ctx->hdrs[i]->appended); i++); + if (i == ctx->msgcount) + return 0; /* nothing to be done */ + + if (mutt_save_confirm (TrashPath, &st) != 0) + { + mutt_error _("message(s) not deleted"); + return -1; + } + + if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino + && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev) + return 0; /* we are in the trash folder: simple sync */ + + if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL) + { + for (i = 0 ; i < ctx->msgcount ; i++) + if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended + && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1) + { + mx_close_mailbox (ctx_trash, NULL); + return -1; + } + + mx_close_mailbox (ctx_trash, NULL); + } + else + { + mutt_error _("Can't open trash folder"); + return -1; + } + + return 0; +} + /* save changes and close mailbox */ int mx_close_mailbox (CONTEXT *ctx, int *index_hint) { @@ -909,6 +956,7 @@ if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0) { mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1); + mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1); } else { @@ -931,6 +979,14 @@ return 0; } + /* copy mails to the trash before expunging */ + if (purge && ctx->deleted && mutt_strcmp(ctx->path, TrashPath)) + if (trash_append (ctx) != 0) + { + ctx->closing = 0; + return -1; + } + #ifdef USE_IMAP /* allow IMAP to preserve the deleted flag across sessions */ if (ctx->magic == M_IMAP) @@ -1130,6 +1186,12 @@ msgcount = ctx->msgcount; deleted = ctx->deleted; + if (purge && ctx->deleted && mutt_strcmp(ctx->path, TrashPath)) + { + if (trash_append (ctx) == -1) + return -1; + } + #ifdef USE_IMAP if (ctx->magic == M_IMAP) rc = imap_sync_mailbox (ctx, purge, index_hint); Index: mutt/postpone.c =================================================================== --- mutt.orig/postpone.c 2009-06-25 12:35:37.000000000 +0200 +++ mutt/postpone.c 2009-06-25 12:35:48.000000000 +0200 @@ -276,6 +276,9 @@ /* finished with this message, so delete it. */ mutt_set_flag (PostContext, h, M_DELETE, 1); + /* and consider it saved, so that it won't be moved to the trash folder */ + mutt_set_flag (PostContext, h, M_APPENDED, 1); + /* update the count for the status display */ PostCount = PostContext->msgcount - PostContext->deleted;