Emacs + Wanderlust: refile message to archive folder by year

Christian Kruse,

One thing I always liked about Thunderbird is the archive feature. It can refile a message to a subfolder of the year the message was received, e.g. if you archive a message from 2013 it will get stored to Archive/2013. This keeps the mailboxes managable and small enough.

During my migration to Wanderlust, an Emacs mail client, this feature was one of my most missed ones. So I decided to implement it myself:

(defun cjk-archive-message ()
  (interactive)

  (let ((folder wl-summary-buffer-elmo-folder)
        (msg-no (wl-summary-message-number))
        date entity year)

    (setq entity (elmo-message-entity folder msg-no))
    (setq date (elmo-message-entity-field entity 'date 'string))

    (when (eq date "")
      (setq date (elmo-message-entity-field entity 'received 'string)))

    (string-match "\\([1-9][0-9]\\{3\\}\\)" date)
    (setq year (match-string 1 date))
    (when (eq year nil)
      (error "no valid date header field"))

    (setq folder (concat ".Archive." year))

    (if (funcall (symbol-function 'wl-summary-refile) msg-no folder)
        (message "Refiled message to folder %s" folder)
      (message "Error refiling message to folder %s" folder))))

Emacs rocks! :-)