25 June, 2012

Org Mode VM Mairix Link Type, Evolved

Almost one year ago I posted a rather crude solution on how to create and use a mairix link type in VM and Org mode. This functionality is now invaluable in my every day work. During this last year it has also evolved to be less crude, it now
  • decodes mime-encoded strings,
  • stores enough properties for
    • org-email-link-description-format to be used in full, and
    • all vm specific information, including %:fromto, to be used in org-capture-templates.
Below is the current implementation, the reset of :fromto in org-store-link-plist to expand the escape is not nice but works:
;; Add mairix as new link type in org
(org-add-link-type "mairix" 'ra/org-vm-mairix-open)
(add-hook 'org-store-link-functions 'ra/org-vm-store-mairix-link)

;; Store link as mairix search for message ID
;; Most from org-vm-store-link in org-vm.el, modded
(defun ra/org-vm-store-mairix-link ()
  "Store a link to the current VM message as a Mairix search for
  its Message ID."
      (and (or (eq major-mode 'vm-summary-mode)
               (eq major-mode 'vm-presentation-mode))
             (vm-select-folder-buffer) buffer-file-name))
    (and (eq major-mode 'vm-presentation-mode)
      (let* (
             ;; Collect properties
              (car vm-message-pointer))
               (vm-su-subject message)))
               (vm-get-header-contents message "From")))
               (vm-get-header-contents message "To")))
              (vm-su-message-id message))
             ;; Date needed for full use of org-email-link-description
             ;; and for use in capture templates
              (vm-get-header-contents message "Date"))
             ;; Date active/inactive in correct time stamp format for
             ;; use in capture templates
              (and date
                      (org-time-stamp-format t)
                      (date-to-time date)))))
              (and date
                      (org-time-stamp-format t t)
                      (date-to-time date)))))
               (format "m:%s"
                       (org-remove-angle-brackets message-id)))))
         :type "mairix"
         :message-id message-id
         :from from
         :to to
         :subject subject
         :date date
         :date-timestamp date-ts
         :date-timestamp-inactive date-ts-ia
         :link link)
         :description (org-email-link-description))
        ;; It seems we need to reset :fromto with expanded escapes
        ;; after :description to use %:fromto in
        ;; org-capture-templates... bug?
         :fromto (org-replace-escapes 
                  (or (plist-get org-store-link-plist :fromto)
                      "from %f") ; if org-from-is-user-regexp is nil
                    (or (plist-get org-store-link-plist :fromname)
                        (plist-get org-store-link-plist :fromaddress)
                    (or (plist-get org-store-link-plist :toname)
                        (plist-get org-store-link-plist :toaddress)
        ;; save vm folder and update the mairix db
        (mairix-update-database) ; Make sure mairix-synchronous-update
                                 ; is nil, default, so we do not have
                                 ; to wait

;; Open the mairix link, making use of mairix.el to make the search
;; and display the results
(defun ra/org-vm-mairix-open (msearch)
  "Search for messages with message-id with mairix, and display
  them in a VM folder. This requires a proper mairix.el
  (mairix-search msearch t))
Of course, as stated in my old blog post, a working setup of Mairix and the Emacs Mairix mode is still required.

No comments:

Post a Comment