@@ -112,6 +112,48 @@ async def apply(self, ui):
112112 await ui .apply_command (globals .PromptCommand (cmdstring ))
113113
114114
115+ async def delete_previous_draft (envelope , ui ):
116+ try :
117+ if envelope .previous_draft is None :
118+ return
119+ except AttributeError :
120+ return
121+ if settings .get ('envelope_always_delete_old_drafts' ):
122+ del_old_draft = True
123+ else :
124+ msg = 'Do you want to delete the old draft?'
125+ del_old_draft = (await ui .choice (msg , cancel = 'no' ,
126+ msg_position = 'left' ))
127+ del_old_draft = (del_old_draft == 'yes' )
128+ if del_old_draft :
129+ try :
130+ message_path = envelope .previous_draft .get_filename ()
131+ except Exception as e :
132+ logging .error (e )
133+ ui .notify ('could not get draft path:\n %s' % e ,
134+ priority = 'error' , block = True )
135+ return
136+
137+ try :
138+ ui .dbman .remove_message (envelope .previous_draft )
139+ await ui .apply_command (globals .FlushCommand ())
140+ except DatabaseError as e :
141+ logging .error (e )
142+ ui .notify ('could not remove draft from db:\n %s' % e ,
143+ priority = 'error' , block = True )
144+ return
145+
146+ try :
147+ os .unlink (message_path )
148+ except OSError as e :
149+ logging .error (e )
150+ ui .notify ('could not delete draft file:\n %s' % e ,
151+ priority = 'error' , block = True )
152+ return
153+
154+ envelope .previous_draft = None
155+
156+
115157@registerCommand (MODE , 'save' )
116158class SaveCommand (Command ):
117159 """save draft"""
@@ -138,23 +180,26 @@ async def apply(self, ui):
138180 path = account .store_draft_mail (
139181 mail .as_string (policy = email .policy .SMTP , maxheaderlen = sys .maxsize ))
140182
141- msg = 'draft saved successfully'
142-
143183 # add mail to index if maildir path available
144184 if path is not None :
145- ui .notify (msg + ' to %s' % path )
185+ ui .notify ('draft saved successfully to %s' % path )
146186 logging .debug ('adding new mail to index' )
147187 try :
148188 ui .dbman .add_message (path , account .draft_tags + envelope .tags )
149189 await ui .apply_command (globals .FlushCommand ())
150- await ui .apply_command (commands .globals .BufferCloseCommand ())
151190 except DatabaseError as e :
152191 logging .error (str (e ))
153192 ui .notify ('could not index message:\n %s' % str (e ),
154193 priority = 'error' ,
155194 block = True )
156- else :
157- await ui .apply_command (commands .globals .BufferCloseCommand ())
195+ return
196+
197+ await delete_previous_draft (envelope , ui )
198+
199+ # strip the outside '<' and '>' characters from the id
200+ mid = mail ['Message-ID' ][1 :- 1 ]
201+ envelope .previous_draft = ui .dbman .get_message (mid )
202+ await ui .apply_command (commands .globals .BufferCloseCommand ())
158203
159204
160205@registerCommand (MODE , 'send' )
@@ -315,6 +360,8 @@ async def apply(self, ui):
315360 ui .dbman .add_message (path , account .sent_tags + initial_tags )
316361 await ui .apply_command (globals .FlushCommand ())
317362
363+ await delete_previous_draft (self .envelope , ui )
364+
318365
319366@registerCommand (MODE , 'edit' , arguments = [
320367 (['--spawn' ], {'action' : cargparse .BooleanAction , 'default' : None ,
0 commit comments