2 This is the trash folder patch by Cedric Duval <cedricduval@free.fr>.
4 With this patch, if the trash variable is set to a path (unset by default), the
5 deleted mails will be moved to a trash folder instead of being irremediably
6 purged when syncing the mailbox.
8 For instance, set trash="~/Mail/trash" will cause every deleted mail to go to
11 Note that the append to the trash folder doesn't occur until the resync is
12 done. This allows you to change your mind and undo deletes, and thus the moves
13 to the trash folder are unnecessary.
17 * You might also want to have a look at the purge message feature below
18 which is related to this patch.
19 * IMAP is now supported. To retain the previous behavior, add this to your
21 folder-hook ^imap:// 'unset trash'
25 Every once in a while, someone asks what are the advantages of this patch over
26 a macro based solution. Here's an attempt to answer this question:
28 * The folder history doesn't clutter up with unwanted trash entries.
29 * Delayed move to the trash allows to change one's mind.
30 * No need to treat the case of "normal folders" and trash folders
31 separately with folder-hooks, and to create two sets of macros (one for
32 the index, one for the pager).
33 * Works not only with delete-message, but also with every deletion
34 functions like delete-pattern, delete-thread or delete-subthread.
36 To sum up, it's more integrated and transparent to the user.
38 * Patch last synced with upstream:
40 - File: http://cedricduval.free.fr/mutt/patches/download/patch-1.5.5.1.cd.trash_folder.3.4
44 - structure of _mutt_save_message changed (commands.c)
45 - context of option (OPTCONFIRMAPPEND) changed (muttlib.c)
46 - Fixed indentation of "appended" in mutt.h.
51 @@ -688,6 +688,7 @@ int _mutt_save_message (HEADER *h, CONTE
52 if (option (OPTDELETEUNTAG))
53 mutt_set_flag (Context, h, M_TAG, 0);
55 + mutt_set_flag (Context, h, M_APPENDED, 1);
61 @@ -69,7 +69,13 @@ void _mutt_set_flag (CONTEXT *ctx, HEADE
65 - if (upd_ctx) ctx->deleted--;
72 + h->appended = 0; /* when undeleting, also reset the appended flag */
74 /* see my comment above */
75 if (ctx->magic == M_IMAP)
76 @@ -91,6 +97,17 @@ void _mutt_set_flag (CONTEXT *ctx, HEADE
86 + if (upd_ctx) ctx->appended++;
93 if (!mutt_bit_isset(ctx->rights,M_ACL_SEEN))
96 @@ -142,6 +142,7 @@ WHERE char *StChars;
100 +WHERE char *TrashPath;
101 WHERE char *Username;
103 WHERE char *XtermTitle;
106 @@ -816,6 +816,7 @@ int imap_copy_messages (CONTEXT* ctx, HE
107 if (ctx->hdrs[n]->tagged)
109 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
110 + mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
111 if (option (OPTDELETEUNTAG))
112 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
114 @@ -823,6 +824,7 @@ int imap_copy_messages (CONTEXT* ctx, HE
117 mutt_set_flag (ctx, h, M_DELETE, 1);
118 + mutt_set_flag (ctx, h, M_APPENDED, 1);
119 if (option (OPTDELETEUNTAG))
120 mutt_set_flag (ctx, h, M_TAG, 0);
124 @@ -2881,6 +2881,16 @@ struct option_t MuttVars[] = {
125 ** by \fIyou\fP. The sixth character is used to indicate when a mail
126 ** was sent to a mailing-list you subscribe to (default: L).
128 + { "trash", DT_PATH, R_NONE, UL &TrashPath, 0 },
131 + ** If set, this variable specifies the path of the trash folder where the
132 + ** mails marked for deletion will be moved, instead of being irremediably
135 + ** NOTE: When you delete a message in the trash folder, it is really
136 + ** deleted, so that you have a way to clean the trash.
139 { "tunnel", DT_STR, R_NONE, UL &Tunnel, UL 0 },
143 @@ -201,6 +201,7 @@ enum
151 @@ -712,6 +713,7 @@ typedef struct header
152 unsigned int mime : 1; /* has a MIME-Version header? */
153 unsigned int flagged : 1; /* marked important? */
154 unsigned int tagged : 1;
155 + unsigned int appended : 1; /* has been saved */
156 unsigned int deleted : 1;
157 unsigned int changed : 1;
158 unsigned int attach_del : 1; /* has an attachment marked for deletion */
159 @@ -883,6 +885,7 @@ typedef struct _context
160 int new; /* how many new messages? */
161 int unread; /* how many unread messages? */
162 int deleted; /* how many deleted messages */
163 + int appended; /* how many saved messages? */
164 int flagged; /* how many flagged messages */
165 int msgnotreadyet; /* which msg "new" in pager, -1 if none */
169 @@ -1430,7 +1430,9 @@ int mutt_save_confirm (const char *s, st
171 if (magic > 0 && !mx_access (s, W_OK))
173 - if (option (OPTCONFIRMAPPEND))
174 + if (option (OPTCONFIRMAPPEND) &&
175 + (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
176 + /* if we're appending to the trash, there's no point in asking */
178 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
179 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
182 @@ -803,6 +803,53 @@ static int sync_mailbox (CONTEXT *ctx, i
186 +/* move deleted mails to the trash folder */
187 +static int trash_append (CONTEXT *ctx)
189 + CONTEXT *ctx_trash;
191 + struct stat st, stc;
193 + if (!TrashPath || !ctx->deleted ||
194 + (ctx->magic == M_MAILDIR && option (OPTMAILDIRTRASH)))
197 + for (;i < ctx->msgcount && (!ctx->hdrs[i]->deleted ||
198 + ctx->hdrs[i]->appended); i++);
199 + if (i == ctx->msgcount)
200 + return 0; /* nothing to be done */
202 + if (mutt_save_confirm (TrashPath, &st) != 0)
204 + mutt_error _("message(s) not deleted");
208 + if (lstat (ctx->path, &stc) == 0 && stc.st_ino == st.st_ino
209 + && stc.st_dev == st.st_dev && stc.st_rdev == st.st_rdev)
210 + return 0; /* we are in the trash folder: simple sync */
212 + if ((ctx_trash = mx_open_mailbox (TrashPath, M_APPEND, NULL)) != NULL)
214 + for (i = 0 ; i < ctx->msgcount ; i++)
215 + if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->appended
216 + && mutt_append_message (ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
218 + mx_close_mailbox (ctx_trash, NULL);
222 + mx_close_mailbox (ctx_trash, NULL);
226 + mutt_error _("Can't open trash folder");
233 /* save changes and close mailbox */
234 int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
236 @@ -938,6 +985,7 @@ int mx_close_mailbox (CONTEXT *ctx, int
237 if (mutt_append_message (&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
239 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, 1);
240 + mutt_set_flag (ctx, ctx->hdrs[i], M_APPENDED, 1);
244 @@ -959,6 +1007,14 @@ int mx_close_mailbox (CONTEXT *ctx, int
248 + /* copy mails to the trash before expunging */
249 + if (purge && ctx->deleted)
250 + if (trash_append (ctx) != 0)
257 /* allow IMAP to preserve the deleted flag across sessions */
258 if (ctx->magic == M_IMAP)
259 @@ -1154,6 +1210,12 @@ int mx_sync_mailbox (CONTEXT *ctx, int *
260 msgcount = ctx->msgcount;
261 deleted = ctx->deleted;
263 + if (purge && ctx->deleted)
265 + if (trash_append (ctx) == -1)
270 if (ctx->magic == M_IMAP)
271 rc = imap_sync_mailbox (ctx, purge, index_hint);
274 @@ -279,6 +279,9 @@ int mutt_get_postponed (CONTEXT *ctx, HE
275 /* finished with this message, so delete it. */
276 mutt_set_flag (PostContext, h, M_DELETE, 1);
278 + /* and consider it saved, so that it won't be moved to the trash folder */
279 + mutt_set_flag (PostContext, h, M_APPENDED, 1);
281 /* update the count for the status display */
282 PostCount = PostContext->msgcount - PostContext->deleted;