Discussion:
[VM] A trash folder for the primary inbox (code review requested)
Daniel Barrett
2014-09-16 20:36:37 UTC
Permalink
I'd like to share some elisp functions that create a "trash" folder
for the VM primary inbox. Since I rarely program in elisp, I welcome
any code suggestions from more experienced VM developers, for example:

- How can I obtain the strings "Summary" and "Presentation" without
hard-coding them?
- Is there an easy way to write a "trash size" function that says how
many messages are in the trash?
- Am I doing anything the wrong way where elisp/VM is concerned?

This trash folder works only for the primary inbox (by design). My use
case is that I sometimes delete and expunge a message by accident, and
VM provides no backup. This happens most often in the primary inbox.
(Perhaps it would be fancier to provide a list of folders to which the
trash applies.)

For myself, I bind the "d" keystroke to the new command
vm-trash-or-delete-message, which copies the current message to a
"Trash" folder and marks it for deletion. If this command is run
outside the primary inbox, it merely marks the message for deletion
without copying it to the trash.

I also wrote simple functions to visit and empty the Trash folder.

I stole the vm-trash-folder variable and vm-save-message-to-trash
function from http://permalink.gmane.org/gmane.emacs.viewmail/112.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar vm-trash-folder
(concat vm-folder-directory "Trash")
"The full path to the Trash folder for VM.")

(defun vm-save-message-to-trash ()
"Move the current message to the Trash folder and mark it as deleted."
(interactive)
(vm-save-message vm-trash-folder))

(defun vm-trash-or-delete-message ()
"If the current message is in the primary inbox, move it to the trash
and mark it for deletion. Otherwise, just mark it for deletion."
(interactive)
; Name of inbox
(let ((my-inbox-name
(file-name-nondirectory vm-primary-inbox)))
; Names of buffers
(let ((my-inbox-summary-buffer-name
(concat my-inbox-name " Summary"))
(my-inbox-presentation-buffer-name
(concat my-inbox-name " Presentation")))

; If we're in the inbox, send to trash, otherwise delete
(cond ((or
(string= (buffer-name) my-inbox-summary-buffer-name)
(string= (buffer-name) my-inbox-presentation-buffer-name))
(vm-save-message-to-trash))
(t (vm-delete-message 1))))))

(defun vm-visit-trash-folder ()
"Visit the VM Trash folder as a normal mail folder."
(interactive)
(vm-visit-folder vm-trash-folder))

(defun vm-empty-trash-folder ()
"Permanently delete all messages from the VM Trash folder."
(interactive)
(if (file-exists-p vm-trash-folder)
(let ((ans (yes-or-no-p
(concat "OK to empty '" vm-trash-folder "'? "))))
(if ans
(write-region "" nil vm-trash-folder)
(message "Trash folder NOT emptied")))
(message
(concat "Trash folder '" vm-trash-folder "' not found"))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

--
Dan Barrett
***@blazemonger.com
Daniel Barrett
2014-10-03 18:02:32 UTC
Permalink
Post by Daniel Barrett
I'd like to share some elisp functions that create a "trash" folder
for the VM primary inbox [...].
After using this trash feature for three weeks, it seems really
helpful for protecting against deleting an email by accident. I hope
the code is useful to others.

I'd also still appreciate any code review from more experienced
elisp developers.

http://lists.gnu.org/archive/html/viewmail-info/2014-09/msg00006.html

For example, I hard-coded the strings "Summary" and "Presentation" -- is
there a better way to obtain them automatically? Any other tips?

--
Dan Barrett
***@blazemonger.com
Uday Reddy
2014-11-14 17:46:00 UTC
Permalink
Post by Daniel Barrett
For example, I hard-coded the strings "Summary" and "Presentation" -- is
there a better way to obtain them automatically? Any other tips?
For this older question, note that there are variables `vm-summary-buffer' and
`vm-presentation-buffer', which are set in the folder buffer.

Cheers,
uday
Daniel Barrett
2014-11-14 12:43:55 UTC
Permalink
I'm trying to keep track of the last folder name that received a saved
message, so I can restore it after a save:

(setq default vm-last-save-folder)
(vm-save-message 'my-other-folder)
(setq vm-last-save-folder default)

However, vm-last-save-folder always ends up blank. What am I doing
wrong?

Thanks,
Dan

--
Dan Barrett
***@blazemonger.com
Uday Reddy
2014-11-14 17:43:17 UTC
Permalink
Post by Daniel Barrett
I'm trying to keep track of the last folder name that received a saved
(setq default vm-last-save-folder)
(vm-save-message 'my-other-folder)
(setq vm-last-save-folder default)
Indeed, as Piet said it is a buffer-local variable that is only set in the
folder buffer (which is called `vm-mail-buffer' for some reason). So, the
correct way to do this is:

(setq default (with-current-buffer vm-mail-buffer vm-last-save-folder))
(vm-save-message 'my-other-folder)
(with-current-buffer vm-mail-buffer (setq vm-last-save-folder default))

or more simply

(with-current-buffer vm-mail-buffer
(setq default vm-last-save-folder)
(vm-save-message 'my-other-folder)
(setq vm-last-save-folder default))

Cheers,
Uday
Daniel Barrett
2014-11-14 18:35:13 UTC
Permalink
Post by Uday Reddy
Indeed, as Piet said it is a buffer-local variable that is only set in the
folder buffer...
(with-current-buffer vm-mail-buffer
(setq default vm-last-save-folder)
(vm-save-message 'my-other-folder)
(setq vm-last-save-folder default))
Thanks so much, Uday and Piet! Your technique worked perfectly.

Dan

Piet van Oostrum
2014-11-14 13:59:47 UTC
Permalink
Post by Daniel Barrett
I'm trying to keep track of the last folder name that received a saved
(setq default vm-last-save-folder)
(vm-save-message 'my-other-folder)
(setq vm-last-save-folder default)
However, vm-last-save-folder always ends up blank. What am I doing
wrong?
Thanks,
Dan
vm-last-save-folder is a buffer-local variable. It is defined in the
buffer that contails the mail folder, but not in the Summary or
Presentation buffer. So I guess you are in either of these when you are
trying this construction. So maybe you have to add a
(vm-select-folder-buffer) somewhere.
--
Piet van Oostrum <***@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Loading...